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