2 * $XFree86: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
28 * POSIX has broken stdio so that getc must do thread-safe locking,
29 * this is a serious performance problem for applications doing large
30 * amounts of IO with getc (as is done here). If available, use
31 * the getc_unlocked varient instead.
34 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
35 #define GETC(f) getc_unlocked(f)
36 #define PUTC(c,f) putc_unlocked(c,f)
38 #define GETC(f) getc(f)
39 #define PUTC(c,f) putc(c,f)
42 #define FC_DBG_CACHE_REF 1024
45 FcCacheReadString (FILE *f, FcChar8 *dest, int len)
53 while ((c = GETC (f)) != EOF)
65 while ((c = GETC (f)) != EOF)
80 FcChar8 *new = malloc (size * 2); /* freed in caller */
83 memcpy (new, d, size);
100 FcCacheReadUlong (FILE *f, unsigned long *dest)
105 while ((c = GETC (f)) != EOF)
115 if (c == EOF || isspace (c))
119 t = t * 10 + (c - '0');
127 FcCacheReadInt (FILE *f, int *dest)
132 ret = FcCacheReadUlong (f, &t);
139 FcCacheReadTime (FILE *f, time_t *dest)
144 ret = FcCacheReadUlong (f, &t);
151 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
154 while ((c = *chars++))
159 if (PUTC ('\\', f) == EOF)
163 if (PUTC (c, f) == EOF)
171 FcCacheWriteString (FILE *f, const FcChar8 *string)
174 if (PUTC ('"', f) == EOF)
176 if (!FcCacheWriteChars (f, string))
178 if (PUTC ('"', f) == EOF)
184 FcCacheWritePath (FILE *f, const FcChar8 *dir, const FcChar8 *file)
186 if (PUTC ('"', f) == EOF)
189 if (!FcCacheWriteChars (f, dir))
191 if (dir && dir[strlen((const char *) dir) - 1] != '/')
192 if (PUTC ('/', f) == EOF)
194 if (!FcCacheWriteChars (f, file))
196 if (PUTC ('"', f) == EOF)
202 FcCacheWriteUlong (FILE *f, unsigned long t)
205 unsigned long temp, digit;
218 if (PUTC ((char) digit + '0', f) == EOF)
220 temp = temp - pow * digit;
227 FcCacheWriteInt (FILE *f, int i)
229 return FcCacheWriteUlong (f, (unsigned long) i);
233 FcCacheWriteTime (FILE *f, time_t t)
235 return FcCacheWriteUlong (f, (unsigned long) t);
239 FcCacheFontSetAdd (FcFontSet *set,
246 FcChar8 path_buf[8192], *path;
248 FcBool ret = FcFalse;
253 len = (dir_len + 1 + strlen ((const char *) file) + 1);
254 if (len > sizeof (path_buf))
256 path = malloc (len); /* freed down below */
260 strncpy ((char *) path, (const char *) dir, dir_len);
261 if (dir[dir_len - 1] != '/')
262 path[dir_len++] = '/';
263 strcpy ((char *) path + dir_len, (const char *) file);
264 if (!FcStrCmp (name, FC_FONT_FILE_DIR))
266 if (FcDebug () & FC_DBG_CACHEV)
267 printf (" dir cache dir \"%s\"\n", path);
268 ret = FcStrSetAdd (dirs, path);
270 else if (!FcStrCmp (name, FC_FONT_FILE_INVALID))
276 font = FcNameParse (name);
279 if (FcDebug () & FC_DBG_CACHEV)
280 printf (" dir cache file \"%s\"\n", file);
281 ret = FcPatternAddString (font, FC_FILE, path);
284 frozen = FcPatternFreeze (font);
287 ret = FcFontSetAdd (set, frozen);
289 FcPatternDestroy (font);
292 if (path != path_buf) free (path);
298 FcCacheHash (const FcChar8 *string, int len)
303 while (len-- && (c = *string++))
309 * Verify the saved timestamp for a file
312 FcGlobalCacheCheckTime (FcGlobalCacheInfo *info)
316 if (stat ((char *) info->file, &statb) < 0)
318 if (FcDebug () & FC_DBG_CACHE)
319 printf (" file missing\n");
322 if (statb.st_mtime != info->time)
324 if (FcDebug () & FC_DBG_CACHE)
325 printf (" timestamp mismatch (was %d is %d)\n",
326 (int) info->time, (int) statb.st_mtime);
333 FcGlobalCacheReferenced (FcGlobalCache *cache,
334 FcGlobalCacheInfo *info)
336 if (!info->referenced)
338 info->referenced = FcTrue;
340 if (FcDebug () & FC_DBG_CACHE_REF)
341 printf ("Reference %d %s\n", cache->referenced, info->file);
346 * Break a path into dir/base elements and compute the base hash
347 * and the dir length. This is shared between the functions
348 * which walk the file caches
351 typedef struct _FcFilePathInfo {
355 unsigned int base_hash;
358 static FcFilePathInfo
359 FcFilePathInfoGet (const FcChar8 *path)
364 slash = (FcChar8 *) strrchr ((const char *) path, '/');
368 i.dir_len = slash - path;
375 i.dir = (const FcChar8 *) ".";
379 i.base_hash = FcCacheHash (i.base, -1);
384 FcGlobalCacheDirGet (FcGlobalCache *cache,
387 FcBool create_missing)
389 unsigned int hash = FcCacheHash (dir, len);
390 FcGlobalCacheDir *d, **prev;
392 for (prev = &cache->ents[hash % FC_GLOBAL_CACHE_DIR_HASH_SIZE];
394 prev = &(*prev)->next)
396 if (d->info.hash == hash && d->len == len &&
397 !strncmp ((const char *) d->info.file,
398 (const char *) dir, len))
406 d = malloc (sizeof (FcGlobalCacheDir) + len + 1);
409 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCacheDir) + len + 1);
413 d->info.file = (FcChar8 *) (d + 1);
414 strncpy ((char *) d->info.file, (const char *) dir, len);
415 d->info.file[len] = '\0';
417 d->info.referenced = FcFalse;
419 for (i = 0; i < FC_GLOBAL_CACHE_FILE_HASH_SIZE; i++)
426 static FcGlobalCacheInfo *
427 FcGlobalCacheDirAdd (FcGlobalCache *cache,
431 FcBool create_missing)
435 FcGlobalCacheSubdir *subdir;
436 FcGlobalCacheDir *parent;
438 i = FcFilePathInfoGet (dir);
439 parent = FcGlobalCacheDirGet (cache, i.dir, i.dir_len, create_missing);
441 * Tricky here -- directories containing fonts.cache-1 files
442 * need entries only when the parent doesn't have a cache file.
443 * That is, when the parent already exists in the cache, is
444 * referenced and has a "real" timestamp. The time of 0 is
445 * special and marks directories which got stuck in the
446 * global cache for this very reason. Yes, it could
447 * use a separate boolean field, and probably should.
449 if (!parent || (!create_missing &&
450 (!parent->info.referenced ||
451 (parent->info.time == 0))))
454 * Add this directory to the cache
456 d = FcGlobalCacheDirGet (cache, dir, strlen ((const char *) dir), FcTrue);
461 * Add this directory to the subdirectory list of the parent
463 subdir = malloc (sizeof (FcGlobalCacheSubdir));
466 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir));
468 subdir->next = parent->subdirs;
469 parent->subdirs = subdir;
474 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
476 FcGlobalCacheFile *f, *next;
478 FcGlobalCacheSubdir *s, *nexts;
480 for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
481 for (f = d->ents[h]; f; f = next)
484 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
485 strlen ((char *) f->info.file) + 1 +
486 strlen ((char *) f->name) + 1);
489 for (s = d->subdirs; s; s = nexts)
492 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir));
495 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir) + d->len + 1);
500 * If the parent is in the global cache and referenced, add
501 * an entry for 'dir' to the global cache. This is used
502 * for directories with fonts.cache files
506 FcGlobalCacheReferenceSubdir (FcGlobalCache *cache,
509 FcGlobalCacheInfo *info;
510 info = FcGlobalCacheDirAdd (cache, dir, 0, FcFalse, FcFalse);
511 if (info && !info->referenced)
513 info->referenced = FcTrue;
519 * Check to see if the global cache contains valid data for 'dir'.
520 * If so, scan the global cache for files and directories in 'dir'.
521 * else, return False.
524 FcGlobalCacheScanDir (FcFontSet *set,
526 FcGlobalCache *cache,
529 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, dir,
530 strlen ((const char *) dir),
532 FcGlobalCacheFile *f;
535 FcGlobalCacheSubdir *subdir;
536 FcBool any_in_cache = FcFalse;
538 if (FcDebug() & FC_DBG_CACHE)
539 printf ("FcGlobalCacheScanDir %s\n", dir);
543 if (FcDebug () & FC_DBG_CACHE)
544 printf ("\tNo dir cache entry\n");
549 * See if the timestamp recorded in the global cache
550 * matches the directory time, if not, return False
552 if (!FcGlobalCacheCheckTime (&d->info))
554 if (FcDebug () & FC_DBG_CACHE)
555 printf ("\tdir cache entry time mismatch\n");
560 * Add files from 'dir' to the fontset
562 dir_len = strlen ((const char *) dir);
563 for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
564 for (f = d->ents[h]; f; f = f->next)
566 if (FcDebug() & FC_DBG_CACHEV)
567 printf ("FcGlobalCacheScanDir add file %s\n", f->info.file);
568 any_in_cache = FcTrue;
569 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
570 f->info.file, f->name))
572 cache->broken = FcTrue;
575 FcGlobalCacheReferenced (cache, &f->info);
578 * Add directories in 'dir' to 'dirs'
580 for (subdir = d->subdirs; subdir; subdir = subdir->next)
582 FcFilePathInfo info = FcFilePathInfoGet (subdir->ent->info.file);
584 any_in_cache = FcTrue;
585 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
586 info.base, FC_FONT_FILE_DIR))
588 cache->broken = FcTrue;
591 FcGlobalCacheReferenced (cache, &subdir->ent->info);
594 FcGlobalCacheReferenced (cache, &d->info);
597 * To recover from a bug in previous versions of fontconfig,
598 * return FcFalse if no entries in the cache were found
599 * for this directory. This will cause any empty directories
600 * to get rescanned every time fontconfig is initialized. This
601 * might get removed at some point when the older cache files are
608 * Locate the cache entry for a particular file
611 FcGlobalCacheFileGet (FcGlobalCache *cache,
616 FcFilePathInfo i = FcFilePathInfoGet (file);
617 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
619 FcGlobalCacheFile *f, *match = 0;
624 for (f = d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE]; f; f = f->next)
626 if (f->info.hash == i.base_hash &&
627 !strcmp ((const char *) f->info.file, (const char *) i.base))
641 * Add a file entry to the cache
643 static FcGlobalCacheInfo *
644 FcGlobalCacheFileAdd (FcGlobalCache *cache,
651 FcFilePathInfo i = FcFilePathInfoGet (path);
652 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
654 FcGlobalCacheFile *f, **prev;
659 for (prev = &d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE];
661 prev = &(*prev)->next)
663 if (f->info.hash == i.base_hash &&
665 !strcmp ((const char *) f->info.file, (const char *) i.base))
676 if (f->info.referenced)
679 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
680 strlen ((char *) f->info.file) + 1 +
681 strlen ((char *) f->name) + 1);
684 size = (sizeof (FcGlobalCacheFile) +
685 strlen ((char *) i.base) + 1 +
686 strlen ((char *) name) + 1);
690 FcMemAlloc (FC_MEM_CACHE, size);
693 f->info.hash = i.base_hash;
694 f->info.file = (FcChar8 *) (f + 1);
696 f->info.referenced = FcFalse;
698 f->name = f->info.file + strlen ((char *) i.base) + 1;
699 strcpy ((char *) f->info.file, (const char *) i.base);
700 strcpy ((char *) f->name, (const char *) name);
705 FcGlobalCacheCreate (void)
707 FcGlobalCache *cache;
710 cache = malloc (sizeof (FcGlobalCache));
713 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
714 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
717 cache->referenced = 0;
718 cache->updated = FcFalse;
719 cache->broken = FcFalse;
724 FcGlobalCacheDestroy (FcGlobalCache *cache)
726 FcGlobalCacheDir *d, *next;
729 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
731 for (d = cache->ents[h]; d; d = next)
734 FcGlobalCacheDirDestroy (d);
737 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
742 * Cache file syntax is quite simple:
744 * "file_name" id time "font_name" \n
748 FcGlobalCacheLoad (FcGlobalCache *cache,
749 const FcChar8 *cache_file)
752 FcChar8 file_buf[8192], *file;
755 FcChar8 name_buf[8192], *name;
756 FcGlobalCacheInfo *info;
758 f = fopen ((char *) cache_file, "r");
762 cache->updated = FcFalse;
765 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
766 FcCacheReadInt (f, &id) &&
767 FcCacheReadTime (f, &time) &&
768 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
770 if (FcDebug () & FC_DBG_CACHEV)
771 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file, name);
772 if (!FcStrCmp (name, FC_FONT_FILE_DIR))
773 info = FcGlobalCacheDirAdd (cache, file, time, FcFalse, FcTrue);
775 info = FcGlobalCacheFileAdd (cache, file, id, time, name, FcFalse);
777 cache->broken = FcTrue;
780 if (FcDebug () & FC_DBG_CACHE_REF)
781 printf ("FcGlobalCacheLoad entry %d %s\n",
782 cache->entries, file);
783 if (file != file_buf)
785 if (name != name_buf)
790 if (file && file != file_buf)
792 if (name && name != name_buf)
798 FcGlobalCacheUpdate (FcGlobalCache *cache,
803 const FcChar8 *match;
805 FcGlobalCacheInfo *info;
809 if (stat ((char *) file, &statb) < 0)
811 if (S_ISDIR (statb.st_mode))
812 info = FcGlobalCacheDirAdd (cache, file, statb.st_mtime,
815 info = FcGlobalCacheFileAdd (cache, file, id, statb.st_mtime,
819 FcGlobalCacheReferenced (cache, info);
820 cache->updated = FcTrue;
823 cache->broken = FcTrue;
828 FcGlobalCacheSave (FcGlobalCache *cache,
829 const FcChar8 *cache_file)
832 int dir_hash, file_hash;
833 FcGlobalCacheDir *dir;
834 FcGlobalCacheFile *file;
837 if (!cache->updated && cache->referenced == cache->entries)
843 /* Set-UID programs can't safely update the cache */
844 if (getuid () != geteuid ())
847 atomic = FcAtomicCreate (cache_file);
850 if (!FcAtomicLock (atomic))
852 f = fopen ((char *) FcAtomicNewFile(atomic), "w");
856 for (dir_hash = 0; dir_hash < FC_GLOBAL_CACHE_DIR_HASH_SIZE; dir_hash++)
858 for (dir = cache->ents[dir_hash]; dir; dir = dir->next)
860 if (!dir->info.referenced)
862 if (!FcCacheWriteString (f, dir->info.file))
864 if (PUTC (' ', f) == EOF)
866 if (!FcCacheWriteInt (f, 0))
868 if (PUTC (' ', f) == EOF)
870 if (!FcCacheWriteTime (f, dir->info.time))
872 if (PUTC (' ', f) == EOF)
874 if (!FcCacheWriteString (f, (FcChar8 *) FC_FONT_FILE_DIR))
876 if (PUTC ('\n', f) == EOF)
879 for (file_hash = 0; file_hash < FC_GLOBAL_CACHE_FILE_HASH_SIZE; file_hash++)
881 for (file = dir->ents[file_hash]; file; file = file->next)
883 if (!file->info.referenced)
885 if (!FcCacheWritePath (f, dir->info.file, file->info.file))
887 if (PUTC (' ', f) == EOF)
889 if (!FcCacheWriteInt (f, file->id < 0 ? 0 : file->id))
891 if (PUTC (' ', f) == EOF)
893 if (!FcCacheWriteTime (f, file->info.time))
895 if (PUTC (' ', f) == EOF)
897 if (!FcCacheWriteString (f, file->name))
899 if (PUTC ('\n', f) == EOF)
906 if (fclose (f) == EOF)
909 if (!FcAtomicReplaceOrig (atomic))
912 FcAtomicUnlock (atomic);
913 FcAtomicDestroy (atomic);
915 cache->updated = FcFalse;
921 FcAtomicDeleteNew (atomic);
923 FcAtomicUnlock (atomic);
925 FcAtomicDestroy (atomic);
931 FcDirCacheValid (const FcChar8 *dir)
933 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
934 struct stat file_stat, dir_stat;
936 if (stat ((char *) dir, &dir_stat) < 0)
938 FcStrFree (cache_file);
941 if (stat ((char *) cache_file, &file_stat) < 0)
943 FcStrFree (cache_file);
946 FcStrFree (cache_file);
948 * If the directory has been modified more recently than
949 * the cache file, the cache is not valid
951 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
957 FcDirCacheReadDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
959 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
964 FcChar8 file_buf[8192], *file;
965 FcChar8 name_buf[8192], *name;
966 FcBool ret = FcFalse;
971 if (FcDebug () & FC_DBG_CACHE)
972 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file);
974 f = fopen ((char *) cache_file, "r");
977 if (FcDebug () & FC_DBG_CACHE)
978 printf (" no cache file\n");
982 if (!FcDirCacheValid (dir))
984 if (FcDebug () & FC_DBG_CACHE)
985 printf (" cache file older than directory\n");
989 base = (FcChar8 *) strrchr ((char *) cache_file, '/');
993 dir_len = base - cache_file;
997 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
998 FcCacheReadInt (f, &id) &&
999 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
1001 if (!FcCacheFontSetAdd (set, dirs, cache_file, dir_len,
1004 if (file != file_buf)
1006 if (name != name_buf)
1010 if (FcDebug () & FC_DBG_CACHE)
1011 printf (" cache loaded\n");
1015 if (file && file != file_buf)
1017 if (name && name != name_buf)
1022 FcStrFree (cache_file);
1028 * return the path from the directory containing 'cache' to 'file'
1031 static const FcChar8 *
1032 FcFileBaseName (const FcChar8 *cache, const FcChar8 *file)
1034 const FcChar8 *cache_slash;
1036 cache_slash = (const FcChar8 *) strrchr ((const char *) cache, '/');
1037 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
1038 (cache_slash + 1) - cache))
1039 return file + ((cache_slash + 1) - cache);
1044 FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1046 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1050 const FcChar8 *file, *base;
1058 if (FcDebug () & FC_DBG_CACHE)
1059 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1061 f = fopen ((char *) cache_file, "w");
1064 if (FcDebug () & FC_DBG_CACHE)
1065 printf (" can't create \"%s\"\n", cache_file);
1069 list = FcStrListCreate (dirs);
1073 while ((dir = FcStrListNext (list)))
1075 base = FcFileBaseName (cache_file, dir);
1076 if (!FcCacheWriteString (f, base))
1078 if (PUTC (' ', f) == EOF)
1080 if (!FcCacheWriteInt (f, 0))
1082 if (PUTC (' ', f) == EOF)
1084 if (!FcCacheWriteString (f, FC_FONT_FILE_DIR))
1086 if (PUTC ('\n', f) == EOF)
1090 for (n = 0; n < set->nfont; n++)
1092 font = set->fonts[n];
1093 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
1095 base = FcFileBaseName (cache_file, file);
1096 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
1098 if (FcDebug () & FC_DBG_CACHEV)
1099 printf (" write file \"%s\"\n", base);
1100 if (!FcCacheWriteString (f, base))
1102 if (PUTC (' ', f) == EOF)
1104 if (!FcCacheWriteInt (f, id))
1106 if (PUTC (' ', f) == EOF)
1108 name = FcNameUnparse (font);
1111 ret = FcCacheWriteString (f, name);
1115 if (PUTC ('\n', f) == EOF)
1119 FcStrListDone (list);
1121 if (fclose (f) == EOF)
1124 FcStrFree (cache_file);
1126 if (FcDebug () & FC_DBG_CACHE)
1127 printf (" cache written\n");
1131 FcStrListDone (list);
1135 unlink ((char *) cache_file);
1136 FcStrFree (cache_file);