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));
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 FcGlobalCacheScanDir (FcFontSet *set,
490 FcGlobalCache *cache,
493 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, dir,
494 strlen ((const char *) dir),
496 FcGlobalCacheFile *f;
499 FcGlobalCacheSubdir *subdir;
501 if (FcDebug() & FC_DBG_CACHE)
502 printf ("FcGlobalCacheScanDir %s\n", dir);
506 if (FcDebug () & FC_DBG_CACHE)
507 printf ("\tNo dir cache entry\n");
511 if (!FcGlobalCacheCheckTime (&d->info))
513 if (FcDebug () & FC_DBG_CACHE)
514 printf ("\tdir cache entry time mismatch\n");
518 dir_len = strlen ((const char *) dir);
519 for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
520 for (f = d->ents[h]; f; f = f->next)
522 if (FcDebug() & FC_DBG_CACHEV)
523 printf ("FcGlobalCacheScanDir add file %s\n", f->info.file);
524 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
525 f->info.file, f->name))
527 cache->broken = FcTrue;
530 FcGlobalCacheReferenced (cache, &f->info);
532 for (subdir = d->subdirs; subdir; subdir = subdir->next)
534 FcFilePathInfo info = FcFilePathInfoGet (subdir->ent->info.file);
536 if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
537 info.base, FC_FONT_FILE_DIR))
539 cache->broken = FcTrue;
542 FcGlobalCacheReferenced (cache, &subdir->ent->info);
545 FcGlobalCacheReferenced (cache, &d->info);
551 * Locate the cache entry for a particular file
554 FcGlobalCacheFileGet (FcGlobalCache *cache,
559 FcFilePathInfo i = FcFilePathInfoGet (file);
560 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
562 FcGlobalCacheFile *f, *match = 0;
567 for (f = d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE]; f; f = f->next)
569 if (f->info.hash == i.base_hash &&
570 !strcmp ((const char *) f->info.file, (const char *) i.base))
584 * Add a file entry to the cache
586 static FcGlobalCacheInfo *
587 FcGlobalCacheFileAdd (FcGlobalCache *cache,
594 FcFilePathInfo i = FcFilePathInfoGet (path);
595 FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
597 FcGlobalCacheFile *f, **prev;
602 for (prev = &d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE];
604 prev = &(*prev)->next)
606 if (f->info.hash == i.base_hash &&
608 !strcmp ((const char *) f->info.file, (const char *) i.base))
619 if (f->info.referenced)
622 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
623 strlen ((char *) f->info.file) + 1 +
624 strlen ((char *) f->name) + 1);
627 size = (sizeof (FcGlobalCacheFile) +
628 strlen ((char *) i.base) + 1 +
629 strlen ((char *) name) + 1);
633 FcMemAlloc (FC_MEM_CACHE, size);
636 f->info.hash = i.base_hash;
637 f->info.file = (FcChar8 *) (f + 1);
639 f->info.referenced = FcFalse;
641 f->name = f->info.file + strlen ((char *) i.base) + 1;
642 strcpy ((char *) f->info.file, (const char *) i.base);
643 strcpy ((char *) f->name, (const char *) name);
648 FcGlobalCacheCreate (void)
650 FcGlobalCache *cache;
653 cache = malloc (sizeof (FcGlobalCache));
656 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
657 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
660 cache->referenced = 0;
661 cache->updated = FcFalse;
662 cache->broken = FcFalse;
667 FcGlobalCacheDestroy (FcGlobalCache *cache)
669 FcGlobalCacheDir *d, *next;
672 for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
674 for (d = cache->ents[h]; d; d = next)
677 FcGlobalCacheDirDestroy (d);
680 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
685 * Cache file syntax is quite simple:
687 * "file_name" id time "font_name" \n
691 FcGlobalCacheLoad (FcGlobalCache *cache,
692 const FcChar8 *cache_file)
695 FcChar8 file_buf[8192], *file;
698 FcChar8 name_buf[8192], *name;
699 FcGlobalCacheInfo *info;
701 f = fopen ((char *) cache_file, "r");
705 cache->updated = FcFalse;
708 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
709 FcCacheReadInt (f, &id) &&
710 FcCacheReadTime (f, &time) &&
711 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
713 if (FcDebug () & FC_DBG_CACHEV)
714 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file, name);
715 if (!FcStrCmp (name, FC_FONT_FILE_DIR))
716 info = FcGlobalCacheDirAdd (cache, file, time, FcFalse);
718 info = FcGlobalCacheFileAdd (cache, file, id, time, name, FcFalse);
720 cache->broken = FcTrue;
723 if (FcDebug () & FC_DBG_CACHE_REF)
724 printf ("FcGlobalCacheLoad entry %d %s\n",
725 cache->entries, file);
726 if (file != file_buf)
728 if (name != name_buf)
733 if (file && file != file_buf)
735 if (name && name != name_buf)
741 FcGlobalCacheUpdate (FcGlobalCache *cache,
746 const FcChar8 *match;
748 FcGlobalCacheInfo *info;
752 if (stat ((char *) file, &statb) < 0)
754 if (S_ISDIR (statb.st_mode))
755 info = FcGlobalCacheDirAdd (cache, file, statb.st_mtime,
758 info = FcGlobalCacheFileAdd (cache, file, id, statb.st_mtime,
762 FcGlobalCacheReferenced (cache, info);
763 cache->updated = FcTrue;
766 cache->broken = FcTrue;
771 FcGlobalCacheSave (FcGlobalCache *cache,
772 const FcChar8 *cache_file)
775 int dir_hash, file_hash;
776 FcGlobalCacheDir *dir;
777 FcGlobalCacheFile *file;
780 if (!cache->updated && cache->referenced == cache->entries)
786 /* Set-UID programs can't safely update the cache */
787 if (getuid () != geteuid ())
790 atomic = FcAtomicCreate (cache_file);
793 if (!FcAtomicLock (atomic))
795 f = fopen ((char *) FcAtomicNewFile(atomic), "w");
799 for (dir_hash = 0; dir_hash < FC_GLOBAL_CACHE_DIR_HASH_SIZE; dir_hash++)
801 for (dir = cache->ents[dir_hash]; dir; dir = dir->next)
803 if (!dir->info.referenced)
805 if (!FcCacheWriteString (f, dir->info.file))
807 if (PUTC (' ', f) == EOF)
809 if (!FcCacheWriteInt (f, 0))
811 if (PUTC (' ', f) == EOF)
813 if (!FcCacheWriteTime (f, dir->info.time))
815 if (PUTC (' ', f) == EOF)
817 if (!FcCacheWriteString (f, (FcChar8 *) FC_FONT_FILE_DIR))
819 if (PUTC ('\n', f) == EOF)
822 for (file_hash = 0; file_hash < FC_GLOBAL_CACHE_FILE_HASH_SIZE; file_hash++)
824 for (file = dir->ents[file_hash]; file; file = file->next)
826 if (!file->info.referenced)
828 if (!FcCacheWritePath (f, dir->info.file, file->info.file))
830 if (PUTC (' ', f) == EOF)
832 if (!FcCacheWriteInt (f, file->id < 0 ? 0 : file->id))
834 if (PUTC (' ', f) == EOF)
836 if (!FcCacheWriteTime (f, file->info.time))
838 if (PUTC (' ', f) == EOF)
840 if (!FcCacheWriteString (f, file->name))
842 if (PUTC ('\n', f) == EOF)
849 if (fclose (f) == EOF)
852 if (!FcAtomicReplaceOrig (atomic))
855 FcAtomicUnlock (atomic);
856 FcAtomicDestroy (atomic);
858 cache->updated = FcFalse;
864 FcAtomicDeleteNew (atomic);
866 FcAtomicUnlock (atomic);
868 FcAtomicDestroy (atomic);
874 FcDirCacheValid (const FcChar8 *dir)
876 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
877 struct stat file_stat, dir_stat;
879 if (stat ((char *) dir, &dir_stat) < 0)
881 FcStrFree (cache_file);
884 if (stat ((char *) cache_file, &file_stat) < 0)
886 FcStrFree (cache_file);
889 FcStrFree (cache_file);
891 * If the directory has been modified more recently than
892 * the cache file, the cache is not valid
894 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
900 FcDirCacheReadDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
902 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
907 FcChar8 file_buf[8192], *file;
908 FcChar8 name_buf[8192], *name;
909 FcBool ret = FcFalse;
914 if (FcDebug () & FC_DBG_CACHE)
915 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file);
917 f = fopen ((char *) cache_file, "r");
920 if (FcDebug () & FC_DBG_CACHE)
921 printf (" no cache file\n");
925 if (!FcDirCacheValid (dir))
927 if (FcDebug () & FC_DBG_CACHE)
928 printf (" cache file older than directory\n");
932 base = (FcChar8 *) strrchr ((char *) cache_file, '/');
936 dir_len = base - cache_file;
940 while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
941 FcCacheReadInt (f, &id) &&
942 (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
944 if (!FcCacheFontSetAdd (set, dirs, cache_file, dir_len,
947 if (file != file_buf)
949 if (name != name_buf)
953 if (FcDebug () & FC_DBG_CACHE)
954 printf (" cache loaded\n");
958 if (file && file != file_buf)
960 if (name && name != name_buf)
965 FcStrFree (cache_file);
971 * return the path from the directory containing 'cache' to 'file'
974 static const FcChar8 *
975 FcFileBaseName (const FcChar8 *cache, const FcChar8 *file)
977 const FcChar8 *cache_slash;
979 cache_slash = (const FcChar8 *) strrchr ((const char *) cache, '/');
980 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
981 (cache_slash + 1) - cache))
982 return file + ((cache_slash + 1) - cache);
987 FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
989 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
993 const FcChar8 *file, *base;
1001 if (FcDebug () & FC_DBG_CACHE)
1002 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1004 f = fopen ((char *) cache_file, "w");
1007 if (FcDebug () & FC_DBG_CACHE)
1008 printf (" can't create \"%s\"\n", cache_file);
1012 list = FcStrListCreate (dirs);
1016 while ((dir = FcStrListNext (list)))
1018 base = FcFileBaseName (cache_file, dir);
1019 if (!FcCacheWriteString (f, base))
1021 if (PUTC (' ', f) == EOF)
1023 if (!FcCacheWriteInt (f, 0))
1025 if (PUTC (' ', f) == EOF)
1027 if (!FcCacheWriteString (f, FC_FONT_FILE_DIR))
1029 if (PUTC ('\n', f) == EOF)
1033 for (n = 0; n < set->nfont; n++)
1035 font = set->fonts[n];
1036 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
1038 base = FcFileBaseName (cache_file, file);
1039 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
1041 if (FcDebug () & FC_DBG_CACHEV)
1042 printf (" write file \"%s\"\n", base);
1043 if (!FcCacheWriteString (f, base))
1045 if (PUTC (' ', f) == EOF)
1047 if (!FcCacheWriteInt (f, id))
1049 if (PUTC (' ', f) == EOF)
1051 name = FcNameUnparse (font);
1054 ret = FcCacheWriteString (f, name);
1058 if (PUTC ('\n', f) == EOF)
1062 FcStrListDone (list);
1064 if (fclose (f) == EOF)
1067 FcStrFree (cache_file);
1069 if (FcDebug () & FC_DBG_CACHE)
1070 printf (" cache written\n");
1074 FcStrListDone (list);
1078 unlink ((char *) cache_file);
1079 FcStrFree (cache_file);