2 * $XFree86: xc/lib/fontconfig/src/fccache.c,v 1.11 2002/08/19 19:32:05 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);
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))
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)
303 while ((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);
384 FcGlobalCacheDirGet (FcGlobalCache *cache,
387 FcBool create_missing)
389 unsigned int hash = FcCacheHash (dir);
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);
412 d->info.file = (FcChar8 *) (d + 1);
413 strncpy ((char *) d->info.file, (const char *) dir, len);
414 d->info.file[len] = '\0';
416 d->info.referenced = FcFalse;
418 for (i = 0; i < FC_GLOBAL_CACHE_FILE_HASH_SIZE; i++)
425 static FcGlobalCacheInfo *
426 FcGlobalCacheDirAdd (FcGlobalCache *cache,
433 FcGlobalCacheSubdir *subdir;
434 FcGlobalCacheDir *parent;
437 * Add this directory to the cache
439 d = FcGlobalCacheDirGet (cache, dir, strlen ((const char *) dir), FcTrue);
443 i = FcFilePathInfoGet (dir);
445 * Add this directory to the subdirectory list of the parent
447 parent = FcGlobalCacheDirGet (cache, i.dir, i.dir_len, FcTrue);
450 subdir = malloc (sizeof (FcGlobalCacheSubdir) +
451 strlen ((const char *) i.base) + 1);
454 subdir->file = (FcChar8 *) (subdir + 1);
455 strcpy ((char *) subdir->file, (const char *) i.base);
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)
474 for (s = d->subdirs; s; s = nexts)
483 FcGlobalCacheScanDir (FcFontSet *set,
485 FcGlobalCache *cache,
488 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, dir,
489 strlen ((const char *) dir),
491 FcGlobalCacheFile *f;
494 FcGlobalCacheSubdir *subdir;
496 if (FcDebug() & FC_DBG_CACHE)
497 printf ("FcGlobalCacheScanDir %s\n", dir);
501 if (FcDebug () & FC_DBG_CACHE)
502 printf ("\tNo dir cache entry\n");
506 if (!FcGlobalCacheCheckTime (&d->info))
508 if (FcDebug () & FC_DBG_CACHE)
509 printf ("\tdir cache entry time mismatch\n");
513 dir_len = strlen ((const char *) dir);
514 for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
515 for (f = d->ents[h]; f; f = f->next)
517 if (FcDebug() & FC_DBG_CACHEV)
518 printf ("FcGlobalCacheScanDir add file %s\n", f->info.file);
519 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
520 f->info.file, f->name))
522 cache->broken = FcTrue;
525 FcGlobalCacheReferenced (cache, &f->info);
527 for (subdir = d->subdirs; subdir; subdir = subdir->next)
529 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
530 subdir->file, FC_FONT_FILE_DIR))
532 cache->broken = FcTrue;
537 FcGlobalCacheReferenced (cache, &d->info);
543 * Locate the cache entry for a particular file
546 FcGlobalCacheFileGet (FcGlobalCache *cache,
551 FcFilePathInfo i = FcFilePathInfoGet (file);
552 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
554 FcGlobalCacheFile *f, *match = 0;
559 for (f = d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE]; f; f = f->next)
561 if (f->info.hash == i.base_hash &&
562 !strcmp ((const char *) f->info.file, (const char *) i.base))
576 * Add a file entry to the cache
578 static FcGlobalCacheInfo *
579 FcGlobalCacheFileAdd (FcGlobalCache *cache,
586 FcFilePathInfo i = FcFilePathInfoGet (path);
587 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
589 FcGlobalCacheFile *f, **prev;
593 for (prev = &d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE];
595 prev = &(*prev)->next)
597 if (f->info.hash == i.base_hash &&
599 !strcmp ((const char *) f->info.file, (const char *) i.base))
610 if (f->info.referenced)
615 f = malloc (sizeof (FcGlobalCacheFile) +
616 strlen ((char *) i.base) + 1 +
617 strlen ((char *) name) + 1);
622 f->info.hash = i.base_hash;
623 f->info.file = (FcChar8 *) (f + 1);
625 f->info.referenced = FcFalse;
627 f->name = f->info.file + strlen ((char *) i.base) + 1;
628 strcpy ((char *) f->info.file, (const char *) i.base);
629 strcpy ((char *) f->name, (const char *) name);
634 FcGlobalCacheCreate (void)
636 FcGlobalCache *cache;
639 cache = malloc (sizeof (FcGlobalCache));
642 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
645 cache->referenced = 0;
646 cache->updated = FcFalse;
647 cache->broken = FcFalse;
652 FcGlobalCacheDestroy (FcGlobalCache *cache)
654 FcGlobalCacheDir *d, *next;
657 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
659 for (d = cache->ents[h]; d; d = next)
662 FcGlobalCacheDirDestroy (d);
669 * Cache file syntax is quite simple:
671 * "file_name" id time "font_name" \n
675 FcGlobalCacheLoad (FcGlobalCache *cache,
676 const FcChar8 *cache_file)
679 FcChar8 file_buf[8192], *file;
682 FcChar8 name_buf[8192], *name;
683 FcGlobalCacheInfo *info;
685 f = fopen ((char *) cache_file, "r");
689 cache->updated = FcFalse;
692 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
693 FcCacheReadInt (f, &id) &&
694 FcCacheReadTime (f, &time) &&
695 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
697 if (FcDebug () & FC_DBG_CACHEV)
698 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file, name);
699 if (!FcStrCmp (name, FC_FONT_FILE_DIR))
700 info = FcGlobalCacheDirAdd (cache, file, time, FcFalse);
702 info = FcGlobalCacheFileAdd (cache, file, id, time, name, FcFalse);
704 cache->broken = FcTrue;
707 if (FcDebug () & FC_DBG_CACHE_REF)
708 printf ("FcGlobalCacheLoad entry %d %s\n",
709 cache->entries, file);
710 if (file != file_buf)
712 if (name != name_buf)
717 if (file && file != file_buf)
719 if (name && name != name_buf)
725 FcGlobalCacheUpdate (FcGlobalCache *cache,
730 const FcChar8 *match;
732 FcGlobalCacheInfo *info;
736 if (stat ((char *) file, &statb) < 0)
738 if (S_ISDIR (statb.st_mode))
739 info = FcGlobalCacheDirAdd (cache, file, statb.st_mtime,
742 info = FcGlobalCacheFileAdd (cache, file, id, statb.st_mtime,
746 FcGlobalCacheReferenced (cache, info);
747 cache->updated = FcTrue;
750 cache->broken = FcTrue;
755 FcGlobalCacheSave (FcGlobalCache *cache,
756 const FcChar8 *cache_file)
759 int dir_hash, file_hash;
760 FcGlobalCacheDir *dir;
761 FcGlobalCacheFile *file;
764 if (!cache->updated && cache->referenced == cache->entries)
770 /* Set-UID programs can't safely update the cache */
771 if (getuid () != geteuid ())
774 atomic = FcAtomicCreate (cache_file);
777 if (!FcAtomicLock (atomic))
779 f = fopen ((char *) FcAtomicNewFile(atomic), "w");
783 for (dir_hash = 0; dir_hash < FC_GLOBAL_CACHE_DIR_HASH_SIZE; dir_hash++)
785 for (dir = cache->ents[dir_hash]; dir; dir = dir->next)
787 if (!dir->info.referenced)
789 if (!FcCacheWriteString (f, dir->info.file))
791 if (PUTC (' ', f) == EOF)
793 if (!FcCacheWriteInt (f, 0))
795 if (PUTC (' ', f) == EOF)
797 if (!FcCacheWriteTime (f, dir->info.time))
799 if (PUTC (' ', f) == EOF)
801 if (!FcCacheWriteString (f, (FcChar8 *) FC_FONT_FILE_DIR))
803 if (PUTC ('\n', f) == EOF)
806 for (file_hash = 0; file_hash < FC_GLOBAL_CACHE_FILE_HASH_SIZE; file_hash++)
808 for (file = dir->ents[file_hash]; file; file = file->next)
810 if (!file->info.referenced)
812 if (!FcCacheWritePath (f, dir->info.file, file->info.file))
814 if (PUTC (' ', f) == EOF)
816 if (!FcCacheWriteInt (f, file->id < 0 ? 0 : file->id))
818 if (PUTC (' ', f) == EOF)
820 if (!FcCacheWriteTime (f, file->info.time))
822 if (PUTC (' ', f) == EOF)
824 if (!FcCacheWriteString (f, file->name))
826 if (PUTC ('\n', f) == EOF)
833 if (fclose (f) == EOF)
836 if (!FcAtomicReplaceOrig (atomic))
839 FcAtomicUnlock (atomic);
840 FcAtomicDestroy (atomic);
842 cache->updated = FcFalse;
848 FcAtomicDeleteNew (atomic);
850 FcAtomicUnlock (atomic);
852 FcAtomicDestroy (atomic);
858 FcDirCacheValid (const FcChar8 *dir)
860 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
861 struct stat file_stat, dir_stat;
863 if (stat ((char *) dir, &dir_stat) < 0)
865 FcStrFree (cache_file);
868 if (stat ((char *) cache_file, &file_stat) < 0)
870 FcStrFree (cache_file);
873 FcStrFree (cache_file);
875 * If the directory has been modified more recently than
876 * the cache file, the cache is not valid
878 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
884 FcDirCacheReadDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
886 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
891 FcChar8 file_buf[8192], *file;
892 FcChar8 name_buf[8192], *name;
893 FcBool ret = FcFalse;
898 if (FcDebug () & FC_DBG_CACHE)
899 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file);
901 f = fopen ((char *) cache_file, "r");
904 if (FcDebug () & FC_DBG_CACHE)
905 printf (" no cache file\n");
909 if (!FcDirCacheValid (dir))
911 if (FcDebug () & FC_DBG_CACHE)
912 printf (" cache file older than directory\n");
916 base = (FcChar8 *) strrchr ((char *) cache_file, '/');
920 dir_len = base - cache_file;
924 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
925 FcCacheReadInt (f, &id) &&
926 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
928 if (!FcCacheFontSetAdd (set, dirs, cache_file, dir_len,
931 if (file != file_buf)
933 if (name != name_buf)
937 if (FcDebug () & FC_DBG_CACHE)
938 printf (" cache loaded\n");
942 if (file && file != file_buf)
944 if (name && name != name_buf)
955 * return the path from the directory containing 'cache' to 'file'
958 static const FcChar8 *
959 FcFileBaseName (const FcChar8 *cache, const FcChar8 *file)
961 const FcChar8 *cache_slash;
963 cache_slash = (const FcChar8 *) strrchr ((const char *) cache, '/');
964 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
965 (cache_slash + 1) - cache))
966 return file + ((cache_slash + 1) - cache);
971 FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
973 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
977 const FcChar8 *file, *base;
985 if (FcDebug () & FC_DBG_CACHE)
986 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
988 f = fopen ((char *) cache_file, "w");
991 if (FcDebug () & FC_DBG_CACHE)
992 printf (" can't create \"%s\"\n", cache_file);
996 list = FcStrListCreate (dirs);
1000 while ((dir = FcStrListNext (list)))
1002 base = FcFileBaseName (cache_file, dir);
1003 if (!FcCacheWriteString (f, base))
1005 if (PUTC (' ', f) == EOF)
1007 if (!FcCacheWriteInt (f, 0))
1009 if (PUTC (' ', f) == EOF)
1011 if (!FcCacheWriteString (f, FC_FONT_FILE_DIR))
1013 if (PUTC ('\n', f) == EOF)
1017 for (n = 0; n < set->nfont; n++)
1019 font = set->fonts[n];
1020 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
1022 base = FcFileBaseName (cache_file, file);
1023 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
1025 if (FcDebug () & FC_DBG_CACHEV)
1026 printf (" write file \"%s\"\n", base);
1027 if (!FcCacheWriteString (f, base))
1029 if (PUTC (' ', f) == EOF)
1031 if (!FcCacheWriteInt (f, id))
1033 if (PUTC (' ', f) == EOF)
1035 name = FcNameUnparse (font);
1038 ret = FcCacheWriteString (f, name);
1042 if (PUTC ('\n', f) == EOF)
1046 FcStrListDone (list);
1048 if (fclose (f) == EOF)
1053 if (FcDebug () & FC_DBG_CACHE)
1054 printf (" cache written\n");
1058 FcStrListDone (list);
1062 unlink ((char *) cache_file);