]> git.wh0rd.org Git - fontconfig.git/blob - src/fccache.c
f1567da71f656be2a91521693526141502d64bf6
[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     cache->broken = FcFalse;
627     return cache;
628 }
629
630 void
631 FcGlobalCacheDestroy (FcGlobalCache *cache)
632 {
633     FcGlobalCacheDir    *d, *next;
634     int                 h;
635
636     for (h = 0; h < FC_GLOBAL_CACHE_DIR_HASH_SIZE; h++)
637     {
638         for (d = cache->ents[h]; d; d = next)
639         {
640             next = d->next;
641             FcGlobalCacheDirDestroy (d);
642         }
643     }
644     free (cache);
645 }
646
647 /*
648  * Cache file syntax is quite simple:
649  *
650  * "file_name" id time "font_name" \n
651  */
652  
653 void
654 FcGlobalCacheLoad (FcGlobalCache    *cache,
655                    const FcChar8    *cache_file)
656 {
657     FILE                *f;
658     FcChar8             file_buf[8192], *file;
659     int                 id;
660     time_t              time;
661     FcChar8             name_buf[8192], *name;
662     FcGlobalCacheInfo   *info;
663
664     f = fopen ((char *) cache_file, "r");
665     if (!f)
666         return;
667
668     cache->updated = FcFalse;
669     file = 0;
670     name = 0;
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))))
675     {
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);
680         else
681             info = FcGlobalCacheFileAdd (cache, file, id, time, name, FcFalse);
682         if (!info)
683             cache->broken = FcTrue;
684         else
685             cache->entries++;
686         if (FcDebug () & FC_DBG_CACHE_REF)
687             printf ("FcGlobalCacheLoad entry %d %s\n",
688                     cache->entries, file);
689         if (file != file_buf)
690             free (file);
691         if (name != name_buf)
692             free (name);
693         file = 0;
694         name = 0;
695     }
696     if (file && file != file_buf)
697         free (file);
698     if (name && name != name_buf)
699         free (name);
700     fclose (f);
701 }
702
703 FcBool
704 FcGlobalCacheUpdate (FcGlobalCache  *cache,
705                      const FcChar8  *file,
706                      int            id,
707                      const FcChar8  *name)
708 {
709     const FcChar8       *match;
710     struct stat         statb;
711     FcGlobalCacheInfo   *info;
712
713     match = file;
714
715     if (stat ((char *) file, &statb) < 0)
716         return FcFalse;
717     if (S_ISDIR (statb.st_mode))
718         info = FcGlobalCacheDirAdd (cache, file, statb.st_mtime, 
719                                    FcTrue);
720     else
721         info = FcGlobalCacheFileAdd (cache, file, id, statb.st_mtime, 
722                                     name, FcTrue);
723     if (info)
724     {
725         FcGlobalCacheReferenced (cache, info);
726         cache->updated = FcTrue;
727     }
728     else
729         cache->broken = FcTrue;
730     return info != 0;
731 }
732
733 FcBool
734 FcGlobalCacheSave (FcGlobalCache    *cache,
735                    const FcChar8    *cache_file)
736 {
737     FILE                *f;
738     int                 dir_hash, file_hash;
739     FcGlobalCacheDir    *dir;
740     FcGlobalCacheFile   *file;
741     FcAtomic            *atomic;
742
743     if (!cache->updated && cache->referenced == cache->entries)
744         return FcTrue;
745     
746     if (cache->broken)
747         return FcFalse;
748
749     /* Set-UID programs can't safely update the cache */
750     if (getuid () != geteuid ())
751         return FcFalse;
752     
753     atomic = FcAtomicCreate (cache_file);
754     if (!atomic)
755         goto bail0;
756     if (!FcAtomicLock (atomic))
757         goto bail1;
758     f = fopen ((char *) FcAtomicNewFile(atomic), "w");
759     if (!f)
760         goto bail2;
761
762     for (dir_hash = 0; dir_hash < FC_GLOBAL_CACHE_DIR_HASH_SIZE; dir_hash++)
763     {
764         for (dir = cache->ents[dir_hash]; dir; dir = dir->next)
765         {
766             if (!dir->info.referenced)
767                 continue;
768             if (!FcCacheWriteString (f, dir->info.file))
769                 goto bail4;
770             if (putc (' ', f) == EOF)
771                 goto bail4;
772             if (!FcCacheWriteInt (f, 0))
773                 goto bail4;
774             if (putc (' ', f) == EOF)
775                 goto bail4;
776             if (!FcCacheWriteTime (f, dir->info.time))
777                 goto bail4;
778             if (putc (' ', f) == EOF)
779                 goto bail4;
780             if (!FcCacheWriteString (f, (FcChar8 *) FC_FONT_FILE_DIR))
781                 goto bail4;
782             if (putc ('\n', f) == EOF)
783                 goto bail4;
784             
785             for (file_hash = 0; file_hash < FC_GLOBAL_CACHE_FILE_HASH_SIZE; file_hash++)
786             {
787                 for (file = dir->ents[file_hash]; file; file = file->next)
788                 {
789                     if (!file->info.referenced)
790                         continue;
791                     if (!FcCacheWritePath (f, dir->info.file, file->info.file))
792                         goto bail4;
793                     if (putc (' ', f) == EOF)
794                         goto bail4;
795                     if (!FcCacheWriteInt (f, file->id < 0 ? 0 : file->id))
796                         goto bail4;
797                     if (putc (' ', f) == EOF)
798                         goto bail4;
799                     if (!FcCacheWriteTime (f, file->info.time))
800                         goto bail4;
801                     if (putc (' ', f) == EOF)
802                         goto bail4;
803                     if (!FcCacheWriteString (f, file->name))
804                         goto bail4;
805                     if (putc ('\n', f) == EOF)
806                         goto bail4;
807                 }
808             }
809         }
810     }
811
812     if (fclose (f) == EOF)
813         goto bail3;
814     
815     if (!FcAtomicReplaceOrig (atomic))
816         goto bail3;
817     
818     FcAtomicUnlock (atomic);
819     FcAtomicDestroy (atomic);
820
821     cache->updated = FcFalse;
822     return FcTrue;
823
824 bail4:
825     fclose (f);
826 bail3:
827     FcAtomicDeleteNew (atomic);
828 bail2:
829     FcAtomicUnlock (atomic);
830 bail1:
831     FcAtomicDestroy (atomic);
832 bail0:
833     return FcFalse;
834 }
835
836 FcBool
837 FcDirCacheValid (const FcChar8 *dir)
838 {
839     FcChar8     *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
840     struct stat file_stat, dir_stat;
841
842     if (stat ((char *) dir, &dir_stat) < 0)
843     {
844         FcStrFree (cache_file);
845         return FcFalse;
846     }
847     if (stat ((char *) cache_file, &file_stat) < 0)
848     {
849         FcStrFree (cache_file);
850         return FcFalse;
851     }
852     FcStrFree (cache_file);
853     /*
854      * If the directory has been modified more recently than
855      * the cache file, the cache is not valid
856      */
857     if (dir_stat.st_mtime - file_stat.st_mtime > 0)
858         return FcFalse;
859     return FcTrue;
860 }
861
862 FcBool
863 FcDirCacheReadDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
864 {
865     FcChar8         *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
866     FILE            *f;
867     FcChar8         *base;
868     int             id;
869     int             dir_len;
870     FcChar8         file_buf[8192], *file;
871     FcChar8         name_buf[8192], *name;
872     FcBool          ret = FcFalse;
873
874     if (!cache_file)
875         goto bail0;
876     
877     if (FcDebug () & FC_DBG_CACHE)
878         printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file);
879     
880     f = fopen ((char *) cache_file, "r");
881     if (!f)
882     {
883         if (FcDebug () & FC_DBG_CACHE)
884             printf (" no cache file\n");
885         goto bail1;
886     }
887
888     if (!FcDirCacheValid (dir))
889     {
890         if (FcDebug () & FC_DBG_CACHE)
891             printf (" cache file older than directory\n");
892         goto bail2;
893     }
894     
895     base = (FcChar8 *) strrchr ((char *) cache_file, '/');
896     if (!base)
897         goto bail2;
898     base++;
899     dir_len = base - cache_file;
900     
901     file = 0;
902     name = 0;
903     while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
904            FcCacheReadInt (f, &id) &&
905            (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
906     {
907         if (!FcCacheFontSetAdd (set, dirs, cache_file, dir_len,
908                                 file, name))
909             goto bail3;
910         if (file != file_buf)
911             free (file);
912         if (name != name_buf)
913             free (name);
914         file = name = 0;
915     }
916     if (FcDebug () & FC_DBG_CACHE)
917         printf (" cache loaded\n");
918     
919     ret = FcTrue;
920 bail3:
921     if (file && file != file_buf)
922         free (file);
923     if (name && name != name_buf)
924         free (name);
925 bail2:
926     fclose (f);
927 bail1:
928     free (cache_file);
929 bail0:
930     return ret;
931 }
932
933 /*
934  * return the path from the directory containing 'cache' to 'file'
935  */
936
937 static const FcChar8 *
938 FcFileBaseName (const FcChar8 *cache, const FcChar8 *file)
939 {
940     const FcChar8   *cache_slash;
941
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);
946     return file;
947 }
948
949 FcBool
950 FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
951 {
952     FcChar8         *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
953     FcPattern       *font;
954     FILE            *f;
955     FcChar8         *name;
956     const FcChar8   *file, *base;
957     int             n;
958     int             id;
959     FcBool          ret;
960     FcStrList       *list;
961
962     if (!cache_file)
963         goto bail0;
964     if (FcDebug () & FC_DBG_CACHE)
965         printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
966     
967     f = fopen ((char *) cache_file, "w");
968     if (!f)
969     {
970         if (FcDebug () & FC_DBG_CACHE)
971             printf (" can't create \"%s\"\n", cache_file);
972         goto bail1;
973     }
974     
975     list = FcStrListCreate (dirs);
976     if (!list)
977         goto bail2;
978     
979     while ((dir = FcStrListNext (list)))
980     {
981         base = FcFileBaseName (cache_file, dir);
982         if (!FcCacheWriteString (f, base))
983             goto bail3;
984         if (putc (' ', f) == EOF)
985             goto bail3;
986         if (!FcCacheWriteInt (f, 0))
987             goto bail3;
988         if (putc (' ', f) == EOF)
989             goto bail3;
990         if (!FcCacheWriteString (f, FC_FONT_FILE_DIR))
991             goto bail3;
992         if (putc ('\n', f) == EOF)
993             goto bail3;
994     }
995     
996     for (n = 0; n < set->nfont; n++)
997     {
998         font = set->fonts[n];
999         if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
1000             goto bail3;
1001         base = FcFileBaseName (cache_file, file);
1002         if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
1003             goto bail3;
1004         if (FcDebug () & FC_DBG_CACHEV)
1005             printf (" write file \"%s\"\n", base);
1006         if (!FcCacheWriteString (f, base))
1007             goto bail3;
1008         if (putc (' ', f) == EOF)
1009             goto bail3;
1010         if (!FcCacheWriteInt (f, id))
1011             goto bail3;
1012         if (putc (' ', f) == EOF)
1013             goto bail3;
1014         name = FcNameUnparse (font);
1015         if (!name)
1016             goto bail3;
1017         ret = FcCacheWriteString (f, name);
1018         free (name);
1019         if (!ret)
1020             goto bail3;
1021         if (putc ('\n', f) == EOF)
1022             goto bail3;
1023     }
1024     
1025     FcStrListDone (list);
1026
1027     if (fclose (f) == EOF)
1028         goto bail1;
1029     
1030     free (cache_file);
1031
1032     if (FcDebug () & FC_DBG_CACHE)
1033         printf (" cache written\n");
1034     return FcTrue;
1035     
1036 bail3:
1037     FcStrListDone (list);
1038 bail2:
1039     fclose (f);
1040 bail1:
1041     unlink ((char *) cache_file);
1042     free (cache_file);
1043 bail0:
1044     return FcFalse;
1045 }