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.
33 #include <sys/types.h>
36 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
38 # include <sys/mman.h>
52 static void MD5Init(struct MD5Context *ctx);
53 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
54 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
55 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
57 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
64 typedef long long INT64;
65 #define EPOCH_OFFSET 11644473600ll
67 #define EPOCH_OFFSET 11644473600i64
68 typedef __int64 INT64;
71 /* Workaround for problems in the stat() in the Microsoft C library:
73 * 1) stat() uses FindFirstFile() to get the file
74 * attributes. Unfortunately this API doesn't return correct values
75 * for modification time of a directory until some time after a file
76 * or subdirectory has been added to the directory. (This causes
77 * run-test.sh to fail, for instance.) GetFileAttributesEx() is
78 * better, it returns the updated timestamp right away.
80 * 2) stat() does some strange things related to backward
81 * compatibility with the local time timestamps on FAT volumes and
82 * daylight saving time. This causes problems after the switches
83 * to/from daylight saving time. See
84 * http://bugzilla.gnome.org/show_bug.cgi?id=154968 , especially
85 * comment #30, and http://www.codeproject.com/datetime/dstbugs.asp .
86 * We don't need any of that, FAT and Win9x are as good as dead. So
87 * just use the UTC timestamps from NTFS, converted to the Unix epoch.
91 FcStat (const FcChar8 *file, struct stat *statb)
93 WIN32_FILE_ATTRIBUTE_DATA wfad;
94 char full_path_name[MAX_PATH];
98 if (!GetFileAttributesEx (file, GetFileExInfoStandard, &wfad))
103 /* Calculate a pseudo inode number as a hash of the full path name.
104 * Call GetLongPathName() to get the spelling of the path name as it
107 rc = GetFullPathName (file, sizeof (full_path_name), full_path_name, &basename);
108 if (rc == 0 || rc > sizeof (full_path_name))
111 rc = GetLongPathName (full_path_name, full_path_name, sizeof (full_path_name));
112 statb->st_ino = FcStringHash (full_path_name);
114 statb->st_mode = _S_IREAD | _S_IWRITE;
115 statb->st_mode |= (statb->st_mode >> 3) | (statb->st_mode >> 6);
117 if (wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
118 statb->st_mode |= _S_IFDIR;
120 statb->st_mode |= _S_IFREG;
123 statb->st_uid = statb->st_gid = 0;
126 if (wfad.nFileSizeHigh > 0)
128 statb->st_size = wfad.nFileSizeLow;
130 statb->st_atime = (*(INT64 *)&wfad.ftLastAccessTime)/10000000 - EPOCH_OFFSET;
131 statb->st_mtime = (*(INT64 *)&wfad.ftLastWriteTime)/10000000 - EPOCH_OFFSET;
132 statb->st_ctime = statb->st_mtime;
140 FcStat (const FcChar8 *file, struct stat *statb)
142 return stat ((char *) file, statb);
147 static const char bin2hex[] = { '0', '1', '2', '3',
150 'c', 'd', 'e', 'f' };
153 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
155 unsigned char hash[16];
158 struct MD5Context ctx;
161 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
163 MD5Final (hash, &ctx);
166 hex_hash = cache_base + 1;
167 for (cnt = 0; cnt < 16; ++cnt)
169 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
170 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
173 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
179 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
181 FcChar8 *cache_hashed = NULL;
182 FcChar8 cache_base[CACHEBASE_LEN];
186 FcDirCacheBasename (dir, cache_base);
188 list = FcStrListCreate (config->cacheDirs);
192 while ((cache_dir = FcStrListNext (list)))
194 cache_hashed = FcStrPlus (cache_dir, cache_base);
197 (void) unlink ((char *) cache_hashed);
198 FcStrFree (cache_hashed);
200 FcStrListDone (list);
201 /* return FcFalse if something went wrong */
208 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
213 if (FcStat (cache_file, file_stat) < 0)
216 fd = open((char *) cache_file, O_RDONLY | O_BINARY);
220 if (fstat (fd, file_stat) < 0)
230 * Look for a cache file for the specified dir. Attempt
231 * to use each one we find, stopping when the callback
235 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
236 FcBool (*callback) (int fd, struct stat *fd_stat,
237 struct stat *dir_stat, void *closure),
238 void *closure, FcChar8 **cache_file_ret)
241 FcChar8 cache_base[CACHEBASE_LEN];
244 struct stat file_stat, dir_stat;
245 FcBool ret = FcFalse;
247 if (FcStat (dir, &dir_stat) < 0)
250 FcDirCacheBasename (dir, cache_base);
252 list = FcStrListCreate (config->cacheDirs);
256 while ((cache_dir = FcStrListNext (list)))
258 FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base);
261 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
263 ret = (*callback) (fd, &file_stat, &dir_stat, closure);
268 *cache_file_ret = cache_hashed;
270 FcStrFree (cache_hashed);
274 FcStrFree (cache_hashed);
276 FcStrListDone (list);
281 #define FC_CACHE_MIN_MMAP 1024
284 * Skip list element, make sure the 'next' pointer is the last thing
285 * in the structure, it will be allocated large enough to hold all
286 * of the necessary pointers
289 typedef struct _FcCacheSkip FcCacheSkip;
291 struct _FcCacheSkip {
298 FcCacheSkip *next[1];
302 * The head of the skip list; pointers for every possible level
303 * in the skip list, plus the largest level in the list
306 #define FC_CACHE_MAX_LEVEL 16
308 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
309 static int fcCacheMaxLevel;
318 static struct random_data fcrandbuf;
319 static char statebuf[256];
320 static FcBool initialized = FcFalse;
322 if (initialized != FcTrue)
324 initstate_r(time(NULL), statebuf, 256, &fcrandbuf);
325 initialized = FcTrue;
328 random_r(&fcrandbuf, &result);
330 static char statebuf[256];
332 static FcBool initialized = FcFalse;
334 if (initialized != FcTrue)
336 state = initstate(time(NULL), statebuf, 256);
337 initialized = FcTrue;
340 state = setstate(statebuf);
348 static unsigned int seed = time(NULL);
350 result = rand_r(&seed);
352 static FcBool initialized = FcFalse;
354 if (initialized != FcTrue)
357 initialized = FcTrue;
361 # error no random number generator function available.
368 * Generate a random level number, distributed
369 * so that each level is 1/4 as likely as the one before
371 * Note that level numbers run 1 <= level <= MAX_LEVEL
376 /* tricky bit -- each bit is '1' 75% of the time */
377 long int bits = FcRandom () | FcRandom ();
380 while (++level < FC_CACHE_MAX_LEVEL)
390 * Insert cache into the list
393 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
395 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
396 FcCacheSkip *s, **next;
400 * Find links along each chain
402 next = fcCacheChains;
403 for (i = fcCacheMaxLevel; --i >= 0; )
405 for (; (s = next[i]); next = s->next)
406 if (s->cache > cache)
408 update[i] = &next[i];
412 * Create new list element
414 level = random_level ();
415 if (level > fcCacheMaxLevel)
417 level = fcCacheMaxLevel + 1;
418 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
419 fcCacheMaxLevel = level;
422 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
427 s->size = cache->size;
431 s->cache_dev = cache_stat->st_dev;
432 s->cache_ino = cache_stat->st_ino;
433 s->cache_mtime = cache_stat->st_mtime;
443 * Insert into all fcCacheChains
445 for (i = 0; i < level; i++)
447 s->next[i] = *update[i];
454 FcCacheFindByAddr (void *object)
457 FcCacheSkip **next = fcCacheChains;
461 * Walk chain pointers one level at a time
463 for (i = fcCacheMaxLevel; --i >= 0;)
464 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
465 next = next[i]->next;
470 if (s && (char *) object < ((char *) s->cache + s->size))
476 FcCacheRemove (FcCache *cache)
478 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
479 FcCacheSkip *s, **next;
483 * Find links along each chain
485 next = fcCacheChains;
486 for (i = fcCacheMaxLevel; --i >= 0; )
488 for (; (s = next[i]); next = s->next)
489 if (s->cache >= cache)
491 update[i] = &next[i];
494 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
495 *update[i] = s->next[i];
496 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
502 FcCacheFindByStat (struct stat *cache_stat)
506 for (s = fcCacheChains[0]; s; s = s->next[0])
507 if (s->cache_dev == cache_stat->st_dev &&
508 s->cache_ino == cache_stat->st_ino &&
509 s->cache_mtime == cache_stat->st_mtime)
518 FcDirCacheDispose (FcCache *cache)
520 switch (cache->magic) {
521 case FC_CACHE_MAGIC_ALLOC:
524 case FC_CACHE_MAGIC_MMAP:
525 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
526 munmap (cache, cache->size);
527 #elif defined(_WIN32)
528 UnmapViewOfFile (cache);
532 FcCacheRemove (cache);
536 FcCacheObjectReference (void *object)
538 FcCacheSkip *skip = FcCacheFindByAddr (object);
545 FcCacheObjectDereference (void *object)
547 FcCacheSkip *skip = FcCacheFindByAddr (object);
553 FcDirCacheDispose (skip->cache);
562 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
563 assert (fcCacheChains[i] == NULL);
564 assert (fcCacheMaxLevel == 0);
568 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
570 struct stat dir_static;
574 if (FcStat (FcCacheDir (cache), &dir_static) < 0)
576 dir_stat = &dir_static;
578 if (FcDebug () & FC_DBG_CACHE)
579 printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n",
580 FcCacheDir (cache), cache->mtime, (int) dir_stat->st_mtime);
581 return cache->mtime == (int) dir_stat->st_mtime;
585 * Map a cache file into memory
588 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
591 FcBool allocated = FcFalse;
593 if (fd_stat->st_size < sizeof (FcCache))
595 cache = FcCacheFindByStat (fd_stat);
598 if (FcCacheTimeValid (cache, dir_stat))
600 FcDirCacheUnload (cache);
605 * Lage cache files are mmap'ed, smaller cache files are read. This
606 * balances the system cost of mmap against per-process memory usage.
608 if (fd_stat->st_size >= FC_CACHE_MIN_MMAP)
610 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
611 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
612 #ifdef HAVE_POSIX_FADVISE
613 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
615 if (cache == MAP_FAILED)
617 #elif defined(_WIN32)
622 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
623 PAGE_READONLY, 0, 0, NULL);
624 if (hFileMap != NULL)
626 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
628 CloseHandle (hFileMap);
635 cache = malloc (fd_stat->st_size);
639 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
646 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
647 cache->version < FC_CACHE_CONTENT_VERSION ||
648 cache->size != fd_stat->st_size ||
649 !FcCacheTimeValid (cache, dir_stat) ||
650 !FcCacheInsert (cache, fd_stat))
656 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
657 munmap (cache, fd_stat->st_size);
658 #elif defined(_WIN32)
659 UnmapViewOfFile (cache);
665 /* Mark allocated caches so they're freed rather than unmapped */
667 cache->magic = FC_CACHE_MAGIC_ALLOC;
673 FcDirCacheReference (FcCache *cache, int nref)
675 FcCacheSkip *skip = FcCacheFindByAddr (cache);
682 FcDirCacheUnload (FcCache *cache)
684 FcCacheObjectDereference (cache);
688 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
690 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
694 *((FcCache **) closure) = cache;
699 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
701 FcCache *cache = NULL;
703 if (!FcDirCacheProcess (config, dir,
711 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
715 struct stat my_file_stat;
718 file_stat = &my_file_stat;
719 fd = FcDirCacheOpenFile (cache_file, file_stat);
722 cache = FcDirCacheMapFd (fd, file_stat, NULL);
728 * Validate a cache file by reading the header and checking
729 * the magic number and the size field
732 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
737 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
739 else if (c.magic != FC_CACHE_MAGIC_MMAP)
741 else if (c.version < FC_CACHE_CONTENT_VERSION)
743 else if (fd_stat->st_size != c.size)
745 else if (c.mtime != (int) dir_stat->st_mtime)
751 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
753 return FcDirCacheProcess (config, dir,
754 FcDirCacheValidateHelper,
759 FcDirCacheValid (const FcChar8 *dir)
763 config = FcConfigGetCurrent ();
767 return FcDirCacheValidConfig (dir, config);
771 * Build a cache structure from the given contents
774 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
776 FcSerialize *serialize = FcSerializeCreate ();
779 FcChar8 *dir_serialize;
780 intptr_t *dirs_serialize;
781 FcFontSet *set_serialize;
786 * Space for cache structure
788 FcSerializeReserve (serialize, sizeof (FcCache));
792 if (!FcStrSerializeAlloc (serialize, dir))
797 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
798 for (i = 0; i < dirs->num; i++)
799 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
805 if (!FcFontSetSerializeAlloc (serialize, set))
808 /* Serialize layout complete. Now allocate space and fill it */
809 cache = malloc (serialize->size);
812 /* shut up valgrind */
813 memset (cache, 0, serialize->size);
815 serialize->linear = cache;
817 cache->magic = FC_CACHE_MAGIC_ALLOC;
818 cache->version = FC_CACHE_CONTENT_VERSION;
819 cache->size = serialize->size;
820 cache->mtime = (int) dir_stat->st_mtime;
823 * Serialize directory name
825 dir_serialize = FcStrSerialize (serialize, dir);
828 cache->dir = FcPtrToOffset (cache, dir_serialize);
833 dirs_serialize = FcSerializePtr (serialize, dirs);
836 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
837 cache->dirs_count = dirs->num;
838 for (i = 0; i < dirs->num; i++)
840 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
843 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
849 set_serialize = FcFontSetSerialize (serialize, set);
852 cache->set = FcPtrToOffset (cache, set_serialize);
854 FcSerializeDestroy (serialize);
856 FcCacheInsert (cache, NULL);
863 FcSerializeDestroy (serialize);
869 #define mkdir(path,mode) _mkdir(path)
873 FcMakeDirectory (const FcChar8 *dir)
878 if (strlen ((char *) dir) == 0)
881 parent = FcStrDirname (dir);
884 if (access ((char *) parent, F_OK) == 0)
885 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
886 else if (access ((char *) parent, F_OK) == -1)
887 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
894 /* write serialized state to the cache file */
896 FcDirCacheWrite (FcCache *cache, FcConfig *config)
898 FcChar8 *dir = FcCacheDir (cache);
899 FcChar8 cache_base[CACHEBASE_LEN];
900 FcChar8 *cache_hashed;
904 FcChar8 *cache_dir = NULL;
907 struct stat cache_stat;
912 * Write it to the first directory in the list which is writable
915 list = FcStrListCreate (config->cacheDirs);
918 while ((test_dir = FcStrListNext (list))) {
919 if (access ((char *) test_dir, W_OK|X_OK) == 0)
921 cache_dir = test_dir;
927 * If the directory doesn't exist, try to create it
929 if (access ((char *) test_dir, F_OK) == -1) {
930 if (FcMakeDirectory (test_dir))
932 cache_dir = test_dir;
933 /* Create CACHEDIR.TAG */
934 FcDirCacheCreateTagFile (cache_dir);
939 * Otherwise, try making it writable
941 else if (chmod ((char *) test_dir, 0755) == 0)
943 cache_dir = test_dir;
944 /* Try to create CACHEDIR.TAG too */
945 FcDirCacheCreateTagFile (cache_dir);
950 FcStrListDone (list);
954 FcDirCacheBasename (dir, cache_base);
955 cache_hashed = FcStrPlus (cache_dir, cache_base);
959 if (FcDebug () & FC_DBG_CACHE)
960 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
963 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
967 if (!FcAtomicLock (atomic))
970 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
974 /* Temporarily switch magic to MMAP while writing to file */
975 magic = cache->magic;
976 if (magic != FC_CACHE_MAGIC_MMAP)
977 cache->magic = FC_CACHE_MAGIC_MMAP;
980 * Write cache contents to file
982 written = write (fd, cache, cache->size);
984 /* Switch magic back */
985 if (magic != FC_CACHE_MAGIC_MMAP)
986 cache->magic = magic;
988 if (written != cache->size)
990 perror ("write cache");
995 if (!FcAtomicReplaceOrig(atomic))
998 /* If the file is small, update the cache chain entry such that the
999 * new cache file is not read again. If it's large, we don't do that
1000 * such that we reload it, using mmap, which is shared across processes.
1002 if (cache->size < FC_CACHE_MIN_MMAP &&
1003 (skip = FcCacheFindByAddr (cache)) &&
1004 FcStat (cache_hashed, &cache_stat))
1006 skip->cache_dev = cache_stat.st_dev;
1007 skip->cache_ino = cache_stat.st_ino;
1008 skip->cache_mtime = cache_stat.st_mtime;
1011 FcStrFree (cache_hashed);
1012 FcAtomicUnlock (atomic);
1013 FcAtomicDestroy (atomic);
1019 FcAtomicUnlock (atomic);
1021 FcAtomicDestroy (atomic);
1023 FcStrFree (cache_hashed);
1028 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1033 FcBool ret = FcTrue;
1036 struct stat target_stat;
1038 dir_base = FcStrPlus (cache_dir, (FcChar8 *) FC_DIR_SEPARATOR_S);
1041 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1044 if (access ((char *) cache_dir, W_OK) != 0)
1046 if (verbose || FcDebug () & FC_DBG_CACHE)
1047 printf ("%s: not cleaning %s cache directory\n", cache_dir,
1048 access ((char *) cache_dir, F_OK) == 0 ? "unwritable" : "non-existent");
1051 if (verbose || FcDebug () & FC_DBG_CACHE)
1052 printf ("%s: cleaning cache directory\n", cache_dir);
1053 d = opendir ((char *) cache_dir);
1056 perror ((char *) cache_dir);
1060 while ((ent = readdir (d)))
1063 const FcChar8 *target_dir;
1065 if (ent->d_name[0] == '.')
1067 /* skip cache files for different architectures and */
1068 /* files which are not cache files at all */
1069 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1070 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1073 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
1076 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", cache_dir);
1081 cache = FcDirCacheLoadFile (file_name, NULL);
1084 if (verbose || FcDebug () & FC_DBG_CACHE)
1085 printf ("%s: invalid cache file: %s\n", cache_dir, ent->d_name);
1090 target_dir = FcCacheDir (cache);
1091 if (stat ((char *) target_dir, &target_stat) < 0)
1093 if (verbose || FcDebug () & FC_DBG_CACHE)
1094 printf ("%s: %s: missing directory: %s \n",
1095 cache_dir, ent->d_name, target_dir);
1101 if (unlink ((char *) file_name) < 0)
1103 perror ((char *) file_name);
1107 FcDirCacheUnload (cache);
1108 FcStrFree (file_name);
1113 FcStrFree (dir_base);
1119 * Hokey little macro trick to permit the definitions of C functions
1120 * with the same name as CPP macros
1122 #define args1(x) (x)
1123 #define args2(x,y) (x,y)
1126 FcCacheDir args1(const FcCache *c)
1128 return FcCacheDir (c);
1132 FcCacheCopySet args1(const FcCache *c)
1134 FcFontSet *old = FcCacheSet (c);
1135 FcFontSet *new = FcFontSetCreate ();
1140 for (i = 0; i < old->nfont; i++)
1142 FcPattern *font = FcFontSetFont (old, i);
1144 FcPatternReference (font);
1145 if (!FcFontSetAdd (new, font))
1147 FcFontSetDestroy (new);
1155 FcCacheSubdir args2(const FcCache *c, int i)
1157 return FcCacheSubdir (c, i);
1161 FcCacheNumSubdir args1(const FcCache *c)
1163 return c->dirs_count;
1167 FcCacheNumFont args1(const FcCache *c)
1169 return FcCacheSet(c)->nfont;
1173 * This code implements the MD5 message-digest algorithm.
1174 * The algorithm is due to Ron Rivest. This code was
1175 * written by Colin Plumb in 1993, no copyright is claimed.
1176 * This code is in the public domain; do with it what you wish.
1178 * Equivalent code is available from RSA Data Security, Inc.
1179 * This code has been tested against that, and is equivalent,
1180 * except that you don't need to include two pages of legalese
1183 * To compute the message digest of a chunk of bytes, declare an
1184 * MD5Context structure, pass it to MD5Init, call MD5Update as
1185 * needed on buffers full of bytes, and then call MD5Final, which
1186 * will fill a supplied 16-byte array with the digest.
1190 #define byteReverse(buf, len) /* Nothing */
1193 * Note: this code is harmless on little-endian machines.
1195 void byteReverse(unsigned char *buf, unsigned longs)
1199 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1200 ((unsigned) buf[1] << 8 | buf[0]);
1201 *(FcChar32 *) buf = t;
1208 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1209 * initialization constants.
1211 static void MD5Init(struct MD5Context *ctx)
1213 ctx->buf[0] = 0x67452301;
1214 ctx->buf[1] = 0xefcdab89;
1215 ctx->buf[2] = 0x98badcfe;
1216 ctx->buf[3] = 0x10325476;
1223 * Update context to reflect the concatenation of another buffer full
1226 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1230 /* Update bitcount */
1233 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1234 ctx->bits[1]++; /* Carry from low to high */
1235 ctx->bits[1] += len >> 29;
1237 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1239 /* Handle any leading odd-sized chunks */
1242 unsigned char *p = (unsigned char *) ctx->in + t;
1246 memcpy(p, buf, len);
1250 byteReverse(ctx->in, 16);
1251 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1255 /* Process data in 64-byte chunks */
1258 memcpy(ctx->in, buf, 64);
1259 byteReverse(ctx->in, 16);
1260 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1265 /* Handle any remaining bytes of data. */
1267 memcpy(ctx->in, buf, len);
1271 * Final wrapup - pad to 64-byte boundary with the bit pattern
1272 * 1 0* (64-bit count of bits processed, MSB-first)
1274 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1279 /* Compute number of bytes mod 64 */
1280 count = (ctx->bits[0] >> 3) & 0x3F;
1282 /* Set the first char of padding to 0x80. This is safe since there is
1283 always at least one byte free */
1284 p = ctx->in + count;
1287 /* Bytes of padding needed to make 64 bytes */
1288 count = 64 - 1 - count;
1290 /* Pad out to 56 mod 64 */
1292 /* Two lots of padding: Pad the first block to 64 bytes */
1293 memset(p, 0, count);
1294 byteReverse(ctx->in, 16);
1295 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1297 /* Now fill the next block with 56 bytes */
1298 memset(ctx->in, 0, 56);
1300 /* Pad block to 56 bytes */
1301 memset(p, 0, count - 8);
1303 byteReverse(ctx->in, 14);
1305 /* Append length in bits and transform */
1306 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1307 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1309 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1310 byteReverse((unsigned char *) ctx->buf, 4);
1311 memcpy(digest, ctx->buf, 16);
1312 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1316 /* The four core functions - F1 is optimized somewhat */
1318 /* #define F1(x, y, z) (x & y | ~x & z) */
1319 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1320 #define F2(x, y, z) F1(z, x, y)
1321 #define F3(x, y, z) (x ^ y ^ z)
1322 #define F4(x, y, z) (y ^ (x | ~z))
1324 /* This is the central step in the MD5 algorithm. */
1325 #define MD5STEP(f, w, x, y, z, data, s) \
1326 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1329 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1330 * reflect the addition of 16 longwords of new data. MD5Update blocks
1331 * the data and converts bytes into longwords for this routine.
1333 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1335 register FcChar32 a, b, c, d;
1342 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1343 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1344 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1345 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1346 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1347 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1348 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1349 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1350 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1351 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1352 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1353 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1354 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1355 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1356 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1357 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1359 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1360 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1361 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1362 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1363 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1364 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1365 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1366 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1367 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1368 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1369 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1370 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1371 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1372 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1373 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1374 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1376 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1377 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1378 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1379 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1380 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1381 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1382 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1383 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1384 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1385 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1386 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1387 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1388 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1389 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1390 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1391 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1393 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1394 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1395 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1396 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1397 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1398 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1399 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1400 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1401 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1402 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1403 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1404 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1405 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1406 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1407 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1408 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1417 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1423 static const FcChar8 cache_tag_contents[] =
1424 "Signature: 8a477f597d28d172789f06886806bc55\n"
1425 "# This file is a cache directory tag created by fontconfig.\n"
1426 "# For information about cache directory tags, see:\n"
1427 "# http://www.brynosaurus.com/cachedir/\n";
1428 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1429 FcBool ret = FcFalse;
1434 if (access ((char *) cache_dir, W_OK|X_OK) == 0)
1436 /* Create CACHEDIR.TAG */
1437 cache_tag = FcStrPlus (cache_dir, (const FcChar8 *) FC_DIR_SEPARATOR_S "CACHEDIR.TAG");
1440 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1443 if (!FcAtomicLock (atomic))
1445 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1448 fp = fdopen(fd, "wb");
1452 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1455 if (!FcAtomicReplaceOrig(atomic))
1460 FcAtomicUnlock (atomic);
1462 FcAtomicDestroy (atomic);
1464 FcStrFree (cache_tag);
1467 if (FcDebug () & FC_DBG_CACHE)
1470 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1472 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1479 FcCacheCreateTagFile (const FcConfig *config)
1481 FcChar8 *cache_dir = NULL;
1484 list = FcConfigGetCacheDirs (config);
1488 while ((cache_dir = FcStrListNext (list)))
1490 if (FcDirCacheCreateTagFile (cache_dir))
1493 FcStrListDone (list);
1497 #include "fcaliastail.h"