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)
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);
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) +
452 strlen ((const char *) i.base) + 1);
455 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir) +
456 strlen ((const char *) i.base) + 1);
457 subdir->file = (FcChar8 *) (subdir + 1);
458 strcpy ((char *) subdir->file, (const char *) i.base);
459 subdir->next = parent->subdirs;
460 parent->subdirs = subdir;
465 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
467 FcGlobalCacheFile *f, *next;
469 FcGlobalCacheSubdir *s, *nexts;
471 for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
472 for (f = d->ents[h]; f; f = next)
475 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
476 strlen ((char *) f->info.file) + 1 +
477 strlen ((char *) f->name) + 1);
480 for (s = d->subdirs; s; s = nexts)
483 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir) +
484 strlen ((char *) s->file) + 1);
487 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir) + d->len + 1);
492 FcGlobalCacheScanDir (FcFontSet *set,
494 FcGlobalCache *cache,
497 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, dir,
498 strlen ((const char *) dir),
500 FcGlobalCacheFile *f;
503 FcGlobalCacheSubdir *subdir;
505 if (FcDebug() & FC_DBG_CACHE)
506 printf ("FcGlobalCacheScanDir %s\n", dir);
510 if (FcDebug () & FC_DBG_CACHE)
511 printf ("\tNo dir cache entry\n");
515 if (!FcGlobalCacheCheckTime (&d->info))
517 if (FcDebug () & FC_DBG_CACHE)
518 printf ("\tdir cache entry time mismatch\n");
522 dir_len = strlen ((const char *) dir);
523 for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
524 for (f = d->ents[h]; f; f = f->next)
526 if (FcDebug() & FC_DBG_CACHEV)
527 printf ("FcGlobalCacheScanDir add file %s\n", f->info.file);
528 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
529 f->info.file, f->name))
531 cache->broken = FcTrue;
534 FcGlobalCacheReferenced (cache, &f->info);
536 for (subdir = d->subdirs; subdir; subdir = subdir->next)
538 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
539 subdir->file, FC_FONT_FILE_DIR))
541 cache->broken = FcTrue;
546 FcGlobalCacheReferenced (cache, &d->info);
552 * Locate the cache entry for a particular file
555 FcGlobalCacheFileGet (FcGlobalCache *cache,
560 FcFilePathInfo i = FcFilePathInfoGet (file);
561 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
563 FcGlobalCacheFile *f, *match = 0;
568 for (f = d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE]; f; f = f->next)
570 if (f->info.hash == i.base_hash &&
571 !strcmp ((const char *) f->info.file, (const char *) i.base))
585 * Add a file entry to the cache
587 static FcGlobalCacheInfo *
588 FcGlobalCacheFileAdd (FcGlobalCache *cache,
595 FcFilePathInfo i = FcFilePathInfoGet (path);
596 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
598 FcGlobalCacheFile *f, **prev;
603 for (prev = &d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE];
605 prev = &(*prev)->next)
607 if (f->info.hash == i.base_hash &&
609 !strcmp ((const char *) f->info.file, (const char *) i.base))
620 if (f->info.referenced)
623 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
624 strlen ((char *) f->info.file) + 1 +
625 strlen ((char *) f->name) + 1);
628 size = (sizeof (FcGlobalCacheFile) +
629 strlen ((char *) i.base) + 1 +
630 strlen ((char *) name) + 1);
634 FcMemAlloc (FC_MEM_CACHE, size);
637 f->info.hash = i.base_hash;
638 f->info.file = (FcChar8 *) (f + 1);
640 f->info.referenced = FcFalse;
642 f->name = f->info.file + strlen ((char *) i.base) + 1;
643 strcpy ((char *) f->info.file, (const char *) i.base);
644 strcpy ((char *) f->name, (const char *) name);
649 FcGlobalCacheCreate (void)
651 FcGlobalCache *cache;
654 cache = malloc (sizeof (FcGlobalCache));
657 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
658 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
661 cache->referenced = 0;
662 cache->updated = FcFalse;
663 cache->broken = FcFalse;
668 FcGlobalCacheDestroy (FcGlobalCache *cache)
670 FcGlobalCacheDir *d, *next;
673 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
675 for (d = cache->ents[h]; d; d = next)
678 FcGlobalCacheDirDestroy (d);
681 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
686 * Cache file syntax is quite simple:
688 * "file_name" id time "font_name" \n
692 FcGlobalCacheLoad (FcGlobalCache *cache,
693 const FcChar8 *cache_file)
696 FcChar8 file_buf[8192], *file;
699 FcChar8 name_buf[8192], *name;
700 FcGlobalCacheInfo *info;
702 f = fopen ((char *) cache_file, "r");
706 cache->updated = FcFalse;
709 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
710 FcCacheReadInt (f, &id) &&
711 FcCacheReadTime (f, &time) &&
712 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
714 if (FcDebug () & FC_DBG_CACHEV)
715 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file, name);
716 if (!FcStrCmp (name, FC_FONT_FILE_DIR))
717 info = FcGlobalCacheDirAdd (cache, file, time, FcFalse);
719 info = FcGlobalCacheFileAdd (cache, file, id, time, name, FcFalse);
721 cache->broken = FcTrue;
724 if (FcDebug () & FC_DBG_CACHE_REF)
725 printf ("FcGlobalCacheLoad entry %d %s\n",
726 cache->entries, file);
727 if (file != file_buf)
729 if (name != name_buf)
734 if (file && file != file_buf)
736 if (name && name != name_buf)
742 FcGlobalCacheUpdate (FcGlobalCache *cache,
747 const FcChar8 *match;
749 FcGlobalCacheInfo *info;
753 if (stat ((char *) file, &statb) < 0)
755 if (S_ISDIR (statb.st_mode))
756 info = FcGlobalCacheDirAdd (cache, file, statb.st_mtime,
759 info = FcGlobalCacheFileAdd (cache, file, id, statb.st_mtime,
763 FcGlobalCacheReferenced (cache, info);
764 cache->updated = FcTrue;
767 cache->broken = FcTrue;
772 FcGlobalCacheSave (FcGlobalCache *cache,
773 const FcChar8 *cache_file)
776 int dir_hash, file_hash;
777 FcGlobalCacheDir *dir;
778 FcGlobalCacheFile *file;
781 if (!cache->updated && cache->referenced == cache->entries)
787 /* Set-UID programs can't safely update the cache */
788 if (getuid () != geteuid ())
791 atomic = FcAtomicCreate (cache_file);
794 if (!FcAtomicLock (atomic))
796 f = fopen ((char *) FcAtomicNewFile(atomic), "w");
800 for (dir_hash = 0; dir_hash < FC_GLOBAL_CACHE_DIR_HASH_SIZE; dir_hash++)
802 for (dir = cache->ents[dir_hash]; dir; dir = dir->next)
804 if (!dir->info.referenced)
806 if (!FcCacheWriteString (f, dir->info.file))
808 if (PUTC (' ', f) == EOF)
810 if (!FcCacheWriteInt (f, 0))
812 if (PUTC (' ', f) == EOF)
814 if (!FcCacheWriteTime (f, dir->info.time))
816 if (PUTC (' ', f) == EOF)
818 if (!FcCacheWriteString (f, (FcChar8 *) FC_FONT_FILE_DIR))
820 if (PUTC ('\n', f) == EOF)
823 for (file_hash = 0; file_hash < FC_GLOBAL_CACHE_FILE_HASH_SIZE; file_hash++)
825 for (file = dir->ents[file_hash]; file; file = file->next)
827 if (!file->info.referenced)
829 if (!FcCacheWritePath (f, dir->info.file, file->info.file))
831 if (PUTC (' ', f) == EOF)
833 if (!FcCacheWriteInt (f, file->id < 0 ? 0 : file->id))
835 if (PUTC (' ', f) == EOF)
837 if (!FcCacheWriteTime (f, file->info.time))
839 if (PUTC (' ', f) == EOF)
841 if (!FcCacheWriteString (f, file->name))
843 if (PUTC ('\n', f) == EOF)
850 if (fclose (f) == EOF)
853 if (!FcAtomicReplaceOrig (atomic))
856 FcAtomicUnlock (atomic);
857 FcAtomicDestroy (atomic);
859 cache->updated = FcFalse;
865 FcAtomicDeleteNew (atomic);
867 FcAtomicUnlock (atomic);
869 FcAtomicDestroy (atomic);
875 FcDirCacheValid (const FcChar8 *dir)
877 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
878 struct stat file_stat, dir_stat;
880 if (stat ((char *) dir, &dir_stat) < 0)
882 FcStrFree (cache_file);
885 if (stat ((char *) cache_file, &file_stat) < 0)
887 FcStrFree (cache_file);
890 FcStrFree (cache_file);
892 * If the directory has been modified more recently than
893 * the cache file, the cache is not valid
895 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
901 FcDirCacheReadDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
903 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
908 FcChar8 file_buf[8192], *file;
909 FcChar8 name_buf[8192], *name;
910 FcBool ret = FcFalse;
915 if (FcDebug () & FC_DBG_CACHE)
916 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file);
918 f = fopen ((char *) cache_file, "r");
921 if (FcDebug () & FC_DBG_CACHE)
922 printf (" no cache file\n");
926 if (!FcDirCacheValid (dir))
928 if (FcDebug () & FC_DBG_CACHE)
929 printf (" cache file older than directory\n");
933 base = (FcChar8 *) strrchr ((char *) cache_file, '/');
937 dir_len = base - cache_file;
941 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
942 FcCacheReadInt (f, &id) &&
943 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
945 if (!FcCacheFontSetAdd (set, dirs, cache_file, dir_len,
948 if (file != file_buf)
950 if (name != name_buf)
954 if (FcDebug () & FC_DBG_CACHE)
955 printf (" cache loaded\n");
959 if (file && file != file_buf)
961 if (name && name != name_buf)
966 FcStrFree (cache_file);
972 * return the path from the directory containing 'cache' to 'file'
975 static const FcChar8 *
976 FcFileBaseName (const FcChar8 *cache, const FcChar8 *file)
978 const FcChar8 *cache_slash;
980 cache_slash = (const FcChar8 *) strrchr ((const char *) cache, '/');
981 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
982 (cache_slash + 1) - cache))
983 return file + ((cache_slash + 1) - cache);
988 FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
990 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
994 const FcChar8 *file, *base;
1002 if (FcDebug () & FC_DBG_CACHE)
1003 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1005 f = fopen ((char *) cache_file, "w");
1008 if (FcDebug () & FC_DBG_CACHE)
1009 printf (" can't create \"%s\"\n", cache_file);
1013 list = FcStrListCreate (dirs);
1017 while ((dir = FcStrListNext (list)))
1019 base = FcFileBaseName (cache_file, dir);
1020 if (!FcCacheWriteString (f, base))
1022 if (PUTC (' ', f) == EOF)
1024 if (!FcCacheWriteInt (f, 0))
1026 if (PUTC (' ', f) == EOF)
1028 if (!FcCacheWriteString (f, FC_FONT_FILE_DIR))
1030 if (PUTC ('\n', f) == EOF)
1034 for (n = 0; n < set->nfont; n++)
1036 font = set->fonts[n];
1037 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
1039 base = FcFileBaseName (cache_file, file);
1040 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
1042 if (FcDebug () & FC_DBG_CACHEV)
1043 printf (" write file \"%s\"\n", base);
1044 if (!FcCacheWriteString (f, base))
1046 if (PUTC (' ', f) == EOF)
1048 if (!FcCacheWriteInt (f, id))
1050 if (PUTC (' ', f) == EOF)
1052 name = FcNameUnparse (font);
1055 ret = FcCacheWriteString (f, name);
1059 if (PUTC ('\n', f) == EOF)
1063 FcStrListDone (list);
1065 if (fclose (f) == EOF)
1068 FcStrFree (cache_file);
1070 if (FcDebug () & FC_DBG_CACHE)
1071 printf (" cache written\n");
1075 FcStrListDone (list);
1079 unlink ((char *) cache_file);
1080 FcStrFree (cache_file);