]> git.wh0rd.org - fontconfig.git/blob - src/fccache.c
Update documentation -- fc-cache's man page now says that you need to run
[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 char *
64 FcCacheReadString (int fd, char *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 char *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 char 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 off_t targ;
186
187 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
188 if (!strlen(name_buf))
189 break;
190
191 if (stat ((char *) name_buf, &dir_stat) < 0 ||
192 dir_stat.st_mtime > cache_stat.st_mtime)
193 {
194 FcCache md;
195
196 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
197 read (cache->fd, &md, sizeof (FcCache));
198 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
199 continue;
200 }
201
202 d = malloc (sizeof (FcGlobalCacheDir));
203 if (!d)
204 goto bail1;
205
206 d->next = cache->dirs;
207 cache->dirs = d;
208
209 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
210 d->ent = 0;
211 d->offset = lseek (cache->fd, 0, SEEK_CUR);
212 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
213 goto bail1;
214 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
215 if (lseek (cache->fd, targ, SEEK_SET) != targ)
216 goto bail1;
217 }
218 return;
219
220 bail1:
221 for (d = cache->dirs; d; d = next)
222 {
223 next = d->next;
224 free (d);
225 }
226 cache->dirs = 0;
227 bail0:
228 close (cache->fd);
229 cache->fd = -1;
230 return;
231 }
232
233 FcBool
234 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
235 {
236 FcGlobalCacheDir *d;
237 FcBool ret = FcFalse;
238
239 if (cache->fd == -1)
240 return FcFalse;
241
242 for (d = cache->dirs; d; d = d->next)
243 {
244 if (strncmp (d->name, dir, strlen(dir)) == 0)
245 {
246 lseek (cache->fd, d->offset, SEEK_SET);
247 if (!FcDirCacheConsume (cache->fd, set))
248 return FcFalse;
249 if (strcmp (d->name, dir) == 0)
250 ret = FcTrue;
251 }
252 }
253
254 return ret;
255 }
256
257 FcBool
258 FcGlobalCacheUpdate (FcGlobalCache *cache,
259 const char *name,
260 FcFontSet *set)
261 {
262 FcGlobalCacheDir * d;
263
264 if (!set->nfont)
265 return FcTrue;
266
267 for (d = cache->dirs; d; d = d->next)
268 {
269 if (strcmp(d->name, name) == 0)
270 break;
271 }
272
273 if (!d)
274 {
275 d = malloc (sizeof (FcGlobalCacheDir));
276 if (!d)
277 return FcFalse;
278 d->next = cache->dirs;
279 cache->dirs = d;
280 }
281
282 cache->updated = FcTrue;
283
284 d->name = (char *)FcStrCopy ((FcChar8 *)name);
285 d->ent = FcDirCacheProduce (set, &d->metadata);
286 d->offset = 0;
287 return FcTrue;
288 }
289
290 FcBool
291 FcGlobalCacheSave (FcGlobalCache *cache,
292 const FcChar8 *cache_file)
293 {
294 int fd;
295 FcGlobalCacheDir *dir;
296 FcAtomic *atomic;
297 off_t current_arch_start = 0, truncate_to;
298 char * current_arch_machine_name, * header;
299
300 if (!cache->updated)
301 return FcTrue;
302
303 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
304 /* Set-UID programs can't safely update the cache */
305 if (getuid () != geteuid ())
306 return FcFalse;
307 #endif
308
309 atomic = FcAtomicCreate (cache_file);
310 if (!atomic)
311 goto bail0;
312 if (!FcAtomicLock (atomic))
313 goto bail1;
314 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
315 S_IRUSR | S_IWUSR);
316 if (fd == -1)
317 goto bail2;
318
319 current_arch_machine_name = FcCacheMachineSignature ();
320 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
321 if (current_arch_start < 0)
322 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
323
324 if (!FcCacheMoveDown(fd, current_arch_start))
325 goto bail2;
326
327 current_arch_start = lseek(fd, 0, SEEK_CUR);
328 if (ftruncate (fd, current_arch_start) == -1)
329 goto bail2;
330
331 header = malloc (10 + strlen (current_arch_machine_name));
332 if (!header)
333 goto bail1;
334
335 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
336 for (dir = cache->dirs; dir; dir = dir->next)
337 {
338 truncate_to += strlen(dir->name) + 1;
339 truncate_to += sizeof (FcCache);
340 truncate_to = FcCacheNextOffset (current_arch_start + truncate_to);
341 truncate_to += dir->metadata.count;
342 }
343 truncate_to -= current_arch_start;
344
345 sprintf (header, "%8x ", (int)truncate_to);
346 strcat (header, current_arch_machine_name);
347 if (!FcCacheWriteString (fd, header))
348 goto bail1;
349
350 for (dir = cache->dirs; dir; dir = dir->next)
351 {
352 if (dir->ent)
353 {
354 FcCacheWriteString (fd, dir->name);
355 write (fd, &dir->metadata, sizeof(FcCache));
356 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
357 write (fd, dir->ent, dir->metadata.count);
358 free (dir->ent);
359 }
360 }
361 FcCacheWriteString (fd, "");
362
363 if (close (fd) == -1)
364 goto bail3;
365
366 if (!FcAtomicReplaceOrig (atomic))
367 goto bail3;
368
369 FcAtomicUnlock (atomic);
370 FcAtomicDestroy (atomic);
371
372 cache->updated = FcFalse;
373 return FcTrue;
374
375 bail3:
376 FcAtomicDeleteNew (atomic);
377 bail2:
378 FcAtomicUnlock (atomic);
379 bail1:
380 FcAtomicDestroy (atomic);
381 bail0:
382 return FcFalse;
383 }
384
385 #define PAGESIZE 8192
386 /*
387 * Find the next presumably-mmapable offset after the supplied file
388 * position.
389 */
390 static int
391 FcCacheNextOffset(off_t w)
392 {
393 if (w % PAGESIZE == 0)
394 return w;
395 else
396 return ((w / PAGESIZE)+1)*PAGESIZE;
397 }
398
399 /* return the address of the segment for the provided arch,
400 * or -1 if arch not found */
401 static off_t
402 FcCacheSkipToArch (int fd, const char * arch)
403 {
404 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
405 char * candidate_arch;
406 off_t current_arch_start = 0;
407
408 /* skip arches that are not the current arch */
409 while (1)
410 {
411 long bs;
412
413 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
414 return -1;
415
416 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
417 sizeof (candidate_arch_machine_name_count)) == 0)
418 return -1;
419 if (!strlen(candidate_arch_machine_name_count))
420 return -1;
421 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
422
423 // count = 0 should probably be distinguished from the !bs condition
424 if (!bs || bs < strlen (candidate_arch_machine_name_count))
425 return -1;
426
427 candidate_arch++; /* skip leading space */
428
429 if (strcmp (candidate_arch, arch)==0)
430 return current_arch_start;
431 current_arch_start += bs;
432 }
433
434 return -1;
435 }
436
437 /* Cuts out the segment at the file pointer (moves everything else
438 * down to cover it), and leaves the file pointer at the end of the
439 * file. */
440 static FcBool
441 FcCacheMoveDown (int fd, off_t start)
442 {
443 char * buf = malloc (8192);
444 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
445 long bs;
446 int c, bytes_skipped;
447
448 if (!buf)
449 return FcFalse;
450
451 lseek (fd, start, SEEK_SET);
452 if (FcCacheReadString (fd, candidate_arch_machine_name,
453 sizeof (candidate_arch_machine_name)) == 0)
454 goto done;
455 if (!strlen(candidate_arch_machine_name))
456 goto done;
457
458 bs = strtol(candidate_arch_machine_name, 0, 16);
459 if (bs == 0)
460 goto done;
461
462 bytes_skipped = 0;
463 do
464 {
465 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
466 if ((c = read (fd, buf, 8192)) <= 0)
467 break;
468 lseek (fd, start+bytes_skipped, SEEK_SET);
469 if (write (fd, buf, c) < 0)
470 goto bail;
471 bytes_skipped += c;
472 }
473 while (c > 0);
474 lseek (fd, start+bytes_skipped, SEEK_SET);
475
476 done:
477 free (buf);
478 return FcTrue;
479
480 bail:
481 free (buf);
482 return FcFalse;
483 }
484
485 FcBool
486 FcDirCacheValid (const FcChar8 *dir)
487 {
488 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
489 struct stat file_stat, dir_stat;
490
491 if (stat ((char *) dir, &dir_stat) < 0)
492 {
493 FcStrFree (cache_file);
494 return FcFalse;
495 }
496 if (stat ((char *) cache_file, &file_stat) < 0)
497 {
498 FcStrFree (cache_file);
499 return FcFalse;
500 }
501 FcStrFree (cache_file);
502 /*
503 * If the directory has been modified more recently than
504 * the cache file, the cache is not valid
505 */
506 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
507 return FcFalse;
508 return FcTrue;
509 }
510
511 static int
512 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
513 FcStrList *list, FcFontSet * set)
514 {
515 int ret = 0;
516 FcChar8 *dir;
517 FcChar8 *file, *base;
518 FcStrSet *subdirs;
519 FcStrList *sublist;
520 struct stat statb;
521
522 /*
523 * Read in the results from 'list'.
524 */
525 while ((dir = FcStrListNext (list)))
526 {
527 /* freed below */
528 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
529 if (!file)
530 return FcFalse;
531
532 strcpy ((char *) file, (char *) dir);
533 strcat ((char *) file, "/");
534 base = file + strlen ((char *) file);
535
536 subdirs = FcStrSetCreate ();
537 if (!subdirs)
538 {
539 fprintf (stderr, "Can't create directory set\n");
540 ret++;
541 free (file);
542 continue;
543 }
544
545 if (access ((char *) dir, X_OK) < 0)
546 {
547 switch (errno) {
548 case ENOENT:
549 case ENOTDIR:
550 case EACCES:
551 break;
552 default:
553 fprintf (stderr, "\"%s\": ", dir);
554 perror ("");
555 ret++;
556 }
557 FcStrSetDestroy (subdirs);
558 free (file);
559 continue;
560 }
561 if (stat ((char *) dir, &statb) == -1)
562 {
563 fprintf (stderr, "\"%s\": ", dir);
564 perror ("");
565 FcStrSetDestroy (subdirs);
566 ret++;
567 free (file);
568 continue;
569 }
570 if (!S_ISDIR (statb.st_mode))
571 {
572 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
573 FcStrSetDestroy (subdirs);
574 free (file);
575 continue;
576 }
577 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
578 {
579 if (FcDebug () & FC_DBG_FONTSET)
580 printf ("cache scan dir %s\n", dir);
581
582 FcDirScanConfig (set, subdirs, cache,
583 config->blanks, dir, FcFalse, config);
584 }
585 sublist = FcStrListCreate (subdirs);
586 FcStrSetDestroy (subdirs);
587 if (!sublist)
588 {
589 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
590 ret++;
591 free (file);
592 continue;
593 }
594 ret += FcCacheReadDirs (config, cache, sublist, set);
595 free (file);
596 }
597 FcStrListDone (list);
598 return ret;
599 }
600
601 FcFontSet *
602 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
603 {
604 FcFontSet * s = FcFontSetCreate();
605 if (!s)
606 return 0;
607
608 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
609 goto bail;
610
611 return s;
612
613 bail:
614 FcFontSetDestroy (s);
615 return 0;
616 }
617
618 /* read serialized state from the cache file */
619 static FcBool
620 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
621 {
622 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
623 int fd;
624 char * current_arch_machine_name;
625 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
626 off_t current_arch_start = 0;
627 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
628
629 if (!cache_file)
630 goto bail;
631
632 current_arch_machine_name = FcCacheMachineSignature();
633 fd = open(cache_file, O_RDONLY);
634 if (fd == -1)
635 goto bail;
636
637 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
638 if (current_arch_start < 0)
639 goto bail1;
640
641 lseek (fd, current_arch_start, SEEK_SET);
642 if (FcCacheReadString (fd, candidate_arch_machine_name,
643 sizeof (candidate_arch_machine_name)) == 0)
644 goto bail1;
645
646 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
647 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
648
649 if (!FcDirCacheConsume (fd, set))
650 goto bail1;
651
652 close(fd);
653 free (cache_file);
654 return FcTrue;
655
656 bail1:
657 close (fd);
658 bail:
659 free (cache_file);
660 return FcFalse;
661 }
662
663 static FcBool
664 FcDirCacheConsume (int fd, FcFontSet *set)
665 {
666 FcCache metadata;
667 void * current_dir_block;
668 off_t pos;
669
670 read(fd, &metadata, sizeof(FcCache));
671 if (metadata.magic != FC_CACHE_MAGIC)
672 return FcFalse;
673
674 if (!metadata.count)
675 return FcTrue;
676
677 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
678 current_dir_block = mmap (0, metadata.count,
679 PROT_READ, MAP_SHARED, fd, pos);
680 if (current_dir_block == MAP_FAILED)
681 return FcFalse;
682
683 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
684 return FcFalse;
685
686 return FcTrue;
687 }
688
689 static void *
690 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
691 {
692 void * current_dir_block, * final_dir_block;
693 static unsigned int rand_state = 0;
694 int bank;
695
696 if (!rand_state)
697 rand_state = time(0L);
698 bank = rand_r(&rand_state);
699
700 while (FcCacheHaveBank(bank))
701 bank = rand_r(&rand_state);
702
703 memset (metadata, 0, sizeof(FcCache));
704 FcFontSetNewBank();
705 metadata->count = FcFontSetNeededBytes (set);
706 metadata->magic = FC_CACHE_MAGIC;
707 metadata->bank = bank;
708
709 if (!metadata->count) /* not a failure, no fonts to write */
710 return 0;
711
712 current_dir_block = malloc (metadata->count);
713 if (!current_dir_block)
714 goto bail;
715 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
716
717 if ((char *)current_dir_block + metadata->count != final_dir_block)
718 goto bail;
719
720 if (!FcFontSetSerialize (bank, set))
721 goto bail;
722
723 return current_dir_block;
724
725 bail:
726 free (current_dir_block);
727 return 0;
728 }
729
730 /* write serialized state to the cache file */
731 FcBool
732 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
733 {
734 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
735 int fd, i;
736 FcCache metadata;
737 off_t current_arch_start = 0, truncate_to;
738
739 char * current_arch_machine_name, * header;
740 void * current_dir_block;
741
742 if (!cache_file)
743 goto bail;
744
745 current_dir_block = FcDirCacheProduce (set, &metadata);
746
747 if (!metadata.count && !dirs->size)
748 {
749 unlink ((char *)cache_file);
750 free (cache_file);
751 return FcTrue;
752 }
753
754 if (metadata.count && !current_dir_block)
755 goto bail;
756
757 if (FcDebug () & FC_DBG_CACHE)
758 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
759
760 fd = open((char *)cache_file, O_RDWR | O_CREAT, 0666);
761 if (fd == -1)
762 goto bail0;
763
764 current_arch_machine_name = FcCacheMachineSignature ();
765 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
766 if (current_arch_start < 0)
767 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
768
769 if (!FcCacheMoveDown(fd, current_arch_start))
770 goto bail0;
771
772 current_arch_start = lseek(fd, 0, SEEK_CUR);
773 if (ftruncate (fd, current_arch_start) == -1)
774 goto bail0;
775
776 /* now write the address of the next offset */
777 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start;
778 header = malloc (10 + strlen (current_arch_machine_name));
779 if (!header)
780 goto bail0;
781 sprintf (header, "%8x ", (int)truncate_to);
782 strcat (header, current_arch_machine_name);
783 if (!FcCacheWriteString (fd, header))
784 goto bail1;
785
786 for (i = 0; i < dirs->size; i++)
787 FcCacheWriteString (fd, (char *)dirs->strs[i]);
788 FcCacheWriteString (fd, "");
789
790 write (fd, &metadata, sizeof(FcCache));
791 if (metadata.count)
792 {
793 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
794 write (fd, current_dir_block, metadata.count);
795 free (current_dir_block);
796 }
797
798 /* this actually serves to pad out the cache file, if needed */
799 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
800 goto bail1;
801
802 close(fd);
803 return FcTrue;
804
805 bail1:
806 free (header);
807 bail0:
808 free (current_dir_block);
809 bail:
810 unlink ((char *)cache_file);
811 free (cache_file);
812 return FcFalse;
813 }
814
815 static char *
816 FcCacheMachineSignature ()
817 {
818 static char buf[MACHINE_SIGNATURE_SIZE];
819 int magic = ENDIAN_TEST;
820 char * m = (char *)&magic;
821
822 sprintf (buf, "%2x%2x%2x%2x "
823 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
824 "%4x %4x %4x %4x %4x %4x %4x\n",
825 m[0], m[1], m[2], m[3],
826 sizeof (char),
827 sizeof (char *),
828 sizeof (int),
829 sizeof (FcPattern),
830 sizeof (FcPatternEltPtr),
831 sizeof (struct _FcPatternElt *),
832 sizeof (FcPatternElt),
833 sizeof (FcObjectPtr),
834 sizeof (FcValueListPtr),
835 sizeof (FcValue),
836 sizeof (FcValueBinding),
837 sizeof (struct _FcValueList *),
838 sizeof (FcCharSet),
839 sizeof (FcCharLeaf **),
840 sizeof (FcChar16 *),
841 sizeof (FcChar16),
842 sizeof (FcCharLeaf),
843 sizeof (FcChar32),
844 sizeof (FcCache));
845
846 return buf;
847 }
848
849 static int banks_ptr = 0, banks_alloc = 0;
850 static int * bankId = 0;
851
852 static FcBool
853 FcCacheHaveBank (int bank)
854 {
855 int i;
856
857 if (bank < FC_BANK_FIRST)
858 return FcTrue;
859
860 for (i = 0; i < banks_ptr; i++)
861 if (bankId[i] == bank)
862 return FcTrue;
863
864 return FcFalse;
865 }
866
867 int
868 FcCacheBankToIndex (int bank)
869 {
870 static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1;
871 int i;
872 int * b;
873
874 if (bank == lastBank)
875 return lastIndex;
876
877 for (i = 0; i < banks_ptr; i++)
878 if (bankId[i] == bank)
879 return i;
880
881 if (banks_ptr >= banks_alloc)
882 {
883 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
884 if (!b)
885 return -1;
886
887 bankId = b;
888 banks_alloc += 4;
889 }
890
891 i = banks_ptr++;
892 bankId[i] = bank;
893 return i;
894 }