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>
35 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
37 # include <sys/mman.h>
40 #include <sys/locking.h>
42 #include <uuid/uuid.h>
50 FcDirCacheCreateUUID (FcChar8 *dir,
54 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
61 target = FcStrBuildFilename (sysroot, dir, NULL);
63 target = FcStrdup (dir);
64 uuidname = FcStrBuildFilename (target, ".uuid", NULL);
72 if (force || access ((const char *) uuidname, F_OK) < 0)
78 FcBool (* hash_add) (FcHashTable *, void*, void*);
80 struct timeval times[2];
82 if (FcStat (target, &statb) != 0)
87 atomic = FcAtomicCreate (uuidname);
93 if (!FcAtomicLock (atomic))
98 fd = FcOpen ((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
104 uuid_generate_random (uuid);
106 hash_add = FcHashTableReplace;
108 hash_add = FcHashTableAdd;
109 if (!hash_add (config->uuid_table, target, uuid))
112 FcAtomicDeleteNew (atomic);
116 uuid_unparse (uuid, out);
117 if (FcDebug () & FC_DBG_CACHE)
118 printf ("FcDirCacheCreateUUID %s: %s\n", uuidname, out);
119 write (fd, out, strlen (out));
121 FcAtomicReplaceOrig (atomic);
123 FcAtomicUnlock (atomic);
125 FcAtomicDestroy (atomic);
129 /* revert mtime of the directory */
130 times[0].tv_sec = statb.st_atime;
131 times[1].tv_sec = statb.st_mtime;
132 #ifdef HAVE_STRUCT_STAT_ST_MTIM
133 times[0].tv_usec = statb.st_atim.tv_nsec / 1000;
134 times[1].tv_usec = statb.st_mtim.tv_nsec / 1000;
136 times[0].tv_usec = 0;
137 times[1].tv_usec = 0;
139 if (utimes ((const char *) target, times) != 0)
141 fprintf (stderr, "Unable to revert mtime: %s\n", target);
146 FcStrFree (uuidname);
154 FcDirCacheDeleteUUID (const FcChar8 *dir,
157 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
162 target = FcStrBuildFilename (sysroot, dir, ".uuid", NULL);
164 target = FcStrBuildFilename (dir, ".uuid", NULL);
166 ret = unlink ((char *) target) == 0;
167 FcHashTableRemove (config->uuid_table, target);
175 FcDirCacheReadUUID (FcChar8 *dir,
180 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
184 target = FcStrBuildFilename (sysroot, dir, NULL);
186 target = FcStrdup (dir);
188 if (!FcHashTableFind (config->uuid_table, target, &u))
190 FcChar8 *uuidname = FcStrBuildFilename (target, ".uuid", NULL);
193 if ((fd = FcOpen ((char *) uuidname, O_RDONLY)) >= 0)
198 memset (suuid, 0, sizeof (suuid));
199 len = read (fd, suuid, 36);
203 memset (uuid, 0, sizeof (uuid));
204 if (uuid_parse (suuid, uuid) == 0)
206 if (FcDebug () & FC_DBG_CACHE)
207 printf ("FcDirCacheReadUUID %s -> %s\n", uuidname, suuid);
208 FcHashTableAdd (config->uuid_table, target, uuid);
215 if (FcDebug () & FC_DBG_CACHE)
216 printf ("FcDirCacheReadUUID Unable to read %s\n", uuidname);
218 FcStrFree (uuidname);
229 unsigned char in[64];
232 static void MD5Init(struct MD5Context *ctx);
233 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
234 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
235 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
237 #define CACHEBASE_LEN (1 + 36 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
240 FcCacheIsMmapSafe (int fd)
243 MMAP_NOT_INITIALIZED = 0,
248 static void *static_status;
250 status = (intptr_t) fc_atomic_ptr_get (&static_status);
252 if (status == MMAP_NOT_INITIALIZED)
254 const char *env = getenv ("FONTCONFIG_USE_MMAP");
256 if (env && FcNameBool ((const FcChar8 *) env, &use))
257 status = use ? MMAP_USE : MMAP_DONT_USE;
259 status = MMAP_CHECK_FS;
260 (void) fc_atomic_ptr_cmpexch (&static_status, NULL, (void *) status);
263 if (status == MMAP_CHECK_FS)
264 return FcIsFsMmapSafe (fd);
266 return status == MMAP_USE;
270 static const char bin2hex[] = { '0', '1', '2', '3',
273 'c', 'd', 'e', 'f' };
276 FcDirCacheBasenameMD5 (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN])
278 unsigned char hash[16];
281 struct MD5Context ctx;
284 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
286 MD5Final (hash, &ctx);
289 hex_hash = cache_base + 1;
290 for (cnt = 0; cnt < 16; ++cnt)
292 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
293 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
296 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
303 FcDirCacheBasenameUUID (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN], FcConfig *config)
307 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
310 target = FcStrBuildFilename (sysroot, dir, NULL);
312 target = FcStrdup (dir);
313 if (FcHashTableFind (config->uuid_table, target, &u))
315 uuid_unparse (u, (char *) cache_base);
316 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
327 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
329 FcChar8 *cache_hashed = NULL;
330 FcChar8 cache_base[CACHEBASE_LEN];
333 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
336 if (!FcDirCacheBasenameUUID (dir, cache_base, config))
338 FcDirCacheBasenameMD5 (dir, cache_base);
340 list = FcStrListCreate (config->cacheDirs);
344 while ((cache_dir = FcStrListNext (list)))
347 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
349 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
352 (void) unlink ((char *) cache_hashed);
353 FcDirCacheDeleteUUID (dir, config);
354 FcStrFree (cache_hashed);
356 FcStrListDone (list);
357 /* return FcFalse if something went wrong */
364 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
369 if (FcStat (cache_file, file_stat) < 0)
372 fd = FcOpen((char *) cache_file, O_RDONLY | O_BINARY);
376 if (fstat (fd, file_stat) < 0)
386 * Look for a cache file for the specified dir. Attempt
387 * to use each one we find, stopping when the callback
391 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
392 FcBool (*callback) (FcConfig *config, int fd, struct stat *fd_stat,
393 struct stat *dir_stat, void *closure),
394 void *closure, FcChar8 **cache_file_ret)
397 FcChar8 cache_base[CACHEBASE_LEN];
399 FcChar8 *cache_dir, *d;
400 struct stat file_stat, dir_stat;
401 FcBool ret = FcFalse;
402 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
405 d = FcStrBuildFilename (sysroot, dir, NULL);
408 if (FcStatChecksum (d, &dir_stat) < 0)
416 if (!FcDirCacheBasenameUUID (dir, cache_base, config))
418 FcDirCacheBasenameMD5 (dir, cache_base);
420 list = FcStrListCreate (config->cacheDirs);
424 while ((cache_dir = FcStrListNext (list)))
426 FcChar8 *cache_hashed;
429 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
431 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
434 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
436 ret = (*callback) (config, fd, &file_stat, &dir_stat, closure);
441 *cache_file_ret = cache_hashed;
443 FcStrFree (cache_hashed);
447 FcStrFree (cache_hashed);
449 FcStrListDone (list);
454 #define FC_CACHE_MIN_MMAP 1024
457 * Skip list element, make sure the 'next' pointer is the last thing
458 * in the structure, it will be allocated large enough to hold all
459 * of the necessary pointers
462 typedef struct _FcCacheSkip FcCacheSkip;
464 struct _FcCacheSkip {
472 long cache_mtime_nano;
473 FcCacheSkip *next[1];
477 * The head of the skip list; pointers for every possible level
478 * in the skip list, plus the largest level in the list
481 #define FC_CACHE_MAX_LEVEL 16
483 /* Protected by cache_lock below */
484 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
485 static int fcCacheMaxLevel;
488 static FcMutex *cache_lock;
495 lock = fc_atomic_ptr_get (&cache_lock);
497 lock = (FcMutex *) malloc (sizeof (FcMutex));
499 if (!fc_atomic_ptr_cmpexch (&cache_lock, NULL, lock)) {
500 FcMutexFinish (lock);
505 /* Initialize random state */
515 FcMutexUnlock (cache_lock);
522 lock = fc_atomic_ptr_get (&cache_lock);
523 if (lock && fc_atomic_ptr_cmpexch (&cache_lock, lock, NULL)) {
524 FcMutexFinish (lock);
532 * Generate a random level number, distributed
533 * so that each level is 1/4 as likely as the one before
535 * Note that level numbers run 1 <= level <= MAX_LEVEL
540 /* tricky bit -- each bit is '1' 75% of the time */
541 long int bits = FcRandom () | FcRandom ();
544 while (++level < FC_CACHE_MAX_LEVEL)
554 * Insert cache into the list
557 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
559 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
560 FcCacheSkip *s, **next;
566 * Find links along each chain
568 next = fcCacheChains;
569 for (i = fcCacheMaxLevel; --i >= 0; )
571 for (; (s = next[i]); next = s->next)
572 if (s->cache > cache)
574 update[i] = &next[i];
578 * Create new list element
580 level = random_level ();
581 if (level > fcCacheMaxLevel)
583 level = fcCacheMaxLevel + 1;
584 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
585 fcCacheMaxLevel = level;
588 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
593 s->size = cache->size;
595 FcRefInit (&s->ref, 1);
598 s->cache_dev = cache_stat->st_dev;
599 s->cache_ino = cache_stat->st_ino;
600 s->cache_mtime = cache_stat->st_mtime;
601 #ifdef HAVE_STRUCT_STAT_ST_MTIM
602 s->cache_mtime_nano = cache_stat->st_mtim.tv_nsec;
604 s->cache_mtime_nano = 0;
612 s->cache_mtime_nano = 0;
616 * Insert into all fcCacheChains
618 for (i = 0; i < level; i++)
620 s->next[i] = *update[i];
629 FcCacheFindByAddrUnlocked (void *object)
632 FcCacheSkip **next = fcCacheChains;
639 * Walk chain pointers one level at a time
641 for (i = fcCacheMaxLevel; --i >= 0;)
642 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
643 next = next[i]->next;
648 if (s && (char *) object < ((char *) s->cache + s->size))
654 FcCacheFindByAddr (void *object)
658 ret = FcCacheFindByAddrUnlocked (object);
664 FcCacheRemoveUnlocked (FcCache *cache)
666 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
667 FcCacheSkip *s, **next;
672 * Find links along each chain
674 next = fcCacheChains;
675 for (i = fcCacheMaxLevel; --i >= 0; )
677 for (; (s = next[i]); next = s->next)
678 if (s->cache >= cache)
680 update[i] = &next[i];
683 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
684 *update[i] = s->next[i];
685 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
688 allocated = s->allocated;
691 /* First element in allocated chunk is the free list */
692 next = *(void **)allocated;
700 FcCacheFindByStat (struct stat *cache_stat)
705 for (s = fcCacheChains[0]; s; s = s->next[0])
706 if (s->cache_dev == cache_stat->st_dev &&
707 s->cache_ino == cache_stat->st_ino &&
708 s->cache_mtime == cache_stat->st_mtime)
710 #ifdef HAVE_STRUCT_STAT_ST_MTIM
711 if (s->cache_mtime_nano != cache_stat->st_mtim.tv_nsec)
723 FcDirCacheDisposeUnlocked (FcCache *cache)
725 FcCacheRemoveUnlocked (cache);
727 switch (cache->magic) {
728 case FC_CACHE_MAGIC_ALLOC:
731 case FC_CACHE_MAGIC_MMAP:
732 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
733 munmap (cache, cache->size);
734 #elif defined(_WIN32)
735 UnmapViewOfFile (cache);
742 FcCacheObjectReference (void *object)
744 FcCacheSkip *skip = FcCacheFindByAddr (object);
747 FcRefInc (&skip->ref);
751 FcCacheObjectDereference (void *object)
756 skip = FcCacheFindByAddrUnlocked (object);
759 if (FcRefDec (&skip->ref) == 1)
760 FcDirCacheDisposeUnlocked (skip->cache);
766 FcCacheAllocate (FcCache *cache, size_t len)
769 void *allocated = NULL;
772 skip = FcCacheFindByAddrUnlocked (cache);
775 void *chunk = malloc (sizeof (void *) + len);
778 /* First element in allocated chunk is the free list */
779 *(void **)chunk = skip->allocated;
780 skip->allocated = chunk;
781 /* Return the rest */
782 allocated = ((FcChar8 *)chunk) + sizeof (void *);
794 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
795 assert (fcCacheChains[i] == NULL);
796 assert (fcCacheMaxLevel == 0);
802 FcCacheTimeValid (FcConfig *config, FcCache *cache, struct stat *dir_stat)
804 struct stat dir_static;
805 FcBool fnano = FcTrue;
809 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
813 d = FcStrBuildFilename (sysroot, FcCacheDir (cache), NULL);
815 d = FcStrdup (FcCacheDir (cache));
816 if (FcStatChecksum (d, &dir_static) < 0)
822 dir_stat = &dir_static;
824 #ifdef HAVE_STRUCT_STAT_ST_MTIM
825 fnano = (cache->checksum_nano == dir_stat->st_mtim.tv_nsec);
826 if (FcDebug () & FC_DBG_CACHE)
827 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d.%ld dir checksum %d.%ld\n",
828 FcCacheDir (cache), cache->checksum, (long)cache->checksum_nano, (int) dir_stat->st_mtime, dir_stat->st_mtim.tv_nsec);
830 if (FcDebug () & FC_DBG_CACHE)
831 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
832 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
835 return cache->checksum == (int) dir_stat->st_mtime && fnano;
839 FcCacheOffsetsValid (FcCache *cache)
841 char *base = (char *)cache;
842 char *end = base + cache->size;
847 if (cache->dir < 0 || cache->dir > cache->size - sizeof (intptr_t) ||
848 memchr (base + cache->dir, '\0', cache->size - cache->dir) == NULL)
851 if (cache->dirs < 0 || cache->dirs >= cache->size ||
852 cache->dirs_count < 0 ||
853 cache->dirs_count > (cache->size - cache->dirs) / sizeof (intptr_t))
856 dirs = FcCacheDirs (cache);
859 for (i = 0; i < cache->dirs_count; i++)
864 dirs[i] > end - (char *) dirs - sizeof (intptr_t))
867 dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
868 if (memchr (dir, '\0', end - (char *) dir) == NULL)
873 if (cache->set < 0 || cache->set > cache->size - sizeof (FcFontSet))
876 fs = FcCacheSet (cache);
879 if (fs->nfont > (end - (char *) fs) / sizeof (FcPattern))
882 if (!FcIsEncodedOffset(fs->fonts))
885 for (i = 0; i < fs->nfont; i++)
887 FcPattern *font = FcFontSetFont (fs, i);
892 if ((char *) font < base ||
893 (char *) font > end - sizeof (FcFontSet) ||
894 font->elts_offset < 0 ||
895 font->elts_offset > end - (char *) font ||
896 font->num > (end - (char *) font - font->elts_offset) / sizeof (FcPatternElt) ||
897 !FcRefIsConst (&font->ref))
901 e = FcPatternElts(font);
902 if (e->values != 0 && !FcIsEncodedOffset(e->values))
905 for (j = 0; j < font->num; j++)
907 last_offset = (char *) font + font->elts_offset;
908 for (l = FcPatternEltValues(&e[j]); l; l = FcValueListNext(l))
910 if ((char *) l < last_offset || (char *) l > end - sizeof (*l) ||
911 (l->next != NULL && !FcIsEncodedOffset(l->next)))
913 last_offset = (char *) l + 1;
923 * Map a cache file into memory
926 FcDirCacheMapFd (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat)
929 FcBool allocated = FcFalse;
931 if (fd_stat->st_size > INTPTR_MAX ||
932 fd_stat->st_size < (int) sizeof (FcCache))
934 cache = FcCacheFindByStat (fd_stat);
937 if (FcCacheTimeValid (config, cache, dir_stat))
939 FcDirCacheUnload (cache);
944 * Large cache files are mmap'ed, smaller cache files are read. This
945 * balances the system cost of mmap against per-process memory usage.
947 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
949 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
950 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
951 #if (HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
952 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
954 if (cache == MAP_FAILED)
956 #elif defined(_WIN32)
961 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
962 PAGE_READONLY, 0, 0, NULL);
963 if (hFileMap != NULL)
965 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
967 CloseHandle (hFileMap);
974 cache = malloc (fd_stat->st_size);
978 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
985 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
986 cache->version < FC_CACHE_VERSION_NUMBER ||
987 cache->size != (intptr_t) fd_stat->st_size ||
988 !FcCacheOffsetsValid (cache) ||
989 !FcCacheTimeValid (config, cache, dir_stat) ||
990 !FcCacheInsert (cache, fd_stat))
996 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
997 munmap (cache, fd_stat->st_size);
998 #elif defined(_WIN32)
999 UnmapViewOfFile (cache);
1005 /* Mark allocated caches so they're freed rather than unmapped */
1007 cache->magic = FC_CACHE_MAGIC_ALLOC;
1013 FcDirCacheReference (FcCache *cache, int nref)
1015 FcCacheSkip *skip = FcCacheFindByAddr (cache);
1018 FcRefAdd (&skip->ref, nref);
1022 FcDirCacheUnload (FcCache *cache)
1024 FcCacheObjectDereference (cache);
1028 FcDirCacheMapHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
1030 FcCache *cache = FcDirCacheMapFd (config, fd, fd_stat, dir_stat);
1034 *((FcCache **) closure) = cache;
1039 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
1041 FcCache *cache = NULL;
1044 FcDirCacheReadUUID ((FcChar8 *) dir, config);
1046 if (!FcDirCacheProcess (config, dir,
1047 FcDirCacheMapHelper,
1048 &cache, cache_file))
1055 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
1059 struct stat my_file_stat;
1062 file_stat = &my_file_stat;
1063 fd = FcDirCacheOpenFile (cache_file, file_stat);
1066 cache = FcDirCacheMapFd (FcConfigGetCurrent (), fd, file_stat, NULL);
1072 FcDirChecksum (struct stat *statb)
1074 int ret = (int) statb->st_mtime;
1076 char *source_date_epoch;
1077 unsigned long long epoch;
1079 source_date_epoch = getenv("SOURCE_DATE_EPOCH");
1080 if (source_date_epoch)
1082 epoch = strtoull(source_date_epoch, &endptr, 10);
1084 if (endptr == source_date_epoch)
1086 "Fontconfig: SOURCE_DATE_EPOCH invalid\n");
1087 else if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0))
1088 || (errno != 0 && epoch == 0))
1090 "Fontconfig: SOURCE_DATE_EPOCH: strtoull: %s: %llu\n",
1091 strerror(errno), epoch);
1092 else if (*endptr != '\0')
1094 "Fontconfig: SOURCE_DATE_EPOCH has trailing garbage\n");
1095 else if (epoch > ULONG_MAX)
1097 "Fontconfig: SOURCE_DATE_EPOCH must be <= %lu but saw: %llu\n",
1099 else if (epoch < ret)
1100 /* Only override if directory is newer */
1108 FcDirChecksumNano (struct stat *statb)
1110 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1111 /* No nanosecond component to parse */
1112 if (getenv("SOURCE_DATE_EPOCH"))
1114 return statb->st_mtim.tv_nsec;
1121 * Validate a cache file by reading the header and checking
1122 * the magic number and the size field
1125 FcDirCacheValidateHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
1127 FcBool ret = FcTrue;
1130 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
1132 else if (c.magic != FC_CACHE_MAGIC_MMAP)
1134 else if (c.version < FC_CACHE_VERSION_NUMBER)
1136 else if (fd_stat->st_size != c.size)
1138 else if (c.checksum != FcDirChecksum (dir_stat))
1140 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1141 else if (c.checksum_nano != FcDirChecksumNano (dir_stat))
1148 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
1150 return FcDirCacheProcess (config, dir,
1151 FcDirCacheValidateHelper,
1156 FcDirCacheValid (const FcChar8 *dir)
1160 config = FcConfigGetCurrent ();
1164 return FcDirCacheValidConfig (dir, config);
1168 * Build a cache structure from the given contents
1171 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
1173 FcSerialize *serialize = FcSerializeCreate ();
1176 FcChar8 *dir_serialize;
1177 intptr_t *dirs_serialize;
1178 FcFontSet *set_serialize;
1183 * Space for cache structure
1185 FcSerializeReserve (serialize, sizeof (FcCache));
1189 if (!FcStrSerializeAlloc (serialize, dir))
1194 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
1195 for (i = 0; i < dirs->num; i++)
1196 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
1202 if (!FcFontSetSerializeAlloc (serialize, set))
1205 /* Serialize layout complete. Now allocate space and fill it */
1206 cache = malloc (serialize->size);
1209 /* shut up valgrind */
1210 memset (cache, 0, serialize->size);
1212 serialize->linear = cache;
1214 cache->magic = FC_CACHE_MAGIC_ALLOC;
1215 cache->version = FC_CACHE_VERSION_NUMBER;
1216 cache->size = serialize->size;
1217 cache->checksum = FcDirChecksum (dir_stat);
1218 cache->checksum_nano = FcDirChecksumNano (dir_stat);
1221 * Serialize directory name
1223 dir_serialize = FcStrSerialize (serialize, dir);
1226 cache->dir = FcPtrToOffset (cache, dir_serialize);
1229 * Serialize sub dirs
1231 dirs_serialize = FcSerializePtr (serialize, dirs);
1232 if (!dirs_serialize)
1234 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
1235 cache->dirs_count = dirs->num;
1236 for (i = 0; i < dirs->num; i++)
1238 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
1241 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
1245 * Serialize font set
1247 set_serialize = FcFontSetSerialize (serialize, set);
1250 cache->set = FcPtrToOffset (cache, set_serialize);
1252 FcSerializeDestroy (serialize);
1254 FcCacheInsert (cache, NULL);
1261 FcSerializeDestroy (serialize);
1266 FcDirCacheRebuild (FcCache *cache, struct stat *dir_stat, FcStrSet *dirs)
1269 FcFontSet *set = FcFontSetDeserialize (FcCacheSet (cache));
1270 const FcChar8 *dir = FcCacheDir (cache);
1272 new = FcDirCacheBuild (set, dir, dir_stat, dirs);
1273 FcFontSetDestroy (set);
1278 /* write serialized state to the cache file */
1280 FcDirCacheWrite (FcCache *cache, FcConfig *config)
1282 FcChar8 *dir = FcCacheDir (cache);
1283 FcChar8 cache_base[CACHEBASE_LEN];
1284 FcChar8 *cache_hashed;
1288 FcChar8 *cache_dir = NULL;
1289 FcChar8 *test_dir, *d = NULL;
1291 struct stat cache_stat;
1294 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
1297 * Write it to the first directory in the list which is writable
1300 list = FcStrListCreate (config->cacheDirs);
1303 while ((test_dir = FcStrListNext (list)))
1308 d = FcStrBuildFilename (sysroot, test_dir, NULL);
1310 d = FcStrCopyFilename (test_dir);
1312 if (access ((char *) d, W_OK) == 0)
1314 cache_dir = FcStrCopyFilename (d);
1320 * If the directory doesn't exist, try to create it
1322 if (access ((char *) d, F_OK) == -1) {
1323 if (FcMakeDirectory (d))
1325 cache_dir = FcStrCopyFilename (d);
1326 /* Create CACHEDIR.TAG */
1327 FcDirCacheCreateTagFile (d);
1332 * Otherwise, try making it writable
1334 else if (chmod ((char *) d, 0755) == 0)
1336 cache_dir = FcStrCopyFilename (d);
1337 /* Try to create CACHEDIR.TAG too */
1338 FcDirCacheCreateTagFile (d);
1345 FcStrListDone (list);
1350 if (!FcDirCacheBasenameUUID (dir, cache_base, config))
1352 FcDirCacheBasenameMD5 (dir, cache_base);
1353 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
1354 FcStrFree (cache_dir);
1358 if (FcDebug () & FC_DBG_CACHE)
1359 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
1362 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1366 if (!FcAtomicLock (atomic))
1369 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
1373 /* Temporarily switch magic to MMAP while writing to file */
1374 magic = cache->magic;
1375 if (magic != FC_CACHE_MAGIC_MMAP)
1376 cache->magic = FC_CACHE_MAGIC_MMAP;
1379 * Write cache contents to file
1381 written = write (fd, cache, cache->size);
1383 /* Switch magic back */
1384 if (magic != FC_CACHE_MAGIC_MMAP)
1385 cache->magic = magic;
1387 if (written != cache->size)
1389 perror ("write cache");
1394 if (!FcAtomicReplaceOrig(atomic))
1397 /* If the file is small, update the cache chain entry such that the
1398 * new cache file is not read again. If it's large, we don't do that
1399 * such that we reload it, using mmap, which is shared across processes.
1401 if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat))
1404 if ((skip = FcCacheFindByAddrUnlocked (cache)))
1406 skip->cache_dev = cache_stat.st_dev;
1407 skip->cache_ino = cache_stat.st_ino;
1408 skip->cache_mtime = cache_stat.st_mtime;
1409 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1410 skip->cache_mtime_nano = cache_stat.st_mtim.tv_nsec;
1412 skip->cache_mtime_nano = 0;
1418 FcStrFree (cache_hashed);
1419 FcAtomicUnlock (atomic);
1420 FcAtomicDestroy (atomic);
1426 FcAtomicUnlock (atomic);
1428 FcAtomicDestroy (atomic);
1430 FcStrFree (cache_hashed);
1435 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1440 FcBool ret = FcTrue;
1443 struct stat target_stat;
1444 const FcChar8 *sysroot;
1446 /* FIXME: this API needs to support non-current FcConfig */
1447 sysroot = FcConfigGetSysRoot (NULL);
1449 dir = FcStrBuildFilename (sysroot, cache_dir, NULL);
1451 dir = FcStrCopyFilename (cache_dir);
1454 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1457 if (access ((char *) dir, W_OK) != 0)
1459 if (verbose || FcDebug () & FC_DBG_CACHE)
1460 printf ("%s: not cleaning %s cache directory\n", dir,
1461 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
1464 if (verbose || FcDebug () & FC_DBG_CACHE)
1465 printf ("%s: cleaning cache directory\n", dir);
1466 d = opendir ((char *) dir);
1469 perror ((char *) dir);
1473 while ((ent = readdir (d)))
1476 const FcChar8 *target_dir;
1478 if (ent->d_name[0] == '.')
1480 /* skip cache files for different architectures and */
1481 /* files which are not cache files at all */
1482 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1483 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1486 file_name = FcStrBuildFilename (dir, (FcChar8 *)ent->d_name, NULL);
1489 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", dir);
1494 cache = FcDirCacheLoadFile (file_name, NULL);
1497 if (verbose || FcDebug () & FC_DBG_CACHE)
1498 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
1505 target_dir = FcCacheDir (cache);
1507 s = FcStrBuildFilename (sysroot, target_dir, NULL);
1509 s = FcStrdup (target_dir);
1510 if (stat ((char *) s, &target_stat) < 0)
1512 if (verbose || FcDebug () & FC_DBG_CACHE)
1513 printf ("%s: %s: missing directory: %s \n",
1514 dir, ent->d_name, s);
1517 FcDirCacheUnload (cache);
1522 if (unlink ((char *) file_name) < 0)
1524 perror ((char *) file_name);
1528 FcStrFree (file_name);
1539 FcDirCacheLock (const FcChar8 *dir,
1542 FcChar8 *cache_hashed = NULL;
1543 FcChar8 cache_base[CACHEBASE_LEN];
1546 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
1550 if (!FcDirCacheBasenameUUID (dir, cache_base, config))
1552 FcDirCacheBasenameMD5 (dir, cache_base);
1553 list = FcStrListCreate (config->cacheDirs);
1557 while ((cache_dir = FcStrListNext (list)))
1560 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
1562 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
1565 fd = FcOpen ((const char *)cache_hashed, O_RDWR);
1566 FcStrFree (cache_hashed);
1567 /* No caches in that directory. simply retry with another one */
1571 if (_locking (fd, _LK_LOCK, 1) == -1)
1576 fl.l_type = F_WRLCK;
1577 fl.l_whence = SEEK_SET;
1580 fl.l_pid = getpid ();
1581 if (fcntl (fd, F_SETLKW, &fl) == -1)
1587 FcStrListDone (list);
1590 FcStrListDone (list);
1597 FcDirCacheUnlock (int fd)
1602 _locking (fd, _LK_UNLCK, 1);
1606 fl.l_type = F_UNLCK;
1607 fl.l_whence = SEEK_SET;
1610 fl.l_pid = getpid ();
1611 fcntl (fd, F_SETLK, &fl);
1618 * Hokey little macro trick to permit the definitions of C functions
1619 * with the same name as CPP macros
1621 #define args1(x) (x)
1622 #define args2(x,y) (x,y)
1625 FcCacheDir args1(const FcCache *c)
1627 return FcCacheDir (c);
1631 FcCacheCopySet args1(const FcCache *c)
1633 FcFontSet *old = FcCacheSet (c);
1634 FcFontSet *new = FcFontSetCreate ();
1639 for (i = 0; i < old->nfont; i++)
1641 FcPattern *font = FcFontSetFont (old, i);
1643 FcPatternReference (font);
1644 if (!FcFontSetAdd (new, font))
1646 FcFontSetDestroy (new);
1654 FcCacheSubdir args2(const FcCache *c, int i)
1656 return FcCacheSubdir (c, i);
1660 FcCacheNumSubdir args1(const FcCache *c)
1662 return c->dirs_count;
1666 FcCacheNumFont args1(const FcCache *c)
1668 return FcCacheSet(c)->nfont;
1672 * This code implements the MD5 message-digest algorithm.
1673 * The algorithm is due to Ron Rivest. This code was
1674 * written by Colin Plumb in 1993, no copyright is claimed.
1675 * This code is in the public domain; do with it what you wish.
1677 * Equivalent code is available from RSA Data Security, Inc.
1678 * This code has been tested against that, and is equivalent,
1679 * except that you don't need to include two pages of legalese
1682 * To compute the message digest of a chunk of bytes, declare an
1683 * MD5Context structure, pass it to MD5Init, call MD5Update as
1684 * needed on buffers full of bytes, and then call MD5Final, which
1685 * will fill a supplied 16-byte array with the digest.
1689 #define byteReverse(buf, len) /* Nothing */
1692 * Note: this code is harmless on little-endian machines.
1694 void byteReverse(unsigned char *buf, unsigned longs)
1698 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1699 ((unsigned) buf[1] << 8 | buf[0]);
1700 *(FcChar32 *) buf = t;
1707 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1708 * initialization constants.
1710 static void MD5Init(struct MD5Context *ctx)
1712 ctx->buf[0] = 0x67452301;
1713 ctx->buf[1] = 0xefcdab89;
1714 ctx->buf[2] = 0x98badcfe;
1715 ctx->buf[3] = 0x10325476;
1722 * Update context to reflect the concatenation of another buffer full
1725 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1729 /* Update bitcount */
1732 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1733 ctx->bits[1]++; /* Carry from low to high */
1734 ctx->bits[1] += len >> 29;
1736 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1738 /* Handle any leading odd-sized chunks */
1741 unsigned char *p = (unsigned char *) ctx->in + t;
1745 memcpy(p, buf, len);
1749 byteReverse(ctx->in, 16);
1750 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1754 /* Process data in 64-byte chunks */
1757 memcpy(ctx->in, buf, 64);
1758 byteReverse(ctx->in, 16);
1759 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1764 /* Handle any remaining bytes of data. */
1766 memcpy(ctx->in, buf, len);
1770 * Final wrapup - pad to 64-byte boundary with the bit pattern
1771 * 1 0* (64-bit count of bits processed, MSB-first)
1773 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1778 /* Compute number of bytes mod 64 */
1779 count = (ctx->bits[0] >> 3) & 0x3F;
1781 /* Set the first char of padding to 0x80. This is safe since there is
1782 always at least one byte free */
1783 p = ctx->in + count;
1786 /* Bytes of padding needed to make 64 bytes */
1787 count = 64 - 1 - count;
1789 /* Pad out to 56 mod 64 */
1791 /* Two lots of padding: Pad the first block to 64 bytes */
1792 memset(p, 0, count);
1793 byteReverse(ctx->in, 16);
1794 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1796 /* Now fill the next block with 56 bytes */
1797 memset(ctx->in, 0, 56);
1799 /* Pad block to 56 bytes */
1800 memset(p, 0, count - 8);
1802 byteReverse(ctx->in, 14);
1804 /* Append length in bits and transform */
1805 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1806 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1808 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1809 byteReverse((unsigned char *) ctx->buf, 4);
1810 memcpy(digest, ctx->buf, 16);
1811 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1815 /* The four core functions - F1 is optimized somewhat */
1817 /* #define F1(x, y, z) (x & y | ~x & z) */
1818 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1819 #define F2(x, y, z) F1(z, x, y)
1820 #define F3(x, y, z) (x ^ y ^ z)
1821 #define F4(x, y, z) (y ^ (x | ~z))
1823 /* This is the central step in the MD5 algorithm. */
1824 #define MD5STEP(f, w, x, y, z, data, s) \
1825 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1828 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1829 * reflect the addition of 16 longwords of new data. MD5Update blocks
1830 * the data and converts bytes into longwords for this routine.
1832 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1834 register FcChar32 a, b, c, d;
1841 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1842 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1843 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1844 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1845 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1846 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1847 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1848 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1849 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1850 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1851 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1852 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1853 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1854 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1855 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1856 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1858 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1859 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1860 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1861 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1862 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1863 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1864 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1865 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1866 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1867 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1868 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1869 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1870 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1871 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1872 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1873 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1875 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1876 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1877 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1878 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1879 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1880 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1881 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1882 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1883 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1884 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1885 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1886 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1887 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1888 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1889 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1890 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1892 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1893 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1894 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1895 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1896 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1897 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1898 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1899 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1900 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1901 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1902 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1903 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1904 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1905 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1906 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1907 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1916 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1922 static const FcChar8 cache_tag_contents[] =
1923 "Signature: 8a477f597d28d172789f06886806bc55\n"
1924 "# This file is a cache directory tag created by fontconfig.\n"
1925 "# For information about cache directory tags, see:\n"
1926 "# http://www.brynosaurus.com/cachedir/\n";
1927 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1928 FcBool ret = FcFalse;
1933 if (access ((char *) cache_dir, W_OK) == 0)
1935 /* Create CACHEDIR.TAG */
1936 cache_tag = FcStrBuildFilename (cache_dir, "CACHEDIR.TAG", NULL);
1939 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1942 if (!FcAtomicLock (atomic))
1944 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1947 fp = fdopen(fd, "wb");
1951 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1954 if (!FcAtomicReplaceOrig(atomic))
1959 FcAtomicUnlock (atomic);
1961 FcAtomicDestroy (atomic);
1963 FcStrFree (cache_tag);
1966 if (FcDebug () & FC_DBG_CACHE)
1969 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1971 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1978 FcCacheCreateTagFile (const FcConfig *config)
1980 FcChar8 *cache_dir = NULL, *d = NULL;
1982 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
1984 list = FcConfigGetCacheDirs (config);
1988 while ((cache_dir = FcStrListNext (list)))
1993 d = FcStrBuildFilename (sysroot, cache_dir, NULL);
1995 d = FcStrCopyFilename (cache_dir);
1996 if (FcDirCacheCreateTagFile (d))
2001 FcStrListDone (list);
2005 #include "fcaliastail.h"