]> git.wh0rd.org Git - fontconfig.git/blob - src/fccache.c
Merge with HEAD and finish the GCC 4 cleanups (no more warnings!)
[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         if (!bs || bs < strlen (candidate_arch_machine_name_count))
424             return -1;
425
426         candidate_arch++; /* skip leading space */
427
428         if (strcmp (candidate_arch, arch)==0)
429             return current_arch_start;
430         current_arch_start += bs;
431     }
432
433     return -1;
434 }
435
436 /* Cuts out the segment at the file pointer (moves everything else
437  * down to cover it), and leaves the file pointer at the end of the
438  * file. */
439 static FcBool 
440 FcCacheMoveDown (int fd, off_t start)
441 {
442     char * buf = malloc (8192);
443     char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
444     long bs;
445     int c, bytes_skipped;
446
447     if (!buf)
448         return FcFalse;
449
450     lseek (fd, start, SEEK_SET);
451     if (FcCacheReadString (fd, candidate_arch_machine_name, 
452                            sizeof (candidate_arch_machine_name)) == 0)
453         goto done;
454     if (!strlen(candidate_arch_machine_name))
455         goto done;
456
457     bs = strtol(candidate_arch_machine_name, 0, 16);
458     if (bs == 0)
459         goto done;
460
461     bytes_skipped = 0;
462     do
463     {
464         lseek (fd, start+bs+bytes_skipped, SEEK_SET);
465         if ((c = read (fd, buf, 8192)) <= 0)
466             break;
467         lseek (fd, start+bytes_skipped, SEEK_SET);
468         if (write (fd, buf, c) < 0)
469             goto bail;
470         bytes_skipped += c;
471     }
472     while (c > 0);
473     lseek (fd, start+bytes_skipped, SEEK_SET);
474
475  done:
476     free (buf);
477     return FcTrue;
478
479  bail:
480     free (buf);
481     return FcFalse;
482 }
483
484 FcBool
485 FcDirCacheValid (const FcChar8 *dir)
486 {
487     FcChar8     *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
488     struct stat file_stat, dir_stat;
489
490     if (stat ((char *) dir, &dir_stat) < 0)
491     {
492         FcStrFree (cache_file);
493         return FcFalse;
494     }
495     if (stat ((char *) cache_file, &file_stat) < 0)
496     {
497         FcStrFree (cache_file);
498         return FcFalse;
499     }
500     FcStrFree (cache_file);
501     /*
502      * If the directory has been modified more recently than
503      * the cache file, the cache is not valid
504      */
505     if (dir_stat.st_mtime - file_stat.st_mtime > 0)
506         return FcFalse;
507     return FcTrue;
508 }
509
510 static int
511 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache, 
512                  FcStrList *list, FcFontSet * set)
513 {
514     int                 ret = 0;
515     FcChar8             *dir;
516     FcChar8             *file, *base;
517     FcStrSet            *subdirs;
518     FcStrList           *sublist;
519     struct stat         statb;
520
521     /*
522      * Read in the results from 'list'.
523      */
524     while ((dir = FcStrListNext (list)))
525     {
526         /* freed below */
527         file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
528         if (!file)
529             return FcFalse;
530
531         strcpy ((char *) file, (char *) dir);
532         strcat ((char *) file, "/");
533         base = file + strlen ((char *) file);
534
535         subdirs = FcStrSetCreate ();
536         if (!subdirs)
537         {
538             fprintf (stderr, "Can't create directory set\n");
539             ret++;
540             free (file);
541             continue;
542         }
543         
544         if (access ((char *) dir, X_OK) < 0)
545         {
546             switch (errno) {
547             case ENOENT:
548             case ENOTDIR:
549             case EACCES:
550                 break;
551             default:
552                 fprintf (stderr, "\"%s\": ", dir);
553                 perror ("");
554                 ret++;
555             }
556             FcStrSetDestroy (subdirs);
557             free (file);
558             continue;
559         }
560         if (stat ((char *) dir, &statb) == -1)
561         {
562             fprintf (stderr, "\"%s\": ", dir);
563             perror ("");
564             FcStrSetDestroy (subdirs);
565             ret++;
566             free (file);
567             continue;
568         }
569         if (!S_ISDIR (statb.st_mode))
570         {
571             fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
572             FcStrSetDestroy (subdirs);
573             free (file);
574             continue;
575         }
576         if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
577         {
578             if (FcDebug () & FC_DBG_FONTSET)
579                 printf ("cache scan dir %s\n", dir);
580
581             FcDirScanConfig (set, subdirs, cache, 
582                              config->blanks, dir, FcFalse, config);
583         }
584         sublist = FcStrListCreate (subdirs);
585         FcStrSetDestroy (subdirs);
586         if (!sublist)
587         {
588             fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
589             ret++;
590             free (file);
591             continue;
592         }
593         ret += FcCacheReadDirs (config, cache, sublist, set);
594         free (file);
595     }
596     FcStrListDone (list);
597     return ret;
598 }
599
600 FcFontSet *
601 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
602 {
603     FcFontSet * s = FcFontSetCreate();
604     if (!s) 
605         return 0;
606
607     if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
608         goto bail;
609
610     return s;
611
612  bail:
613     FcFontSetDestroy (s);
614     return 0;
615 }
616
617 /* read serialized state from the cache file */
618 static FcBool
619 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
620 {
621     char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
622     int fd;
623     char * current_arch_machine_name;
624     char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
625     off_t current_arch_start = 0;
626     char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
627
628     if (!cache_file)
629         goto bail;
630
631     current_arch_machine_name = FcCacheMachineSignature();
632     fd = open(cache_file, O_RDONLY);
633     if (fd == -1)
634         goto bail;
635
636     current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
637     if (current_arch_start < 0)
638         goto bail1;
639
640     lseek (fd, current_arch_start, SEEK_SET);
641     if (FcCacheReadString (fd, candidate_arch_machine_name, 
642                            sizeof (candidate_arch_machine_name)) == 0)
643         goto bail1;
644
645     while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
646         FcStrSetAdd (dirs, (FcChar8 *)subdirName);
647
648     if (!FcDirCacheConsume (fd, set))
649         goto bail1;
650         
651     close(fd);
652     free (cache_file);
653     return FcTrue;
654
655  bail1:
656     close (fd);
657  bail:
658     free (cache_file);
659     return FcFalse;
660 }
661
662 static FcBool
663 FcDirCacheConsume (int fd, FcFontSet *set)
664 {
665     FcCache metadata;
666     void * current_dir_block;
667     off_t pos;
668
669     read(fd, &metadata, sizeof(FcCache));
670     if (metadata.magic != FC_CACHE_MAGIC)
671         return FcFalse;
672
673     if (!metadata.count)
674         return FcTrue;
675
676     pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
677     current_dir_block = mmap (0, metadata.count, 
678                               PROT_READ, MAP_SHARED, fd, pos);
679     if (current_dir_block == MAP_FAILED)
680         return FcFalse;
681     
682     if (!FcFontSetUnserialize (metadata, set, current_dir_block))
683         return FcFalse;
684
685     return FcTrue;
686 }
687
688 static void *
689 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
690 {
691     void * current_dir_block, * final_dir_block;
692     static unsigned int rand_state = 0;
693     int bank;
694
695     if (!rand_state) 
696         rand_state = time(0L);
697     bank = rand_r(&rand_state);
698
699     while (FcCacheHaveBank(bank))
700         bank = rand_r(&rand_state);
701
702     memset (metadata, 0, sizeof(FcCache));
703     FcFontSetNewBank();
704     metadata->count = FcFontSetNeededBytes (set);
705     metadata->magic = FC_CACHE_MAGIC;
706     metadata->bank = bank;
707
708     if (!metadata->count) /* not a failure, no fonts to write */
709         return 0;
710
711     current_dir_block = malloc (metadata->count);
712     if (!current_dir_block)
713         goto bail;
714     final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
715
716     if ((char *)current_dir_block + metadata->count != final_dir_block)
717         goto bail;
718                               
719     if (!FcFontSetSerialize (bank, set))
720         goto bail;
721
722     return current_dir_block;
723
724  bail:
725     free (current_dir_block);
726     return 0;
727 }
728
729 /* write serialized state to the cache file */
730 FcBool
731 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
732 {
733     FcChar8         *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
734     int fd, i;
735     FcCache metadata;
736     off_t current_arch_start = 0, truncate_to;
737
738     char * current_arch_machine_name, * header;
739     void * current_dir_block;
740
741     if (!cache_file)
742         goto bail;
743
744     current_dir_block = FcDirCacheProduce (set, &metadata);
745
746     if (!metadata.count)
747     {
748         unlink ((char *)cache_file);
749         free (cache_file);
750         return FcTrue;
751     }
752
753     if (!current_dir_block)
754         goto bail;
755
756     if (FcDebug () & FC_DBG_CACHE)
757         printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
758
759     fd = open((char *)cache_file, O_RDWR | O_CREAT, 0666);
760     if (fd == -1)
761         goto bail0;
762
763     current_arch_machine_name = FcCacheMachineSignature ();
764     current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
765     if (current_arch_start < 0)
766         current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
767
768     if (!FcCacheMoveDown(fd, current_arch_start))
769         goto bail0;
770
771     current_arch_start = lseek(fd, 0, SEEK_CUR);
772     if (ftruncate (fd, current_arch_start) == -1)
773         goto bail0;
774
775     /* now write the address of the next offset */
776     truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start;
777     header = malloc (10 + strlen (current_arch_machine_name));
778     if (!header)
779         goto bail0;
780     sprintf (header, "%8x ", (int)truncate_to);
781     strcat (header, current_arch_machine_name);
782     if (!FcCacheWriteString (fd, header))
783         goto bail1;
784
785     for (i = 0; i < dirs->size; i++)
786         FcCacheWriteString (fd, (char *)dirs->strs[i]);
787     FcCacheWriteString (fd, "");
788
789     write (fd, &metadata, sizeof(FcCache));
790     lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
791     write (fd, current_dir_block, metadata.count);
792     free (current_dir_block);
793
794     /* this actually serves to pad out the cache file, if needed */
795     if (ftruncate (fd, current_arch_start + truncate_to) == -1)
796         goto bail1;
797
798     close(fd);
799     return FcTrue;
800
801  bail1:
802     free (header);
803  bail0:
804     free (current_dir_block);
805  bail:
806     unlink ((char *)cache_file);
807     free (cache_file);
808     return FcFalse;
809 }
810
811 static char *
812 FcCacheMachineSignature ()
813 {
814     static char buf[MACHINE_SIGNATURE_SIZE];
815     int magic = ENDIAN_TEST;
816     char * m = (char *)&magic;
817
818     sprintf (buf, "%2x%2x%2x%2x "
819              "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
820              "%4x %4x %4x %4x %4x %4x %4x\n", 
821              m[0], m[1], m[2], m[3],
822              sizeof (char),
823              sizeof (char *),
824              sizeof (int),
825              sizeof (FcPattern),
826              sizeof (FcPatternEltPtr),
827              sizeof (struct _FcPatternElt *),
828              sizeof (FcPatternElt),
829              sizeof (FcObjectPtr),
830              sizeof (FcValueListPtr),
831              sizeof (FcValue),
832              sizeof (FcValueBinding),
833              sizeof (struct _FcValueList *),
834              sizeof (FcCharSet),
835              sizeof (FcCharLeaf **),
836              sizeof (FcChar16 *),
837              sizeof (FcChar16),
838              sizeof (FcCharLeaf),
839              sizeof (FcChar32),
840              sizeof (FcCache));
841
842     return buf;
843 }
844
845 static int banks_ptr = 0, banks_alloc = 0;
846 static int * bankId = 0;
847
848 static FcBool
849 FcCacheHaveBank (int bank)
850 {
851     int i;
852
853     if (bank < FC_BANK_FIRST)
854         return FcTrue;
855
856     for (i = 0; i < banks_ptr; i++)
857         if (bankId[i] == bank)
858             return FcTrue;
859
860     return FcFalse;
861 }
862
863 int
864 FcCacheBankToIndex (int bank)
865 {
866     static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1;
867     int i;
868     int * b;
869
870     if (bank == lastBank)
871         return lastIndex;
872
873     for (i = 0; i < banks_ptr; i++)
874         if (bankId[i] == bank)
875             return i;
876
877     if (banks_ptr >= banks_alloc)
878     {
879         b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
880         if (!b)
881             return -1;
882
883         bankId = b;
884         banks_alloc += 4;
885     }
886
887     i = banks_ptr++;
888     bankId[i] = bank;
889     return i;
890 }