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,
434 FcGlobalCacheSubdir *subdir;
435 FcGlobalCacheDir *parent;
438 * Add this directory to the cache
440 d = FcGlobalCacheDirGet (cache, dir, strlen ((const char *) dir), FcTrue);
444 i = FcFilePathInfoGet (dir);
446 * Add this directory to the subdirectory list of the parent
448 parent = FcGlobalCacheDirGet (cache, i.dir, i.dir_len, FcTrue);
451 subdir = malloc (sizeof (FcGlobalCacheSubdir));
454 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir));
456 subdir->next = parent->subdirs;
457 parent->subdirs = subdir;
462 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
464 FcGlobalCacheFile *f, *next;
466 FcGlobalCacheSubdir *s, *nexts;
468 for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
469 for (f = d->ents[h]; f; f = next)
472 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
473 strlen ((char *) f->info.file) + 1 +
474 strlen ((char *) f->name) + 1);
477 for (s = d->subdirs; s; s = nexts)
480 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir));
483 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir) + d->len + 1);
488 * Check to see if the global cache contains valid data for 'dir'.
489 * If so, scan the global cache for files and directories in 'dir'.
490 * else, return False.
493 FcGlobalCacheScanDir (FcFontSet *set,
495 FcGlobalCache *cache,
498 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, dir,
499 strlen ((const char *) dir),
501 FcGlobalCacheFile *f;
504 FcGlobalCacheSubdir *subdir;
505 FcBool any_in_cache = FcFalse;
507 if (FcDebug() & FC_DBG_CACHE)
508 printf ("FcGlobalCacheScanDir %s\n", dir);
512 if (FcDebug () & FC_DBG_CACHE)
513 printf ("\tNo dir cache entry\n");
518 * See if the timestamp recorded in the global cache
519 * matches the directory time, if not, return False
521 if (!FcGlobalCacheCheckTime (&d->info))
523 if (FcDebug () & FC_DBG_CACHE)
524 printf ("\tdir cache entry time mismatch\n");
529 * Add files from 'dir' to the fontset
531 dir_len = strlen ((const char *) dir);
532 for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
533 for (f = d->ents[h]; f; f = f->next)
535 if (FcDebug() & FC_DBG_CACHEV)
536 printf ("FcGlobalCacheScanDir add file %s\n", f->info.file);
537 any_in_cache = FcTrue;
538 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
539 f->info.file, f->name))
541 cache->broken = FcTrue;
544 FcGlobalCacheReferenced (cache, &f->info);
547 * Add directories in 'dir' to 'dirs'
549 for (subdir = d->subdirs; subdir; subdir = subdir->next)
551 FcFilePathInfo info = FcFilePathInfoGet (subdir->ent->info.file);
553 any_in_cache = FcTrue;
554 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
555 info.base, FC_FONT_FILE_DIR))
557 cache->broken = FcTrue;
560 FcGlobalCacheReferenced (cache, &subdir->ent->info);
563 FcGlobalCacheReferenced (cache, &d->info);
566 * To recover from a bug in previous versions of fontconfig,
567 * return FcFalse if no entries in the cache were found
568 * for this directory. This will cause any empty directories
569 * to get rescanned every time fontconfig is initialized. This
570 * might get removed at some point when the older cache files are
577 * Locate the cache entry for a particular file
580 FcGlobalCacheFileGet (FcGlobalCache *cache,
585 FcFilePathInfo i = FcFilePathInfoGet (file);
586 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
588 FcGlobalCacheFile *f, *match = 0;
593 for (f = d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE]; f; f = f->next)
595 if (f->info.hash == i.base_hash &&
596 !strcmp ((const char *) f->info.file, (const char *) i.base))
610 * Add a file entry to the cache
612 static FcGlobalCacheInfo *
613 FcGlobalCacheFileAdd (FcGlobalCache *cache,
620 FcFilePathInfo i = FcFilePathInfoGet (path);
621 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
623 FcGlobalCacheFile *f, **prev;
628 for (prev = &d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE];
630 prev = &(*prev)->next)
632 if (f->info.hash == i.base_hash &&
634 !strcmp ((const char *) f->info.file, (const char *) i.base))
645 if (f->info.referenced)
648 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
649 strlen ((char *) f->info.file) + 1 +
650 strlen ((char *) f->name) + 1);
653 size = (sizeof (FcGlobalCacheFile) +
654 strlen ((char *) i.base) + 1 +
655 strlen ((char *) name) + 1);
659 FcMemAlloc (FC_MEM_CACHE, size);
662 f->info.hash = i.base_hash;
663 f->info.file = (FcChar8 *) (f + 1);
665 f->info.referenced = FcFalse;
667 f->name = f->info.file + strlen ((char *) i.base) + 1;
668 strcpy ((char *) f->info.file, (const char *) i.base);
669 strcpy ((char *) f->name, (const char *) name);
674 FcGlobalCacheCreate (void)
676 FcGlobalCache *cache;
679 cache = malloc (sizeof (FcGlobalCache));
682 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
683 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
686 cache->referenced = 0;
687 cache->updated = FcFalse;
688 cache->broken = FcFalse;
693 FcGlobalCacheDestroy (FcGlobalCache *cache)
695 FcGlobalCacheDir *d, *next;
698 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
700 for (d = cache->ents[h]; d; d = next)
703 FcGlobalCacheDirDestroy (d);
706 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
711 * Cache file syntax is quite simple:
713 * "file_name" id time "font_name" \n
717 FcGlobalCacheLoad (FcGlobalCache *cache,
718 const FcChar8 *cache_file)
721 FcChar8 file_buf[8192], *file;
724 FcChar8 name_buf[8192], *name;
725 FcGlobalCacheInfo *info;
727 f = fopen ((char *) cache_file, "r");
731 cache->updated = FcFalse;
734 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
735 FcCacheReadInt (f, &id) &&
736 FcCacheReadTime (f, &time) &&
737 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
739 if (FcDebug () & FC_DBG_CACHEV)
740 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file, name);
741 if (!FcStrCmp (name, FC_FONT_FILE_DIR))
742 info = FcGlobalCacheDirAdd (cache, file, time, FcFalse);
744 info = FcGlobalCacheFileAdd (cache, file, id, time, name, FcFalse);
746 cache->broken = FcTrue;
749 if (FcDebug () & FC_DBG_CACHE_REF)
750 printf ("FcGlobalCacheLoad entry %d %s\n",
751 cache->entries, file);
752 if (file != file_buf)
754 if (name != name_buf)
759 if (file && file != file_buf)
761 if (name && name != name_buf)
767 FcGlobalCacheUpdate (FcGlobalCache *cache,
772 const FcChar8 *match;
774 FcGlobalCacheInfo *info;
778 if (stat ((char *) file, &statb) < 0)
780 if (S_ISDIR (statb.st_mode))
781 info = FcGlobalCacheDirAdd (cache, file, statb.st_mtime,
784 info = FcGlobalCacheFileAdd (cache, file, id, statb.st_mtime,
788 FcGlobalCacheReferenced (cache, info);
789 cache->updated = FcTrue;
792 cache->broken = FcTrue;
797 FcGlobalCacheSave (FcGlobalCache *cache,
798 const FcChar8 *cache_file)
801 int dir_hash, file_hash;
802 FcGlobalCacheDir *dir;
803 FcGlobalCacheFile *file;
806 if (!cache->updated && cache->referenced == cache->entries)
812 /* Set-UID programs can't safely update the cache */
813 if (getuid () != geteuid ())
816 atomic = FcAtomicCreate (cache_file);
819 if (!FcAtomicLock (atomic))
821 f = fopen ((char *) FcAtomicNewFile(atomic), "w");
825 for (dir_hash = 0; dir_hash < FC_GLOBAL_CACHE_DIR_HASH_SIZE; dir_hash++)
827 for (dir = cache->ents[dir_hash]; dir; dir = dir->next)
829 if (!dir->info.referenced)
831 if (!FcCacheWriteString (f, dir->info.file))
833 if (PUTC (' ', f) == EOF)
835 if (!FcCacheWriteInt (f, 0))
837 if (PUTC (' ', f) == EOF)
839 if (!FcCacheWriteTime (f, dir->info.time))
841 if (PUTC (' ', f) == EOF)
843 if (!FcCacheWriteString (f, (FcChar8 *) FC_FONT_FILE_DIR))
845 if (PUTC ('\n', f) == EOF)
848 for (file_hash = 0; file_hash < FC_GLOBAL_CACHE_FILE_HASH_SIZE; file_hash++)
850 for (file = dir->ents[file_hash]; file; file = file->next)
852 if (!file->info.referenced)
854 if (!FcCacheWritePath (f, dir->info.file, file->info.file))
856 if (PUTC (' ', f) == EOF)
858 if (!FcCacheWriteInt (f, file->id < 0 ? 0 : file->id))
860 if (PUTC (' ', f) == EOF)
862 if (!FcCacheWriteTime (f, file->info.time))
864 if (PUTC (' ', f) == EOF)
866 if (!FcCacheWriteString (f, file->name))
868 if (PUTC ('\n', f) == EOF)
875 if (fclose (f) == EOF)
878 if (!FcAtomicReplaceOrig (atomic))
881 FcAtomicUnlock (atomic);
882 FcAtomicDestroy (atomic);
884 cache->updated = FcFalse;
890 FcAtomicDeleteNew (atomic);
892 FcAtomicUnlock (atomic);
894 FcAtomicDestroy (atomic);
900 FcDirCacheValid (const FcChar8 *dir)
902 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
903 struct stat file_stat, dir_stat;
905 if (stat ((char *) dir, &dir_stat) < 0)
907 FcStrFree (cache_file);
910 if (stat ((char *) cache_file, &file_stat) < 0)
912 FcStrFree (cache_file);
915 FcStrFree (cache_file);
917 * If the directory has been modified more recently than
918 * the cache file, the cache is not valid
920 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
926 FcDirCacheReadDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
928 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
933 FcChar8 file_buf[8192], *file;
934 FcChar8 name_buf[8192], *name;
935 FcBool ret = FcFalse;
940 if (FcDebug () & FC_DBG_CACHE)
941 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file);
943 f = fopen ((char *) cache_file, "r");
946 if (FcDebug () & FC_DBG_CACHE)
947 printf (" no cache file\n");
951 if (!FcDirCacheValid (dir))
953 if (FcDebug () & FC_DBG_CACHE)
954 printf (" cache file older than directory\n");
958 base = (FcChar8 *) strrchr ((char *) cache_file, '/');
962 dir_len = base - cache_file;
966 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
967 FcCacheReadInt (f, &id) &&
968 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
970 if (!FcCacheFontSetAdd (set, dirs, cache_file, dir_len,
973 if (file != file_buf)
975 if (name != name_buf)
979 if (FcDebug () & FC_DBG_CACHE)
980 printf (" cache loaded\n");
984 if (file && file != file_buf)
986 if (name && name != name_buf)
991 FcStrFree (cache_file);
997 * return the path from the directory containing 'cache' to 'file'
1000 static const FcChar8 *
1001 FcFileBaseName (const FcChar8 *cache, const FcChar8 *file)
1003 const FcChar8 *cache_slash;
1005 cache_slash = (const FcChar8 *) strrchr ((const char *) cache, '/');
1006 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
1007 (cache_slash + 1) - cache))
1008 return file + ((cache_slash + 1) - cache);
1013 FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1015 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1019 const FcChar8 *file, *base;
1027 if (FcDebug () & FC_DBG_CACHE)
1028 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1030 f = fopen ((char *) cache_file, "w");
1033 if (FcDebug () & FC_DBG_CACHE)
1034 printf (" can't create \"%s\"\n", cache_file);
1038 list = FcStrListCreate (dirs);
1042 while ((dir = FcStrListNext (list)))
1044 base = FcFileBaseName (cache_file, dir);
1045 if (!FcCacheWriteString (f, base))
1047 if (PUTC (' ', f) == EOF)
1049 if (!FcCacheWriteInt (f, 0))
1051 if (PUTC (' ', f) == EOF)
1053 if (!FcCacheWriteString (f, FC_FONT_FILE_DIR))
1055 if (PUTC ('\n', f) == EOF)
1059 for (n = 0; n < set->nfont; n++)
1061 font = set->fonts[n];
1062 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
1064 base = FcFileBaseName (cache_file, file);
1065 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
1067 if (FcDebug () & FC_DBG_CACHEV)
1068 printf (" write file \"%s\"\n", base);
1069 if (!FcCacheWriteString (f, base))
1071 if (PUTC (' ', f) == EOF)
1073 if (!FcCacheWriteInt (f, id))
1075 if (PUTC (' ', f) == EOF)
1077 name = FcNameUnparse (font);
1080 ret = FcCacheWriteString (f, name);
1084 if (PUTC ('\n', f) == EOF)
1088 FcStrListDone (list);
1090 if (fclose (f) == EOF)
1093 FcStrFree (cache_file);
1095 if (FcDebug () & FC_DBG_CACHE)
1096 printf (" cache written\n");
1100 FcStrListDone (list);
1104 unlink ((char *) cache_file);
1105 FcStrFree (cache_file);