2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
4 * Copyright © 2000 Keith Packard
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
))
193 dir
[strlen((const char *) dir
) - 1] != '/' &&
194 dir
[strlen((const char *) dir
) - 1] != '\\')
196 if (!FcCacheWriteChars (f
, "\\"))
200 if (dir
&& dir
[strlen((const char *) dir
) - 1] != '/')
201 if (PUTC ('/', f
) == EOF
)
204 if (!FcCacheWriteChars (f
, file
))
206 if (PUTC ('"', f
) == EOF
)
212 FcCacheWriteUlong (FILE *f
, unsigned long t
)
215 unsigned long temp
, digit
;
228 if (PUTC ((char) digit
+ '0', f
) == EOF
)
230 temp
= temp
- pow
* digit
;
237 FcCacheWriteInt (FILE *f
, int i
)
239 return FcCacheWriteUlong (f
, (unsigned long) i
);
243 FcCacheWriteTime (FILE *f
, time_t t
)
245 return FcCacheWriteUlong (f
, (unsigned long) t
);
249 FcCacheFontSetAdd (FcFontSet
*set
,
257 FcChar8 path_buf
[8192], *path
;
259 FcBool ret
= FcFalse
;
264 len
= (dir_len
+ 1 + strlen ((const char *) file
) + 1);
265 if (len
> sizeof (path_buf
))
267 path
= malloc (len
); /* freed down below */
271 strncpy ((char *) path
, (const char *) dir
, dir_len
);
273 if (dir
[dir_len
- 1] != '/' && dir
[dir_len
- 1] != '\\' )
274 path
[dir_len
++] = '\\';
276 if (dir
[dir_len
- 1] != '/')
277 path
[dir_len
++] = '/';
279 strcpy ((char *) path
+ dir_len
, (const char *) file
);
280 if (config
&& !FcConfigAcceptFilename (config
, path
))
282 else if (!FcStrCmp (name
, FC_FONT_FILE_DIR
))
284 if (FcDebug () & FC_DBG_CACHEV
)
285 printf (" dir cache dir \"%s\"\n", path
);
286 ret
= FcStrSetAdd (dirs
, path
);
288 else if (!FcStrCmp (name
, FC_FONT_FILE_INVALID
))
294 font
= FcNameParse (name
);
297 if (FcDebug () & FC_DBG_CACHEV
)
298 printf (" dir cache file \"%s\"\n", file
);
299 ret
= FcPatternAddString (font
, FC_FILE
, path
);
302 frozen
= FcPatternFreeze (font
);
305 ret
= FcFontSetAdd (set
, frozen
);
307 FcPatternDestroy (font
);
310 if (path
!= path_buf
) free (path
);
316 FcCacheHash (const FcChar8
*string
, int len
)
321 while (len
-- && (c
= *string
++))
327 * Verify the saved timestamp for a file
330 FcGlobalCacheCheckTime (const FcChar8
*file
, FcGlobalCacheInfo
*info
)
334 if (stat ((char *) file
, &statb
) < 0)
336 if (FcDebug () & FC_DBG_CACHE
)
337 printf (" file %s missing\n", file
);
340 if (statb
.st_mtime
!= info
->time
)
342 if (FcDebug () & FC_DBG_CACHE
)
343 printf (" timestamp mismatch (was %d is %d)\n",
344 (int) info
->time
, (int) statb
.st_mtime
);
351 FcGlobalCacheReferenced (FcGlobalCache
*cache
,
352 FcGlobalCacheInfo
*info
)
354 if (!info
->referenced
)
356 info
->referenced
= FcTrue
;
358 if (FcDebug () & FC_DBG_CACHE_REF
)
359 printf ("Reference %d %s\n", cache
->referenced
, info
->file
);
364 * Break a path into dir/base elements and compute the base hash
365 * and the dir length. This is shared between the functions
366 * which walk the file caches
369 typedef struct _FcFilePathInfo
{
373 unsigned int base_hash
;
376 static FcFilePathInfo
377 FcFilePathInfoGet (const FcChar8
*path
)
382 slash
= FcStrLastSlash (path
);
386 i
.dir_len
= slash
- path
;
393 i
.dir
= (const FcChar8
*) ".";
397 i
.base_hash
= FcCacheHash (i
.base
, -1);
402 FcGlobalCacheDirGet (FcGlobalCache
*cache
,
405 FcBool create_missing
)
407 unsigned int hash
= FcCacheHash (dir
, len
);
408 FcGlobalCacheDir
*d
, **prev
;
410 for (prev
= &cache
->ents
[hash
% FC_GLOBAL_CACHE_DIR_HASH_SIZE
];
412 prev
= &(*prev
)->next
)
414 if (d
->info
.hash
== hash
&& d
->len
== len
&&
415 !strncmp ((const char *) d
->info
.file
,
416 (const char *) dir
, len
))
424 d
= malloc (sizeof (FcGlobalCacheDir
) + len
+ 1);
427 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
) + len
+ 1);
431 d
->info
.file
= (FcChar8
*) (d
+ 1);
432 strncpy ((char *) d
->info
.file
, (const char *) dir
, len
);
433 d
->info
.file
[len
] = '\0';
435 d
->info
.referenced
= FcFalse
;
437 for (i
= 0; i
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; i
++)
444 static FcGlobalCacheInfo
*
445 FcGlobalCacheDirAdd (FcGlobalCache
*cache
,
449 FcBool create_missing
)
453 FcGlobalCacheSubdir
*subdir
;
454 FcGlobalCacheDir
*parent
;
456 i
= FcFilePathInfoGet (dir
);
457 parent
= FcGlobalCacheDirGet (cache
, i
.dir
, i
.dir_len
, create_missing
);
459 * Tricky here -- directories containing fonts.cache-1 files
460 * need entries only when the parent doesn't have a cache file.
461 * That is, when the parent already exists in the cache, is
462 * referenced and has a "real" timestamp. The time of 0 is
463 * special and marks directories which got stuck in the
464 * global cache for this very reason. Yes, it could
465 * use a separate boolean field, and probably should.
467 if (!parent
|| (!create_missing
&&
468 (!parent
->info
.referenced
||
469 (parent
->info
.time
== 0))))
472 * Add this directory to the cache
474 d
= FcGlobalCacheDirGet (cache
, dir
, strlen ((const char *) dir
), FcTrue
);
479 * Add this directory to the subdirectory list of the parent
481 subdir
= malloc (sizeof (FcGlobalCacheSubdir
));
484 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCacheSubdir
));
486 subdir
->next
= parent
->subdirs
;
487 parent
->subdirs
= subdir
;
492 FcGlobalCacheDirDestroy (FcGlobalCacheDir
*d
)
494 FcGlobalCacheFile
*f
, *next
;
496 FcGlobalCacheSubdir
*s
, *nexts
;
498 for (h
= 0; h
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; h
++)
499 for (f
= d
->ents
[h
]; f
; f
= next
)
502 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheFile
) +
503 strlen ((char *) f
->info
.file
) + 1 +
504 strlen ((char *) f
->name
) + 1);
507 for (s
= d
->subdirs
; s
; s
= nexts
)
510 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheSubdir
));
513 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
) + d
->len
+ 1);
518 * If the parent is in the global cache and referenced, add
519 * an entry for 'dir' to the global cache. This is used
520 * for directories with fonts.cache files
524 FcGlobalCacheReferenceSubdir (FcGlobalCache
*cache
,
527 FcGlobalCacheInfo
*info
;
528 info
= FcGlobalCacheDirAdd (cache
, dir
, 0, FcFalse
, FcFalse
);
529 if (info
&& !info
->referenced
)
531 info
->referenced
= FcTrue
;
537 * Check to see if the global cache contains valid data for 'dir'.
538 * If so, scan the global cache for files and directories in 'dir'.
539 * else, return False.
542 FcGlobalCacheScanDir (FcFontSet
*set
,
544 FcGlobalCache
*cache
,
548 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, dir
,
549 strlen ((const char *) dir
),
551 FcGlobalCacheFile
*f
;
554 FcGlobalCacheSubdir
*subdir
;
555 FcBool any_in_cache
= FcFalse
;
557 if (FcDebug() & FC_DBG_CACHE
)
558 printf ("FcGlobalCacheScanDir %s\n", dir
);
562 if (FcDebug () & FC_DBG_CACHE
)
563 printf ("\tNo dir cache entry\n");
568 * See if the timestamp recorded in the global cache
569 * matches the directory time, if not, return False
571 if (!FcGlobalCacheCheckTime (d
->info
.file
, &d
->info
))
573 if (FcDebug () & FC_DBG_CACHE
)
574 printf ("\tdir cache entry time mismatch\n");
579 * Add files from 'dir' to the fontset
581 dir_len
= strlen ((const char *) dir
);
582 for (h
= 0; h
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; h
++)
583 for (f
= d
->ents
[h
]; f
; f
= f
->next
)
585 if (FcDebug() & FC_DBG_CACHEV
)
586 printf ("FcGlobalCacheScanDir add file %s\n", f
->info
.file
);
587 any_in_cache
= FcTrue
;
588 if (!FcCacheFontSetAdd (set
, dirs
, dir
, dir_len
,
589 f
->info
.file
, f
->name
, config
))
591 cache
->broken
= FcTrue
;
594 FcGlobalCacheReferenced (cache
, &f
->info
);
597 * Add directories in 'dir' to 'dirs'
599 for (subdir
= d
->subdirs
; subdir
; subdir
= subdir
->next
)
601 FcFilePathInfo info
= FcFilePathInfoGet (subdir
->ent
->info
.file
);
603 any_in_cache
= FcTrue
;
604 if (!FcCacheFontSetAdd (set
, dirs
, dir
, dir_len
,
605 info
.base
, FC_FONT_FILE_DIR
, config
))
607 cache
->broken
= FcTrue
;
610 FcGlobalCacheReferenced (cache
, &subdir
->ent
->info
);
613 FcGlobalCacheReferenced (cache
, &d
->info
);
616 * To recover from a bug in previous versions of fontconfig,
617 * return FcFalse if no entries in the cache were found
618 * for this directory. This will cause any empty directories
619 * to get rescanned every time fontconfig is initialized. This
620 * might get removed at some point when the older cache files are
627 * Locate the cache entry for a particular file
630 FcGlobalCacheFileGet (FcGlobalCache
*cache
,
635 FcFilePathInfo i
= FcFilePathInfoGet (file
);
636 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, i
.dir
,
638 FcGlobalCacheFile
*f
, *match
= 0;
643 for (f
= d
->ents
[i
.base_hash
% FC_GLOBAL_CACHE_FILE_HASH_SIZE
]; f
; f
= f
->next
)
645 if (f
->info
.hash
== i
.base_hash
&&
646 !strcmp ((const char *) f
->info
.file
, (const char *) i
.base
))
660 * Add a file entry to the cache
662 static FcGlobalCacheInfo
*
663 FcGlobalCacheFileAdd (FcGlobalCache
*cache
,
670 FcFilePathInfo i
= FcFilePathInfoGet (path
);
671 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, i
.dir
,
673 FcGlobalCacheFile
*f
, **prev
;
678 for (prev
= &d
->ents
[i
.base_hash
% FC_GLOBAL_CACHE_FILE_HASH_SIZE
];
680 prev
= &(*prev
)->next
)
682 if (f
->info
.hash
== i
.base_hash
&&
684 !strcmp ((const char *) f
->info
.file
, (const char *) i
.base
))
695 if (f
->info
.referenced
)
698 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheFile
) +
699 strlen ((char *) f
->info
.file
) + 1 +
700 strlen ((char *) f
->name
) + 1);
703 size
= (sizeof (FcGlobalCacheFile
) +
704 strlen ((char *) i
.base
) + 1 +
705 strlen ((char *) name
) + 1);
709 FcMemAlloc (FC_MEM_CACHE
, size
);
712 f
->info
.hash
= i
.base_hash
;
713 f
->info
.file
= (FcChar8
*) (f
+ 1);
715 f
->info
.referenced
= FcFalse
;
717 f
->name
= f
->info
.file
+ strlen ((char *) i
.base
) + 1;
718 strcpy ((char *) f
->info
.file
, (const char *) i
.base
);
719 strcpy ((char *) f
->name
, (const char *) name
);
724 FcGlobalCacheCreate (void)
726 FcGlobalCache
*cache
;
729 cache
= malloc (sizeof (FcGlobalCache
));
732 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
733 for (h
= 0; h
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; h
++)
736 cache
->referenced
= 0;
737 cache
->updated
= FcFalse
;
738 cache
->broken
= FcFalse
;
743 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
745 FcGlobalCacheDir
*d
, *next
;
748 for (h
= 0; h
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; h
++)
750 for (d
= cache
->ents
[h
]; d
; d
= next
)
753 FcGlobalCacheDirDestroy (d
);
756 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
761 * Cache file syntax is quite simple:
763 * "file_name" id time "font_name" \n
767 FcGlobalCacheLoad (FcGlobalCache
*cache
,
768 const FcChar8
*cache_file
)
771 FcChar8 file_buf
[8192], *file
;
774 FcChar8 name_buf
[8192], *name
;
775 FcGlobalCacheInfo
*info
;
777 f
= fopen ((char *) cache_file
, "r");
781 cache
->updated
= FcFalse
;
784 while ((file
= FcCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
785 FcCacheReadInt (f
, &id
) &&
786 FcCacheReadTime (f
, &time
) &&
787 (name
= FcCacheReadString (f
, name_buf
, sizeof (name_buf
))))
789 if (FcDebug () & FC_DBG_CACHEV
)
790 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file
, name
);
791 if (!FcStrCmp (name
, FC_FONT_FILE_DIR
))
792 info
= FcGlobalCacheDirAdd (cache
, file
, time
, FcFalse
, FcTrue
);
794 info
= FcGlobalCacheFileAdd (cache
, file
, id
, time
, name
, FcFalse
);
796 cache
->broken
= FcTrue
;
799 if (FcDebug () & FC_DBG_CACHE_REF
)
800 printf ("FcGlobalCacheLoad entry %d %s\n",
801 cache
->entries
, file
);
802 if (file
!= file_buf
)
804 if (name
!= name_buf
)
809 if (file
&& file
!= file_buf
)
811 if (name
&& name
!= name_buf
)
817 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
822 const FcChar8
*match
;
824 FcGlobalCacheInfo
*info
;
828 if (stat ((char *) file
, &statb
) < 0)
830 if (S_ISDIR (statb
.st_mode
))
831 info
= FcGlobalCacheDirAdd (cache
, file
, statb
.st_mtime
,
834 info
= FcGlobalCacheFileAdd (cache
, file
, id
, statb
.st_mtime
,
838 FcGlobalCacheReferenced (cache
, info
);
839 cache
->updated
= FcTrue
;
842 cache
->broken
= FcTrue
;
847 FcGlobalCacheSave (FcGlobalCache
*cache
,
848 const FcChar8
*cache_file
)
851 int dir_hash
, file_hash
;
852 FcGlobalCacheDir
*dir
;
853 FcGlobalCacheFile
*file
;
856 if (!cache
->updated
&& cache
->referenced
== cache
->entries
)
862 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
863 /* Set-UID programs can't safely update the cache */
864 if (getuid () != geteuid ())
868 atomic
= FcAtomicCreate (cache_file
);
871 if (!FcAtomicLock (atomic
))
873 f
= fopen ((char *) FcAtomicNewFile(atomic
), "w");
877 for (dir_hash
= 0; dir_hash
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; dir_hash
++)
879 for (dir
= cache
->ents
[dir_hash
]; dir
; dir
= dir
->next
)
881 if (!dir
->info
.referenced
)
883 if (!FcCacheWriteString (f
, dir
->info
.file
))
885 if (PUTC (' ', f
) == EOF
)
887 if (!FcCacheWriteInt (f
, 0))
889 if (PUTC (' ', f
) == EOF
)
891 if (!FcCacheWriteTime (f
, dir
->info
.time
))
893 if (PUTC (' ', f
) == EOF
)
895 if (!FcCacheWriteString (f
, (FcChar8
*) FC_FONT_FILE_DIR
))
897 if (PUTC ('\n', f
) == EOF
)
900 for (file_hash
= 0; file_hash
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; file_hash
++)
902 for (file
= dir
->ents
[file_hash
]; file
; file
= file
->next
)
904 if (!file
->info
.referenced
)
906 if (!FcCacheWritePath (f
, dir
->info
.file
, file
->info
.file
))
908 if (PUTC (' ', f
) == EOF
)
910 if (!FcCacheWriteInt (f
, file
->id
< 0 ? 0 : file
->id
))
912 if (PUTC (' ', f
) == EOF
)
914 if (!FcCacheWriteTime (f
, file
->info
.time
))
916 if (PUTC (' ', f
) == EOF
)
918 if (!FcCacheWriteString (f
, file
->name
))
920 if (PUTC ('\n', f
) == EOF
)
927 if (fclose (f
) == EOF
)
930 if (!FcAtomicReplaceOrig (atomic
))
933 FcAtomicUnlock (atomic
);
934 FcAtomicDestroy (atomic
);
936 cache
->updated
= FcFalse
;
942 FcAtomicDeleteNew (atomic
);
944 FcAtomicUnlock (atomic
);
946 FcAtomicDestroy (atomic
);
952 FcDirCacheValid (const FcChar8
*dir
)
954 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
955 struct stat file_stat
, dir_stat
;
957 if (stat ((char *) dir
, &dir_stat
) < 0)
959 FcStrFree (cache_file
);
962 if (stat ((char *) cache_file
, &file_stat
) < 0)
964 FcStrFree (cache_file
);
967 FcStrFree (cache_file
);
969 * If the directory has been modified more recently than
970 * the cache file, the cache is not valid
972 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
978 FcDirCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
, FcConfig
*config
)
980 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
985 FcChar8 file_buf
[8192], *file
;
986 FcChar8 name_buf
[8192], *name
;
987 FcBool ret
= FcFalse
;
992 if (FcDebug () & FC_DBG_CACHE
)
993 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file
);
995 f
= fopen ((char *) cache_file
, "r");
998 if (FcDebug () & FC_DBG_CACHE
)
999 printf (" no cache file\n");
1003 if (!FcDirCacheValid (dir
))
1005 if (FcDebug () & FC_DBG_CACHE
)
1006 printf (" cache file older than directory\n");
1010 base
= (FcChar8
*) strrchr ((char *) cache_file
, '/');
1014 dir_len
= base
- cache_file
;
1018 while ((file
= FcCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
1019 FcCacheReadInt (f
, &id
) &&
1020 (name
= FcCacheReadString (f
, name_buf
, sizeof (name_buf
))))
1022 if (!FcCacheFontSetAdd (set
, dirs
, cache_file
, dir_len
,
1023 file
, name
, config
))
1025 if (file
!= file_buf
)
1027 if (name
!= name_buf
)
1031 if (FcDebug () & FC_DBG_CACHE
)
1032 printf (" cache loaded\n");
1036 if (file
&& file
!= file_buf
)
1038 if (name
&& name
!= name_buf
)
1043 FcStrFree (cache_file
);
1049 * return the path from the directory containing 'cache' to 'file'
1052 static const FcChar8
*
1053 FcFileBaseName (const FcChar8
*cache
, const FcChar8
*file
)
1055 const FcChar8
*cache_slash
;
1057 cache_slash
= FcStrLastSlash (cache
);
1058 if (cache_slash
&& !strncmp ((const char *) cache
, (const char *) file
,
1059 (cache_slash
+ 1) - cache
))
1060 return file
+ ((cache_slash
+ 1) - cache
);
1065 FcDirCacheWriteDir (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
1067 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
1071 const FcChar8
*file
, *base
;
1079 if (FcDebug () & FC_DBG_CACHE
)
1080 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
1082 f
= fopen ((char *) cache_file
, "w");
1085 if (FcDebug () & FC_DBG_CACHE
)
1086 printf (" can't create \"%s\"\n", cache_file
);
1090 list
= FcStrListCreate (dirs
);
1094 while ((dir
= FcStrListNext (list
)))
1096 base
= FcFileBaseName (cache_file
, dir
);
1097 if (!FcCacheWriteString (f
, base
))
1099 if (PUTC (' ', f
) == EOF
)
1101 if (!FcCacheWriteInt (f
, 0))
1103 if (PUTC (' ', f
) == EOF
)
1105 if (!FcCacheWriteString (f
, FC_FONT_FILE_DIR
))
1107 if (PUTC ('\n', f
) == EOF
)
1111 for (n
= 0; n
< set
->nfont
; n
++)
1113 font
= set
->fonts
[n
];
1114 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
1116 base
= FcFileBaseName (cache_file
, file
);
1117 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
1119 if (FcDebug () & FC_DBG_CACHEV
)
1120 printf (" write file \"%s\"\n", base
);
1121 if (!FcCacheWriteString (f
, base
))
1123 if (PUTC (' ', f
) == EOF
)
1125 if (!FcCacheWriteInt (f
, id
))
1127 if (PUTC (' ', f
) == EOF
)
1129 name
= FcNameUnparse (font
);
1132 ret
= FcCacheWriteString (f
, name
);
1136 if (PUTC ('\n', f
) == EOF
)
1140 FcStrListDone (list
);
1142 if (fclose (f
) == EOF
)
1145 FcStrFree (cache_file
);
1147 if (FcDebug () & FC_DBG_CACHE
)
1148 printf (" cache written\n");
1152 FcStrListDone (list
);
1156 unlink ((char *) cache_file
);
1157 FcStrFree (cache_file
);