]> git.wh0rd.org Git - fontconfig.git/blob - src/fccache.c
e5e9d14eff729939d0bf9d58ba9b2b247952b39d
[fontconfig.git] / src / fccache.c
1 /*
2  * $XFree86: xc/lib/fontconfig/src/fccache.c,v 1.7 2002/05/21 17:06:22 keithp Exp $
3  *
4  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
5  *
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.
15  *
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.
23  */
24
25 #include "fcint.h"
26
27 #define FC_DBG_CACHE_REF    1024
28
29 static FcChar8 *
30 FcCacheReadString (FILE *f, FcChar8 *dest, int len)
31 {
32     int         c;
33     FcBool      escape;
34     FcChar8     *d;
35     int         size;
36     int         i;
37
38     while ((c = getc (f)) != EOF)
39         if (c == '"')
40             break;
41     if (c == EOF)
42         return FcFalse;
43     if (len == 0)
44         return FcFalse;
45     
46     size = len;
47     i = 0;
48     d = dest;
49     escape = FcFalse;
50     while ((c = getc (f)) != EOF)
51     {
52         if (!escape)
53         {
54             switch (c) {
55             case '"':
56                 c = '\0';
57                 break;
58             case '\\':
59                 escape = FcTrue;
60                 continue;
61             }
62         }
63         if (i == size)
64         {
65             FcChar8 *new = malloc (size * 2);
66             if (!new)
67                 break;
68             memcpy (new, d, size);
69             size *= 2;
70             if (d != dest)
71                 free (d);
72             d = new;
73         }
74         d[i++] = c;
75         if (c == '\0')
76             return d;
77         escape = FcFalse;
78     }
79     if (d != dest)
80         free (d);
81     return 0;
82 }
83
84 static FcBool
85 FcCacheReadUlong (FILE *f, unsigned long *dest)
86 {
87     unsigned long   t;
88     int             c;
89
90     while ((c = getc (f)) != EOF)
91     {
92         if (!isspace (c))
93             break;
94     }
95     if (c == EOF)
96         return FcFalse;
97     t = 0;
98     for (;;)
99     {
100         if (c == EOF || isspace (c))
101             break;
102         if (!isdigit (c))
103             return FcFalse;
104         t = t * 10 + (c - '0');
105         c = getc (f);
106     }
107     *dest = t;
108     return FcTrue;
109 }
110
111 static FcBool
112 FcCacheReadInt (FILE *f, int *dest)
113 {
114     unsigned long   t;
115     FcBool          ret;
116
117     ret = FcCacheReadUlong (f, &t);
118     if (ret)
119         *dest = (int) t;
120     return ret;
121 }
122
123 static FcBool
124 FcCacheReadTime (FILE *f, time_t *dest)
125 {
126     unsigned long   t;
127     FcBool          ret;
128
129     ret = FcCacheReadUlong (f, &t);
130     if (ret)
131         *dest = (time_t) t;
132     return ret;
133 }
134
135 static FcBool
136 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
137 {
138     FcChar8    c;
139     while ((c = *chars++))
140     {
141         switch (c) {
142         case '"':
143         case '\\':
144             if (putc ('\\', f) == EOF)
145                 return FcFalse;
146             /* fall through */
147         default:
148             if (putc (c, f) == EOF)
149                 return FcFalse;
150         }
151     }
152     return FcTrue;
153 }
154
155 static FcBool
156 FcCacheWriteString (FILE *f, const FcChar8 *string)
157 {
158
159     if (putc ('"', f) == EOF)
160         return FcFalse;
161     if (!FcCacheWriteChars (f, string))
162         return FcFalse;
163     if (putc ('"', f) == EOF)
164         return FcFalse;
165     return FcTrue;
166 }
167
168 static FcBool
169 FcCacheWritePath (FILE *f, const FcChar8 *dir, const FcChar8 *file)
170 {
171     if (putc ('"', f) == EOF)
172         return FcFalse;
173     if (dir)
174         if (!FcCacheWriteChars (f, dir))
175             return FcFalse;
176     if (dir && dir[strlen((const char *) dir) - 1] != '/')
177         if (putc ('/', f) == EOF)
178             return FcFalse;
179     if (!FcCacheWriteChars (f, file))
180         return FcFalse;
181     if (putc ('"', f) == EOF)
182         return FcFalse;
183     return FcTrue;
184 }
185
186 static FcBool
187 FcCacheWriteUlong (FILE *f, unsigned long t)
188 {
189     int     pow;
190     unsigned long   temp, digit;
191
192     temp = t;
193     pow = 1;
194     while (temp >= 10)
195     {
196         temp /= 10;
197         pow *= 10;
198     }
199     temp = t;
200     while (pow)
201     {
202         digit = temp / pow;
203         if (putc ((char) digit + '0', f) == EOF)
204             return FcFalse;
205         temp = temp - pow * digit;
206         pow = pow / 10;
207     }
208     return FcTrue;
209 }
210
211 static FcBool
212 FcCacheWriteInt (FILE *f, int i)
213 {
214     return FcCacheWriteUlong (f, (unsigned long) i);
215 }
216
217 static FcBool
218 FcCacheWriteTime (FILE *f, time_t t)
219 {
220     return FcCacheWriteUlong (f, (unsigned long) t);
221 }
222
223 static FcBool
224 FcCacheFontSetAdd (FcFontSet        *set,
225                    FcStrSet         *dirs,
226                    const FcChar8    *dir,
227                    int              dir_len,
228                    const FcChar8    *file,
229                    const FcChar8    *name)
230 {
231     FcChar8     path_buf[8192], *path;
232     int         len;
233     FcBool      ret = FcFalse;
234     FcPattern   *font;
235
236     path = path_buf;
237     len = (dir_len + 1 + strlen ((const char *) file) + 1);
238     if (len > sizeof (path_buf))
239     {
240         path = malloc (len);
241         if (!path)
242             return FcFalse;
243     }
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))
249     {
250         if (FcDebug () & FC_DBG_CACHEV)
251             printf (" dir cache dir \"%s\"\n", path);
252         ret = FcStrSetAdd (dirs, path);
253     }
254     else if (!FcStrCmp (name, FC_FONT_FILE_INVALID))
255     {
256         ret = FcTrue;
257     }
258     else
259     {
260         font = FcNameParse (name);
261         if (font)
262         {
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));
267             if (!ret)
268                 FcPatternDestroy (font);
269         }
270     }
271     if (path != path_buf) free (path);
272     return ret;
273     
274 }
275
276 static unsigned int
277 FcCacheHash (const FcChar8 *string)
278 {
279     unsigned int    h = 0;
280     FcChar8         c;
281
282     while ((c = *string++))
283         h = (h << 1) ^ c;
284     return 0;
285 }
286
287 /*
288  * Verify the saved timestamp for a file
289  */
290 FcBool
291 FcGlobalCacheCheckTime (FcGlobalCacheInfo *info)
292 {
293     struct stat     statb;
294
295     if (stat ((char *) info->file, &statb) < 0)
296     {
297         if (FcDebug () & FC_DBG_CACHE)
298             printf (" file missing\n");
299         return FcFalse;
300     }
301     if (statb.st_mtime != info->time)
302     {
303         if (FcDebug () & FC_DBG_CACHE)
304             printf (" timestamp mismatch (was %d is %d)\n",
305                     (int) info->time, (int) statb.st_mtime);
306         return FcFalse;
307     }
308     return FcTrue;
309 }
310
311 void
312 FcGlobalCacheReferenced (FcGlobalCache      *cache,
313                          FcGlobalCacheInfo  *info)
314 {
315     if (!info->referenced)
316     {
317         info->referenced = FcTrue;
318         cache->referenced++;
319         if (FcDebug () & FC_DBG_CACHE_REF)
320             printf ("Reference %d %s\n", cache->referenced, info->file);
321     }
322 }
323
324 /*
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
328  */
329
330 typedef struct _FcFilePathInfo {
331     const FcChar8   *dir;
332     int             dir_len;
333     const FcChar8   *base;
334     unsigned int    base_hash;
335 } FcFilePathInfo;
336
337 static FcFilePathInfo
338 FcFilePathInfoGet (const FcChar8    *path)
339 {
340     FcFilePathInfo  i;
341     FcChar8         *slash;
342
343     slash = (FcChar8 *) strrchr ((const char *) path, '/');
344     if (slash)
345     {
346         i.dir = path;
347         i.dir_len = slash - path;
348         if (!i.dir_len)
349             i.dir_len = 1;
350         i.base = slash + 1;
351     }
352     else
353     {
354         i.dir = (const FcChar8 *) ".";
355         i.dir_len = 1;
356         i.base = path;
357     }
358     i.base_hash = FcCacheHash (i.base);
359     return i;
360 }
361
362 FcGlobalCacheDir *
363 FcGlobalCacheDirGet (FcGlobalCache  *cache,
364                      const FcChar8  *dir,
365                      int            len,
366                      FcBool         create_missing)
367 {
368     unsigned int        hash = FcCacheHash (dir);
369     FcGlobalCacheDir    *d, **prev;
370
371     for (prev = &cache->ents[hash % FC_GLOBAL_CACHE_DIR_HASH_SIZE];
372          (d = *prev);
373          prev = &(*prev)->next)
374     {
375         if (d->info.hash == hash && d->len == len &&
376             !strncmp ((const char *) d->info.file,
377                       (const char *) dir, len))
378             break;
379     }
380     if (!(d = *prev))
381     {
382         int     i;
383         if (!create_missing)
384             return 0;
385         d = malloc (sizeof (FcGlobalCacheDir) + len + 1);
386         if (!d)
387             return 0;
388         d->next = *prev;
389         *prev = d;
390         d->info.hash = hash;
391         d->info.file = (FcChar8 *) (d + 1);
392         strncpy ((char *) d->info.file, (const char *) dir, len);
393         d->info.file[len] = '\0';
394         d->info.time = 0;
395         d->info.referenced = FcFalse;
396         d->len = len;
397         for (i = 0; i < FC_GLOBAL_CACHE_FILE_HASH_SIZE; i++)
398             d->ents[i] = 0;
399         d->subdirs = 0;
400     }
401     return d;
402 }
403
404 static FcGlobalCacheInfo *
405 FcGlobalCacheDirAdd (FcGlobalCache  *cache,
406                      const FcChar8  *dir,
407                      time_t         time,
408                      FcBool         replace)
409 {
410     FcGlobalCacheDir    *d;
411     FcFilePathInfo      i;
412     FcGlobalCacheSubdir *subdir;
413     FcGlobalCacheDir    *parent;
414
415     /*
416      * Add this directory to the cache
417      */
418     d = FcGlobalCacheDirGet (cache, dir, strlen ((const char *) dir), FcTrue);
419     if (!d)
420         return 0;
421     d->info.time = time;
422     i = FcFilePathInfoGet (dir);
423     /*
424      * Add this directory to the subdirectory list of the parent
425      */
426     parent = FcGlobalCacheDirGet (cache, i.dir, i.dir_len, FcTrue);
427     if (!parent)
428         return 0;
429     subdir = malloc (sizeof (FcGlobalCacheSubdir) + 
430                      strlen ((const char *) i.base) + 1);
431     if (!subdir)
432         return 0;
433     subdir->file = (FcChar8 *) (subdir + 1);
434     strcpy ((char *) subdir->file, (const char *) i.base);
435     subdir->next = parent->subdirs;
436     parent->subdirs = subdir;
437     return &d->info;
438 }
439
440 static void
441 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
442 {
443     FcGlobalCacheFile   *f, *next;
444     int                 h;
445     FcGlobalCacheSubdir *s, *nexts;
446
447     for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
448         for (f = d->ents[h]; f; f = next)
449         {
450             next = f->next;
451             free (f);
452         }
453     for (s = d->subdirs; s; s = nexts)
454     {
455         nexts = s->next;
456         free (s);
457     }
458     free (d);
459 }
460
461 FcBool
462 FcGlobalCacheScanDir (FcFontSet         *set,
463                       FcStrSet          *dirs,
464                       FcGlobalCache     *cache,
465                       const FcChar8     *dir)
466 {
467     FcGlobalCacheDir    *d = FcGlobalCacheDirGet (cache, dir,
468                                                   strlen ((const char *) dir),
469                                                   FcFalse);
470     FcGlobalCacheFile   *f;
471     int                 h;
472     int                 dir_len;
473     FcGlobalCacheSubdir *subdir;
474
475     if (FcDebug() & FC_DBG_CACHE)
476         printf ("FcGlobalCacheScanDir %s\n", dir);
477     
478     if (!d)
479     {
480         if (FcDebug () & FC_DBG_CACHE)
481             printf ("\tNo dir cache entry\n");
482         return FcFalse;
483     }
484
485     if (!FcGlobalCacheCheckTime (&d->info))
486     {
487         if (FcDebug () & FC_DBG_CACHE)
488             printf ("\tdir cache entry time mismatch\n");
489         return FcFalse;
490     }
491
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)
495         {
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))
500             {
501                 cache->broken = FcTrue;
502                 return FcFalse;
503             }
504             FcGlobalCacheReferenced (cache, &f->info);
505         }
506     for (subdir = d->subdirs; subdir; subdir = subdir->next)
507     {
508         if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
509                                 subdir->file, FC_FONT_FILE_DIR))
510         {
511             cache->broken = FcTrue;
512             return FcFalse;
513         }
514     }
515     
516     FcGlobalCacheReferenced (cache, &d->info);
517
518     return FcTrue;
519 }
520
521 /*
522  * Locate the cache entry for a particular file
523  */
524 FcGlobalCacheFile *
525 FcGlobalCacheFileGet (FcGlobalCache *cache,
526                       const FcChar8 *file,
527                       int           id,
528                       int           *count)
529 {
530     FcFilePathInfo      i = FcFilePathInfoGet (file);
531     FcGlobalCacheDir    *d = FcGlobalCacheDirGet (cache, i.dir, 
532                                                   i.dir_len, FcFalse);
533     FcGlobalCacheFile   *f, *match = 0;
534     int                 max = -1;
535
536     if (!d)
537         return 0;
538     for (f = d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE]; f; f = f->next)
539     {
540         if (f->info.hash == i.base_hash &&
541             !strcmp ((const char *) f->info.file, (const char *) i.base))
542         {
543             if (f->id == id)
544                 match = f;
545             if (f->id > max)
546                 max = f->id;
547         }
548     }
549     if (count)
550         *count = max;
551     return match;
552 }
553     
554 /*
555  * Add a file entry to the cache
556  */
557 static FcGlobalCacheInfo *
558 FcGlobalCacheFileAdd (FcGlobalCache *cache,
559                       const FcChar8 *path,
560                       int           id,
561                       time_t        time,
562                       const FcChar8 *name,
563                       FcBool        replace)
564 {
565     FcFilePathInfo      i = FcFilePathInfoGet (path);
566     FcGlobalCacheDir    *d = FcGlobalCacheDirGet (cache, i.dir, 
567                                                   i.dir_len, FcTrue);
568     FcGlobalCacheFile   *f, **prev;
569
570     if (!d)
571         return 0;
572     for (prev = &d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE];
573          (f = *prev);
574          prev = &(*prev)->next)
575     {
576         if (f->info.hash == i.base_hash && 
577             f->id == id &&
578             !strcmp ((const char *) f->info.file, (const char *) i.base))
579         {
580             break;
581         }
582     }
583     if (*prev)
584     {
585         if (!replace)
586             return 0;
587
588         f = *prev;
589         if (f->info.referenced)
590             cache->referenced--;
591         *prev = f->next;
592         free (f);
593     }
594     f = malloc (sizeof (FcGlobalCacheFile) +
595                 strlen ((char *) i.base) + 1 +
596                 strlen ((char *) name) + 1);
597     if (!f)
598         return 0;
599     f->next = *prev;
600     *prev = f;
601     f->info.hash = i.base_hash;
602     f->info.file = (FcChar8 *) (f + 1);
603     f->info.time = time;
604     f->info.referenced = FcFalse;
605     f->id = id;
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);
609     return &f->info;
610 }
611
612 FcGlobalCache *
613 FcGlobalCacheCreate (void)
614 {
615     FcGlobalCache   *cache;
616     int             h;
617
618     cache = malloc (sizeof (FcGlobalCache));
619     if (!cache)
620         return 0;
621     for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
622         cache->ents[h] = 0;
623     cache->entries = 0;
624     cache->referenced = 0;
625     cache->updated = FcFalse;
626     return cache;
627 }
628
629 void
630 FcGlobalCacheDestroy (FcGlobalCache *cache)
631 {
632     FcGlobalCacheDir    *d, *next;
633     int                 h;
634
635     for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
636     {
637         for (d = cache->ents[h]; d; d = next)
638         {
639             next = d->next;
640             FcGlobalCacheDirDestroy (d);
641         }
642     }
643     free (cache);
644 }
645
646 /*
647  * Cache file syntax is quite simple:
648  *
649  * "file_name" id time "font_name" \n
650  */
651  
652 void
653 FcGlobalCacheLoad (FcGlobalCache    *cache,
654                    const FcChar8    *cache_file)
655 {
656     FILE                *f;
657     FcChar8             file_buf[8192], *file;
658     int                 id;
659     time_t              time;
660     FcChar8             name_buf[8192], *name;
661     FcGlobalCacheInfo   *info;
662
663     f = fopen ((char *) cache_file, "r");
664     if (!f)
665         return;
666
667     cache->updated = FcFalse;
668     file = 0;
669     name = 0;
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))))
674     {
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);
679         else
680             info = FcGlobalCacheFileAdd (cache, file, id, time, name, FcFalse);
681         if (!info)
682             cache->broken = FcTrue;
683         else
684             cache->entries++;
685         if (FcDebug () & FC_DBG_CACHE_REF)
686             printf ("FcGlobalCacheLoad entry %d %s\n",
687                     cache->entries, file);
688         if (file != file_buf)
689             free (file);
690         if (name != name_buf)
691             free (name);
692         file = 0;
693         name = 0;
694     }
695     if (file && file != file_buf)
696         free (file);
697     if (name && name != name_buf)
698         free (name);
699     fclose (f);
700 }
701
702 FcBool
703 FcGlobalCacheUpdate (FcGlobalCache  *cache,
704                      const FcChar8  *file,
705                      int            id,
706                      const FcChar8  *name)
707 {
708     const FcChar8       *match;
709     struct stat         statb;
710     FcGlobalCacheInfo   *info;
711
712     match = file;
713
714     if (stat ((char *) file, &statb) < 0)
715         return FcFalse;
716     if (S_ISDIR (statb.st_mode))
717         info = FcGlobalCacheDirAdd (cache, file, statb.st_mtime, 
718                                    FcTrue);
719     else
720         info = FcGlobalCacheFileAdd (cache, file, id, statb.st_mtime, 
721                                     name, FcTrue);
722     if (info)
723     {
724         FcGlobalCacheReferenced (cache, info);
725         cache->updated = FcTrue;
726     }
727     else
728         cache->broken = FcTrue;
729     return info != 0;
730 }
731
732 FcBool
733 FcGlobalCacheSave (FcGlobalCache    *cache,
734                    const FcChar8    *cache_file)
735 {
736     FILE                *f;
737     int                 dir_hash, file_hash;
738     FcGlobalCacheDir    *dir;
739     FcGlobalCacheFile   *file;
740     FcAtomic            *atomic;
741
742     if (!cache->updated && cache->referenced == cache->entries)
743         return FcTrue;
744     
745     if (cache->broken)
746         return FcFalse;
747
748     /* Set-UID programs can't safely update the cache */
749     if (getuid () != geteuid ())
750         return FcFalse;
751     
752     atomic = FcAtomicCreate (cache_file);
753     if (!atomic)
754         goto bail0;
755     if (!FcAtomicLock (atomic))
756         goto bail1;
757     f = fopen ((char *) FcAtomicNewFile(atomic), "w");
758     if (!f)
759         goto bail2;
760
761     for (dir_hash = 0; dir_hash < FC_GLOBAL_CACHE_DIR_HASH_SIZE; dir_hash++)
762     {
763         for (dir = cache->ents[dir_hash]; dir; dir = dir->next)
764         {
765             if (!dir->info.referenced)
766                 continue;
767             if (!FcCacheWriteString (f, dir->info.file))
768                 goto bail4;
769             if (putc (' ', f) == EOF)
770                 goto bail4;
771             if (!FcCacheWriteInt (f, 0))
772                 goto bail4;
773             if (putc (' ', f) == EOF)
774                 goto bail4;
775             if (!FcCacheWriteTime (f, dir->info.time))
776                 goto bail4;
777             if (putc (' ', f) == EOF)
778                 goto bail4;
779             if (!FcCacheWriteString (f, (FcChar8 *) FC_FONT_FILE_DIR))
780                 goto bail4;
781             if (putc ('\n', f) == EOF)
782                 goto bail4;
783             
784             for (file_hash = 0; file_hash < FC_GLOBAL_CACHE_FILE_HASH_SIZE; file_hash++)
785             {
786                 for (file = dir->ents[file_hash]; file; file = file->next)
787                 {
788                     if (!file->info.referenced)
789                         continue;
790                     if (!FcCacheWritePath (f, dir->info.file, file->info.file))
791                         goto bail4;
792                     if (putc (' ', f) == EOF)
793                         goto bail4;
794                     if (!FcCacheWriteInt (f, file->id < 0 ? 0 : file->id))
795                         goto bail4;
796                     if (putc (' ', f) == EOF)
797                         goto bail4;
798                     if (!FcCacheWriteTime (f, file->info.time))
799                         goto bail4;
800                     if (putc (' ', f) == EOF)
801                         goto bail4;
802                     if (!FcCacheWriteString (f, file->name))
803                         goto bail4;
804                     if (putc ('\n', f) == EOF)
805                         goto bail4;
806                 }
807             }
808         }
809     }
810
811     if (fclose (f) == EOF)
812         goto bail3;
813     
814     if (!FcAtomicReplaceOrig (atomic))
815         goto bail3;
816     
817     FcAtomicUnlock (atomic);
818     FcAtomicDestroy (atomic);
819
820     cache->updated = FcFalse;
821     return FcTrue;
822
823 bail4:
824     fclose (f);
825 bail3:
826     FcAtomicDeleteNew (atomic);
827 bail2:
828     FcAtomicUnlock (atomic);
829 bail1:
830     FcAtomicDestroy (atomic);
831 bail0:
832     return FcFalse;
833 }
834
835 FcBool
836 FcDirCacheValid (const FcChar8 *dir)
837 {
838     FcChar8     *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
839     struct stat file_stat, dir_stat;
840
841     if (stat ((char *) dir, &dir_stat) < 0)
842     {
843         FcStrFree (cache_file);
844         return FcFalse;
845     }
846     if (stat ((char *) cache_file, &file_stat) < 0)
847     {
848         FcStrFree (cache_file);
849         return FcFalse;
850     }
851     FcStrFree (cache_file);
852     /*
853      * If the directory has been modified more recently than
854      * the cache file, the cache is not valid
855      */
856     if (dir_stat.st_mtime - file_stat.st_mtime > 0)
857         return FcFalse;
858     return FcTrue;
859 }
860
861 FcBool
862 FcDirCacheReadDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
863 {
864     FcChar8         *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
865     FILE            *f;
866     FcChar8         *base;
867     int             id;
868     int             dir_len;
869     FcChar8         file_buf[8192], *file;
870     FcChar8         name_buf[8192], *name;
871     FcBool          ret = FcFalse;
872
873     if (!cache_file)
874         goto bail0;
875     
876     if (FcDebug () & FC_DBG_CACHE)
877         printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file);
878     
879     f = fopen ((char *) cache_file, "r");
880     if (!f)
881     {
882         if (FcDebug () & FC_DBG_CACHE)
883             printf (" no cache file\n");
884         goto bail1;
885     }
886
887     if (!FcDirCacheValid (dir))
888     {
889         if (FcDebug () & FC_DBG_CACHE)
890             printf (" cache file older than directory\n");
891         goto bail2;
892     }
893     
894     base = (FcChar8 *) strrchr ((char *) cache_file, '/');
895     if (!base)
896         goto bail2;
897     base++;
898     dir_len = base - cache_file;
899     
900     file = 0;
901     name = 0;
902     while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
903            FcCacheReadInt (f, &id) &&
904            (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
905     {
906         if (!FcCacheFontSetAdd (set, dirs, cache_file, dir_len,
907                                 file, name))
908             goto bail3;
909         if (file != file_buf)
910             free (file);
911         if (name != name_buf)
912             free (name);
913         file = name = 0;
914     }
915     if (FcDebug () & FC_DBG_CACHE)
916         printf (" cache loaded\n");
917     
918     ret = FcTrue;
919 bail3:
920     if (file && file != file_buf)
921         free (file);
922     if (name && name != name_buf)
923         free (name);
924 bail2:
925     fclose (f);
926 bail1:
927     free (cache_file);
928 bail0:
929     return ret;
930 }
931
932 /*
933  * return the path from the directory containing 'cache' to 'file'
934  */
935
936 static const FcChar8 *
937 FcFileBaseName (const FcChar8 *cache, const FcChar8 *file)
938 {
939     const FcChar8   *cache_slash;
940
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);
945     return file;
946 }
947
948 FcBool
949 FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
950 {
951     FcChar8         *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
952     FcPattern       *font;
953     FILE            *f;
954     FcChar8         *name;
955     const FcChar8   *file, *base;
956     int             n;
957     int             id;
958     FcBool          ret;
959     FcStrList       *list;
960
961     if (!cache_file)
962         goto bail0;
963     if (FcDebug () & FC_DBG_CACHE)
964         printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
965     
966     f = fopen ((char *) cache_file, "w");
967     if (!f)
968     {
969         if (FcDebug () & FC_DBG_CACHE)
970             printf (" can't create \"%s\"\n", cache_file);
971         goto bail1;
972     }
973     
974     list = FcStrListCreate (dirs);
975     if (!list)
976         goto bail2;
977     
978     while ((dir = FcStrListNext (list)))
979     {
980         base = FcFileBaseName (cache_file, dir);
981         if (!FcCacheWriteString (f, base))
982             goto bail3;
983         if (putc (' ', f) == EOF)
984             goto bail3;
985         if (!FcCacheWriteInt (f, 0))
986             goto bail3;
987         if (putc (' ', f) == EOF)
988             goto bail3;
989         if (!FcCacheWriteString (f, FC_FONT_FILE_DIR))
990             goto bail3;
991         if (putc ('\n', f) == EOF)
992             goto bail3;
993     }
994     
995     for (n = 0; n < set->nfont; n++)
996     {
997         font = set->fonts[n];
998         if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
999             goto bail3;
1000         base = FcFileBaseName (cache_file, file);
1001         if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
1002             goto bail3;
1003         if (FcDebug () & FC_DBG_CACHEV)
1004             printf (" write file \"%s\"\n", base);
1005         if (!FcCacheWriteString (f, base))
1006             goto bail3;
1007         if (putc (' ', f) == EOF)
1008             goto bail3;
1009         if (!FcCacheWriteInt (f, id))
1010             goto bail3;
1011         if (putc (' ', f) == EOF)
1012             goto bail3;
1013         name = FcNameUnparse (font);
1014         if (!name)
1015             goto bail3;
1016         ret = FcCacheWriteString (f, name);
1017         free (name);
1018         if (!ret)
1019             goto bail3;
1020         if (putc ('\n', f) == EOF)
1021             goto bail3;
1022     }
1023     
1024     FcStrListDone (list);
1025
1026     if (fclose (f) == EOF)
1027         goto bail1;
1028     
1029     free (cache_file);
1030
1031     if (FcDebug () & FC_DBG_CACHE)
1032         printf (" cache written\n");
1033     return FcTrue;
1034     
1035 bail3:
1036     FcStrListDone (list);
1037 bail2:
1038     fclose (f);
1039 bail1:
1040     unlink ((char *) cache_file);
1041     free (cache_file);
1042 bail0:
1043     return FcFalse;
1044 }