2 * Copyright © 2000 Keith Packard
3 * Copyright © 2005 Patrick Lam
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the author(s) not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission. The authors make no
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
15 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
31 #include <sys/types.h>
34 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
36 # include <sys/mman.h>
39 #include <sys/locking.h>
53 static void MD5Init(struct MD5Context *ctx);
54 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
55 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
56 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
58 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
61 FcCacheIsMmapSafe (int fd)
64 MMAP_NOT_INITIALIZED = 0,
69 static void *static_status;
71 status = (intptr_t) fc_atomic_ptr_get (&static_status);
73 if (status == MMAP_NOT_INITIALIZED)
75 const char *env = getenv ("FONTCONFIG_USE_MMAP");
77 if (env && FcNameBool ((const FcChar8 *) env, &use))
78 status = use ? MMAP_USE : MMAP_DONT_USE;
80 status = MMAP_CHECK_FS;
81 (void) fc_atomic_ptr_cmpexch (&static_status, NULL, (void *) status);
84 if (status == MMAP_CHECK_FS)
85 return FcIsFsMmapSafe (fd);
87 return status == MMAP_USE;
91 static const char bin2hex[] = { '0', '1', '2', '3',
97 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
99 unsigned char hash[16];
102 struct MD5Context ctx;
105 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
107 MD5Final (hash, &ctx);
110 hex_hash = cache_base + 1;
111 for (cnt = 0; cnt < 16; ++cnt)
113 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
114 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
117 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
123 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
125 FcChar8 *cache_hashed = NULL;
126 FcChar8 cache_base[CACHEBASE_LEN];
129 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
131 FcDirCacheBasename (dir, cache_base);
133 list = FcStrListCreate (config->cacheDirs);
137 while ((cache_dir = FcStrListNext (list)))
140 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
142 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
145 (void) unlink ((char *) cache_hashed);
146 FcStrFree (cache_hashed);
148 FcStrListDone (list);
149 /* return FcFalse if something went wrong */
156 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
161 if (FcStat (cache_file, file_stat) < 0)
164 fd = FcOpen((char *) cache_file, O_RDONLY | O_BINARY);
168 if (fstat (fd, file_stat) < 0)
178 * Look for a cache file for the specified dir. Attempt
179 * to use each one we find, stopping when the callback
183 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
184 FcBool (*callback) (FcConfig *config, int fd, struct stat *fd_stat,
185 struct stat *dir_stat, void *closure),
186 void *closure, FcChar8 **cache_file_ret)
189 FcChar8 cache_base[CACHEBASE_LEN];
191 FcChar8 *cache_dir, *d;
192 struct stat file_stat, dir_stat;
193 FcBool ret = FcFalse;
194 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
197 d = FcStrBuildFilename (sysroot, dir, NULL);
200 if (FcStatChecksum (d, &dir_stat) < 0)
207 FcDirCacheBasename (dir, cache_base);
209 list = FcStrListCreate (config->cacheDirs);
213 while ((cache_dir = FcStrListNext (list)))
215 FcChar8 *cache_hashed;
218 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
220 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
223 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
225 ret = (*callback) (config, fd, &file_stat, &dir_stat, closure);
230 *cache_file_ret = cache_hashed;
232 FcStrFree (cache_hashed);
236 FcStrFree (cache_hashed);
238 FcStrListDone (list);
243 #define FC_CACHE_MIN_MMAP 1024
246 * Skip list element, make sure the 'next' pointer is the last thing
247 * in the structure, it will be allocated large enough to hold all
248 * of the necessary pointers
251 typedef struct _FcCacheSkip FcCacheSkip;
253 struct _FcCacheSkip {
260 long cache_mtime_nano;
261 FcCacheSkip *next[1];
265 * The head of the skip list; pointers for every possible level
266 * in the skip list, plus the largest level in the list
269 #define FC_CACHE_MAX_LEVEL 16
271 /* Protected by cache_lock below */
272 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
273 static int fcCacheMaxLevel;
276 static FcMutex *cache_lock;
283 lock = fc_atomic_ptr_get (&cache_lock);
285 lock = (FcMutex *) malloc (sizeof (FcMutex));
287 if (!fc_atomic_ptr_cmpexch (&cache_lock, NULL, lock)) {
288 FcMutexFinish (lock);
293 /* Initialize random state */
303 FcMutexUnlock (cache_lock);
310 lock = fc_atomic_ptr_get (&cache_lock);
311 if (lock && fc_atomic_ptr_cmpexch (&cache_lock, lock, NULL)) {
312 FcMutexFinish (lock);
320 * Generate a random level number, distributed
321 * so that each level is 1/4 as likely as the one before
323 * Note that level numbers run 1 <= level <= MAX_LEVEL
328 /* tricky bit -- each bit is '1' 75% of the time */
329 long int bits = FcRandom () | FcRandom ();
332 while (++level < FC_CACHE_MAX_LEVEL)
342 * Insert cache into the list
345 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
347 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
348 FcCacheSkip *s, **next;
354 * Find links along each chain
356 next = fcCacheChains;
357 for (i = fcCacheMaxLevel; --i >= 0; )
359 for (; (s = next[i]); next = s->next)
360 if (s->cache > cache)
362 update[i] = &next[i];
366 * Create new list element
368 level = random_level ();
369 if (level > fcCacheMaxLevel)
371 level = fcCacheMaxLevel + 1;
372 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
373 fcCacheMaxLevel = level;
376 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
381 s->size = cache->size;
382 FcRefInit (&s->ref, 1);
385 s->cache_dev = cache_stat->st_dev;
386 s->cache_ino = cache_stat->st_ino;
387 s->cache_mtime = cache_stat->st_mtime;
388 #ifdef HAVE_STRUCT_STAT_ST_MTIM
389 s->cache_mtime_nano = cache_stat->st_mtim.tv_nsec;
391 s->cache_mtime_nano = 0;
399 s->cache_mtime_nano = 0;
403 * Insert into all fcCacheChains
405 for (i = 0; i < level; i++)
407 s->next[i] = *update[i];
416 FcCacheFindByAddrUnlocked (void *object)
419 FcCacheSkip **next = fcCacheChains;
426 * Walk chain pointers one level at a time
428 for (i = fcCacheMaxLevel; --i >= 0;)
429 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
430 next = next[i]->next;
435 if (s && (char *) object < ((char *) s->cache + s->size))
441 FcCacheFindByAddr (void *object)
445 ret = FcCacheFindByAddrUnlocked (object);
451 FcCacheRemoveUnlocked (FcCache *cache)
453 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
454 FcCacheSkip *s, **next;
458 * Find links along each chain
460 next = fcCacheChains;
461 for (i = fcCacheMaxLevel; --i >= 0; )
463 for (; (s = next[i]); next = s->next)
464 if (s->cache >= cache)
466 update[i] = &next[i];
469 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
470 *update[i] = s->next[i];
471 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
477 FcCacheFindByStat (struct stat *cache_stat)
482 for (s = fcCacheChains[0]; s; s = s->next[0])
483 if (s->cache_dev == cache_stat->st_dev &&
484 s->cache_ino == cache_stat->st_ino &&
485 s->cache_mtime == cache_stat->st_mtime)
487 #ifdef HAVE_STRUCT_STAT_ST_MTIM
488 if (s->cache_mtime != cache_stat->st_mtim.tv_nsec)
500 FcDirCacheDisposeUnlocked (FcCache *cache)
502 FcCacheRemoveUnlocked (cache);
504 switch (cache->magic) {
505 case FC_CACHE_MAGIC_ALLOC:
508 case FC_CACHE_MAGIC_MMAP:
509 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
510 munmap (cache, cache->size);
511 #elif defined(_WIN32)
512 UnmapViewOfFile (cache);
519 FcCacheObjectReference (void *object)
521 FcCacheSkip *skip = FcCacheFindByAddr (object);
524 FcRefInc (&skip->ref);
528 FcCacheObjectDereference (void *object)
533 skip = FcCacheFindByAddrUnlocked (object);
536 if (FcRefDec (&skip->ref) == 1)
537 FcDirCacheDisposeUnlocked (skip->cache);
547 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
548 assert (fcCacheChains[i] == NULL);
549 assert (fcCacheMaxLevel == 0);
555 FcCacheTimeValid (FcConfig *config, FcCache *cache, struct stat *dir_stat)
557 struct stat dir_static;
558 FcBool fnano = FcTrue;
562 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
566 d = FcStrBuildFilename (sysroot, FcCacheDir (cache), NULL);
568 d = FcStrdup (FcCacheDir (cache));
569 if (FcStatChecksum (d, &dir_static) < 0)
575 dir_stat = &dir_static;
577 #ifdef HAVE_STRUCT_STAT_ST_MTIM
578 fnano = (cache->checksum_nano == dir_stat->st_mtim.tv_nsec);
579 if (FcDebug () & FC_DBG_CACHE)
580 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d.%ld dir checksum %d.%ld\n",
581 FcCacheDir (cache), cache->checksum, (long)cache->checksum_nano, (int) dir_stat->st_mtime, dir_stat->st_mtim.tv_nsec);
583 if (FcDebug () & FC_DBG_CACHE)
584 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
585 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
588 return cache->checksum == (int) dir_stat->st_mtime && fnano;
592 FcCacheOffsetsValid (FcCache *cache)
594 char *base = (char *)cache;
595 char *end = base + cache->size;
600 if (cache->dir < 0 || cache->dir > cache->size - sizeof (intptr_t) ||
601 memchr (base + cache->dir, '\0', cache->size - cache->dir) == NULL)
604 if (cache->dirs < 0 || cache->dirs >= cache->size ||
605 cache->dirs_count < 0 ||
606 cache->dirs_count > (cache->size - cache->dirs) / sizeof (intptr_t))
609 dirs = FcCacheDirs (cache);
612 for (i = 0; i < cache->dirs_count; i++)
617 dirs[i] > end - (char *) dirs - sizeof (intptr_t))
620 dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
621 if (memchr (dir, '\0', end - (char *) dir) == NULL)
626 if (cache->set < 0 || cache->set > cache->size - sizeof (FcFontSet))
629 fs = FcCacheSet (cache);
632 if (fs->nfont > (end - (char *) fs) / sizeof (FcPattern))
635 if (fs->fonts != 0 && !FcIsEncodedOffset(fs->fonts))
638 for (i = 0; i < fs->nfont; i++)
640 FcPattern *font = FcFontSetFont (fs, i);
644 if ((char *) font < base ||
645 (char *) font > end - sizeof (FcFontSet) ||
646 font->elts_offset < 0 ||
647 font->elts_offset > end - (char *) font ||
648 font->num > (end - (char *) font - font->elts_offset) / sizeof (FcPatternElt))
652 e = FcPatternElts(font);
653 if (e->values != 0 && !FcIsEncodedOffset(e->values))
656 for (j = font->num, l = FcPatternEltValues(e); j >= 0 && l; j--, l = FcValueListNext(l))
657 if (l->next != NULL && !FcIsEncodedOffset(l->next))
668 * Map a cache file into memory
671 FcDirCacheMapFd (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat)
674 FcBool allocated = FcFalse;
676 if (fd_stat->st_size > INTPTR_MAX ||
677 fd_stat->st_size < (int) sizeof (FcCache))
679 cache = FcCacheFindByStat (fd_stat);
682 if (FcCacheTimeValid (config, cache, dir_stat))
684 FcDirCacheUnload (cache);
689 * Large cache files are mmap'ed, smaller cache files are read. This
690 * balances the system cost of mmap against per-process memory usage.
692 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
694 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
695 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
696 #if (HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
697 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
699 if (cache == MAP_FAILED)
701 #elif defined(_WIN32)
706 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
707 PAGE_READONLY, 0, 0, NULL);
708 if (hFileMap != NULL)
710 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
712 CloseHandle (hFileMap);
719 cache = malloc (fd_stat->st_size);
723 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
730 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
731 cache->version < FC_CACHE_VERSION_NUMBER ||
732 cache->size != (intptr_t) fd_stat->st_size ||
733 !FcCacheOffsetsValid (cache) ||
734 !FcCacheTimeValid (config, cache, dir_stat) ||
735 !FcCacheInsert (cache, fd_stat))
741 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
742 munmap (cache, fd_stat->st_size);
743 #elif defined(_WIN32)
744 UnmapViewOfFile (cache);
750 /* Mark allocated caches so they're freed rather than unmapped */
752 cache->magic = FC_CACHE_MAGIC_ALLOC;
758 FcDirCacheReference (FcCache *cache, int nref)
760 FcCacheSkip *skip = FcCacheFindByAddr (cache);
763 FcRefAdd (&skip->ref, nref);
767 FcDirCacheUnload (FcCache *cache)
769 FcCacheObjectDereference (cache);
773 FcDirCacheMapHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
775 FcCache *cache = FcDirCacheMapFd (config, fd, fd_stat, dir_stat);
779 *((FcCache **) closure) = cache;
784 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
786 FcCache *cache = NULL;
788 if (!FcDirCacheProcess (config, dir,
797 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
801 struct stat my_file_stat;
804 file_stat = &my_file_stat;
805 fd = FcDirCacheOpenFile (cache_file, file_stat);
808 cache = FcDirCacheMapFd (FcConfigGetCurrent (), fd, file_stat, NULL);
814 * Validate a cache file by reading the header and checking
815 * the magic number and the size field
818 FcDirCacheValidateHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
823 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
825 else if (c.magic != FC_CACHE_MAGIC_MMAP)
827 else if (c.version < FC_CACHE_VERSION_NUMBER)
829 else if (fd_stat->st_size != c.size)
831 else if (c.checksum != (int) dir_stat->st_mtime)
833 #ifdef HAVE_STRUCT_STAT_ST_MTIM
834 else if (c.checksum_nano != dir_stat->st_mtim.tv_nsec)
841 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
843 return FcDirCacheProcess (config, dir,
844 FcDirCacheValidateHelper,
849 FcDirCacheValid (const FcChar8 *dir)
853 config = FcConfigGetCurrent ();
857 return FcDirCacheValidConfig (dir, config);
861 * Build a cache structure from the given contents
864 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
866 FcSerialize *serialize = FcSerializeCreate ();
869 FcChar8 *dir_serialize;
870 intptr_t *dirs_serialize;
871 FcFontSet *set_serialize;
876 * Space for cache structure
878 FcSerializeReserve (serialize, sizeof (FcCache));
882 if (!FcStrSerializeAlloc (serialize, dir))
887 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
888 for (i = 0; i < dirs->num; i++)
889 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
895 if (!FcFontSetSerializeAlloc (serialize, set))
898 /* Serialize layout complete. Now allocate space and fill it */
899 cache = malloc (serialize->size);
902 /* shut up valgrind */
903 memset (cache, 0, serialize->size);
905 serialize->linear = cache;
907 cache->magic = FC_CACHE_MAGIC_ALLOC;
908 cache->version = FC_CACHE_VERSION_NUMBER;
909 cache->size = serialize->size;
910 cache->checksum = (int) dir_stat->st_mtime;
911 #ifdef HAVE_STRUCT_STAT_ST_MTIM
912 cache->checksum_nano = dir_stat->st_mtim.tv_nsec;
916 * Serialize directory name
918 dir_serialize = FcStrSerialize (serialize, dir);
921 cache->dir = FcPtrToOffset (cache, dir_serialize);
926 dirs_serialize = FcSerializePtr (serialize, dirs);
929 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
930 cache->dirs_count = dirs->num;
931 for (i = 0; i < dirs->num; i++)
933 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
936 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
942 set_serialize = FcFontSetSerialize (serialize, set);
945 cache->set = FcPtrToOffset (cache, set_serialize);
947 FcSerializeDestroy (serialize);
949 FcCacheInsert (cache, NULL);
956 FcSerializeDestroy (serialize);
961 FcDirCacheRebuild (FcCache *cache, struct stat *dir_stat, FcStrSet *dirs)
964 FcFontSet *set = FcFontSetDeserialize (FcCacheSet (cache));
965 const FcChar8 *dir = FcCacheDir (cache);
967 new = FcDirCacheBuild (set, dir, dir_stat, dirs);
968 FcFontSetDestroy (set);
973 /* write serialized state to the cache file */
975 FcDirCacheWrite (FcCache *cache, FcConfig *config)
977 FcChar8 *dir = FcCacheDir (cache);
978 FcChar8 cache_base[CACHEBASE_LEN];
979 FcChar8 *cache_hashed;
983 FcChar8 *cache_dir = NULL;
984 FcChar8 *test_dir, *d = NULL;
986 struct stat cache_stat;
989 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
992 * Write it to the first directory in the list which is writable
995 list = FcStrListCreate (config->cacheDirs);
998 while ((test_dir = FcStrListNext (list)))
1003 d = FcStrBuildFilename (sysroot, test_dir, NULL);
1005 d = FcStrCopyFilename (test_dir);
1007 if (access ((char *) d, W_OK) == 0)
1009 cache_dir = FcStrCopyFilename (d);
1015 * If the directory doesn't exist, try to create it
1017 if (access ((char *) d, F_OK) == -1) {
1018 if (FcMakeDirectory (d))
1020 cache_dir = FcStrCopyFilename (d);
1021 /* Create CACHEDIR.TAG */
1022 FcDirCacheCreateTagFile (d);
1027 * Otherwise, try making it writable
1029 else if (chmod ((char *) d, 0755) == 0)
1031 cache_dir = FcStrCopyFilename (d);
1032 /* Try to create CACHEDIR.TAG too */
1033 FcDirCacheCreateTagFile (d);
1040 FcStrListDone (list);
1044 FcDirCacheBasename (dir, cache_base);
1045 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
1048 FcStrFree (cache_dir);
1050 if (FcDebug () & FC_DBG_CACHE)
1051 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
1054 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1058 if (!FcAtomicLock (atomic))
1061 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
1065 /* Temporarily switch magic to MMAP while writing to file */
1066 magic = cache->magic;
1067 if (magic != FC_CACHE_MAGIC_MMAP)
1068 cache->magic = FC_CACHE_MAGIC_MMAP;
1071 * Write cache contents to file
1073 written = write (fd, cache, cache->size);
1075 /* Switch magic back */
1076 if (magic != FC_CACHE_MAGIC_MMAP)
1077 cache->magic = magic;
1079 if (written != cache->size)
1081 perror ("write cache");
1086 if (!FcAtomicReplaceOrig(atomic))
1089 /* If the file is small, update the cache chain entry such that the
1090 * new cache file is not read again. If it's large, we don't do that
1091 * such that we reload it, using mmap, which is shared across processes.
1093 if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat))
1096 if ((skip = FcCacheFindByAddrUnlocked (cache)))
1098 skip->cache_dev = cache_stat.st_dev;
1099 skip->cache_ino = cache_stat.st_ino;
1100 skip->cache_mtime = cache_stat.st_mtime;
1101 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1102 skip->cache_mtime_nano = cache_stat.st_mtim.tv_nsec;
1104 skip->cache_mtime_nano = 0;
1110 FcStrFree (cache_hashed);
1111 FcAtomicUnlock (atomic);
1112 FcAtomicDestroy (atomic);
1118 FcAtomicUnlock (atomic);
1120 FcAtomicDestroy (atomic);
1122 FcStrFree (cache_hashed);
1127 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1132 FcBool ret = FcTrue;
1135 struct stat target_stat;
1136 const FcChar8 *sysroot;
1138 /* FIXME: this API needs to support non-current FcConfig */
1139 sysroot = FcConfigGetSysRoot (NULL);
1141 dir = FcStrBuildFilename (sysroot, cache_dir, NULL);
1143 dir = FcStrCopyFilename (cache_dir);
1146 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1149 if (access ((char *) dir, W_OK) != 0)
1151 if (verbose || FcDebug () & FC_DBG_CACHE)
1152 printf ("%s: not cleaning %s cache directory\n", dir,
1153 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
1156 if (verbose || FcDebug () & FC_DBG_CACHE)
1157 printf ("%s: cleaning cache directory\n", dir);
1158 d = opendir ((char *) dir);
1161 perror ((char *) dir);
1165 while ((ent = readdir (d)))
1168 const FcChar8 *target_dir;
1170 if (ent->d_name[0] == '.')
1172 /* skip cache files for different architectures and */
1173 /* files which are not cache files at all */
1174 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1175 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1178 file_name = FcStrBuildFilename (dir, (FcChar8 *)ent->d_name, NULL);
1181 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", dir);
1186 cache = FcDirCacheLoadFile (file_name, NULL);
1189 if (verbose || FcDebug () & FC_DBG_CACHE)
1190 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
1197 target_dir = FcCacheDir (cache);
1199 s = FcStrBuildFilename (sysroot, target_dir, NULL);
1201 s = FcStrdup (target_dir);
1202 if (stat ((char *) s, &target_stat) < 0)
1204 if (verbose || FcDebug () & FC_DBG_CACHE)
1205 printf ("%s: %s: missing directory: %s \n",
1206 dir, ent->d_name, s);
1209 FcDirCacheUnload (cache);
1214 if (unlink ((char *) file_name) < 0)
1216 perror ((char *) file_name);
1220 FcStrFree (file_name);
1231 FcDirCacheLock (const FcChar8 *dir,
1234 FcChar8 *cache_hashed = NULL;
1235 FcChar8 cache_base[CACHEBASE_LEN];
1238 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
1241 FcDirCacheBasename (dir, cache_base);
1242 list = FcStrListCreate (config->cacheDirs);
1246 while ((cache_dir = FcStrListNext (list)))
1249 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
1251 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
1254 fd = FcOpen ((const char *)cache_hashed, O_RDWR);
1255 FcStrFree (cache_hashed);
1256 /* No caches in that directory. simply retry with another one */
1260 if (_locking (fd, _LK_LOCK, 1) == -1)
1265 fl.l_type = F_WRLCK;
1266 fl.l_whence = SEEK_SET;
1269 fl.l_pid = getpid ();
1270 if (fcntl (fd, F_SETLKW, &fl) == -1)
1276 FcStrListDone (list);
1279 FcStrListDone (list);
1286 FcDirCacheUnlock (int fd)
1291 _locking (fd, _LK_UNLCK, 1);
1295 fl.l_type = F_UNLCK;
1296 fl.l_whence = SEEK_SET;
1299 fl.l_pid = getpid ();
1300 fcntl (fd, F_SETLK, &fl);
1307 * Hokey little macro trick to permit the definitions of C functions
1308 * with the same name as CPP macros
1310 #define args1(x) (x)
1311 #define args2(x,y) (x,y)
1314 FcCacheDir args1(const FcCache *c)
1316 return FcCacheDir (c);
1320 FcCacheCopySet args1(const FcCache *c)
1322 FcFontSet *old = FcCacheSet (c);
1323 FcFontSet *new = FcFontSetCreate ();
1328 for (i = 0; i < old->nfont; i++)
1330 FcPattern *font = FcFontSetFont (old, i);
1332 FcPatternReference (font);
1333 if (!FcFontSetAdd (new, font))
1335 FcFontSetDestroy (new);
1343 FcCacheSubdir args2(const FcCache *c, int i)
1345 return FcCacheSubdir (c, i);
1349 FcCacheNumSubdir args1(const FcCache *c)
1351 return c->dirs_count;
1355 FcCacheNumFont args1(const FcCache *c)
1357 return FcCacheSet(c)->nfont;
1361 * This code implements the MD5 message-digest algorithm.
1362 * The algorithm is due to Ron Rivest. This code was
1363 * written by Colin Plumb in 1993, no copyright is claimed.
1364 * This code is in the public domain; do with it what you wish.
1366 * Equivalent code is available from RSA Data Security, Inc.
1367 * This code has been tested against that, and is equivalent,
1368 * except that you don't need to include two pages of legalese
1371 * To compute the message digest of a chunk of bytes, declare an
1372 * MD5Context structure, pass it to MD5Init, call MD5Update as
1373 * needed on buffers full of bytes, and then call MD5Final, which
1374 * will fill a supplied 16-byte array with the digest.
1378 #define byteReverse(buf, len) /* Nothing */
1381 * Note: this code is harmless on little-endian machines.
1383 void byteReverse(unsigned char *buf, unsigned longs)
1387 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1388 ((unsigned) buf[1] << 8 | buf[0]);
1389 *(FcChar32 *) buf = t;
1396 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1397 * initialization constants.
1399 static void MD5Init(struct MD5Context *ctx)
1401 ctx->buf[0] = 0x67452301;
1402 ctx->buf[1] = 0xefcdab89;
1403 ctx->buf[2] = 0x98badcfe;
1404 ctx->buf[3] = 0x10325476;
1411 * Update context to reflect the concatenation of another buffer full
1414 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1418 /* Update bitcount */
1421 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1422 ctx->bits[1]++; /* Carry from low to high */
1423 ctx->bits[1] += len >> 29;
1425 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1427 /* Handle any leading odd-sized chunks */
1430 unsigned char *p = (unsigned char *) ctx->in + t;
1434 memcpy(p, buf, len);
1438 byteReverse(ctx->in, 16);
1439 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1443 /* Process data in 64-byte chunks */
1446 memcpy(ctx->in, buf, 64);
1447 byteReverse(ctx->in, 16);
1448 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1453 /* Handle any remaining bytes of data. */
1455 memcpy(ctx->in, buf, len);
1459 * Final wrapup - pad to 64-byte boundary with the bit pattern
1460 * 1 0* (64-bit count of bits processed, MSB-first)
1462 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1467 /* Compute number of bytes mod 64 */
1468 count = (ctx->bits[0] >> 3) & 0x3F;
1470 /* Set the first char of padding to 0x80. This is safe since there is
1471 always at least one byte free */
1472 p = ctx->in + count;
1475 /* Bytes of padding needed to make 64 bytes */
1476 count = 64 - 1 - count;
1478 /* Pad out to 56 mod 64 */
1480 /* Two lots of padding: Pad the first block to 64 bytes */
1481 memset(p, 0, count);
1482 byteReverse(ctx->in, 16);
1483 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1485 /* Now fill the next block with 56 bytes */
1486 memset(ctx->in, 0, 56);
1488 /* Pad block to 56 bytes */
1489 memset(p, 0, count - 8);
1491 byteReverse(ctx->in, 14);
1493 /* Append length in bits and transform */
1494 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1495 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1497 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1498 byteReverse((unsigned char *) ctx->buf, 4);
1499 memcpy(digest, ctx->buf, 16);
1500 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1504 /* The four core functions - F1 is optimized somewhat */
1506 /* #define F1(x, y, z) (x & y | ~x & z) */
1507 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1508 #define F2(x, y, z) F1(z, x, y)
1509 #define F3(x, y, z) (x ^ y ^ z)
1510 #define F4(x, y, z) (y ^ (x | ~z))
1512 /* This is the central step in the MD5 algorithm. */
1513 #define MD5STEP(f, w, x, y, z, data, s) \
1514 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1517 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1518 * reflect the addition of 16 longwords of new data. MD5Update blocks
1519 * the data and converts bytes into longwords for this routine.
1521 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1523 register FcChar32 a, b, c, d;
1530 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1531 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1532 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1533 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1534 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1535 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1536 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1537 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1538 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1539 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1540 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1541 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1542 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1543 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1544 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1545 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1547 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1548 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1549 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1550 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1551 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1552 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1553 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1554 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1555 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1556 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1557 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1558 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1559 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1560 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1561 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1562 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1564 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1565 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1566 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1567 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1568 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1569 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1570 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1571 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1572 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1573 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1574 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1575 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1576 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1577 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1578 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1579 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1581 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1582 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1583 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1584 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1585 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1586 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1587 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1588 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1589 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1590 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1591 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1592 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1593 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1594 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1595 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1596 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1605 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1611 static const FcChar8 cache_tag_contents[] =
1612 "Signature: 8a477f597d28d172789f06886806bc55\n"
1613 "# This file is a cache directory tag created by fontconfig.\n"
1614 "# For information about cache directory tags, see:\n"
1615 "# http://www.brynosaurus.com/cachedir/\n";
1616 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1617 FcBool ret = FcFalse;
1622 if (access ((char *) cache_dir, W_OK) == 0)
1624 /* Create CACHEDIR.TAG */
1625 cache_tag = FcStrBuildFilename (cache_dir, "CACHEDIR.TAG", NULL);
1628 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1631 if (!FcAtomicLock (atomic))
1633 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1636 fp = fdopen(fd, "wb");
1640 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1643 if (!FcAtomicReplaceOrig(atomic))
1648 FcAtomicUnlock (atomic);
1650 FcAtomicDestroy (atomic);
1652 FcStrFree (cache_tag);
1655 if (FcDebug () & FC_DBG_CACHE)
1658 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1660 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1667 FcCacheCreateTagFile (const FcConfig *config)
1669 FcChar8 *cache_dir = NULL, *d = NULL;
1671 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
1673 list = FcConfigGetCacheDirs (config);
1677 while ((cache_dir = FcStrListNext (list)))
1682 d = FcStrBuildFilename (sysroot, cache_dir, NULL);
1684 d = FcStrCopyFilename (cache_dir);
1685 if (FcDirCacheCreateTagFile (d))
1690 FcStrListDone (list);
1694 #include "fcaliastail.h"