2 * $XFree86: xc/lib/fontconfig/src/fccache.c,v 1.7 2002/05/21 17:06:22 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.
27 #define FC_DBG_CACHE_REF 1024
30 FcCacheReadString (FILE *f
, FcChar8
*dest
, int len
)
38 while ((c
= getc (f
)) != EOF
)
50 while ((c
= getc (f
)) != EOF
)
65 FcChar8
*new = malloc (size
* 2);
68 memcpy (new, d
, size
);
85 FcCacheReadUlong (FILE *f
, unsigned long *dest
)
90 while ((c
= getc (f
)) != EOF
)
100 if (c
== EOF
|| isspace (c
))
104 t
= t
* 10 + (c
- '0');
112 FcCacheReadInt (FILE *f
, int *dest
)
117 ret
= FcCacheReadUlong (f
, &t
);
124 FcCacheReadTime (FILE *f
, time_t *dest
)
129 ret
= FcCacheReadUlong (f
, &t
);
136 FcCacheWriteChars (FILE *f
, const FcChar8
*chars
)
139 while ((c
= *chars
++))
144 if (putc ('\\', f
) == EOF
)
148 if (putc (c
, f
) == EOF
)
156 FcCacheWriteString (FILE *f
, const FcChar8
*string
)
159 if (putc ('"', f
) == EOF
)
161 if (!FcCacheWriteChars (f
, string
))
163 if (putc ('"', f
) == EOF
)
169 FcCacheWritePath (FILE *f
, const FcChar8
*dir
, const FcChar8
*file
)
171 if (putc ('"', f
) == EOF
)
174 if (!FcCacheWriteChars (f
, dir
))
176 if (dir
&& dir
[strlen((const char *) dir
) - 1] != '/')
177 if (putc ('/', f
) == EOF
)
179 if (!FcCacheWriteChars (f
, file
))
181 if (putc ('"', f
) == EOF
)
187 FcCacheWriteUlong (FILE *f
, unsigned long t
)
190 unsigned long temp
, digit
;
203 if (putc ((char) digit
+ '0', f
) == EOF
)
205 temp
= temp
- pow
* digit
;
212 FcCacheWriteInt (FILE *f
, int i
)
214 return FcCacheWriteUlong (f
, (unsigned long) i
);
218 FcCacheWriteTime (FILE *f
, time_t t
)
220 return FcCacheWriteUlong (f
, (unsigned long) t
);
224 FcCacheFontSetAdd (FcFontSet
*set
,
231 FcChar8 path_buf
[8192], *path
;
233 FcBool ret
= FcFalse
;
237 len
= (dir_len
+ 1 + strlen ((const char *) file
) + 1);
238 if (len
> sizeof (path_buf
))
244 strncpy ((char *) path
, (const char *) dir
, dir_len
);
245 if (dir
[dir_len
- 1] != '/')
246 path
[dir_len
++] = '/';
247 strcpy ((char *) path
+ dir_len
, (const char *) file
);
248 if (!FcStrCmp (name
, FC_FONT_FILE_DIR
))
250 if (FcDebug () & FC_DBG_CACHEV
)
251 printf (" dir cache dir \"%s\"\n", path
);
252 ret
= FcStrSetAdd (dirs
, path
);
254 else if (!FcStrCmp (name
, FC_FONT_FILE_INVALID
))
260 font
= FcNameParse (name
);
263 if (FcDebug () & FC_DBG_CACHEV
)
264 printf (" dir cache file \"%s\"\n", file
);
265 ret
= (FcPatternAddString (font
, FC_FILE
, path
) &&
266 FcFontSetAdd (set
, font
));
268 FcPatternDestroy (font
);
271 if (path
!= path_buf
) free (path
);
277 FcCacheHash (const FcChar8
*string
)
282 while ((c
= *string
++))
288 * Verify the saved timestamp for a file
291 FcGlobalCacheCheckTime (FcGlobalCacheInfo
*info
)
295 if (stat ((char *) info
->file
, &statb
) < 0)
297 if (FcDebug () & FC_DBG_CACHE
)
298 printf (" file missing\n");
301 if (statb
.st_mtime
!= info
->time
)
303 if (FcDebug () & FC_DBG_CACHE
)
304 printf (" timestamp mismatch (was %d is %d)\n",
305 (int) info
->time
, (int) statb
.st_mtime
);
312 FcGlobalCacheReferenced (FcGlobalCache
*cache
,
313 FcGlobalCacheInfo
*info
)
315 if (!info
->referenced
)
317 info
->referenced
= FcTrue
;
319 if (FcDebug () & FC_DBG_CACHE_REF
)
320 printf ("Reference %d %s\n", cache
->referenced
, info
->file
);
325 * Break a path into dir/base elements and compute the base hash
326 * and the dir length. This is shared between the functions
327 * which walk the file caches
330 typedef struct _FcFilePathInfo
{
334 unsigned int base_hash
;
337 static FcFilePathInfo
338 FcFilePathInfoGet (const FcChar8
*path
)
343 slash
= (FcChar8
*) strrchr ((const char *) path
, '/');
347 i
.dir_len
= slash
- path
;
354 i
.dir
= (const FcChar8
*) ".";
358 i
.base_hash
= FcCacheHash (i
.base
);
363 FcGlobalCacheDirGet (FcGlobalCache
*cache
,
366 FcBool create_missing
)
368 unsigned int hash
= FcCacheHash (dir
);
369 FcGlobalCacheDir
*d
, **prev
;
371 for (prev
= &cache
->ents
[hash
% FC_GLOBAL_CACHE_DIR_HASH_SIZE
];
373 prev
= &(*prev
)->next
)
375 if (d
->info
.hash
== hash
&& d
->len
== len
&&
376 !strncmp ((const char *) d
->info
.file
,
377 (const char *) dir
, len
))
385 d
= malloc (sizeof (FcGlobalCacheDir
) + len
+ 1);
391 d
->info
.file
= (FcChar8
*) (d
+ 1);
392 strncpy ((char *) d
->info
.file
, (const char *) dir
, len
);
393 d
->info
.file
[len
] = '\0';
395 d
->info
.referenced
= FcFalse
;
397 for (i
= 0; i
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; i
++)
404 static FcGlobalCacheInfo
*
405 FcGlobalCacheDirAdd (FcGlobalCache
*cache
,
412 FcGlobalCacheSubdir
*subdir
;
413 FcGlobalCacheDir
*parent
;
416 * Add this directory to the cache
418 d
= FcGlobalCacheDirGet (cache
, dir
, strlen ((const char *) dir
), FcTrue
);
422 i
= FcFilePathInfoGet (dir
);
424 * Add this directory to the subdirectory list of the parent
426 parent
= FcGlobalCacheDirGet (cache
, i
.dir
, i
.dir_len
, FcTrue
);
429 subdir
= malloc (sizeof (FcGlobalCacheSubdir
) +
430 strlen ((const char *) i
.base
) + 1);
433 subdir
->file
= (FcChar8
*) (subdir
+ 1);
434 strcpy ((char *) subdir
->file
, (const char *) i
.base
);
435 subdir
->next
= parent
->subdirs
;
436 parent
->subdirs
= subdir
;
441 FcGlobalCacheDirDestroy (FcGlobalCacheDir
*d
)
443 FcGlobalCacheFile
*f
, *next
;
445 FcGlobalCacheSubdir
*s
, *nexts
;
447 for (h
= 0; h
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; h
++)
448 for (f
= d
->ents
[h
]; f
; f
= next
)
453 for (s
= d
->subdirs
; s
; s
= nexts
)
462 FcGlobalCacheScanDir (FcFontSet
*set
,
464 FcGlobalCache
*cache
,
467 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, dir
,
468 strlen ((const char *) dir
),
470 FcGlobalCacheFile
*f
;
473 FcGlobalCacheSubdir
*subdir
;
475 if (FcDebug() & FC_DBG_CACHE
)
476 printf ("FcGlobalCacheScanDir %s\n", dir
);
480 if (FcDebug () & FC_DBG_CACHE
)
481 printf ("\tNo dir cache entry\n");
485 if (!FcGlobalCacheCheckTime (&d
->info
))
487 if (FcDebug () & FC_DBG_CACHE
)
488 printf ("\tdir cache entry time mismatch\n");
492 dir_len
= strlen ((const char *) dir
);
493 for (h
= 0; h
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; h
++)
494 for (f
= d
->ents
[h
]; f
; f
= f
->next
)
496 if (FcDebug() & FC_DBG_CACHEV
)
497 printf ("FcGlobalCacheScanDir add file %s\n", f
->info
.file
);
498 if (!FcCacheFontSetAdd (set
, dirs
, dir
, dir_len
,
499 f
->info
.file
, f
->name
))
501 cache
->broken
= FcTrue
;
504 FcGlobalCacheReferenced (cache
, &f
->info
);
506 for (subdir
= d
->subdirs
; subdir
; subdir
= subdir
->next
)
508 if (!FcCacheFontSetAdd (set
, dirs
, dir
, dir_len
,
509 subdir
->file
, FC_FONT_FILE_DIR
))
511 cache
->broken
= FcTrue
;
516 FcGlobalCacheReferenced (cache
, &d
->info
);
522 * Locate the cache entry for a particular file
525 FcGlobalCacheFileGet (FcGlobalCache
*cache
,
530 FcFilePathInfo i
= FcFilePathInfoGet (file
);
531 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, i
.dir
,
533 FcGlobalCacheFile
*f
, *match
= 0;
538 for (f
= d
->ents
[i
.base_hash
% FC_GLOBAL_CACHE_FILE_HASH_SIZE
]; f
; f
= f
->next
)
540 if (f
->info
.hash
== i
.base_hash
&&
541 !strcmp ((const char *) f
->info
.file
, (const char *) i
.base
))
555 * Add a file entry to the cache
557 static FcGlobalCacheInfo
*
558 FcGlobalCacheFileAdd (FcGlobalCache
*cache
,
565 FcFilePathInfo i
= FcFilePathInfoGet (path
);
566 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, i
.dir
,
568 FcGlobalCacheFile
*f
, **prev
;
572 for (prev
= &d
->ents
[i
.base_hash
% FC_GLOBAL_CACHE_FILE_HASH_SIZE
];
574 prev
= &(*prev
)->next
)
576 if (f
->info
.hash
== i
.base_hash
&&
578 !strcmp ((const char *) f
->info
.file
, (const char *) i
.base
))
589 if (f
->info
.referenced
)
594 f
= malloc (sizeof (FcGlobalCacheFile
) +
595 strlen ((char *) i
.base
) + 1 +
596 strlen ((char *) name
) + 1);
601 f
->info
.hash
= i
.base_hash
;
602 f
->info
.file
= (FcChar8
*) (f
+ 1);
604 f
->info
.referenced
= FcFalse
;
606 f
->name
= f
->info
.file
+ strlen ((char *) i
.base
) + 1;
607 strcpy ((char *) f
->info
.file
, (const char *) i
.base
);
608 strcpy ((char *) f
->name
, (const char *) name
);
613 FcGlobalCacheCreate (void)
615 FcGlobalCache
*cache
;
618 cache
= malloc (sizeof (FcGlobalCache
));
621 for (h
= 0; h
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; h
++)
624 cache
->referenced
= 0;
625 cache
->updated
= FcFalse
;
630 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
632 FcGlobalCacheDir
*d
, *next
;
635 for (h
= 0; h
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; h
++)
637 for (d
= cache
->ents
[h
]; d
; d
= next
)
640 FcGlobalCacheDirDestroy (d
);
647 * Cache file syntax is quite simple:
649 * "file_name" id time "font_name" \n
653 FcGlobalCacheLoad (FcGlobalCache
*cache
,
654 const FcChar8
*cache_file
)
657 FcChar8 file_buf
[8192], *file
;
660 FcChar8 name_buf
[8192], *name
;
661 FcGlobalCacheInfo
*info
;
663 f
= fopen ((char *) cache_file
, "r");
667 cache
->updated
= FcFalse
;
670 while ((file
= FcCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
671 FcCacheReadInt (f
, &id
) &&
672 FcCacheReadTime (f
, &time
) &&
673 (name
= FcCacheReadString (f
, name_buf
, sizeof (name_buf
))))
675 if (FcDebug () & FC_DBG_CACHEV
)
676 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file
, name
);
677 if (!FcStrCmp (name
, FC_FONT_FILE_DIR
))
678 info
= FcGlobalCacheDirAdd (cache
, file
, time
, FcFalse
);
680 info
= FcGlobalCacheFileAdd (cache
, file
, id
, time
, name
, FcFalse
);
682 cache
->broken
= FcTrue
;
685 if (FcDebug () & FC_DBG_CACHE_REF
)
686 printf ("FcGlobalCacheLoad entry %d %s\n",
687 cache
->entries
, file
);
688 if (file
!= file_buf
)
690 if (name
!= name_buf
)
695 if (file
&& file
!= file_buf
)
697 if (name
&& name
!= name_buf
)
703 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
708 const FcChar8
*match
;
710 FcGlobalCacheInfo
*info
;
714 if (stat ((char *) file
, &statb
) < 0)
716 if (S_ISDIR (statb
.st_mode
))
717 info
= FcGlobalCacheDirAdd (cache
, file
, statb
.st_mtime
,
720 info
= FcGlobalCacheFileAdd (cache
, file
, id
, statb
.st_mtime
,
724 FcGlobalCacheReferenced (cache
, info
);
725 cache
->updated
= FcTrue
;
728 cache
->broken
= FcTrue
;
733 FcGlobalCacheSave (FcGlobalCache
*cache
,
734 const FcChar8
*cache_file
)
737 int dir_hash
, file_hash
;
738 FcGlobalCacheDir
*dir
;
739 FcGlobalCacheFile
*file
;
742 if (!cache
->updated
&& cache
->referenced
== cache
->entries
)
748 /* Set-UID programs can't safely update the cache */
749 if (getuid () != geteuid ())
752 atomic
= FcAtomicCreate (cache_file
);
755 if (!FcAtomicLock (atomic
))
757 f
= fopen ((char *) FcAtomicNewFile(atomic
), "w");
761 for (dir_hash
= 0; dir_hash
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; dir_hash
++)
763 for (dir
= cache
->ents
[dir_hash
]; dir
; dir
= dir
->next
)
765 if (!dir
->info
.referenced
)
767 if (!FcCacheWriteString (f
, dir
->info
.file
))
769 if (putc (' ', f
) == EOF
)
771 if (!FcCacheWriteInt (f
, 0))
773 if (putc (' ', f
) == EOF
)
775 if (!FcCacheWriteTime (f
, dir
->info
.time
))
777 if (putc (' ', f
) == EOF
)
779 if (!FcCacheWriteString (f
, (FcChar8
*) FC_FONT_FILE_DIR
))
781 if (putc ('\n', f
) == EOF
)
784 for (file_hash
= 0; file_hash
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; file_hash
++)
786 for (file
= dir
->ents
[file_hash
]; file
; file
= file
->next
)
788 if (!file
->info
.referenced
)
790 if (!FcCacheWritePath (f
, dir
->info
.file
, file
->info
.file
))
792 if (putc (' ', f
) == EOF
)
794 if (!FcCacheWriteInt (f
, file
->id
< 0 ? 0 : file
->id
))
796 if (putc (' ', f
) == EOF
)
798 if (!FcCacheWriteTime (f
, file
->info
.time
))
800 if (putc (' ', f
) == EOF
)
802 if (!FcCacheWriteString (f
, file
->name
))
804 if (putc ('\n', f
) == EOF
)
811 if (fclose (f
) == EOF
)
814 if (!FcAtomicReplaceOrig (atomic
))
817 FcAtomicUnlock (atomic
);
818 FcAtomicDestroy (atomic
);
820 cache
->updated
= FcFalse
;
826 FcAtomicDeleteNew (atomic
);
828 FcAtomicUnlock (atomic
);
830 FcAtomicDestroy (atomic
);
836 FcDirCacheValid (const FcChar8
*dir
)
838 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
839 struct stat file_stat
, dir_stat
;
841 if (stat ((char *) dir
, &dir_stat
) < 0)
843 FcStrFree (cache_file
);
846 if (stat ((char *) cache_file
, &file_stat
) < 0)
848 FcStrFree (cache_file
);
851 FcStrFree (cache_file
);
853 * If the directory has been modified more recently than
854 * the cache file, the cache is not valid
856 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
862 FcDirCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
864 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
869 FcChar8 file_buf
[8192], *file
;
870 FcChar8 name_buf
[8192], *name
;
871 FcBool ret
= FcFalse
;
876 if (FcDebug () & FC_DBG_CACHE
)
877 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file
);
879 f
= fopen ((char *) cache_file
, "r");
882 if (FcDebug () & FC_DBG_CACHE
)
883 printf (" no cache file\n");
887 if (!FcDirCacheValid (dir
))
889 if (FcDebug () & FC_DBG_CACHE
)
890 printf (" cache file older than directory\n");
894 base
= (FcChar8
*) strrchr ((char *) cache_file
, '/');
898 dir_len
= base
- cache_file
;
902 while ((file
= FcCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
903 FcCacheReadInt (f
, &id
) &&
904 (name
= FcCacheReadString (f
, name_buf
, sizeof (name_buf
))))
906 if (!FcCacheFontSetAdd (set
, dirs
, cache_file
, dir_len
,
909 if (file
!= file_buf
)
911 if (name
!= name_buf
)
915 if (FcDebug () & FC_DBG_CACHE
)
916 printf (" cache loaded\n");
920 if (file
&& file
!= file_buf
)
922 if (name
&& name
!= name_buf
)
933 * return the path from the directory containing 'cache' to 'file'
936 static const FcChar8
*
937 FcFileBaseName (const FcChar8
*cache
, const FcChar8
*file
)
939 const FcChar8
*cache_slash
;
941 cache_slash
= (const FcChar8
*) strrchr ((const char *) cache
, '/');
942 if (cache_slash
&& !strncmp ((const char *) cache
, (const char *) file
,
943 (cache_slash
+ 1) - cache
))
944 return file
+ ((cache_slash
+ 1) - cache
);
949 FcDirCacheWriteDir (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
951 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
955 const FcChar8
*file
, *base
;
963 if (FcDebug () & FC_DBG_CACHE
)
964 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
966 f
= fopen ((char *) cache_file
, "w");
969 if (FcDebug () & FC_DBG_CACHE
)
970 printf (" can't create \"%s\"\n", cache_file
);
974 list
= FcStrListCreate (dirs
);
978 while ((dir
= FcStrListNext (list
)))
980 base
= FcFileBaseName (cache_file
, dir
);
981 if (!FcCacheWriteString (f
, base
))
983 if (putc (' ', f
) == EOF
)
985 if (!FcCacheWriteInt (f
, 0))
987 if (putc (' ', f
) == EOF
)
989 if (!FcCacheWriteString (f
, FC_FONT_FILE_DIR
))
991 if (putc ('\n', f
) == EOF
)
995 for (n
= 0; n
< set
->nfont
; n
++)
997 font
= set
->fonts
[n
];
998 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
1000 base
= FcFileBaseName (cache_file
, file
);
1001 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
1003 if (FcDebug () & FC_DBG_CACHEV
)
1004 printf (" write file \"%s\"\n", base
);
1005 if (!FcCacheWriteString (f
, base
))
1007 if (putc (' ', f
) == EOF
)
1009 if (!FcCacheWriteInt (f
, id
))
1011 if (putc (' ', f
) == EOF
)
1013 name
= FcNameUnparse (font
);
1016 ret
= FcCacheWriteString (f
, name
);
1020 if (putc ('\n', f
) == EOF
)
1024 FcStrListDone (list
);
1026 if (fclose (f
) == EOF
)
1031 if (FcDebug () & FC_DBG_CACHE
)
1032 printf (" cache written\n");
1036 FcStrListDone (list
);
1040 unlink ((char *) cache_file
);