X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=src%2Ffccache.c;h=350b80521da9fe3ea34cfba7a125c7297b5a7932;hb=8ba2b3c59b2cd0bf24b6045f5c6babdf49b4b30d;hp=53df860bd3f7b649513a8a1a68bb47947c8dbfe8;hpb=8ae1e3d5dc323542e7def06a42deea62c7ba7027;p=fontconfig.git diff --git a/src/fccache.c b/src/fccache.c index 53df860..350b805 100644 --- a/src/fccache.c +++ b/src/fccache.c @@ -6,15 +6,15 @@ * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting - * documentation, and that the name of Keith Packard not be used in + * documentation, and that the name of the author(s) not be used in * advertising or publicity pertaining to distribution of the software without - * specific, written prior permission. Keith Packard makes no + * specific, written prior permission. The authors make no * representations about the suitability of this software for any purpose. It * is provided "as is" without express or implied warranty. * - * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO - * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR @@ -22,7 +22,7 @@ */ #include "fcint.h" -#include "../fc-arch/fcarch.h" +#include "fcarch.h" #include #include #include @@ -41,6 +41,7 @@ #define O_BINARY 0 #endif + struct MD5Context { FcChar32 buf[4]; FcChar32 bits[2]; @@ -86,16 +87,16 @@ typedef __int64 INT64; */ int -FcStat (const char *file, struct stat *statb) +FcStat (FcConfig *config, const FcChar8 *file, struct stat *statb) { WIN32_FILE_ATTRIBUTE_DATA wfad; char full_path_name[MAX_PATH]; char *basename; DWORD rc; - + if (!GetFileAttributesEx (file, GetFileExInfoStandard, &wfad)) return -1; - + statb->st_dev = 0; /* Calculate a pseudo inode number as a hash of the full path name. @@ -108,7 +109,7 @@ FcStat (const char *file, struct stat *statb) rc = GetLongPathName (full_path_name, full_path_name, sizeof (full_path_name)); statb->st_ino = FcStringHash (full_path_name); - + statb->st_mode = _S_IREAD | _S_IWRITE; statb->st_mode |= (statb->st_mode >> 3) | (statb->st_mode >> 6); @@ -116,21 +117,39 @@ FcStat (const char *file, struct stat *statb) statb->st_mode |= _S_IFDIR; else statb->st_mode |= _S_IFREG; - + statb->st_nlink = 1; statb->st_uid = statb->st_gid = 0; statb->st_rdev = 0; - + if (wfad.nFileSizeHigh > 0) return -1; statb->st_size = wfad.nFileSizeLow; - + statb->st_atime = (*(INT64 *)&wfad.ftLastAccessTime)/10000000 - EPOCH_OFFSET; statb->st_mtime = (*(INT64 *)&wfad.ftLastWriteTime)/10000000 - EPOCH_OFFSET; statb->st_ctime = statb->st_mtime; - + return 0; } + +#else + +int +FcStat (FcConfig *config, const FcChar8 *file, struct stat *statb) +{ + int ret; + FcChar8 *fullFile = FcConfigGetRootPlus (config, file); + + if (fullFile) + file = fullFile; + ret = stat ((char *) file, statb); + if (fullFile) + FcStrFree (fullFile); + + return ret; +} + #endif static const char bin2hex[] = { '0', '1', '2', '3', @@ -194,15 +213,21 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config) } static int -FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat) +FcDirCacheOpenFile (FcConfig *config, const FcChar8 *cache_file, struct stat *file_stat) { int fd; + FcChar8 *fullFile; #ifdef _WIN32 - if (FcStat (cache_file, file_stat) < 0) + if (FcStat (config, cache_file, file_stat) < 0) return -1; #endif - fd = open((char *) cache_file, O_RDONLY | O_BINARY); + fullFile = FcConfigGetRootPlus (config, cache_file); + if (fullFile) + cache_file = fullFile; + fd = open ((char *) cache_file, O_RDONLY | O_BINARY); + if (fullFile) + FcStrFree (fullFile); if (fd < 0) return fd; #ifndef _WIN32 @@ -215,14 +240,14 @@ FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat) return fd; } -/* +/* * Look for a cache file for the specified dir. Attempt * to use each one we find, stopping when the callback * indicates success */ static FcBool -FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, - FcBool (*callback) (int fd, struct stat *fd_stat, +FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, + FcBool (*callback) (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure), void *closure, FcChar8 **cache_file_ret) { @@ -233,7 +258,7 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, struct stat file_stat, dir_stat; FcBool ret = FcFalse; - if (FcStat ((char *) dir, &dir_stat) < 0) + if (FcStat (config, dir, &dir_stat) < 0) return FcFalse; FcDirCacheBasename (dir, cache_base); @@ -247,9 +272,9 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base); if (!cache_hashed) break; - fd = FcDirCacheOpenFile (cache_hashed, &file_stat); + fd = FcDirCacheOpenFile (config, cache_hashed, &file_stat); if (fd >= 0) { - ret = (*callback) (fd, &file_stat, &dir_stat, closure); + ret = (*callback) (config, fd, &file_stat, &dir_stat, closure); close (fd); if (ret) { @@ -263,7 +288,7 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, FcStrFree (cache_hashed); } FcStrListDone (list); - + return ret; } @@ -362,7 +387,7 @@ FcCacheInsert (FcCache *cache, struct stat *cache_stat) update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel]; fcCacheMaxLevel = level; } - + s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *)); if (!s) return FcFalse; @@ -382,7 +407,7 @@ FcCacheInsert (FcCache *cache, struct stat *cache_stat) s->cache_ino = 0; s->cache_mtime = 0; } - + /* * Insert into all fcCacheChains */ @@ -509,13 +534,13 @@ FcCacheFini (void) } static FcBool -FcCacheTimeValid (FcCache *cache, struct stat *dir_stat) +FcCacheTimeValid (FcConfig *config, FcCache *cache, struct stat *dir_stat) { struct stat dir_static; if (!dir_stat) { - if (FcStat ((const char *) FcCacheDir (cache), &dir_static) < 0) + if (FcStat (config, FcCacheDir (cache), &dir_static) < 0) return FcFalse; dir_stat = &dir_static; } @@ -529,7 +554,7 @@ FcCacheTimeValid (FcCache *cache, struct stat *dir_stat) * Map a cache file into memory */ static FcCache * -FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat) +FcDirCacheMapFd (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat) { FcCache *cache; FcBool allocated = FcFalse; @@ -538,7 +563,13 @@ FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat) return NULL; cache = FcCacheFindByStat (fd_stat); if (cache) - return cache; + { + if (FcCacheTimeValid (config, cache, dir_stat)) + return cache; + FcDirCacheUnload (cache); + cache = NULL; + } + /* * Lage cache files are mmap'ed, smaller cache files are read. This * balances the system cost of mmap against per-process memory usage. @@ -547,6 +578,8 @@ FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat) { #if defined(HAVE_MMAP) || defined(__CYGWIN__) cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0); + if (cache == MAP_FAILED) + cache = NULL; #elif defined(_WIN32) { HANDLE hFileMap; @@ -556,7 +589,7 @@ FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat) PAGE_READONLY, 0, 0, NULL); if (hFileMap != NULL) { - cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, + cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, fd_stat->st_size); CloseHandle (hFileMap); } @@ -575,11 +608,11 @@ FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat) return NULL; } allocated = FcTrue; - } - if (cache->magic != FC_CACHE_MAGIC_MMAP || + } + if (cache->magic != FC_CACHE_MAGIC_MMAP || cache->version < FC_CACHE_CONTENT_VERSION || cache->size != fd_stat->st_size || - !FcCacheTimeValid (cache, dir_stat) || + !FcCacheTimeValid (config, cache, dir_stat) || !FcCacheInsert (cache, fd_stat)) { if (allocated) @@ -618,9 +651,9 @@ FcDirCacheUnload (FcCache *cache) } static FcBool -FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure) +FcDirCacheMapHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure) { - FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat); + FcCache *cache = FcDirCacheMapFd (config, fd, fd_stat, dir_stat); if (!cache) return FcFalse; @@ -642,6 +675,12 @@ FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file) FcCache * FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) +{ + return FcDirCacheLoadFile2 (cache_file, FcConfigGetCurrent (), file_stat); +} + +FcCache * +FcDirCacheLoadFile2 (const FcChar8 *cache_file, FcConfig *config, struct stat *file_stat) { int fd; FcCache *cache; @@ -649,10 +688,10 @@ FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) if (!file_stat) file_stat = &my_file_stat; - fd = FcDirCacheOpenFile (cache_file, file_stat); + fd = FcDirCacheOpenFile (config, cache_file, file_stat); if (fd < 0) return NULL; - cache = FcDirCacheMapFd (fd, file_stat, NULL); + cache = FcDirCacheMapFd (config, fd, file_stat, NULL); close (fd); return cache; } @@ -662,11 +701,11 @@ FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) * the magic number and the size field */ static FcBool -FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure) +FcDirCacheValidateHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure) { FcBool ret = FcTrue; FcCache c; - + if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache)) ret = FcFalse; else if (c.magic != FC_CACHE_MAGIC_MMAP) @@ -683,7 +722,7 @@ FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, v static FcBool FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config) { - return FcDirCacheProcess (config, dir, + return FcDirCacheProcess (config, dir, FcDirCacheValidateHelper, NULL, NULL); } @@ -692,7 +731,7 @@ FcBool FcDirCacheValid (const FcChar8 *dir) { FcConfig *config; - + config = FcConfigGetCurrent (); if (!config) return FcFalse; @@ -709,18 +748,16 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt FcSerialize *serialize = FcSerializeCreate (); FcCache *cache; int i; - intptr_t cache_offset; - intptr_t dirs_offset; FcChar8 *dir_serialize; intptr_t *dirs_serialize; FcFontSet *set_serialize; - + if (!serialize) return NULL; /* * Space for cache structure */ - cache_offset = FcSerializeReserve (serialize, sizeof (FcCache)); + FcSerializeReserve (serialize, sizeof (FcCache)); /* * Directory name */ @@ -729,7 +766,7 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt /* * Subdirs */ - dirs_offset = FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *)); + FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *)); for (i = 0; i < dirs->num; i++) if (!FcStrSerializeAlloc (serialize, dirs->strs[i])) goto bail1; @@ -739,7 +776,7 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt */ if (!FcFontSetSerializeAlloc (serialize, set)) goto bail1; - + /* Serialize layout complete. Now allocate space and fill it */ cache = malloc (serialize->size); if (!cache) @@ -761,7 +798,7 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt if (!dir_serialize) goto bail2; cache->dir = FcPtrToOffset (cache, dir_serialize); - + /* * Serialize sub dirs */ @@ -770,14 +807,14 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt goto bail2; cache->dirs = FcPtrToOffset (cache, dirs_serialize); cache->dirs_count = dirs->num; - for (i = 0; i < dirs->num; i++) + for (i = 0; i < dirs->num; i++) { FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]); if (!d_serialize) goto bail2; dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize); } - + /* * Serialize font set */ @@ -787,7 +824,7 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt cache->set = FcPtrToOffset (cache, set_serialize); FcSerializeDestroy (serialize); - + FcCacheInsert (cache, NULL); return cache; @@ -809,10 +846,10 @@ FcMakeDirectory (const FcChar8 *dir) { FcChar8 *parent; FcBool ret; - + if (strlen ((char *) dir) == 0) return FcFalse; - + parent = FcStrDirname (dir); if (!parent) return FcFalse; @@ -838,17 +875,33 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) FcStrList *list; FcChar8 *cache_dir = NULL; FcChar8 *test_dir; + FcChar8 *full_test_dir; + FcBool free_test_dir; + FcCacheSkip *skip; + struct stat cache_stat; int magic; int written; /* * Write it to the first directory in the list which is writable */ - + list = FcStrListCreate (config->cacheDirs); if (!list) return FcFalse; + free_test_dir = FcFalse; while ((test_dir = FcStrListNext (list))) { + if (free_test_dir) + FcStrFree (full_test_dir); + free_test_dir = FcFalse; + + full_test_dir = FcConfigGetRootPlus (config, test_dir); + if (full_test_dir) + { + free_test_dir = FcTrue; + test_dir = full_test_dir; + } + if (access ((char *) test_dir, W_OK|X_OK) == 0) { cache_dir = test_dir; @@ -878,12 +931,12 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) } FcStrListDone (list); if (!cache_dir) - return FcFalse; + goto bail0; FcDirCacheBasename (dir, cache_base); cache_hashed = FcStrPlus (cache_dir, cache_base); if (!cache_hashed) - return FcFalse; + goto bail0; if (FcDebug () & FC_DBG_CACHE) printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n", @@ -893,27 +946,27 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) if (!atomic) goto bail1; - if (!FcAtomicLock (atomic)) + if (!FcAtomicLock2 (config, atomic)) goto bail3; fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666); if (fd == -1) goto bail4; - + /* Temporarily switch magic to MMAP while writing to file */ magic = cache->magic; if (magic != FC_CACHE_MAGIC_MMAP) cache->magic = FC_CACHE_MAGIC_MMAP; - + /* * Write cache contents to file */ written = write (fd, cache, cache->size); - + /* Switch magic back */ if (magic != FC_CACHE_MAGIC_MMAP) cache->magic = magic; - + if (written != cache->size) { perror ("write cache"); @@ -923,6 +976,20 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) close(fd); if (!FcAtomicReplaceOrig(atomic)) goto bail4; + + /* If the file is small, update the cache chain entry such that the + * new cache file is not read again. If it's large, we don't do that + * such that we reload it, using mmap, which is shared across processes. + */ + if (cache->size < FC_CACHE_MIN_MMAP && + (skip = FcCacheFindByAddr (cache)) && + FcStat (config, cache_hashed, &cache_stat)) + { + skip->cache_dev = cache_stat.st_dev; + skip->cache_ino = cache_stat.st_ino; + skip->cache_mtime = cache_stat.st_mtime; + } + FcStrFree (cache_hashed); FcAtomicUnlock (atomic); FcAtomicDestroy (atomic); @@ -936,6 +1003,125 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) FcAtomicDestroy (atomic); bail1: FcStrFree (cache_hashed); + bail0: + if (free_test_dir) + FcStrFree (full_test_dir); + return FcFalse; +} + +static void +align_fd (int fd, off_t *pos, int align) +{ + int adj = *pos & (align - 1); + if (adj) + { + lseek (fd, adj, SEEK_CUR); + *pos += adj; + } +} + +static uint64_t +_conv_num (int src_fd, int dst_fd, off_t *src_pos, off_t *dst_pos, + FcBool src_be, FcBool dst_be, int src_psize, int dst_psize, + int src_read, int dst_write) +{ + unsigned char src[8], dst[8], *s, *d; + int i, src_step, dst_step; + uint64_t ret; + + align_fd (src_fd, src_pos, src_read); + read (src_fd, src, src_read); + *src_pos += src_read; + + if (src_be) + s = &src[src_read - 1], src_step = -1; + else + s = src, src_step = 1; + if (dst_be) + d = &dst[dst_write - 1], dst_step = -1; + else + d = dst, dst_step = 1; + + ret = 0; + for (i = 0; i < src_read; ++i) + { + ret |= ((uint64_t) *s) << (8 * i); + *d = *s; + s += src_step; + d += dst_step; + } + for (; i < dst_write; ++i) + { + *d = 0; + d += dst_step; + } + + align_fd (dst_fd, dst_pos, dst_write); + write (dst_fd, dst, dst_write); + *dst_pos += dst_write; + + return ret; +} +#define conv_num(r, w) _conv_num (src_fd, dst_fd, &src_pos, &dst_pos, src_be, dst_be, src_psize, dst_psize, r, w) +#define conv_ptr() conv_num (src_psize, dst_psize) +#define conv_int() conv_num (4, 4) + +FcBool +FcDirCacheConvert (const FcChar8 *src_cache, const FcChar8 *src_type, + const FcChar8 *dst_cache, const FcChar8 *dst_type) +{ + /* See fcarch.h for the spec used here */ + FcCache c; + FcBool src_be; + FcBool dst_be; + int src_psize; + int dst_psize; + int src_dalign; + int dst_dalign; + int src_fd; + int dst_fd; + off_t src_pos; + off_t dst_pos; + struct stat src_st; + + src_be = !strncmp ((const char *) src_type, "be", 2); + dst_be = !strncmp ((const char *) dst_type, "be", 2); + + src_psize = !strncmp ((const char *) src_type + 2, "32", 2) ? 4 : 8; + dst_psize = !strncmp ((const char *) dst_type + 2, "32", 2) ? 4 : 8; + + if (src_psize == 4) + src_dalign = !strncmp ((const char *) src_type + 4, "d4", 2) ? 4 : 8; + else + src_dalign = src_psize; + + if (dst_psize == 4) + dst_dalign = !strncmp ((const char *) dst_type + 4, "d4", 2) ? 4 : 8; + else + dst_dalign = dst_psize; + + src_fd = open ((const char *) src_cache, O_RDONLY); + if (src_fd == -1) + return FcFalse; + dst_fd = open ((const char *) dst_cache, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); + if (dst_fd == -1) + { + close (src_fd); + return FcFalse; + } + src_pos = dst_pos = 0; + + /* Process FcCache at start of file */ + c.magic = conv_int (); + c.version = conv_int (); + c.size = conv_ptr (); + c.dir = conv_ptr (); + c.dirs = conv_ptr (); + c.dirs_count = conv_int (); + c.set = conv_ptr (); + c.mtime = conv_int (); + align_fd (dst_fd, &dst_pos, dst_psize); + return FcFalse; } @@ -958,7 +1144,7 @@ FcCacheCopySet args1(const FcCache *c) FcFontSet *old = FcCacheSet (c); FcFontSet *new = FcFontSetCreate (); int i; - + if (!new) return NULL; for (i = 0; i < old->nfont; i++) @@ -1092,7 +1278,7 @@ static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned } /* - * Final wrapup - pad to 64-byte boundary with the bit pattern + * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) @@ -1133,7 +1319,7 @@ static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) MD5Transform(ctx->buf, (FcChar32 *) ctx->in); byteReverse((unsigned char *) ctx->buf, 4); memcpy(digest, ctx->buf, 16); - memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ + memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ }