]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
src/fcint.c
[fontconfig.git] / src / fccache.c
CommitLineData
24330d27 1/*
4bd4418a 2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
24330d27 3 *
46b51147 4 * Copyright © 2000 Keith Packard
24330d27
KP
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
212c9f43 25#include <fcntl.h>
4262e0b3 26#include <dirent.h>
212c9f43
PL
27#include <sys/mman.h>
28#include <sys/utsname.h>
24330d27
KP
29#include "fcint.h"
30
2dbe7597
PL
31#define ENDIAN_TEST 0x12345678
32#define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1
33
eb0cf671
PL
34static off_t
35FcCacheSkipToArch (int fd, const char * arch);
1b7be377 36
eb0cf671
PL
37static FcBool
38FcCacheMoveDown (int fd, off_t start);
1b7be377 39
eb0cf671
PL
40static void *
41FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
1b7be377
PL
42
43static FcBool
eb0cf671 44FcDirCacheConsume (int fd, FcFontSet *set);
1b7be377
PL
45
46static FcBool
eb0cf671 47FcDirCacheRead (FcFontSet * set, const FcChar8 *dir);
1b7be377 48
eb0cf671
PL
49static int
50FcCacheNextOffset(off_t w);
1b7be377 51
eb0cf671
PL
52static char *
53FcCacheMachineSignature (void);
1b7be377
PL
54
55static FcBool
eb0cf671 56FcCacheHaveBank (int bank);
1b7be377 57
eb0cf671 58#define FC_DBG_CACHE_REF 1024
1b7be377
PL
59
60static FcChar8 *
eb0cf671 61FcCacheReadString (int fd, FcChar8 *dest, int len)
24330d27 62{
212c9f43 63 FcChar8 c;
ccb3e93b 64 FcBool escape;
ccb3e93b
KP
65 int size;
66 int i;
24330d27 67
24330d27
KP
68 if (len == 0)
69 return FcFalse;
70
ccb3e93b
KP
71 size = len;
72 i = 0;
24330d27 73 escape = FcFalse;
212c9f43 74 while (read (fd, &c, 1) == 1)
24330d27
KP
75 {
76 if (!escape)
77 {
78 switch (c) {
79 case '"':
ccb3e93b
KP
80 c = '\0';
81 break;
24330d27
KP
82 case '\\':
83 escape = FcTrue;
84 continue;
85 }
86 }
ccb3e93b
KP
87 if (i == size)
88 {
212c9f43
PL
89 dest[i++] = 0;
90 return dest;
ccb3e93b 91 }
212c9f43 92 dest[i++] = c;
ccb3e93b 93 if (c == '\0')
212c9f43 94 return dest;
24330d27
KP
95 escape = FcFalse;
96 }
ccb3e93b 97 return 0;
24330d27
KP
98}
99
100static FcBool
eb0cf671 101FcCacheWriteString (int fd, const FcChar8 *chars)
327a7fd4 102{
212c9f43 103 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
327a7fd4 104 return FcFalse;
327a7fd4
KP
105 return FcTrue;
106}
107
eb0cf671
PL
108static void
109FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
1b7be377 110{
eb0cf671
PL
111 FcGlobalCacheDir *dd, *next;
112
113 for (dd = d; dd; dd = next)
1b7be377 114 {
eb0cf671
PL
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);
1b7be377 120 }
1b7be377
PL
121}
122
eb0cf671
PL
123FcGlobalCache *
124FcGlobalCacheCreate (void)
1b7be377 125{
eb0cf671 126 FcGlobalCache *cache;
1b7be377 127
eb0cf671
PL
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;
1b7be377
PL
136}
137
eb0cf671
PL
138void
139FcGlobalCacheDestroy (FcGlobalCache *cache)
327a7fd4 140{
eb0cf671 141 FcGlobalCacheDir *d, *next;
327a7fd4 142
eb0cf671 143 for (d = cache->dirs; d; d = next)
327a7fd4 144 {
eb0cf671
PL
145 next = d->next;
146 FcGlobalCacheDirDestroy (d);
327a7fd4 147 }
eb0cf671
PL
148 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
149 free (cache);
327a7fd4
KP
150}
151
152void
eb0cf671
PL
153FcGlobalCacheLoad (FcGlobalCache *cache,
154 const FcChar8 *cache_file)
327a7fd4 155{
eb0cf671
PL
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;
327a7fd4 161
eb0cf671
PL
162 cache->fd = open ((char *) cache_file, O_RDONLY);
163 if (cache->fd == -1)
164 return;
327a7fd4 165
eb0cf671 166 cache->updated = FcFalse;
327a7fd4 167
eb0cf671
PL
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;
327a7fd4 173
eb0cf671
PL
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;
1b7be377 178
eb0cf671 179 while (1)
1b7be377 180 {
eb0cf671
PL
181 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
182 if (!strlen(name_buf))
1b7be377 183 break;
1b7be377 184
eb0cf671
PL
185 d = malloc (sizeof (FcGlobalCacheDir));
186 if (!d)
187 goto bail1;
1b7be377 188
eb0cf671
PL
189 d->next = cache->dirs;
190 cache->dirs = d;
1b7be377 191
eb0cf671
PL
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);
1b7be377 197 }
eb0cf671 198 return;
1b7be377 199
eb0cf671
PL
200 bail1:
201 for (d = cache->dirs; d; d = next)
1b7be377 202 {
eb0cf671
PL
203 next = d->next;
204 free (d);
1b7be377 205 }
eb0cf671
PL
206 cache->dirs = 0;
207 bail0:
208 free (current_arch_machine_name);
209 close (cache->fd);
210 cache->fd = -1;
211 return;
1b7be377
PL
212}
213
1b7be377 214FcBool
eb0cf671 215FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const FcChar8 *dir, FcConfig *config)
1b7be377 216{
eb0cf671 217 FcGlobalCacheDir *d;
1b7be377 218
eb0cf671 219 if (cache->fd == -1)
1b7be377 220 return FcFalse;
1b7be377 221
eb0cf671 222 for (d = cache->dirs; d; d = d->next)
1b7be377 223 {
eb0cf671 224 if (strcmp (d->name, dir) == 0)
1b7be377 225 {
eb0cf671
PL
226 lseek (cache->fd, d->offset, SEEK_SET);
227 if (!FcDirCacheConsume (cache->fd, set))
228 return FcFalse;
229 return FcTrue;
1b7be377 230 }
1b7be377 231 }
1b7be377 232
eb0cf671 233 return FcFalse;
1b7be377
PL
234}
235
eb0cf671
PL
236FcBool
237FcGlobalCacheUpdate (FcGlobalCache *cache,
238 const FcChar8 *name,
239 FcFontSet *set)
1b7be377 240{
eb0cf671 241 FcGlobalCacheDir * d;
1b7be377 242
eb0cf671
PL
243 if (!set->nfont)
244 return FcTrue;
1b7be377 245
eb0cf671 246 for (d = cache->dirs; d; d = d->next)
1b7be377 247 {
eb0cf671 248 if (strcmp(d->name, name) == 0)
1b7be377 249 break;
24330d27 250 }
24330d27 251
eb0cf671 252 if (!d)
24330d27 253 {
eb0cf671
PL
254 d = malloc (sizeof (FcGlobalCacheDir));
255 if (!d)
256 return FcFalse;
257 d->next = cache->dirs;
258 cache->dirs = d;
24330d27 259 }
24330d27 260
eb0cf671 261 cache->updated = FcTrue;
24330d27 262
eb0cf671
PL
263 d->name = FcStrCopy (name);
264 d->ent = FcDirCacheProduce (set, &d->metadata);
265 d->offset = 0;
266 return FcTrue;
24330d27
KP
267}
268
269FcBool
327a7fd4
KP
270FcGlobalCacheSave (FcGlobalCache *cache,
271 const FcChar8 *cache_file)
24330d27 272{
eb0cf671 273 int fd;
327a7fd4 274 FcGlobalCacheDir *dir;
327a7fd4 275 FcAtomic *atomic;
eb0cf671
PL
276 off_t current_arch_start = 0, truncate_to;
277 char * current_arch_machine_name, * header;
24330d27 278
eb0cf671 279 if (!cache->updated)
24330d27
KP
280 return FcTrue;
281
daeed6e0 282#if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
a391da8f
KP
283 /* Set-UID programs can't safely update the cache */
284 if (getuid () != geteuid ())
285 return FcFalse;
daeed6e0 286#endif
a391da8f 287
134f6011
KP
288 atomic = FcAtomicCreate (cache_file);
289 if (!atomic)
24330d27 290 goto bail0;
134f6011 291 if (!FcAtomicLock (atomic))
24330d27 292 goto bail1;
eb0cf671
PL
293 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
294 S_IRUSR | S_IWUSR);
295 if (fd == -1)
24330d27
KP
296 goto bail2;
297
eb0cf671
PL
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)
24330d27 312 {
eb0cf671
PL
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);
24330d27 333 }
eb0cf671 334 FcCacheWriteString (fd, "");
24330d27 335
eb0cf671 336 if (close (fd) == -1)
24330d27
KP
337 goto bail3;
338
134f6011 339 if (!FcAtomicReplaceOrig (atomic))
24330d27
KP
340 goto bail3;
341
134f6011
KP
342 FcAtomicUnlock (atomic);
343 FcAtomicDestroy (atomic);
344
24330d27
KP
345 cache->updated = FcFalse;
346 return FcTrue;
347
24330d27 348bail3:
134f6011 349 FcAtomicDeleteNew (atomic);
24330d27 350bail2:
134f6011 351 FcAtomicUnlock (atomic);
24330d27 352bail1:
134f6011 353 FcAtomicDestroy (atomic);
24330d27
KP
354bail0:
355 return FcFalse;
356}
357
eb0cf671 358#define PAGESIZE 8192
212c9f43 359/*
eb0cf671
PL
360 * Find the next presumably-mmapable offset after the supplied file
361 * position.
212c9f43 362 */
4262e0b3
PL
363static int
364FcCacheNextOffset(off_t w)
179c3995 365{
212c9f43
PL
366 if (w % PAGESIZE == 0)
367 return w;
368 else
369 return ((w / PAGESIZE)+1)*PAGESIZE;
179c3995
KP
370}
371
212c9f43
PL
372/* return the address of the segment for the provided arch,
373 * or -1 if arch not found */
374static off_t
375FcCacheSkipToArch (int fd, const char * arch)
376{
2dbe7597
PL
377 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
378 char * candidate_arch;
212c9f43
PL
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);
eb0cf671 387 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
2dbe7597 388 sizeof (candidate_arch_machine_name_count)) == 0)
212c9f43 389 break;
2dbe7597
PL
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 */
212c9f43 394
2dbe7597 395 if (strcmp (candidate_arch, arch)==0)
212c9f43
PL
396 break;
397 current_arch_start += bs;
398 }
399
eb0cf671 400 if (candidate_arch && strcmp (candidate_arch, arch)!=0)
212c9f43
PL
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. */
212c9f43
PL
409static FcBool
410FcCacheMoveDown (int fd, off_t start)
411{
eb0cf671 412 char * buf = malloc (8192);
2dbe7597 413 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
4262e0b3 414 long bs;
212c9f43
PL
415 int c, bytes_skipped;
416
417 if (!buf)
418 return FcFalse;
419
420 lseek (fd, start, SEEK_SET);
eb0cf671 421 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43
PL
422 sizeof (candidate_arch_machine_name)) == 0)
423 goto done;
2dbe7597 424 if (!strlen(candidate_arch_machine_name))
212c9f43
PL
425 goto done;
426
2dbe7597 427 bs = strtol(candidate_arch_machine_name, 0, 16);
212c9f43
PL
428 if (bs == 0)
429 goto done;
430
431 bytes_skipped = 0;
432 do
433 {
434 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
eb0cf671 435 if ((c = read (fd, buf, 8192)) <= 0)
212c9f43
PL
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
1b7be377
PL
454FcBool
455FcDirCacheValid (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
4262e0b3 480static int
1b7be377
PL
481FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
482 FcStrList *list, FcFontSet * set)
4262e0b3
PL
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);
2dbe7597 566 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, dir))
4262e0b3 567 {
1b7be377
PL
568 if (FcDebug () & FC_DBG_FONTSET)
569 printf ("scan dir %s\n", dir);
570 FcDirScanConfig (set, subdirs, cache,
571 config->blanks, dir, FcFalse, config);
4262e0b3
PL
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 }
1b7be377 582 ret += FcCacheReadDirs (config, cache, sublist, set);
4262e0b3
PL
583 free (file);
584 }
585 FcStrListDone (list);
586 return ret;
587}
588
589FcFontSet *
1b7be377 590FcCacheRead (FcConfig *config, FcGlobalCache * cache)
4262e0b3
PL
591{
592 FcFontSet * s = FcFontSetCreate();
593 if (!s)
594 return 0;
595
1b7be377 596 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
4262e0b3
PL
597 goto bail;
598
599 return s;
600
601 bail:
602 FcFontSetDestroy (s);
603 return 0;
604}
605
212c9f43 606/* read serialized state from the cache file */
eb0cf671 607static FcBool
4262e0b3 608FcDirCacheRead (FcFontSet * set, const FcChar8 *dir)
212c9f43 609{
4262e0b3
PL
610 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
611 int fd;
212c9f43 612 char * current_arch_machine_name;
2dbe7597 613 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
212c9f43
PL
614 off_t current_arch_start = 0;
615
4262e0b3
PL
616 if (!cache_file)
617 goto bail;
212c9f43 618
eb0cf671 619 current_arch_machine_name = FcCacheMachineSignature();
4262e0b3 620 fd = open(cache_file, O_RDONLY);
212c9f43 621 if (fd == -1)
2dbe7597 622 goto bail;
212c9f43 623
212c9f43
PL
624 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
625 if (current_arch_start < 0)
4262e0b3 626 goto bail1;
212c9f43
PL
627
628 lseek (fd, current_arch_start, SEEK_SET);
eb0cf671 629 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43 630 sizeof (candidate_arch_machine_name)) == 0)
4262e0b3 631 goto bail1;
eb0cf671
PL
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
647static FcBool
648FcDirCacheConsume (int fd, FcFontSet *set)
649{
650 FcCache metadata;
651 void * current_dir_block;
212c9f43 652
212c9f43
PL
653 read(fd, &metadata, sizeof(FcCache));
654 if (metadata.magic != FC_CACHE_MAGIC)
eb0cf671 655 return FcFalse;
212c9f43 656
2dbe7597 657 if (!metadata.count)
eb0cf671 658 return FcTrue;
2dbe7597
PL
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)
eb0cf671 664 return FcFalse;
2dbe7597
PL
665
666 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
eb0cf671
PL
667 return FcFalse;
668
212c9f43 669 return FcTrue;
eb0cf671
PL
670}
671
672static void *
673FcDirCacheProduce (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;
212c9f43 707
4262e0b3 708 bail:
eb0cf671
PL
709 free (current_dir_block);
710 return 0;
212c9f43
PL
711}
712
713/* write serialized state to the cache file */
714FcBool
eb0cf671 715FcDirCacheWrite (FcFontSet *set, const FcChar8 *dir)
212c9f43 716{
4262e0b3 717 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
eb0cf671 718 int fd;
212c9f43
PL
719 FcCache metadata;
720 off_t current_arch_start = 0, truncate_to;
2dbe7597 721 char * current_arch_machine_name, * header;
eb0cf671 722 void * current_dir_block;
212c9f43 723
4262e0b3
PL
724 if (!cache_file)
725 goto bail;
726
eb0cf671 727 current_dir_block = FcDirCacheProduce (set, &metadata);
212c9f43 728
eb0cf671 729 if (!metadata.count)
4262e0b3
PL
730 {
731 unlink (cache_file);
732 free (cache_file);
733 return FcTrue;
734 }
735
4262e0b3
PL
736 if (!current_dir_block)
737 goto bail;
212c9f43 738
4262e0b3
PL
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);
212c9f43 743 if (fd == -1)
eb0cf671 744 goto bail0;
212c9f43 745
eb0cf671 746 current_arch_machine_name = FcCacheMachineSignature ();
212c9f43
PL
747 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
748 if (current_arch_start < 0)
4262e0b3 749 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
212c9f43
PL
750
751 if (!FcCacheMoveDown(fd, current_arch_start))
eb0cf671 752 goto bail2;
212c9f43
PL
753
754 current_arch_start = lseek(fd, 0, SEEK_CUR);
755 if (ftruncate (fd, current_arch_start) == -1)
eb0cf671 756 goto bail2;
212c9f43 757
212c9f43 758 /* now write the address of the next offset */
eb0cf671 759 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start;
2dbe7597 760 header = malloc (10 + strlen (current_arch_machine_name));
eb0cf671
PL
761 if (!header)
762 goto bail1;
2dbe7597
PL
763 sprintf (header, "%8x ", (int)truncate_to);
764 strcat (header, current_arch_machine_name);
eb0cf671 765 if (!FcCacheWriteString (fd, header))
4262e0b3 766 goto bail1;
212c9f43 767
4262e0b3
PL
768 write (fd, &metadata, sizeof(FcCache));
769 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
eb0cf671 770 write (fd, current_dir_block, metadata.count);
4262e0b3 771
2dbe7597 772 /* this actually serves to pad out the cache file, if needed */
212c9f43 773 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
4262e0b3 774 goto bail1;
212c9f43
PL
775
776 close(fd);
777 return FcTrue;
778
eb0cf671
PL
779 bail2:
780 free (header);
4262e0b3 781 bail1:
212c9f43 782 free (current_arch_machine_name);
eb0cf671
PL
783 bail0:
784 free (current_dir_block);
4262e0b3
PL
785 bail:
786 unlink (cache_file);
787 free (cache_file);
212c9f43
PL
788 return FcFalse;
789}
790
2dbe7597 791static char *
eb0cf671 792FcCacheMachineSignature ()
2dbe7597
PL
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
4262e0b3
PL
825static int banks_ptr = 0, banks_alloc = 0;
826static int * bankId = 0;
827
eb0cf671 828static FcBool
4262e0b3
PL
829FcCacheHaveBank (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
843int
844FcCacheBankToIndex (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
2dbe7597 857 if (banks_ptr >= banks_alloc)
4262e0b3 858 {
2dbe7597 859 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
4262e0b3
PL
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}