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