]> git.wh0rd.org - fontconfig.git/blame_incremental - src/fccache.c
Fix bug 2878 (excessive relocations at startup for charsets, reported by
[fontconfig.git] / src / fccache.c
... / ...
CommitLineData
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
37static off_t
38FcCacheSkipToArch (int fd, const char * arch);
39
40static FcBool
41FcCacheCopyOld (int fd, int fd_orig, off_t start);
42
43static void *
44FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
45
46static FcBool
47FcDirCacheConsume (int fd, const char * dir, FcFontSet *set);
48
49FcBool
50FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
51
52static int
53FcCacheNextOffset(off_t w);
54
55static char *
56FcCacheMachineSignature (void);
57
58static FcBool
59FcCacheHaveBank (int bank);
60
61static void
62FcCacheAddBankDir (int bank, const char * dir);
63
64#define FC_DBG_CACHE_REF 1024
65
66static char *
67FcCacheReadString (int fd, char *dest, int len)
68{
69 FcChar8 c;
70 FcBool escape;
71 int size;
72 int i;
73
74 if (len == 0)
75 return 0;
76
77 size = len;
78 i = 0;
79 escape = FcFalse;
80 while (read (fd, &c, 1) == 1)
81 {
82 if (!escape)
83 {
84 switch (c) {
85 case '"':
86 c = '\0';
87 break;
88 case '\\':
89 escape = FcTrue;
90 continue;
91 }
92 }
93 if (i == size)
94 {
95 dest[i++] = 0;
96 return dest;
97 }
98 dest[i++] = c;
99 if (c == '\0')
100 return dest;
101 escape = FcFalse;
102 }
103 return 0;
104}
105
106static FcBool
107FcCacheWriteString (int fd, const char *chars)
108{
109 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
110 return FcFalse;
111 return FcTrue;
112}
113
114static void
115FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
116{
117 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
118 free (d->name);
119 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
120 free (d);
121}
122
123FcGlobalCache *
124FcGlobalCacheCreate (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
138void
139FcGlobalCacheDestroy (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
152void
153FcGlobalCacheLoad (FcGlobalCache *cache,
154 FcStrSet *staleDirs,
155 const FcChar8 *cache_file,
156 FcConfig *config)
157{
158 char name_buf[8192];
159 FcGlobalCacheDir *d, *next;
160 FcFileTime config_time = FcConfigModifiedTime (config);
161 char * current_arch_machine_name;
162 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
163 off_t current_arch_start;
164
165 struct stat cache_stat, dir_stat;
166
167 if (stat ((char *) cache_file, &cache_stat) < 0)
168 return;
169
170 cache->fd = open ((char *) cache_file, O_RDONLY);
171 if (cache->fd == -1)
172 return;
173
174 cache->updated = FcFalse;
175
176 current_arch_machine_name = FcCacheMachineSignature ();
177 current_arch_start = FcCacheSkipToArch(cache->fd,
178 current_arch_machine_name);
179 if (current_arch_start < 0)
180 goto bail_and_destroy;
181
182 lseek (cache->fd, current_arch_start, SEEK_SET);
183 FcCacheReadString (cache->fd, candidate_arch_machine_name,
184 sizeof (candidate_arch_machine_name));
185 if (strlen(candidate_arch_machine_name) == 0)
186 goto bail_and_destroy;
187
188 while (1)
189 {
190 off_t targ;
191
192 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
193 if (!strlen(name_buf))
194 break;
195
196 /* Directory must be older than the global cache file; also
197 cache must be newer than the config file. */
198 if (stat ((char *) name_buf, &dir_stat) < 0 ||
199 dir_stat.st_mtime > cache_stat.st_mtime ||
200 (config_time.set && cache_stat.st_mtime < config_time.time))
201 {
202 FcCache md;
203
204 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
205 read (cache->fd, &md, sizeof (FcCache));
206 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
207 continue;
208 }
209
210 d = malloc (sizeof (FcGlobalCacheDir));
211 if (!d)
212 goto bail1;
213
214 d->next = cache->dirs;
215 cache->dirs = d;
216
217 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
218 d->ent = 0;
219 d->offset = lseek (cache->fd, 0, SEEK_CUR);
220 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
221 goto bail1;
222 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
223 if (lseek (cache->fd, targ, SEEK_SET) != targ)
224 goto bail1;
225 }
226 return;
227
228 bail1:
229 for (d = cache->dirs; d; d = next)
230 {
231 next = d->next;
232 free (d);
233 }
234 cache->dirs = 0;
235
236 close (cache->fd);
237 cache->fd = -1;
238 return;
239
240 bail_and_destroy:
241 close (cache->fd);
242 cache->fd = -1;
243
244 if (stat ((char *) cache_file, &cache_stat) == 0)
245 unlink ((char *)cache_file);
246
247 return;
248
249}
250
251FcBool
252FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
253{
254 FcGlobalCacheDir *d;
255 FcBool ret = FcFalse;
256
257 if (cache->fd == -1)
258 return FcFalse;
259
260 for (d = cache->dirs; d; d = d->next)
261 {
262 if (strncmp (d->name, dir, strlen(dir)) == 0)
263 {
264 lseek (cache->fd, d->offset, SEEK_SET);
265 if (!FcDirCacheConsume (cache->fd, dir, set))
266 return FcFalse;
267 if (strcmp (d->name, dir) == 0)
268 ret = FcTrue;
269 }
270 }
271
272 return ret;
273}
274
275FcBool
276FcGlobalCacheUpdate (FcGlobalCache *cache,
277 const char *name,
278 FcFontSet *set)
279{
280 FcGlobalCacheDir * d;
281
282 if (!set->nfont)
283 return FcTrue;
284
285 for (d = cache->dirs; d; d = d->next)
286 {
287 if (strcmp(d->name, name) == 0)
288 break;
289 }
290
291 if (!d)
292 {
293 d = malloc (sizeof (FcGlobalCacheDir));
294 if (!d)
295 return FcFalse;
296 d->next = cache->dirs;
297 cache->dirs = d;
298 }
299
300 cache->updated = FcTrue;
301
302 d->name = (char *)FcStrCopy ((FcChar8 *)name);
303 d->ent = FcDirCacheProduce (set, &d->metadata);
304 d->offset = 0;
305 return FcTrue;
306}
307
308FcBool
309FcGlobalCacheSave (FcGlobalCache *cache,
310 const FcChar8 *cache_file)
311{
312 int fd, fd_orig;
313 FcGlobalCacheDir *dir;
314 FcAtomic *atomic;
315 off_t current_arch_start = 0, truncate_to;
316 char * current_arch_machine_name, * header;
317
318 if (!cache->updated)
319 return FcTrue;
320
321#if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
322 /* Set-UID programs can't safely update the cache */
323 if (getuid () != geteuid ())
324 return FcFalse;
325#endif
326
327 atomic = FcAtomicCreate (cache_file);
328 if (!atomic)
329 return FcFalse;
330
331 if (!FcAtomicLock (atomic))
332 goto bail1;
333 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
334 S_IRUSR | S_IWUSR);
335 if (fd == -1)
336 goto bail2;
337
338 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
339
340 current_arch_machine_name = FcCacheMachineSignature ();
341 if (fd_orig == -1)
342 current_arch_start = 0;
343 else
344 current_arch_start = FcCacheSkipToArch (fd_orig,
345 current_arch_machine_name);
346
347 if (current_arch_start < 0)
348 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
349
350 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
351 goto bail3;
352
353 close (fd_orig);
354 fd_orig = -1;
355
356 current_arch_start = lseek(fd, 0, SEEK_CUR);
357 if (ftruncate (fd, current_arch_start) == -1)
358 goto bail3;
359
360 header = malloc (10 + strlen (current_arch_machine_name));
361 if (!header)
362 goto bail3;
363
364 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
365 for (dir = cache->dirs; dir; dir = dir->next)
366 {
367 truncate_to += strlen(dir->name) + 1;
368 truncate_to += sizeof (FcCache);
369 truncate_to = FcCacheNextOffset (current_arch_start + truncate_to);
370 truncate_to += dir->metadata.count;
371 }
372 truncate_to -= current_arch_start;
373
374 sprintf (header, "%8x ", (int)truncate_to);
375 strcat (header, current_arch_machine_name);
376 if (!FcCacheWriteString (fd, header))
377 goto bail4;
378
379 for (dir = cache->dirs; dir; dir = dir->next)
380 {
381 if (dir->ent)
382 {
383 FcCacheWriteString (fd, dir->name);
384 write (fd, &dir->metadata, sizeof(FcCache));
385 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
386 write (fd, dir->ent, dir->metadata.count);
387 free (dir->ent);
388 }
389 }
390 FcCacheWriteString (fd, "");
391
392 if (close (fd) == -1)
393 goto bail25;
394
395 if (!FcAtomicReplaceOrig (atomic))
396 goto bail25;
397
398 FcAtomicUnlock (atomic);
399 FcAtomicDestroy (atomic);
400
401 cache->updated = FcFalse;
402 return FcTrue;
403
404 bail4:
405 free (header);
406 bail3:
407 if (fd_orig != -1)
408 close (fd_orig);
409
410 close (fd);
411 bail25:
412 FcAtomicDeleteNew (atomic);
413 bail2:
414 FcAtomicUnlock (atomic);
415 bail1:
416 FcAtomicDestroy (atomic);
417 return FcFalse;
418}
419
420#define PAGESIZE 8192
421/*
422 * Find the next presumably-mmapable offset after the supplied file
423 * position.
424 */
425static int
426FcCacheNextOffset(off_t w)
427{
428 if (w % PAGESIZE == 0)
429 return w;
430 else
431 return ((w / PAGESIZE)+1)*PAGESIZE;
432}
433
434/* return the address of the segment for the provided arch,
435 * or -1 if arch not found */
436static off_t
437FcCacheSkipToArch (int fd, const char * arch)
438{
439 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
440 char * candidate_arch;
441 off_t current_arch_start = 0;
442
443 /* skip arches that are not the current arch */
444 while (1)
445 {
446 long bs;
447
448 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
449 return -1;
450
451 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
452 sizeof (candidate_arch_machine_name_count)) == 0)
453 return -1;
454 if (!strlen(candidate_arch_machine_name_count))
455 return -1;
456 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
457
458 // count = 0 should probably be distinguished from the !bs condition
459 if (!bs || bs < strlen (candidate_arch_machine_name_count))
460 return -1;
461
462 candidate_arch++; /* skip leading space */
463
464 if (strcmp (candidate_arch, arch)==0)
465 return current_arch_start;
466 current_arch_start += bs;
467 }
468
469 return -1;
470}
471
472/* Cuts out the segment at the file pointer (moves everything else
473 * down to cover it), and leaves the file pointer at the end of the
474 * file. */
475static FcBool
476FcCacheCopyOld (int fd, int fd_orig, off_t start)
477{
478 char * buf = malloc (8192);
479 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
480 long bs;
481 int c, bytes_skipped;
482 off_t loc;
483
484 if (!buf)
485 return FcFalse;
486
487 loc = 0;
488 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
489 do
490 {
491 int b = 8192;
492 if (loc + b > start)
493 b = start - loc;
494
495 if ((c = read (fd_orig, buf, b)) <= 0)
496 break;
497 if (write (fd, buf, c) < 0)
498 goto bail;
499
500 loc += c;
501 }
502 while (c > 0);
503
504 lseek (fd, start, SEEK_SET);
505 if (FcCacheReadString (fd, candidate_arch_machine_name,
506 sizeof (candidate_arch_machine_name)) == 0)
507 goto done;
508 if (!strlen(candidate_arch_machine_name))
509 goto done;
510
511 bs = strtol(candidate_arch_machine_name, 0, 16);
512 if (bs == 0)
513 goto done;
514
515 bytes_skipped = 0;
516 do
517 {
518 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
519 if ((c = read (fd, buf, 8192)) <= 0)
520 break;
521 lseek (fd, start+bytes_skipped, SEEK_SET);
522 if (write (fd, buf, c) < 0)
523 goto bail;
524 bytes_skipped += c;
525 }
526 while (c > 0);
527 lseek (fd, start+bytes_skipped, SEEK_SET);
528
529 done:
530 free (buf);
531 return FcTrue;
532
533 bail:
534 free (buf);
535 return FcFalse;
536}
537
538/* Does not check that the cache has the appropriate arch section. */
539FcBool
540FcDirCacheValid (const FcChar8 *dir)
541{
542 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
543 struct stat file_stat, dir_stat;
544
545 if (stat ((char *) dir, &dir_stat) < 0)
546 {
547 FcStrFree (cache_file);
548 return FcFalse;
549 }
550 if (stat ((char *) cache_file, &file_stat) < 0)
551 {
552 FcStrFree (cache_file);
553 return FcFalse;
554 }
555
556 FcStrFree (cache_file);
557 /*
558 * If the directory has been modified more recently than
559 * the cache file, the cache is not valid
560 */
561 if (dir_stat.st_mtime > file_stat.st_mtime)
562 return FcFalse;
563
564 return FcTrue;
565}
566
567/* Assumes that the cache file in 'dir' exists.
568 * Checks that the cache has the appropriate arch section. */
569FcBool
570FcDirCacheHasCurrentArch (const FcChar8 *dir)
571{
572 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
573 int fd;
574 off_t current_arch_start;
575 char *current_arch_machine_name;
576
577 current_arch_machine_name = FcCacheMachineSignature();
578 fd = open ((char *)cache_file, O_RDONLY);
579 if (fd == -1)
580 return FcFalse;
581
582 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
583 close (fd);
584
585 if (current_arch_start < 0)
586 return FcFalse;
587
588 return FcTrue;
589}
590
591FcBool
592FcDirCacheUnlink (const FcChar8 *dir)
593{
594 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
595 struct stat cache_stat;
596
597 if (stat ((char *) cache_file, &cache_stat) == 0 &&
598 unlink ((char *)cache_file) != 0)
599 {
600 FcStrFree (cache_file);
601 return FcFalse;
602 }
603
604 FcStrFree (cache_file);
605 return FcTrue;
606}
607
608static int
609FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
610 FcStrList *list, FcFontSet * set)
611{
612 int ret = 0;
613 FcChar8 *dir;
614 FcChar8 *file, *base;
615 FcStrSet *subdirs;
616 FcStrList *sublist;
617 struct stat statb;
618
619 /*
620 * Read in the results from 'list'.
621 */
622 while ((dir = FcStrListNext (list)))
623 {
624 /* freed below */
625 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
626 if (!file)
627 return FcFalse;
628
629 strcpy ((char *) file, (char *) dir);
630 strcat ((char *) file, "/");
631 base = file + strlen ((char *) file);
632
633 subdirs = FcStrSetCreate ();
634 if (!subdirs)
635 {
636 fprintf (stderr, "Can't create directory set\n");
637 ret++;
638 free (file);
639 continue;
640 }
641
642 if (access ((char *) dir, X_OK) < 0)
643 {
644 switch (errno) {
645 case ENOENT:
646 case ENOTDIR:
647 case EACCES:
648 break;
649 default:
650 fprintf (stderr, "\"%s\": ", dir);
651 perror ("");
652 ret++;
653 }
654 FcStrSetDestroy (subdirs);
655 free (file);
656 continue;
657 }
658 if (stat ((char *) dir, &statb) == -1)
659 {
660 fprintf (stderr, "\"%s\": ", dir);
661 perror ("");
662 FcStrSetDestroy (subdirs);
663 ret++;
664 free (file);
665 continue;
666 }
667 if (!S_ISDIR (statb.st_mode))
668 {
669 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
670 FcStrSetDestroy (subdirs);
671 free (file);
672 continue;
673 }
674 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
675 {
676 if (FcDebug () & FC_DBG_FONTSET)
677 printf ("cache scan dir %s\n", dir);
678
679 FcDirScanConfig (set, subdirs, cache,
680 config->blanks, dir, FcFalse, config);
681 }
682 sublist = FcStrListCreate (subdirs);
683 FcStrSetDestroy (subdirs);
684 if (!sublist)
685 {
686 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
687 ret++;
688 free (file);
689 continue;
690 }
691 ret += FcCacheReadDirs (config, cache, sublist, set);
692 free (file);
693 }
694 FcStrListDone (list);
695 return ret;
696}
697
698FcFontSet *
699FcCacheRead (FcConfig *config, FcGlobalCache * cache)
700{
701 FcFontSet * s = FcFontSetCreate();
702 if (!s)
703 return 0;
704
705 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
706 goto bail;
707
708 return s;
709
710 bail:
711 FcFontSetDestroy (s);
712 return 0;
713}
714
715/* read serialized state from the cache file */
716FcBool
717FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
718{
719 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
720 int fd;
721 char * current_arch_machine_name;
722 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
723 off_t current_arch_start = 0;
724 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
725
726 if (!cache_file)
727 goto bail;
728
729 current_arch_machine_name = FcCacheMachineSignature();
730 fd = open(cache_file, O_RDONLY);
731 if (fd == -1)
732 goto bail;
733
734 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
735 if (current_arch_start < 0)
736 goto bail1;
737
738 lseek (fd, current_arch_start, SEEK_SET);
739 if (FcCacheReadString (fd, candidate_arch_machine_name,
740 sizeof (candidate_arch_machine_name)) == 0)
741 goto bail1;
742
743 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
744 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
745
746 if (!FcDirCacheConsume (fd, (const char *)dir, set))
747 goto bail1;
748
749 close(fd);
750 free (cache_file);
751 return FcTrue;
752
753 bail1:
754 close (fd);
755 bail:
756 free (cache_file);
757 return FcFalse;
758}
759
760static FcBool
761FcDirCacheConsume (int fd, const char * dir, FcFontSet *set)
762{
763 FcCache metadata;
764 void * current_dir_block;
765 off_t pos;
766
767 read(fd, &metadata, sizeof(FcCache));
768 if (metadata.magic != FC_CACHE_MAGIC)
769 return FcFalse;
770
771 if (!metadata.count)
772 return FcTrue;
773
774 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
775 current_dir_block = mmap (0, metadata.count,
776 PROT_READ, MAP_SHARED, fd, pos);
777 if (current_dir_block == MAP_FAILED)
778 return FcFalse;
779
780 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
781 return FcFalse;
782
783 FcCacheAddBankDir (metadata.bank, dir);
784
785 return FcTrue;
786}
787
788static void *
789FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
790{
791 void * current_dir_block, * final_dir_block;
792 static unsigned int rand_state = 0;
793 int bank;
794
795 if (!rand_state)
796 rand_state = time(0L);
797 bank = rand_r(&rand_state);
798
799 while (FcCacheHaveBank(bank))
800 bank = rand_r(&rand_state);
801
802 memset (metadata, 0, sizeof(FcCache));
803 FcFontSetNewBank();
804 metadata->count = FcFontSetNeededBytes (set);
805 metadata->magic = FC_CACHE_MAGIC;
806 metadata->bank = bank;
807
808 if (!metadata->count) /* not a failure, no fonts to write */
809 return 0;
810
811 current_dir_block = malloc (metadata->count);
812 if (!current_dir_block)
813 goto bail;
814 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
815
816 if ((char *)current_dir_block + metadata->count != final_dir_block)
817 goto bail;
818
819 if (!FcFontSetSerialize (bank, set))
820 goto bail;
821
822 return current_dir_block;
823
824 bail:
825 free (current_dir_block);
826 return 0;
827}
828
829/* write serialized state to the cache file */
830FcBool
831FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
832{
833 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
834 int fd, fd_orig, i, dirs_count;
835 FcAtomic *atomic;
836 FcCache metadata;
837 off_t current_arch_start = 0, truncate_to;
838
839 char *current_arch_machine_name, * header;
840 void *current_dir_block;
841
842 if (!cache_file)
843 goto bail;
844
845 current_dir_block = FcDirCacheProduce (set, &metadata);
846
847 if (metadata.count && !current_dir_block)
848 goto bail0;
849
850 if (FcDebug () & FC_DBG_CACHE)
851 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
852
853 atomic = FcAtomicCreate (cache_file);
854 if (!atomic)
855 goto bail0;
856
857 if (!FcAtomicLock (atomic))
858 goto bail1;
859
860 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666);
861
862 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
863 if (fd == -1)
864 goto bail2;
865
866 current_arch_machine_name = FcCacheMachineSignature ();
867 current_arch_start = 0;
868
869 if (fd_orig != -1)
870 current_arch_start =
871 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
872
873 if (current_arch_start < 0)
874 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
875
876 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
877 goto bail3;
878
879 if (fd_orig != -1)
880 close (fd_orig);
881
882 current_arch_start = lseek(fd, 0, SEEK_CUR);
883 if (ftruncate (fd, current_arch_start) == -1)
884 goto bail3;
885
886 /* allocate space for subdir names in this block */
887 dirs_count = 0;
888 for (i = 0; i < dirs->size; i++)
889 dirs_count += strlen((char *)dirs->strs[i]) + 1;
890 dirs_count ++;
891
892 /* now write the address of the next offset */
893 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
894 header = malloc (10 + strlen (current_arch_machine_name));
895 if (!header)
896 goto bail3;
897 sprintf (header, "%8x ", (int)truncate_to);
898 strcat (header, current_arch_machine_name);
899 if (!FcCacheWriteString (fd, header))
900 goto bail4;
901
902 for (i = 0; i < dirs->size; i++)
903 FcCacheWriteString (fd, (char *)dirs->strs[i]);
904 FcCacheWriteString (fd, "");
905
906 write (fd, &metadata, sizeof(FcCache));
907 if (metadata.count)
908 {
909 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
910 write (fd, current_dir_block, metadata.count);
911 free (current_dir_block);
912 }
913
914 /* this actually serves to pad out the cache file, if needed */
915 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
916 goto bail4;
917
918 close(fd);
919 if (!FcAtomicReplaceOrig(atomic))
920 goto bail4;
921 FcAtomicUnlock (atomic);
922 FcAtomicDestroy (atomic);
923 return FcTrue;
924
925 bail4:
926 free (header);
927 bail3:
928 close (fd);
929 bail2:
930 FcAtomicUnlock (atomic);
931 bail1:
932 FcAtomicDestroy (atomic);
933 bail0:
934 unlink ((char *)cache_file);
935 free (cache_file);
936 if (current_dir_block)
937 free (current_dir_block);
938 bail:
939 return FcFalse;
940}
941
942static char *
943FcCacheMachineSignature ()
944{
945 static char buf[MACHINE_SIGNATURE_SIZE];
946 int magic = ENDIAN_TEST;
947 char * m = (char *)&magic;
948
949 sprintf (buf, "%2x%2x%2x%2x "
950 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
951 "%4x %4x %4x %4x %4x %4x %4x\n",
952 m[0], m[1], m[2], m[3],
953 (unsigned int)sizeof (char),
954 (unsigned int)sizeof (char *),
955 (unsigned int)sizeof (int),
956 (unsigned int)sizeof (FcPattern),
957 (unsigned int)sizeof (FcPatternEltPtr),
958 (unsigned int)sizeof (struct _FcPatternElt *),
959 (unsigned int)sizeof (FcPatternElt),
960 (unsigned int)sizeof (FcObjectPtr),
961 (unsigned int)sizeof (FcValueListPtr),
962 (unsigned int)sizeof (FcValue),
963 (unsigned int)sizeof (FcValueBinding),
964 (unsigned int)sizeof (struct _FcValueList *),
965 (unsigned int)sizeof (FcCharSet),
966 (unsigned int)sizeof (FcCharLeaf **),
967 (unsigned int)sizeof (FcChar16 *),
968 (unsigned int)sizeof (FcChar16),
969 (unsigned int)sizeof (FcCharLeaf),
970 (unsigned int)sizeof (FcChar32),
971 (unsigned int)sizeof (FcCache));
972
973 return buf;
974}
975
976static int banks_ptr = 0, banks_alloc = 0;
977static int * bankId = 0, * bankIdx = 0;
978static const char ** bankDirs = 0;
979
980static FcBool
981FcCacheHaveBank (int bank)
982{
983 int i;
984
985 if (bank < FC_BANK_FIRST)
986 return FcTrue;
987
988 for (i = 0; i < banks_ptr; i++)
989 if (bankId[i] == bank)
990 return FcTrue;
991
992 return FcFalse;
993}
994
995int
996FcCacheBankToIndex (int bank)
997{
998 int i, j;
999
1000 for (i = 0; i < banks_ptr; i++)
1001 if (bankId[bankIdx[i]] == bank)
1002 {
1003 int t = bankIdx[i];
1004
1005 for (j = i; j > 0; j--)
1006 bankIdx[j] = bankIdx[j-1];
1007 bankIdx[0] = t;
1008 return t;
1009 }
1010
1011 if (banks_ptr >= banks_alloc)
1012 {
1013 int * b, * bidx;
1014 const char ** bds;
1015
1016 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
1017 if (!b)
1018 return -1;
1019 bankId = b;
1020
1021 bidx = realloc (bankIdx, (banks_alloc + 4) * sizeof(int));
1022 if (!bidx)
1023 return -1;
1024 bankIdx = bidx;
1025
1026 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1027 if (!bds)
1028 return -1;
1029 bankDirs = bds;
1030
1031 banks_alloc += 4;
1032 }
1033
1034 i = banks_ptr++;
1035 bankId[i] = bank;
1036 bankIdx[i] = i;
1037 return i;
1038}
1039
1040static void
1041FcCacheAddBankDir (int bank, const char * dir)
1042{
1043 int bi = FcCacheBankToIndex (bank);
1044
1045 if (bi < 0)
1046 return;
1047
1048 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1049}
1050
1051const char *
1052FcCacheFindBankDir (int bank)
1053{
1054 int bi = FcCacheBankToIndex (bank);
1055 return bankDirs[bi];
1056}
1057