]> git.wh0rd.org - fontconfig.git/blob - src/fccache.c
Really fix the global cache: make sure we're reading and writing the same
[fontconfig.git] / src / fccache.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
3 *
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2005 Patrick Lam
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Keith Packard not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Keith Packard makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
16 *
17 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
24 */
25
26 #include <fcntl.h>
27 #include <dirent.h>
28 #include <sys/mman.h>
29 #include <sys/utsname.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include "fcint.h"
33
34 #define ENDIAN_TEST 0x12345678
35 #define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1
36
37 static off_t
38 FcCacheSkipToArch (int fd, const char * arch);
39
40 static FcBool
41 FcCacheMoveDown (int fd, off_t start);
42
43 static void *
44 FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
45
46 static FcBool
47 FcDirCacheConsume (int fd, FcFontSet *set);
48
49 static FcBool
50 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
51
52 static int
53 FcCacheNextOffset(off_t w);
54
55 static char *
56 FcCacheMachineSignature (void);
57
58 static FcBool
59 FcCacheHaveBank (int bank);
60
61 #define FC_DBG_CACHE_REF 1024
62
63 static FcChar8 *
64 FcCacheReadString (int fd, FcChar8 *dest, int len)
65 {
66 FcChar8 c;
67 FcBool escape;
68 int size;
69 int i;
70
71 if (len == 0)
72 return 0;
73
74 size = len;
75 i = 0;
76 escape = FcFalse;
77 while (read (fd, &c, 1) == 1)
78 {
79 if (!escape)
80 {
81 switch (c) {
82 case '"':
83 c = '\0';
84 break;
85 case '\\':
86 escape = FcTrue;
87 continue;
88 }
89 }
90 if (i == size)
91 {
92 dest[i++] = 0;
93 return dest;
94 }
95 dest[i++] = c;
96 if (c == '\0')
97 return dest;
98 escape = FcFalse;
99 }
100 return 0;
101 }
102
103 static FcBool
104 FcCacheWriteString (int fd, const FcChar8 *chars)
105 {
106 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
107 return FcFalse;
108 return FcTrue;
109 }
110
111 static void
112 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
113 {
114 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
115 free (d->name);
116 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
117 free (d);
118 }
119
120 FcGlobalCache *
121 FcGlobalCacheCreate (void)
122 {
123 FcGlobalCache *cache;
124
125 cache = malloc (sizeof (FcGlobalCache));
126 if (!cache)
127 return 0;
128 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
129 cache->dirs = 0;
130 cache->updated = FcFalse;
131 cache->fd = -1;
132 return cache;
133 }
134
135 void
136 FcGlobalCacheDestroy (FcGlobalCache *cache)
137 {
138 FcGlobalCacheDir *d, *next;
139
140 for (d = cache->dirs; d; d = next)
141 {
142 next = d->next;
143 FcGlobalCacheDirDestroy (d);
144 }
145 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
146 free (cache);
147 }
148
149 void
150 FcGlobalCacheLoad (FcGlobalCache *cache,
151 FcStrSet *staleDirs,
152 const FcChar8 *cache_file)
153 {
154 FcChar8 name_buf[8192];
155 FcGlobalCacheDir *d, *next;
156 char * current_arch_machine_name;
157 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
158 off_t current_arch_start;
159
160 struct stat cache_stat, dir_stat;
161
162 if (stat ((char *) cache_file, &cache_stat) < 0)
163 return;
164
165 cache->fd = open ((char *) cache_file, O_RDONLY);
166 if (cache->fd == -1)
167 return;
168
169 cache->updated = FcFalse;
170
171 current_arch_machine_name = FcCacheMachineSignature ();
172 current_arch_start = FcCacheSkipToArch(cache->fd,
173 current_arch_machine_name);
174 if (current_arch_start < 0)
175 goto bail0;
176
177 lseek (cache->fd, current_arch_start, SEEK_SET);
178 FcCacheReadString (cache->fd, candidate_arch_machine_name,
179 sizeof (candidate_arch_machine_name));
180 if (strlen(candidate_arch_machine_name) == 0)
181 goto bail0;
182
183 while (1)
184 {
185 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
186 if (!strlen(name_buf))
187 break;
188
189 if (stat ((char *) name_buf, &dir_stat) < 0 ||
190 dir_stat.st_mtime > cache_stat.st_mtime)
191 {
192 FcCache md;
193
194 FcStrSetAdd (staleDirs, FcStrCopy (name_buf));
195 read (cache->fd, &md, sizeof (FcCache));
196 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
197 continue;
198 }
199
200 d = malloc (sizeof (FcGlobalCacheDir));
201 if (!d)
202 goto bail1;
203
204 d->next = cache->dirs;
205 cache->dirs = d;
206
207 d->name = FcStrCopy (name_buf);
208 d->ent = 0;
209 d->offset = lseek (cache->fd, 0, SEEK_CUR);
210 read (cache->fd, &d->metadata, sizeof (FcCache));
211 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count, SEEK_SET);
212 }
213 return;
214
215 bail1:
216 for (d = cache->dirs; d; d = next)
217 {
218 next = d->next;
219 free (d);
220 }
221 cache->dirs = 0;
222 bail0:
223 close (cache->fd);
224 cache->fd = -1;
225 return;
226 }
227
228 FcBool
229 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const FcChar8 *dir, FcConfig *config)
230 {
231 FcGlobalCacheDir *d;
232 FcBool ret = FcFalse;
233
234 if (cache->fd == -1)
235 return FcFalse;
236
237 for (d = cache->dirs; d; d = d->next)
238 {
239 if (strncmp (d->name, dir, strlen(dir)) == 0)
240 {
241 lseek (cache->fd, d->offset, SEEK_SET);
242 if (!FcDirCacheConsume (cache->fd, set))
243 return FcFalse;
244 if (strcmp (d->name, dir) == 0)
245 ret = FcTrue;
246 }
247 }
248
249 return ret;
250 }
251
252 FcBool
253 FcGlobalCacheUpdate (FcGlobalCache *cache,
254 const FcChar8 *name,
255 FcFontSet *set)
256 {
257 FcGlobalCacheDir * d;
258
259 if (!set->nfont)
260 return FcTrue;
261
262 for (d = cache->dirs; d; d = d->next)
263 {
264 if (strcmp(d->name, name) == 0)
265 break;
266 }
267
268 if (!d)
269 {
270 d = malloc (sizeof (FcGlobalCacheDir));
271 if (!d)
272 return FcFalse;
273 d->next = cache->dirs;
274 cache->dirs = d;
275 }
276
277 cache->updated = FcTrue;
278
279 d->name = FcStrCopy (name);
280 d->ent = FcDirCacheProduce (set, &d->metadata);
281 d->offset = 0;
282 return FcTrue;
283 }
284
285 FcBool
286 FcGlobalCacheSave (FcGlobalCache *cache,
287 const FcChar8 *cache_file)
288 {
289 int fd;
290 FcGlobalCacheDir *dir;
291 FcAtomic *atomic;
292 off_t current_arch_start = 0, truncate_to;
293 char * current_arch_machine_name, * header;
294
295 if (!cache->updated)
296 return FcTrue;
297
298 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
299 /* Set-UID programs can't safely update the cache */
300 if (getuid () != geteuid ())
301 return FcFalse;
302 #endif
303
304 atomic = FcAtomicCreate (cache_file);
305 if (!atomic)
306 goto bail0;
307 if (!FcAtomicLock (atomic))
308 goto bail1;
309 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
310 S_IRUSR | S_IWUSR);
311 if (fd == -1)
312 goto bail2;
313
314 current_arch_machine_name = FcCacheMachineSignature ();
315 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
316 if (current_arch_start < 0)
317 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
318
319 if (!FcCacheMoveDown(fd, current_arch_start))
320 goto bail2;
321
322 current_arch_start = lseek(fd, 0, SEEK_CUR);
323 if (ftruncate (fd, current_arch_start) == -1)
324 goto bail2;
325
326 header = malloc (10 + strlen (current_arch_machine_name));
327 if (!header)
328 goto bail1;
329 sprintf (header, "%8x ", (int)truncate_to);
330 strcat (header, current_arch_machine_name);
331 if (!FcCacheWriteString (fd, header))
332 goto bail1;
333
334 truncate_to = current_arch_start + strlen(header) + 1;
335 for (dir = cache->dirs; dir; dir = dir->next)
336 {
337 truncate_to += strlen(dir->name) + 1;
338 truncate_to += sizeof (FcCache);
339 truncate_to = FcCacheNextOffset (current_arch_start + truncate_to);
340 truncate_to += dir->metadata.count;
341 }
342 truncate_to -= current_arch_start;
343
344 for (dir = cache->dirs; dir; dir = dir->next)
345 {
346 if (dir->ent)
347 {
348 FcCacheWriteString (fd, dir->name);
349 write (fd, &dir->metadata, sizeof(FcCache));
350 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
351 write (fd, dir->ent, dir->metadata.count);
352 free (dir->ent);
353 }
354 }
355 FcCacheWriteString (fd, "");
356
357 if (close (fd) == -1)
358 goto bail3;
359
360 if (!FcAtomicReplaceOrig (atomic))
361 goto bail3;
362
363 FcAtomicUnlock (atomic);
364 FcAtomicDestroy (atomic);
365
366 cache->updated = FcFalse;
367 return FcTrue;
368
369 bail3:
370 FcAtomicDeleteNew (atomic);
371 bail2:
372 FcAtomicUnlock (atomic);
373 bail1:
374 FcAtomicDestroy (atomic);
375 bail0:
376 return FcFalse;
377 }
378
379 #define PAGESIZE 8192
380 /*
381 * Find the next presumably-mmapable offset after the supplied file
382 * position.
383 */
384 static int
385 FcCacheNextOffset(off_t w)
386 {
387 if (w % PAGESIZE == 0)
388 return w;
389 else
390 return ((w / PAGESIZE)+1)*PAGESIZE;
391 }
392
393 /* return the address of the segment for the provided arch,
394 * or -1 if arch not found */
395 static off_t
396 FcCacheSkipToArch (int fd, const char * arch)
397 {
398 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
399 char * candidate_arch;
400 off_t current_arch_start = 0;
401
402 /* skip arches that are not the current arch */
403 while (1)
404 {
405 long bs;
406
407 lseek (fd, current_arch_start, SEEK_SET);
408 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
409 sizeof (candidate_arch_machine_name_count)) == 0)
410 return -1;
411 if (!strlen(candidate_arch_machine_name_count))
412 return -1;
413 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
414 candidate_arch++; /* skip leading space */
415
416 if (strcmp (candidate_arch, arch)==0)
417 return current_arch_start;
418 current_arch_start += bs;
419 }
420
421 return -1;
422 }
423
424 /* Cuts out the segment at the file pointer (moves everything else
425 * down to cover it), and leaves the file pointer at the end of the
426 * file. */
427 static FcBool
428 FcCacheMoveDown (int fd, off_t start)
429 {
430 char * buf = malloc (8192);
431 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
432 long bs;
433 int c, bytes_skipped;
434
435 if (!buf)
436 return FcFalse;
437
438 lseek (fd, start, SEEK_SET);
439 if (FcCacheReadString (fd, candidate_arch_machine_name,
440 sizeof (candidate_arch_machine_name)) == 0)
441 goto done;
442 if (!strlen(candidate_arch_machine_name))
443 goto done;
444
445 bs = strtol(candidate_arch_machine_name, 0, 16);
446 if (bs == 0)
447 goto done;
448
449 bytes_skipped = 0;
450 do
451 {
452 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
453 if ((c = read (fd, buf, 8192)) <= 0)
454 break;
455 lseek (fd, start+bytes_skipped, SEEK_SET);
456 if (write (fd, buf, c) < 0)
457 goto bail;
458 bytes_skipped += c;
459 }
460 while (c > 0);
461 lseek (fd, start+bytes_skipped, SEEK_SET);
462
463 done:
464 free (buf);
465 return FcTrue;
466
467 bail:
468 free (buf);
469 return FcFalse;
470 }
471
472 FcBool
473 FcDirCacheValid (const FcChar8 *dir)
474 {
475 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
476 struct stat file_stat, dir_stat;
477
478 if (stat ((char *) dir, &dir_stat) < 0)
479 {
480 FcStrFree (cache_file);
481 return FcFalse;
482 }
483 if (stat ((char *) cache_file, &file_stat) < 0)
484 {
485 FcStrFree (cache_file);
486 return FcFalse;
487 }
488 FcStrFree (cache_file);
489 /*
490 * If the directory has been modified more recently than
491 * the cache file, the cache is not valid
492 */
493 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
494 return FcFalse;
495 return FcTrue;
496 }
497
498 static int
499 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
500 FcStrList *list, FcFontSet * set)
501 {
502 int ret = 0;
503 FcChar8 *dir;
504 FcChar8 *file, *base;
505 FcStrSet *subdirs;
506 FcStrList *sublist;
507 struct stat statb;
508
509 /*
510 * Read in the results from 'list'.
511 */
512 while ((dir = FcStrListNext (list)))
513 {
514 /* freed below */
515 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
516 if (!file)
517 return FcFalse;
518
519 strcpy ((char *) file, (char *) dir);
520 strcat ((char *) file, "/");
521 base = file + strlen ((char *) file);
522
523 subdirs = FcStrSetCreate ();
524 if (!subdirs)
525 {
526 fprintf (stderr, "Can't create directory set\n");
527 ret++;
528 free (file);
529 continue;
530 }
531
532 if (access ((char *) dir, X_OK) < 0)
533 {
534 switch (errno) {
535 case ENOENT:
536 case ENOTDIR:
537 case EACCES:
538 break;
539 default:
540 fprintf (stderr, "\"%s\": ", dir);
541 perror ("");
542 ret++;
543 }
544 FcStrSetDestroy (subdirs);
545 free (file);
546 continue;
547 }
548 if (stat ((char *) dir, &statb) == -1)
549 {
550 fprintf (stderr, "\"%s\": ", dir);
551 perror ("");
552 FcStrSetDestroy (subdirs);
553 ret++;
554 free (file);
555 continue;
556 }
557 if (!S_ISDIR (statb.st_mode))
558 {
559 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
560 FcStrSetDestroy (subdirs);
561 free (file);
562 continue;
563 }
564 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
565 {
566 if (FcDebug () & FC_DBG_FONTSET)
567 printf ("cache scan dir %s\n", dir);
568
569 FcDirScanConfig (set, subdirs, cache,
570 config->blanks, dir, FcFalse, config);
571 }
572 sublist = FcStrListCreate (subdirs);
573 FcStrSetDestroy (subdirs);
574 if (!sublist)
575 {
576 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
577 ret++;
578 free (file);
579 continue;
580 }
581 ret += FcCacheReadDirs (config, cache, sublist, set);
582 free (file);
583 }
584 FcStrListDone (list);
585 return ret;
586 }
587
588 FcFontSet *
589 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
590 {
591 FcFontSet * s = FcFontSetCreate();
592 if (!s)
593 return 0;
594
595 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
596 goto bail;
597
598 return s;
599
600 bail:
601 FcFontSetDestroy (s);
602 return 0;
603 }
604
605 /* read serialized state from the cache file */
606 static FcBool
607 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
608 {
609 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
610 int fd;
611 char * current_arch_machine_name;
612 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
613 off_t current_arch_start = 0;
614 FcChar8 subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
615
616 if (!cache_file)
617 goto bail;
618
619 current_arch_machine_name = FcCacheMachineSignature();
620 fd = open(cache_file, O_RDONLY);
621 if (fd == -1)
622 goto bail;
623
624 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
625 if (current_arch_start < 0)
626 goto bail1;
627
628 lseek (fd, current_arch_start, SEEK_SET);
629 if (FcCacheReadString (fd, candidate_arch_machine_name,
630 sizeof (candidate_arch_machine_name)) == 0)
631 goto bail1;
632
633 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
634 FcStrSetAdd (dirs, subdirName);
635
636 if (!FcDirCacheConsume (fd, set))
637 goto bail1;
638
639 close(fd);
640 free (cache_file);
641 return FcTrue;
642
643 bail1:
644 close (fd);
645 bail:
646 free (cache_file);
647 return FcFalse;
648 }
649
650 static FcBool
651 FcDirCacheConsume (int fd, FcFontSet *set)
652 {
653 FcCache metadata;
654 void * current_dir_block;
655 off_t pos;
656
657 read(fd, &metadata, sizeof(FcCache));
658 if (metadata.magic != FC_CACHE_MAGIC)
659 return FcFalse;
660
661 if (!metadata.count)
662 return FcTrue;
663
664 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
665 current_dir_block = mmap (0, metadata.count,
666 PROT_READ, MAP_SHARED, fd, pos);
667 if (current_dir_block == MAP_FAILED)
668 return FcFalse;
669
670 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
671 return FcFalse;
672
673 return FcTrue;
674 }
675
676 static void *
677 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
678 {
679 void * current_dir_block, * final_dir_block;
680 static int rand_state = 0;
681 int bank;
682
683 if (!rand_state)
684 rand_state = time(0L);
685 bank = rand_r(&rand_state);
686
687 while (FcCacheHaveBank(bank))
688 bank = rand_r(&rand_state);
689
690 memset (metadata, 0, sizeof(FcCache));
691 FcFontSetNewBank();
692 metadata->count = FcFontSetNeededBytes (set);
693 metadata->magic = FC_CACHE_MAGIC;
694 metadata->bank = bank;
695
696 if (!metadata->count) /* not a failure, no fonts to write */
697 return 0;
698
699 current_dir_block = malloc (metadata->count);
700 if (!current_dir_block)
701 goto bail;
702 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
703
704 if ((char *)current_dir_block + metadata->count != final_dir_block)
705 goto bail;
706
707 if (!FcFontSetSerialize (bank, set))
708 goto bail;
709
710 return current_dir_block;
711
712 bail:
713 free (current_dir_block);
714 return 0;
715 }
716
717 /* write serialized state to the cache file */
718 FcBool
719 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
720 {
721 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
722 int fd, i;
723 FcCache metadata;
724 off_t current_arch_start = 0, truncate_to;
725 char * current_arch_machine_name, * header;
726 void * current_dir_block;
727
728 if (!cache_file)
729 goto bail;
730
731 current_dir_block = FcDirCacheProduce (set, &metadata);
732
733 if (!metadata.count)
734 {
735 unlink (cache_file);
736 free (cache_file);
737 return FcTrue;
738 }
739
740 if (!current_dir_block)
741 goto bail;
742
743 if (FcDebug () & FC_DBG_CACHE)
744 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
745
746 fd = open(cache_file, O_RDWR | O_CREAT, 0666);
747 if (fd == -1)
748 goto bail0;
749
750 current_arch_machine_name = FcCacheMachineSignature ();
751 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
752 if (current_arch_start < 0)
753 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
754
755 if (!FcCacheMoveDown(fd, current_arch_start))
756 goto bail1;
757
758 current_arch_start = lseek(fd, 0, SEEK_CUR);
759 if (ftruncate (fd, current_arch_start) == -1)
760 goto bail1;
761
762 /* now write the address of the next offset */
763 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start;
764 header = malloc (10 + strlen (current_arch_machine_name));
765 if (!header)
766 goto bail1;
767 sprintf (header, "%8x ", (int)truncate_to);
768 strcat (header, current_arch_machine_name);
769 if (!FcCacheWriteString (fd, header))
770 goto bail1;
771
772 for (i = 0; i < dirs->size; i++)
773 FcCacheWriteString (fd, dirs->strs[i]);
774 FcCacheWriteString (fd, "");
775
776 write (fd, &metadata, sizeof(FcCache));
777 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
778 write (fd, current_dir_block, metadata.count);
779 free (current_dir_block);
780
781 /* this actually serves to pad out the cache file, if needed */
782 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
783 goto bail1;
784
785 close(fd);
786 return FcTrue;
787
788 bail1:
789 free (header);
790 bail0:
791 free (current_dir_block);
792 bail:
793 unlink (cache_file);
794 free (cache_file);
795 return FcFalse;
796 }
797
798 static char *
799 FcCacheMachineSignature ()
800 {
801 static char buf[MACHINE_SIGNATURE_SIZE];
802 int magic = ENDIAN_TEST;
803 char * m = (char *)&magic;
804
805 sprintf (buf, "%2x%2x%2x%2x "
806 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
807 "%4x %4x %4x %4x %4x %4x %4x\n",
808 m[0], m[1], m[2], m[3],
809 sizeof (char),
810 sizeof (char *),
811 sizeof (int),
812 sizeof (FcPattern),
813 sizeof (FcPatternEltPtr),
814 sizeof (struct _FcPatternElt *),
815 sizeof (FcPatternElt),
816 sizeof (FcObjectPtr),
817 sizeof (FcValueListPtr),
818 sizeof (FcValue),
819 sizeof (FcValueBinding),
820 sizeof (struct _FcValueList *),
821 sizeof (FcCharSet),
822 sizeof (FcCharLeaf **),
823 sizeof (FcChar16 *),
824 sizeof (FcChar16),
825 sizeof (FcCharLeaf),
826 sizeof (FcChar32),
827 sizeof (FcCache));
828
829 return buf;
830 }
831
832 static int banks_ptr = 0, banks_alloc = 0;
833 static int * bankId = 0;
834
835 static FcBool
836 FcCacheHaveBank (int bank)
837 {
838 int i;
839
840 if (bank < FC_BANK_FIRST)
841 return FcTrue;
842
843 for (i = 0; i < banks_ptr; i++)
844 if (bankId[i] == bank)
845 return FcTrue;
846
847 return FcFalse;
848 }
849
850 int
851 FcCacheBankToIndex (int bank)
852 {
853 static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1;
854 int i;
855 int * b;
856
857 if (bank == lastBank)
858 return lastIndex;
859
860 for (i = 0; i < banks_ptr; i++)
861 if (bankId[i] == bank)
862 return i;
863
864 if (banks_ptr >= banks_alloc)
865 {
866 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
867 if (!b)
868 return -1;
869
870 bankId = b;
871 banks_alloc += 4;
872 }
873
874 i = banks_ptr++;
875 bankId[i] = bank;
876 return i;
877 }