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
,
256 FcChar8 path_buf
[8192], *path
;
258 FcBool ret
= FcFalse
;
263 len
= (dir_len
+ 1 + strlen ((const char *) file
) + 1);
264 if (len
> sizeof (path_buf
))
266 path
= malloc (len
); /* freed down below */
270 strncpy ((char *) path
, (const char *) dir
, dir_len
);
272 if (dir
[dir_len
- 1] != '/' && dir
[dir_len
- 1] != '\\' )
273 path
[dir_len
++] = '\\';
275 if (dir
[dir_len
- 1] != '/')
276 path
[dir_len
++] = '/';
278 strcpy ((char *) path
+ dir_len
, (const char *) file
);
279 if (!FcStrCmp (name
, FC_FONT_FILE_DIR
))
281 if (FcDebug () & FC_DBG_CACHEV
)
282 printf (" dir cache dir \"%s\"\n", path
);
283 ret
= FcStrSetAdd (dirs
, path
);
285 else if (!FcStrCmp (name
, FC_FONT_FILE_INVALID
))
291 font
= FcNameParse (name
);
294 if (FcDebug () & FC_DBG_CACHEV
)
295 printf (" dir cache file \"%s\"\n", file
);
296 ret
= FcPatternAddString (font
, FC_FILE
, path
);
299 frozen
= FcPatternFreeze (font
);
302 ret
= FcFontSetAdd (set
, frozen
);
304 FcPatternDestroy (font
);
307 if (path
!= path_buf
) free (path
);
313 FcCacheHash (const FcChar8
*string
, int len
)
318 while (len
-- && (c
= *string
++))
324 * Verify the saved timestamp for a file
327 FcGlobalCacheCheckTime (const FcChar8
*file
, FcGlobalCacheInfo
*info
)
331 if (stat ((char *) file
, &statb
) < 0)
333 if (FcDebug () & FC_DBG_CACHE
)
334 printf (" file %s missing\n", file
);
337 if (statb
.st_mtime
!= info
->time
)
339 if (FcDebug () & FC_DBG_CACHE
)
340 printf (" timestamp mismatch (was %d is %d)\n",
341 (int) info
->time
, (int) statb
.st_mtime
);
348 FcGlobalCacheReferenced (FcGlobalCache
*cache
,
349 FcGlobalCacheInfo
*info
)
351 if (!info
->referenced
)
353 info
->referenced
= FcTrue
;
355 if (FcDebug () & FC_DBG_CACHE_REF
)
356 printf ("Reference %d %s\n", cache
->referenced
, info
->file
);
361 * Break a path into dir/base elements and compute the base hash
362 * and the dir length. This is shared between the functions
363 * which walk the file caches
366 typedef struct _FcFilePathInfo
{
370 unsigned int base_hash
;
373 static FcFilePathInfo
374 FcFilePathInfoGet (const FcChar8
*path
)
379 slash
= FcStrLastSlash (path
);
383 i
.dir_len
= slash
- path
;
390 i
.dir
= (const FcChar8
*) ".";
394 i
.base_hash
= FcCacheHash (i
.base
, -1);
399 FcGlobalCacheDirGet (FcGlobalCache
*cache
,
402 FcBool create_missing
)
404 unsigned int hash
= FcCacheHash (dir
, len
);
405 FcGlobalCacheDir
*d
, **prev
;
407 for (prev
= &cache
->ents
[hash
% FC_GLOBAL_CACHE_DIR_HASH_SIZE
];
409 prev
= &(*prev
)->next
)
411 if (d
->info
.hash
== hash
&& d
->len
== len
&&
412 !strncmp ((const char *) d
->info
.file
,
413 (const char *) dir
, len
))
421 d
= malloc (sizeof (FcGlobalCacheDir
) + len
+ 1);
424 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
) + len
+ 1);
428 d
->info
.file
= (FcChar8
*) (d
+ 1);
429 strncpy ((char *) d
->info
.file
, (const char *) dir
, len
);
430 d
->info
.file
[len
] = '\0';
432 d
->info
.referenced
= FcFalse
;
434 for (i
= 0; i
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; i
++)
441 static FcGlobalCacheInfo
*
442 FcGlobalCacheDirAdd (FcGlobalCache
*cache
,
446 FcBool create_missing
)
450 FcGlobalCacheSubdir
*subdir
;
451 FcGlobalCacheDir
*parent
;
453 i
= FcFilePathInfoGet (dir
);
454 parent
= FcGlobalCacheDirGet (cache
, i
.dir
, i
.dir_len
, create_missing
);
456 * Tricky here -- directories containing fonts.cache-1 files
457 * need entries only when the parent doesn't have a cache file.
458 * That is, when the parent already exists in the cache, is
459 * referenced and has a "real" timestamp. The time of 0 is
460 * special and marks directories which got stuck in the
461 * global cache for this very reason. Yes, it could
462 * use a separate boolean field, and probably should.
464 if (!parent
|| (!create_missing
&&
465 (!parent
->info
.referenced
||
466 (parent
->info
.time
== 0))))
469 * Add this directory to the cache
471 d
= FcGlobalCacheDirGet (cache
, dir
, strlen ((const char *) dir
), FcTrue
);
476 * Add this directory to the subdirectory list of the parent
478 subdir
= malloc (sizeof (FcGlobalCacheSubdir
));
481 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCacheSubdir
));
483 subdir
->next
= parent
->subdirs
;
484 parent
->subdirs
= subdir
;
489 FcGlobalCacheDirDestroy (FcGlobalCacheDir
*d
)
491 FcGlobalCacheFile
*f
, *next
;
493 FcGlobalCacheSubdir
*s
, *nexts
;
495 for (h
= 0; h
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; h
++)
496 for (f
= d
->ents
[h
]; f
; f
= next
)
499 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheFile
) +
500 strlen ((char *) f
->info
.file
) + 1 +
501 strlen ((char *) f
->name
) + 1);
504 for (s
= d
->subdirs
; s
; s
= nexts
)
507 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheSubdir
));
510 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
) + d
->len
+ 1);
515 * If the parent is in the global cache and referenced, add
516 * an entry for 'dir' to the global cache. This is used
517 * for directories with fonts.cache files
521 FcGlobalCacheReferenceSubdir (FcGlobalCache
*cache
,
524 FcGlobalCacheInfo
*info
;
525 info
= FcGlobalCacheDirAdd (cache
, dir
, 0, FcFalse
, FcFalse
);
526 if (info
&& !info
->referenced
)
528 info
->referenced
= FcTrue
;
534 * Check to see if the global cache contains valid data for 'dir'.
535 * If so, scan the global cache for files and directories in 'dir'.
536 * else, return False.
539 FcGlobalCacheScanDir (FcFontSet
*set
,
541 FcGlobalCache
*cache
,
544 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, dir
,
545 strlen ((const char *) dir
),
547 FcGlobalCacheFile
*f
;
550 FcGlobalCacheSubdir
*subdir
;
551 FcBool any_in_cache
= FcFalse
;
553 if (FcDebug() & FC_DBG_CACHE
)
554 printf ("FcGlobalCacheScanDir %s\n", dir
);
558 if (FcDebug () & FC_DBG_CACHE
)
559 printf ("\tNo dir cache entry\n");
564 * See if the timestamp recorded in the global cache
565 * matches the directory time, if not, return False
567 if (!FcGlobalCacheCheckTime (d
->info
.file
, &d
->info
))
569 if (FcDebug () & FC_DBG_CACHE
)
570 printf ("\tdir cache entry time mismatch\n");
575 * Add files from 'dir' to the fontset
577 dir_len
= strlen ((const char *) dir
);
578 for (h
= 0; h
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; h
++)
579 for (f
= d
->ents
[h
]; f
; f
= f
->next
)
581 if (FcDebug() & FC_DBG_CACHEV
)
582 printf ("FcGlobalCacheScanDir add file %s\n", f
->info
.file
);
583 any_in_cache
= FcTrue
;
584 if (!FcCacheFontSetAdd (set
, dirs
, dir
, dir_len
,
585 f
->info
.file
, f
->name
))
587 cache
->broken
= FcTrue
;
590 FcGlobalCacheReferenced (cache
, &f
->info
);
593 * Add directories in 'dir' to 'dirs'
595 for (subdir
= d
->subdirs
; subdir
; subdir
= subdir
->next
)
597 FcFilePathInfo info
= FcFilePathInfoGet (subdir
->ent
->info
.file
);
599 any_in_cache
= FcTrue
;
600 if (!FcCacheFontSetAdd (set
, dirs
, dir
, dir_len
,
601 info
.base
, FC_FONT_FILE_DIR
))
603 cache
->broken
= FcTrue
;
606 FcGlobalCacheReferenced (cache
, &subdir
->ent
->info
);
609 FcGlobalCacheReferenced (cache
, &d
->info
);
612 * To recover from a bug in previous versions of fontconfig,
613 * return FcFalse if no entries in the cache were found
614 * for this directory. This will cause any empty directories
615 * to get rescanned every time fontconfig is initialized. This
616 * might get removed at some point when the older cache files are
623 * Locate the cache entry for a particular file
626 FcGlobalCacheFileGet (FcGlobalCache
*cache
,
631 FcFilePathInfo i
= FcFilePathInfoGet (file
);
632 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, i
.dir
,
634 FcGlobalCacheFile
*f
, *match
= 0;
639 for (f
= d
->ents
[i
.base_hash
% FC_GLOBAL_CACHE_FILE_HASH_SIZE
]; f
; f
= f
->next
)
641 if (f
->info
.hash
== i
.base_hash
&&
642 !strcmp ((const char *) f
->info
.file
, (const char *) i
.base
))
656 * Add a file entry to the cache
658 static FcGlobalCacheInfo
*
659 FcGlobalCacheFileAdd (FcGlobalCache
*cache
,
666 FcFilePathInfo i
= FcFilePathInfoGet (path
);
667 FcGlobalCacheDir
*d
= FcGlobalCacheDirGet (cache
, i
.dir
,
669 FcGlobalCacheFile
*f
, **prev
;
674 for (prev
= &d
->ents
[i
.base_hash
% FC_GLOBAL_CACHE_FILE_HASH_SIZE
];
676 prev
= &(*prev
)->next
)
678 if (f
->info
.hash
== i
.base_hash
&&
680 !strcmp ((const char *) f
->info
.file
, (const char *) i
.base
))
691 if (f
->info
.referenced
)
694 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheFile
) +
695 strlen ((char *) f
->info
.file
) + 1 +
696 strlen ((char *) f
->name
) + 1);
699 size
= (sizeof (FcGlobalCacheFile
) +
700 strlen ((char *) i
.base
) + 1 +
701 strlen ((char *) name
) + 1);
705 FcMemAlloc (FC_MEM_CACHE
, size
);
708 f
->info
.hash
= i
.base_hash
;
709 f
->info
.file
= (FcChar8
*) (f
+ 1);
711 f
->info
.referenced
= FcFalse
;
713 f
->name
= f
->info
.file
+ strlen ((char *) i
.base
) + 1;
714 strcpy ((char *) f
->info
.file
, (const char *) i
.base
);
715 strcpy ((char *) f
->name
, (const char *) name
);
720 FcGlobalCacheCreate (void)
722 FcGlobalCache
*cache
;
725 cache
= malloc (sizeof (FcGlobalCache
));
728 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
729 for (h
= 0; h
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; h
++)
732 cache
->referenced
= 0;
733 cache
->updated
= FcFalse
;
734 cache
->broken
= FcFalse
;
739 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
741 FcGlobalCacheDir
*d
, *next
;
744 for (h
= 0; h
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; h
++)
746 for (d
= cache
->ents
[h
]; d
; d
= next
)
749 FcGlobalCacheDirDestroy (d
);
752 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
757 * Cache file syntax is quite simple:
759 * "file_name" id time "font_name" \n
763 FcGlobalCacheLoad (FcGlobalCache
*cache
,
764 const FcChar8
*cache_file
)
767 FcChar8 file_buf
[8192], *file
;
770 FcChar8 name_buf
[8192], *name
;
771 FcGlobalCacheInfo
*info
;
773 f
= fopen ((char *) cache_file
, "r");
777 cache
->updated
= FcFalse
;
780 while ((file
= FcCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
781 FcCacheReadInt (f
, &id
) &&
782 FcCacheReadTime (f
, &time
) &&
783 (name
= FcCacheReadString (f
, name_buf
, sizeof (name_buf
))))
785 if (FcDebug () & FC_DBG_CACHEV
)
786 printf ("FcGlobalCacheLoad \"%s\" \"%20.20s\"\n", file
, name
);
787 if (!FcStrCmp (name
, FC_FONT_FILE_DIR
))
788 info
= FcGlobalCacheDirAdd (cache
, file
, time
, FcFalse
, FcTrue
);
790 info
= FcGlobalCacheFileAdd (cache
, file
, id
, time
, name
, FcFalse
);
792 cache
->broken
= FcTrue
;
795 if (FcDebug () & FC_DBG_CACHE_REF
)
796 printf ("FcGlobalCacheLoad entry %d %s\n",
797 cache
->entries
, file
);
798 if (file
!= file_buf
)
800 if (name
!= name_buf
)
805 if (file
&& file
!= file_buf
)
807 if (name
&& name
!= name_buf
)
813 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
818 const FcChar8
*match
;
820 FcGlobalCacheInfo
*info
;
824 if (stat ((char *) file
, &statb
) < 0)
826 if (S_ISDIR (statb
.st_mode
))
827 info
= FcGlobalCacheDirAdd (cache
, file
, statb
.st_mtime
,
830 info
= FcGlobalCacheFileAdd (cache
, file
, id
, statb
.st_mtime
,
834 FcGlobalCacheReferenced (cache
, info
);
835 cache
->updated
= FcTrue
;
838 cache
->broken
= FcTrue
;
843 FcGlobalCacheSave (FcGlobalCache
*cache
,
844 const FcChar8
*cache_file
)
847 int dir_hash
, file_hash
;
848 FcGlobalCacheDir
*dir
;
849 FcGlobalCacheFile
*file
;
852 if (!cache
->updated
&& cache
->referenced
== cache
->entries
)
858 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
859 /* Set-UID programs can't safely update the cache */
860 if (getuid () != geteuid ())
864 atomic
= FcAtomicCreate (cache_file
);
867 if (!FcAtomicLock (atomic
))
869 f
= fopen ((char *) FcAtomicNewFile(atomic
), "w");
873 for (dir_hash
= 0; dir_hash
< FC_GLOBAL_CACHE_DIR_HASH_SIZE
; dir_hash
++)
875 for (dir
= cache
->ents
[dir_hash
]; dir
; dir
= dir
->next
)
877 if (!dir
->info
.referenced
)
879 if (!FcCacheWriteString (f
, dir
->info
.file
))
881 if (PUTC (' ', f
) == EOF
)
883 if (!FcCacheWriteInt (f
, 0))
885 if (PUTC (' ', f
) == EOF
)
887 if (!FcCacheWriteTime (f
, dir
->info
.time
))
889 if (PUTC (' ', f
) == EOF
)
891 if (!FcCacheWriteString (f
, (FcChar8
*) FC_FONT_FILE_DIR
))
893 if (PUTC ('\n', f
) == EOF
)
896 for (file_hash
= 0; file_hash
< FC_GLOBAL_CACHE_FILE_HASH_SIZE
; file_hash
++)
898 for (file
= dir
->ents
[file_hash
]; file
; file
= file
->next
)
900 if (!file
->info
.referenced
)
902 if (!FcCacheWritePath (f
, dir
->info
.file
, file
->info
.file
))
904 if (PUTC (' ', f
) == EOF
)
906 if (!FcCacheWriteInt (f
, file
->id
< 0 ? 0 : file
->id
))
908 if (PUTC (' ', f
) == EOF
)
910 if (!FcCacheWriteTime (f
, file
->info
.time
))
912 if (PUTC (' ', f
) == EOF
)
914 if (!FcCacheWriteString (f
, file
->name
))
916 if (PUTC ('\n', f
) == EOF
)
923 if (fclose (f
) == EOF
)
926 if (!FcAtomicReplaceOrig (atomic
))
929 FcAtomicUnlock (atomic
);
930 FcAtomicDestroy (atomic
);
932 cache
->updated
= FcFalse
;
938 FcAtomicDeleteNew (atomic
);
940 FcAtomicUnlock (atomic
);
942 FcAtomicDestroy (atomic
);
948 FcDirCacheValid (const FcChar8
*dir
)
950 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
951 struct stat file_stat
, dir_stat
;
953 if (stat ((char *) dir
, &dir_stat
) < 0)
955 FcStrFree (cache_file
);
958 if (stat ((char *) cache_file
, &file_stat
) < 0)
960 FcStrFree (cache_file
);
963 FcStrFree (cache_file
);
965 * If the directory has been modified more recently than
966 * the cache file, the cache is not valid
968 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
974 FcDirCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
976 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
981 FcChar8 file_buf
[8192], *file
;
982 FcChar8 name_buf
[8192], *name
;
983 FcBool ret
= FcFalse
;
988 if (FcDebug () & FC_DBG_CACHE
)
989 printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file
);
991 f
= fopen ((char *) cache_file
, "r");
994 if (FcDebug () & FC_DBG_CACHE
)
995 printf (" no cache file\n");
999 if (!FcDirCacheValid (dir
))
1001 if (FcDebug () & FC_DBG_CACHE
)
1002 printf (" cache file older than directory\n");
1006 base
= (FcChar8
*) strrchr ((char *) cache_file
, '/');
1010 dir_len
= base
- cache_file
;
1014 while ((file
= FcCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
1015 FcCacheReadInt (f
, &id
) &&
1016 (name
= FcCacheReadString (f
, name_buf
, sizeof (name_buf
))))
1018 if (!FcCacheFontSetAdd (set
, dirs
, cache_file
, dir_len
,
1021 if (file
!= file_buf
)
1023 if (name
!= name_buf
)
1027 if (FcDebug () & FC_DBG_CACHE
)
1028 printf (" cache loaded\n");
1032 if (file
&& file
!= file_buf
)
1034 if (name
&& name
!= name_buf
)
1039 FcStrFree (cache_file
);
1045 * return the path from the directory containing 'cache' to 'file'
1048 static const FcChar8
*
1049 FcFileBaseName (const FcChar8
*cache
, const FcChar8
*file
)
1051 const FcChar8
*cache_slash
;
1053 cache_slash
= FcStrLastSlash (cache
);
1054 if (cache_slash
&& !strncmp ((const char *) cache
, (const char *) file
,
1055 (cache_slash
+ 1) - cache
))
1056 return file
+ ((cache_slash
+ 1) - cache
);
1061 FcDirCacheWriteDir (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
1063 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
1067 const FcChar8
*file
, *base
;
1075 if (FcDebug () & FC_DBG_CACHE
)
1076 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
1078 f
= fopen ((char *) cache_file
, "w");
1081 if (FcDebug () & FC_DBG_CACHE
)
1082 printf (" can't create \"%s\"\n", cache_file
);
1086 list
= FcStrListCreate (dirs
);
1090 while ((dir
= FcStrListNext (list
)))
1092 base
= FcFileBaseName (cache_file
, dir
);
1093 if (!FcCacheWriteString (f
, base
))
1095 if (PUTC (' ', f
) == EOF
)
1097 if (!FcCacheWriteInt (f
, 0))
1099 if (PUTC (' ', f
) == EOF
)
1101 if (!FcCacheWriteString (f
, FC_FONT_FILE_DIR
))
1103 if (PUTC ('\n', f
) == EOF
)
1107 for (n
= 0; n
< set
->nfont
; n
++)
1109 font
= set
->fonts
[n
];
1110 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
1112 base
= FcFileBaseName (cache_file
, file
);
1113 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
1115 if (FcDebug () & FC_DBG_CACHEV
)
1116 printf (" write file \"%s\"\n", base
);
1117 if (!FcCacheWriteString (f
, base
))
1119 if (PUTC (' ', f
) == EOF
)
1121 if (!FcCacheWriteInt (f
, id
))
1123 if (PUTC (' ', f
) == EOF
)
1125 name
= FcNameUnparse (font
);
1128 ret
= FcCacheWriteString (f
, name
);
1132 if (PUTC ('\n', f
) == EOF
)
1136 FcStrListDone (list
);
1138 if (fclose (f
) == EOF
)
1141 FcStrFree (cache_file
);
1143 if (FcDebug () & FC_DBG_CACHE
)
1144 printf (" cache written\n");
1148 FcStrListDone (list
);
1152 unlink ((char *) cache_file
);
1153 FcStrFree (cache_file
);