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.
30 #include <sys/types.h>
33 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
35 # include <sys/mman.h>
49 static void MD5Init(struct MD5Context *ctx);
50 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
51 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
52 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
54 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
57 FcCacheIsMmapSafe (int fd)
60 MMAP_NOT_INITIALIZED = 0,
65 static void *static_status;
67 status = (intptr_t) fc_atomic_ptr_get (&static_status);
69 if (status == MMAP_NOT_INITIALIZED)
71 const char *env = getenv ("FONTCONFIG_USE_MMAP");
73 if (env && FcNameBool ((const FcChar8 *) env, &use))
74 status = use ? MMAP_USE : MMAP_DONT_USE;
76 status = MMAP_CHECK_FS;
77 (void) fc_atomic_ptr_cmpexch (&static_status, NULL, (void *) status);
80 if (status == MMAP_CHECK_FS)
81 return FcIsFsMmapSafe (fd);
83 return status == MMAP_USE;
87 static const char bin2hex[] = { '0', '1', '2', '3',
93 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
95 unsigned char hash[16];
98 struct MD5Context ctx;
101 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
103 MD5Final (hash, &ctx);
106 hex_hash = cache_base + 1;
107 for (cnt = 0; cnt < 16; ++cnt)
109 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
110 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
113 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
119 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
121 FcChar8 *cache_hashed = NULL;
122 FcChar8 cache_base[CACHEBASE_LEN];
125 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
127 FcDirCacheBasename (dir, cache_base);
129 list = FcStrListCreate (config->cacheDirs);
133 while ((cache_dir = FcStrListNext (list)))
136 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
138 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
141 (void) unlink ((char *) cache_hashed);
142 FcStrFree (cache_hashed);
144 FcStrListDone (list);
145 /* return FcFalse if something went wrong */
152 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
157 if (FcStat (cache_file, file_stat) < 0)
160 fd = FcOpen((char *) cache_file, O_RDONLY | O_BINARY);
164 if (fstat (fd, file_stat) < 0)
174 * Look for a cache file for the specified dir. Attempt
175 * to use each one we find, stopping when the callback
179 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
180 FcBool (*callback) (FcConfig *config, int fd, struct stat *fd_stat,
181 struct stat *dir_stat, void *closure),
182 void *closure, FcChar8 **cache_file_ret)
185 FcChar8 cache_base[CACHEBASE_LEN];
187 FcChar8 *cache_dir, *d;
188 struct stat file_stat, dir_stat;
189 FcBool ret = FcFalse;
190 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
193 d = FcStrBuildFilename (sysroot, dir, NULL);
196 if (FcStatChecksum (d, &dir_stat) < 0)
203 FcDirCacheBasename (dir, cache_base);
205 list = FcStrListCreate (config->cacheDirs);
209 while ((cache_dir = FcStrListNext (list)))
211 FcChar8 *cache_hashed;
214 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
216 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
219 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
221 ret = (*callback) (config, fd, &file_stat, &dir_stat, closure);
226 *cache_file_ret = cache_hashed;
228 FcStrFree (cache_hashed);
232 FcStrFree (cache_hashed);
234 FcStrListDone (list);
239 #define FC_CACHE_MIN_MMAP 1024
242 * Skip list element, make sure the 'next' pointer is the last thing
243 * in the structure, it will be allocated large enough to hold all
244 * of the necessary pointers
247 typedef struct _FcCacheSkip FcCacheSkip;
249 struct _FcCacheSkip {
256 FcCacheSkip *next[1];
260 * The head of the skip list; pointers for every possible level
261 * in the skip list, plus the largest level in the list
264 #define FC_CACHE_MAX_LEVEL 16
266 /* Protected by cache_lock below */
267 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
268 static int fcCacheMaxLevel;
271 static FcMutex *cache_lock;
278 lock = fc_atomic_ptr_get (&cache_lock);
280 lock = (FcMutex *) malloc (sizeof (FcMutex));
282 if (!fc_atomic_ptr_cmpexch (&cache_lock, NULL, lock)) {
283 FcMutexFinish (lock);
288 /* Initialize random state */
298 FcMutexUnlock (cache_lock);
305 lock = fc_atomic_ptr_get (&cache_lock);
306 if (lock && fc_atomic_ptr_cmpexch (&cache_lock, lock, NULL)) {
307 FcMutexFinish (lock);
315 * Generate a random level number, distributed
316 * so that each level is 1/4 as likely as the one before
318 * Note that level numbers run 1 <= level <= MAX_LEVEL
323 /* tricky bit -- each bit is '1' 75% of the time */
324 long int bits = FcRandom () | FcRandom ();
327 while (++level < FC_CACHE_MAX_LEVEL)
337 * Insert cache into the list
340 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
342 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
343 FcCacheSkip *s, **next;
349 * Find links along each chain
351 next = fcCacheChains;
352 for (i = fcCacheMaxLevel; --i >= 0; )
354 for (; (s = next[i]); next = s->next)
355 if (s->cache > cache)
357 update[i] = &next[i];
361 * Create new list element
363 level = random_level ();
364 if (level > fcCacheMaxLevel)
366 level = fcCacheMaxLevel + 1;
367 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
368 fcCacheMaxLevel = level;
371 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
376 s->size = cache->size;
377 FcRefInit (&s->ref, 1);
380 s->cache_dev = cache_stat->st_dev;
381 s->cache_ino = cache_stat->st_ino;
382 s->cache_mtime = cache_stat->st_mtime;
392 * Insert into all fcCacheChains
394 for (i = 0; i < level; i++)
396 s->next[i] = *update[i];
405 FcCacheFindByAddrUnlocked (void *object)
408 FcCacheSkip **next = fcCacheChains;
415 * Walk chain pointers one level at a time
417 for (i = fcCacheMaxLevel; --i >= 0;)
418 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
419 next = next[i]->next;
424 if (s && (char *) object < ((char *) s->cache + s->size))
430 FcCacheFindByAddr (void *object)
434 ret = FcCacheFindByAddrUnlocked (object);
440 FcCacheRemoveUnlocked (FcCache *cache)
442 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
443 FcCacheSkip *s, **next;
447 * Find links along each chain
449 next = fcCacheChains;
450 for (i = fcCacheMaxLevel; --i >= 0; )
452 for (; (s = next[i]); next = s->next)
453 if (s->cache >= cache)
455 update[i] = &next[i];
458 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
459 *update[i] = s->next[i];
460 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
466 FcCacheFindByStat (struct stat *cache_stat)
471 for (s = fcCacheChains[0]; s; s = s->next[0])
472 if (s->cache_dev == cache_stat->st_dev &&
473 s->cache_ino == cache_stat->st_ino &&
474 s->cache_mtime == cache_stat->st_mtime)
485 FcDirCacheDisposeUnlocked (FcCache *cache)
487 FcCacheRemoveUnlocked (cache);
489 switch (cache->magic) {
490 case FC_CACHE_MAGIC_ALLOC:
493 case FC_CACHE_MAGIC_MMAP:
494 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
495 munmap (cache, cache->size);
496 #elif defined(_WIN32)
497 UnmapViewOfFile (cache);
504 FcCacheObjectReference (void *object)
506 FcCacheSkip *skip = FcCacheFindByAddr (object);
509 FcRefInc (&skip->ref);
513 FcCacheObjectDereference (void *object)
518 skip = FcCacheFindByAddrUnlocked (object);
521 if (FcRefDec (&skip->ref) == 1)
522 FcDirCacheDisposeUnlocked (skip->cache);
532 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
533 assert (fcCacheChains[i] == NULL);
534 assert (fcCacheMaxLevel == 0);
540 FcCacheTimeValid (FcConfig *config, FcCache *cache, struct stat *dir_stat)
542 struct stat dir_static;
546 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
550 d = FcStrBuildFilename (sysroot, FcCacheDir (cache), NULL);
552 d = FcStrdup (FcCacheDir (cache));
553 if (FcStatChecksum (d, &dir_static) < 0)
559 dir_stat = &dir_static;
561 if (FcDebug () & FC_DBG_CACHE)
562 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
563 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
564 return cache->checksum == (int) dir_stat->st_mtime;
568 FcCacheDirsValid (FcConfig *config, FcCache *cache)
570 FcStrSet *dirs = FcStrSetCreate ();
571 FcBool ret = FcFalse;
572 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
578 d = FcStrBuildFilename (sysroot, FcCacheDir (cache), NULL);
580 d = FcStrdup (FcCacheDir (cache));
581 if (!FcDirScanOnly (dirs, d, config))
583 ret = cache->dirs_count == dirs->num;
584 if (FcDebug () & FC_DBG_CACHE)
585 printf ("%s: cache: %d, fs: %d\n", d, cache->dirs_count, dirs->num);
588 FcStrSetDestroy (dirs);
595 * Map a cache file into memory
598 FcDirCacheMapFd (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat)
601 FcBool allocated = FcFalse;
603 if (fd_stat->st_size < (int) sizeof (FcCache))
605 cache = FcCacheFindByStat (fd_stat);
608 if (FcCacheTimeValid (config, cache, dir_stat) &&
609 FcCacheDirsValid (config, cache))
611 FcDirCacheUnload (cache);
616 * Large cache files are mmap'ed, smaller cache files are read. This
617 * balances the system cost of mmap against per-process memory usage.
619 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
621 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
622 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
623 #if (HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
624 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
626 if (cache == MAP_FAILED)
628 #elif defined(_WIN32)
633 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
634 PAGE_READONLY, 0, 0, NULL);
635 if (hFileMap != NULL)
637 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
639 CloseHandle (hFileMap);
646 cache = malloc (fd_stat->st_size);
650 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
657 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
658 cache->version < FC_CACHE_CONTENT_VERSION ||
659 cache->size != (intptr_t) fd_stat->st_size ||
660 !FcCacheTimeValid (config, cache, dir_stat) ||
661 !FcCacheDirsValid (config, cache) ||
662 !FcCacheInsert (cache, fd_stat))
668 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
669 munmap (cache, fd_stat->st_size);
670 #elif defined(_WIN32)
671 UnmapViewOfFile (cache);
677 /* Mark allocated caches so they're freed rather than unmapped */
679 cache->magic = FC_CACHE_MAGIC_ALLOC;
685 FcDirCacheReference (FcCache *cache, int nref)
687 FcCacheSkip *skip = FcCacheFindByAddr (cache);
690 FcRefAdd (&skip->ref, nref);
694 FcDirCacheUnload (FcCache *cache)
696 FcCacheObjectDereference (cache);
700 FcDirCacheMapHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
702 FcCache *cache = FcDirCacheMapFd (config, fd, fd_stat, dir_stat);
706 *((FcCache **) closure) = cache;
711 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
713 FcCache *cache = NULL;
715 if (!FcDirCacheProcess (config, dir,
724 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
728 struct stat my_file_stat;
731 file_stat = &my_file_stat;
732 fd = FcDirCacheOpenFile (cache_file, file_stat);
735 cache = FcDirCacheMapFd (FcConfigGetCurrent (), fd, file_stat, NULL);
741 * Validate a cache file by reading the header and checking
742 * the magic number and the size field
745 FcDirCacheValidateHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
750 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
752 else if (c.magic != FC_CACHE_MAGIC_MMAP)
754 else if (c.version < FC_CACHE_CONTENT_VERSION)
756 else if (fd_stat->st_size != c.size)
758 else if (c.checksum != (int) dir_stat->st_mtime)
764 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
766 return FcDirCacheProcess (config, dir,
767 FcDirCacheValidateHelper,
772 FcDirCacheValid (const FcChar8 *dir)
776 config = FcConfigGetCurrent ();
780 return FcDirCacheValidConfig (dir, config);
784 * Build a cache structure from the given contents
787 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
789 FcSerialize *serialize = FcSerializeCreate ();
792 FcChar8 *dir_serialize;
793 intptr_t *dirs_serialize;
794 FcFontSet *set_serialize;
799 * Space for cache structure
801 FcSerializeReserve (serialize, sizeof (FcCache));
805 if (!FcStrSerializeAlloc (serialize, dir))
810 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
811 for (i = 0; i < dirs->num; i++)
812 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
818 if (!FcFontSetSerializeAlloc (serialize, set))
821 /* Serialize layout complete. Now allocate space and fill it */
822 cache = malloc (serialize->size);
825 /* shut up valgrind */
826 memset (cache, 0, serialize->size);
828 serialize->linear = cache;
830 cache->magic = FC_CACHE_MAGIC_ALLOC;
831 cache->version = FC_CACHE_CONTENT_VERSION;
832 cache->size = serialize->size;
833 cache->checksum = (int) dir_stat->st_mtime;
836 * Serialize directory name
838 dir_serialize = FcStrSerialize (serialize, dir);
841 cache->dir = FcPtrToOffset (cache, dir_serialize);
846 dirs_serialize = FcSerializePtr (serialize, dirs);
849 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
850 cache->dirs_count = dirs->num;
851 for (i = 0; i < dirs->num; i++)
853 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
856 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
862 set_serialize = FcFontSetSerialize (serialize, set);
865 cache->set = FcPtrToOffset (cache, set_serialize);
867 FcSerializeDestroy (serialize);
869 FcCacheInsert (cache, NULL);
876 FcSerializeDestroy (serialize);
881 FcDirCacheRebuild (FcCache *cache, struct stat *dir_stat, FcStrSet *dirs)
884 FcFontSet *set = FcFontSetDeserialize (FcCacheSet (cache));
885 const FcChar8 *dir = FcCacheDir (cache);
887 new = FcDirCacheBuild (set, dir, dir_stat, dirs);
888 FcFontSetDestroy (set);
893 /* write serialized state to the cache file */
895 FcDirCacheWrite (FcCache *cache, FcConfig *config)
897 FcChar8 *dir = FcCacheDir (cache);
898 FcChar8 cache_base[CACHEBASE_LEN];
899 FcChar8 *cache_hashed;
903 FcChar8 *cache_dir = NULL;
904 FcChar8 *test_dir, *d = NULL;
906 struct stat cache_stat;
909 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
912 * Write it to the first directory in the list which is writable
915 list = FcStrListCreate (config->cacheDirs);
918 while ((test_dir = FcStrListNext (list)))
923 d = FcStrBuildFilename (sysroot, test_dir, NULL);
925 d = FcStrCopyFilename (test_dir);
927 if (access ((char *) d, W_OK) == 0)
929 cache_dir = FcStrCopyFilename (d);
935 * If the directory doesn't exist, try to create it
937 if (access ((char *) d, F_OK) == -1) {
938 if (FcMakeDirectory (d))
940 cache_dir = FcStrCopyFilename (d);
941 /* Create CACHEDIR.TAG */
942 FcDirCacheCreateTagFile (d);
947 * Otherwise, try making it writable
949 else if (chmod ((char *) d, 0755) == 0)
951 cache_dir = FcStrCopyFilename (d);
952 /* Try to create CACHEDIR.TAG too */
953 FcDirCacheCreateTagFile (d);
960 FcStrListDone (list);
964 FcDirCacheBasename (dir, cache_base);
965 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
968 FcStrFree (cache_dir);
970 if (FcDebug () & FC_DBG_CACHE)
971 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
974 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
978 if (!FcAtomicLock (atomic))
981 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
985 /* Temporarily switch magic to MMAP while writing to file */
986 magic = cache->magic;
987 if (magic != FC_CACHE_MAGIC_MMAP)
988 cache->magic = FC_CACHE_MAGIC_MMAP;
991 * Write cache contents to file
993 written = write (fd, cache, cache->size);
995 /* Switch magic back */
996 if (magic != FC_CACHE_MAGIC_MMAP)
997 cache->magic = magic;
999 if (written != cache->size)
1001 perror ("write cache");
1006 if (!FcAtomicReplaceOrig(atomic))
1009 /* If the file is small, update the cache chain entry such that the
1010 * new cache file is not read again. If it's large, we don't do that
1011 * such that we reload it, using mmap, which is shared across processes.
1013 if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat))
1016 if ((skip = FcCacheFindByAddrUnlocked (cache)))
1018 skip->cache_dev = cache_stat.st_dev;
1019 skip->cache_ino = cache_stat.st_ino;
1020 skip->cache_mtime = cache_stat.st_mtime;
1025 FcStrFree (cache_hashed);
1026 FcAtomicUnlock (atomic);
1027 FcAtomicDestroy (atomic);
1033 FcAtomicUnlock (atomic);
1035 FcAtomicDestroy (atomic);
1037 FcStrFree (cache_hashed);
1042 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1047 FcBool ret = FcTrue;
1050 struct stat target_stat;
1051 const FcChar8 *sysroot;
1053 /* FIXME: this API needs to support non-current FcConfig */
1054 sysroot = FcConfigGetSysRoot (NULL);
1056 dir = FcStrBuildFilename (sysroot, cache_dir, NULL);
1058 dir = FcStrCopyFilename (cache_dir);
1061 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1064 if (access ((char *) dir, W_OK) != 0)
1066 if (verbose || FcDebug () & FC_DBG_CACHE)
1067 printf ("%s: not cleaning %s cache directory\n", dir,
1068 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
1071 if (verbose || FcDebug () & FC_DBG_CACHE)
1072 printf ("%s: cleaning cache directory\n", dir);
1073 d = opendir ((char *) dir);
1076 perror ((char *) dir);
1080 while ((ent = readdir (d)))
1083 const FcChar8 *target_dir;
1085 if (ent->d_name[0] == '.')
1087 /* skip cache files for different architectures and */
1088 /* files which are not cache files at all */
1089 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1090 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1093 file_name = FcStrBuildFilename (dir, (FcChar8 *)ent->d_name, NULL);
1096 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", dir);
1101 cache = FcDirCacheLoadFile (file_name, NULL);
1104 if (verbose || FcDebug () & FC_DBG_CACHE)
1105 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
1112 target_dir = FcCacheDir (cache);
1114 s = FcStrBuildFilename (sysroot, target_dir, NULL);
1116 s = FcStrdup (target_dir);
1117 if (stat ((char *) s, &target_stat) < 0)
1119 if (verbose || FcDebug () & FC_DBG_CACHE)
1120 printf ("%s: %s: missing directory: %s \n",
1121 dir, ent->d_name, s);
1124 FcDirCacheUnload (cache);
1129 if (unlink ((char *) file_name) < 0)
1131 perror ((char *) file_name);
1135 FcStrFree (file_name);
1146 * Hokey little macro trick to permit the definitions of C functions
1147 * with the same name as CPP macros
1149 #define args1(x) (x)
1150 #define args2(x,y) (x,y)
1153 FcCacheDir args1(const FcCache *c)
1155 return FcCacheDir (c);
1159 FcCacheCopySet args1(const FcCache *c)
1161 FcFontSet *old = FcCacheSet (c);
1162 FcFontSet *new = FcFontSetCreate ();
1167 for (i = 0; i < old->nfont; i++)
1169 FcPattern *font = FcFontSetFont (old, i);
1171 FcPatternReference (font);
1172 if (!FcFontSetAdd (new, font))
1174 FcFontSetDestroy (new);
1182 FcCacheSubdir args2(const FcCache *c, int i)
1184 return FcCacheSubdir (c, i);
1188 FcCacheNumSubdir args1(const FcCache *c)
1190 return c->dirs_count;
1194 FcCacheNumFont args1(const FcCache *c)
1196 return FcCacheSet(c)->nfont;
1200 * This code implements the MD5 message-digest algorithm.
1201 * The algorithm is due to Ron Rivest. This code was
1202 * written by Colin Plumb in 1993, no copyright is claimed.
1203 * This code is in the public domain; do with it what you wish.
1205 * Equivalent code is available from RSA Data Security, Inc.
1206 * This code has been tested against that, and is equivalent,
1207 * except that you don't need to include two pages of legalese
1210 * To compute the message digest of a chunk of bytes, declare an
1211 * MD5Context structure, pass it to MD5Init, call MD5Update as
1212 * needed on buffers full of bytes, and then call MD5Final, which
1213 * will fill a supplied 16-byte array with the digest.
1217 #define byteReverse(buf, len) /* Nothing */
1220 * Note: this code is harmless on little-endian machines.
1222 void byteReverse(unsigned char *buf, unsigned longs)
1226 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1227 ((unsigned) buf[1] << 8 | buf[0]);
1228 *(FcChar32 *) buf = t;
1235 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1236 * initialization constants.
1238 static void MD5Init(struct MD5Context *ctx)
1240 ctx->buf[0] = 0x67452301;
1241 ctx->buf[1] = 0xefcdab89;
1242 ctx->buf[2] = 0x98badcfe;
1243 ctx->buf[3] = 0x10325476;
1250 * Update context to reflect the concatenation of another buffer full
1253 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1257 /* Update bitcount */
1260 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1261 ctx->bits[1]++; /* Carry from low to high */
1262 ctx->bits[1] += len >> 29;
1264 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1266 /* Handle any leading odd-sized chunks */
1269 unsigned char *p = (unsigned char *) ctx->in + t;
1273 memcpy(p, buf, len);
1277 byteReverse(ctx->in, 16);
1278 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1282 /* Process data in 64-byte chunks */
1285 memcpy(ctx->in, buf, 64);
1286 byteReverse(ctx->in, 16);
1287 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1292 /* Handle any remaining bytes of data. */
1294 memcpy(ctx->in, buf, len);
1298 * Final wrapup - pad to 64-byte boundary with the bit pattern
1299 * 1 0* (64-bit count of bits processed, MSB-first)
1301 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1306 /* Compute number of bytes mod 64 */
1307 count = (ctx->bits[0] >> 3) & 0x3F;
1309 /* Set the first char of padding to 0x80. This is safe since there is
1310 always at least one byte free */
1311 p = ctx->in + count;
1314 /* Bytes of padding needed to make 64 bytes */
1315 count = 64 - 1 - count;
1317 /* Pad out to 56 mod 64 */
1319 /* Two lots of padding: Pad the first block to 64 bytes */
1320 memset(p, 0, count);
1321 byteReverse(ctx->in, 16);
1322 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1324 /* Now fill the next block with 56 bytes */
1325 memset(ctx->in, 0, 56);
1327 /* Pad block to 56 bytes */
1328 memset(p, 0, count - 8);
1330 byteReverse(ctx->in, 14);
1332 /* Append length in bits and transform */
1333 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1334 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1336 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1337 byteReverse((unsigned char *) ctx->buf, 4);
1338 memcpy(digest, ctx->buf, 16);
1339 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1343 /* The four core functions - F1 is optimized somewhat */
1345 /* #define F1(x, y, z) (x & y | ~x & z) */
1346 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1347 #define F2(x, y, z) F1(z, x, y)
1348 #define F3(x, y, z) (x ^ y ^ z)
1349 #define F4(x, y, z) (y ^ (x | ~z))
1351 /* This is the central step in the MD5 algorithm. */
1352 #define MD5STEP(f, w, x, y, z, data, s) \
1353 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1356 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1357 * reflect the addition of 16 longwords of new data. MD5Update blocks
1358 * the data and converts bytes into longwords for this routine.
1360 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1362 register FcChar32 a, b, c, d;
1369 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1370 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1371 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1372 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1373 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1374 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1375 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1376 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1377 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1378 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1379 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1380 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1381 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1382 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1383 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1384 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1386 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1387 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1388 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1389 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1390 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1391 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1392 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1393 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1394 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1395 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1396 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1397 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1398 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1399 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1400 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1401 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1403 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1404 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1405 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1406 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1407 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1408 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1409 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1410 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1411 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1412 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1413 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1414 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1415 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1416 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1417 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1418 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1420 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1421 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1422 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1423 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1424 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1425 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1426 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1427 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1428 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1429 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1430 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1431 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1432 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1433 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1434 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1435 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1444 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1450 static const FcChar8 cache_tag_contents[] =
1451 "Signature: 8a477f597d28d172789f06886806bc55\n"
1452 "# This file is a cache directory tag created by fontconfig.\n"
1453 "# For information about cache directory tags, see:\n"
1454 "# http://www.brynosaurus.com/cachedir/\n";
1455 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1456 FcBool ret = FcFalse;
1461 if (access ((char *) cache_dir, W_OK) == 0)
1463 /* Create CACHEDIR.TAG */
1464 cache_tag = FcStrBuildFilename (cache_dir, "CACHEDIR.TAG", NULL);
1467 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1470 if (!FcAtomicLock (atomic))
1472 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1475 fp = fdopen(fd, "wb");
1479 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1482 if (!FcAtomicReplaceOrig(atomic))
1487 FcAtomicUnlock (atomic);
1489 FcAtomicDestroy (atomic);
1491 FcStrFree (cache_tag);
1494 if (FcDebug () & FC_DBG_CACHE)
1497 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1499 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1506 FcCacheCreateTagFile (const FcConfig *config)
1508 FcChar8 *cache_dir = NULL, *d = NULL;
1510 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
1512 list = FcConfigGetCacheDirs (config);
1516 while ((cache_dir = FcStrListNext (list)))
1521 d = FcStrBuildFilename (sysroot, cache_dir, NULL);
1523 d = FcStrCopyFilename (cache_dir);
1524 if (FcDirCacheCreateTagFile (d))
1529 FcStrListDone (list);
1533 #include "fcaliastail.h"