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
;
626 cache
->broken
= FcFalse
;
631 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
633 FcGlobalCacheDir
*d
, *next
;
636 for (h
= 0; h
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; h
++)
638 for (d
= cache
->ents
[h
]; d
; d
= next
)
641 FcGlobalCacheDirDestroy (d
);
648 * Cache file syntax is quite simple:
650 * "file_name" id time "font_name" \n
654 FcGlobalCacheLoad (FcGlobalCache
*cache
,
655 const FcChar8
*cache_file
)
658 FcChar8 file_buf
[8192], *file
;
661 FcChar8 name_buf
[8192], *name
;
662 FcGlobalCacheInfo
*info
;
664 f
= fopen ((char *) cache_file
, "r");
668 cache
->updated
= FcFalse
;
671 while ((file
= FcCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
672 FcCacheReadInt (f
, &id
) &&
673 FcCacheReadTime (f
, &time
) &&
674 (name
= FcCacheReadString (f
, name_buf
, sizeof (name_buf
))))
676 if (FcDebug () & FC_DBG_CACHEV
)
677 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file
, name
);
678 if (!FcStrCmp (name
, FC_FONT_FILE_DIR
))
679 info
= FcGlobalCacheDirAdd (cache
, file
, time
, FcFalse
);
681 info
= FcGlobalCacheFileAdd (cache
, file
, id
, time
, name
, FcFalse
);
683 cache
->broken
= FcTrue
;
686 if (FcDebug () & FC_DBG_CACHE_REF
)
687 printf ("FcGlobalCacheLoad entry %d %s\n",
688 cache
->entries
, file
);
689 if (file
!= file_buf
)
691 if (name
!= name_buf
)
696 if (file
&& file
!= file_buf
)
698 if (name
&& name
!= name_buf
)
704 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
709 const FcChar8
*match
;
711 FcGlobalCacheInfo
*info
;
715 if (stat ((char *) file
, &statb
) < 0)
717 if (S_ISDIR (statb
.st_mode
))
718 info
= FcGlobalCacheDirAdd (cache
, file
, statb
.st_mtime
,
721 info
= FcGlobalCacheFileAdd (cache
, file
, id
, statb
.st_mtime
,
725 FcGlobalCacheReferenced (cache
, info
);
726 cache
->updated
= FcTrue
;
729 cache
->broken
= FcTrue
;
734 FcGlobalCacheSave (FcGlobalCache
*cache
,
735 const FcChar8
*cache_file
)
738 int dir_hash
, file_hash
;
739 FcGlobalCacheDir
*dir
;
740 FcGlobalCacheFile
*file
;
743 if (!cache
->updated
&& cache
->referenced
== cache
->entries
)
749 /* Set-UID programs can't safely update the cache */
750 if (getuid () != geteuid ())
753 atomic
= FcAtomicCreate (cache_file
);
756 if (!FcAtomicLock (atomic
))
758 f
= fopen ((char *) FcAtomicNewFile(atomic
), "w");
762 for (dir_hash
= 0; dir_hash
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; dir_hash
++)
764 for (dir
= cache
->ents
[dir_hash
]; dir
; dir
= dir
->next
)
766 if (!dir
->info
.referenced
)
768 if (!FcCacheWriteString (f
, dir
->info
.file
))
770 if (putc (' ', f
) == EOF
)
772 if (!FcCacheWriteInt (f
, 0))
774 if (putc (' ', f
) == EOF
)
776 if (!FcCacheWriteTime (f
, dir
->info
.time
))
778 if (putc (' ', f
) == EOF
)
780 if (!FcCacheWriteString (f
, (FcChar8
*) FC_FONT_FILE_DIR
))
782 if (putc ('\n', f
) == EOF
)
785 for (file_hash
= 0; file_hash
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; file_hash
++)
787 for (file
= dir
->ents
[file_hash
]; file
; file
= file
->next
)
789 if (!file
->info
.referenced
)
791 if (!FcCacheWritePath (f
, dir
->info
.file
, file
->info
.file
))
793 if (putc (' ', f
) == EOF
)
795 if (!FcCacheWriteInt (f
, file
->id
< 0 ? 0 : file
->id
))
797 if (putc (' ', f
) == EOF
)
799 if (!FcCacheWriteTime (f
, file
->info
.time
))
801 if (putc (' ', f
) == EOF
)
803 if (!FcCacheWriteString (f
, file
->name
))
805 if (putc ('\n', f
) == EOF
)
812 if (fclose (f
) == EOF
)
815 if (!FcAtomicReplaceOrig (atomic
))
818 FcAtomicUnlock (atomic
);
819 FcAtomicDestroy (atomic
);
821 cache
->updated
= FcFalse
;
827 FcAtomicDeleteNew (atomic
);
829 FcAtomicUnlock (atomic
);
831 FcAtomicDestroy (atomic
);
837 FcDirCacheValid (const FcChar8
*dir
)
839 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
840 struct stat file_stat
, dir_stat
;
842 if (stat ((char *) dir
, &dir_stat
) < 0)
844 FcStrFree (cache_file
);
847 if (stat ((char *) cache_file
, &file_stat
) < 0)
849 FcStrFree (cache_file
);
852 FcStrFree (cache_file
);
854 * If the directory has been modified more recently than
855 * the cache file, the cache is not valid
857 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
863 FcDirCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
865 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
870 FcChar8 file_buf
[8192], *file
;
871 FcChar8 name_buf
[8192], *name
;
872 FcBool ret
= FcFalse
;
877 if (FcDebug () & FC_DBG_CACHE
)
878 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file
);
880 f
= fopen ((char *) cache_file
, "r");
883 if (FcDebug () & FC_DBG_CACHE
)
884 printf (" no cache file\n");
888 if (!FcDirCacheValid (dir
))
890 if (FcDebug () & FC_DBG_CACHE
)
891 printf (" cache file older than directory\n");
895 base
= (FcChar8
*) strrchr ((char *) cache_file
, '/');
899 dir_len
= base
- cache_file
;
903 while ((file
= FcCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
904 FcCacheReadInt (f
, &id
) &&
905 (name
= FcCacheReadString (f
, name_buf
, sizeof (name_buf
))))
907 if (!FcCacheFontSetAdd (set
, dirs
, cache_file
, dir_len
,
910 if (file
!= file_buf
)
912 if (name
!= name_buf
)
916 if (FcDebug () & FC_DBG_CACHE
)
917 printf (" cache loaded\n");
921 if (file
&& file
!= file_buf
)
923 if (name
&& name
!= name_buf
)
934 * return the path from the directory containing 'cache' to 'file'
937 static const FcChar8
*
938 FcFileBaseName (const FcChar8
*cache
, const FcChar8
*file
)
940 const FcChar8
*cache_slash
;
942 cache_slash
= (const FcChar8
*) strrchr ((const char *) cache
, '/');
943 if (cache_slash
&& !strncmp ((const char *) cache
, (const char *) file
,
944 (cache_slash
+ 1) - cache
))
945 return file
+ ((cache_slash
+ 1) - cache
);
950 FcDirCacheWriteDir (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
952 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
956 const FcChar8
*file
, *base
;
964 if (FcDebug () & FC_DBG_CACHE
)
965 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
967 f
= fopen ((char *) cache_file
, "w");
970 if (FcDebug () & FC_DBG_CACHE
)
971 printf (" can't create \"%s\"\n", cache_file
);
975 list
= FcStrListCreate (dirs
);
979 while ((dir
= FcStrListNext (list
)))
981 base
= FcFileBaseName (cache_file
, dir
);
982 if (!FcCacheWriteString (f
, base
))
984 if (putc (' ', f
) == EOF
)
986 if (!FcCacheWriteInt (f
, 0))
988 if (putc (' ', f
) == EOF
)
990 if (!FcCacheWriteString (f
, FC_FONT_FILE_DIR
))
992 if (putc ('\n', f
) == EOF
)
996 for (n
= 0; n
< set
->nfont
; n
++)
998 font
= set
->fonts
[n
];
999 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
1001 base
= FcFileBaseName (cache_file
, file
);
1002 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
1004 if (FcDebug () & FC_DBG_CACHEV
)
1005 printf (" write file \"%s\"\n", base
);
1006 if (!FcCacheWriteString (f
, base
))
1008 if (putc (' ', f
) == EOF
)
1010 if (!FcCacheWriteInt (f
, id
))
1012 if (putc (' ', f
) == EOF
)
1014 name
= FcNameUnparse (font
);
1017 ret
= FcCacheWriteString (f
, name
);
1021 if (putc ('\n', f
) == EOF
)
1025 FcStrListDone (list
);
1027 if (fclose (f
) == EOF
)
1032 if (FcDebug () & FC_DBG_CACHE
)
1033 printf (" cache written\n");
1037 FcStrListDone (list
);
1041 unlink ((char *) cache_file
);