]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
Merge with HEAD and finish the GCC 4 cleanups (no more warnings!)
[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
03a212e5 5 * Copyright © 2005 Patrick Lam
24330d27
KP
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
212c9f43 26#include <fcntl.h>
4262e0b3 27#include <dirent.h>
212c9f43
PL
28#include <sys/mman.h>
29#include <sys/utsname.h>
fd77c154
PL
30#include <sys/types.h>
31#include <unistd.h>
24330d27
KP
32#include "fcint.h"
33
2dbe7597
PL
34#define ENDIAN_TEST 0x12345678
35#define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1
36
eb0cf671
PL
37static off_t
38FcCacheSkipToArch (int fd, const char * arch);
1b7be377 39
eb0cf671
PL
40static FcBool
41FcCacheMoveDown (int fd, off_t start);
1b7be377 42
eb0cf671
PL
43static void *
44FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
1b7be377
PL
45
46static FcBool
eb0cf671 47FcDirCacheConsume (int fd, FcFontSet *set);
1b7be377
PL
48
49static FcBool
2304e38f 50FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
1b7be377 51
eb0cf671
PL
52static int
53FcCacheNextOffset(off_t w);
1b7be377 54
eb0cf671
PL
55static char *
56FcCacheMachineSignature (void);
1b7be377
PL
57
58static FcBool
eb0cf671 59FcCacheHaveBank (int bank);
1b7be377 60
eb0cf671 61#define FC_DBG_CACHE_REF 1024
1b7be377 62
8245771d
PL
63static char *
64FcCacheReadString (int fd, char *dest, int len)
24330d27 65{
212c9f43 66 FcChar8 c;
ccb3e93b 67 FcBool escape;
ccb3e93b
KP
68 int size;
69 int i;
24330d27 70
24330d27 71 if (len == 0)
03a212e5 72 return 0;
24330d27 73
ccb3e93b
KP
74 size = len;
75 i = 0;
24330d27 76 escape = FcFalse;
212c9f43 77 while (read (fd, &c, 1) == 1)
24330d27
KP
78 {
79 if (!escape)
80 {
81 switch (c) {
82 case '"':
ccb3e93b
KP
83 c = '\0';
84 break;
24330d27
KP
85 case '\\':
86 escape = FcTrue;
87 continue;
88 }
89 }
ccb3e93b
KP
90 if (i == size)
91 {
212c9f43
PL
92 dest[i++] = 0;
93 return dest;
ccb3e93b 94 }
212c9f43 95 dest[i++] = c;
ccb3e93b 96 if (c == '\0')
212c9f43 97 return dest;
24330d27
KP
98 escape = FcFalse;
99 }
ccb3e93b 100 return 0;
24330d27
KP
101}
102
103static FcBool
8245771d 104FcCacheWriteString (int fd, const char *chars)
327a7fd4 105{
212c9f43 106 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
327a7fd4 107 return FcFalse;
327a7fd4
KP
108 return FcTrue;
109}
110
eb0cf671
PL
111static void
112FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
1b7be377 113{
5e678e94
PL
114 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
115 free (d->name);
116 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
117 free (d);
1b7be377
PL
118}
119
eb0cf671
PL
120FcGlobalCache *
121FcGlobalCacheCreate (void)
1b7be377 122{
eb0cf671 123 FcGlobalCache *cache;
1b7be377 124
eb0cf671
PL
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;
1b7be377
PL
133}
134
eb0cf671
PL
135void
136FcGlobalCacheDestroy (FcGlobalCache *cache)
327a7fd4 137{
eb0cf671 138 FcGlobalCacheDir *d, *next;
327a7fd4 139
eb0cf671 140 for (d = cache->dirs; d; d = next)
327a7fd4 141 {
eb0cf671
PL
142 next = d->next;
143 FcGlobalCacheDirDestroy (d);
327a7fd4 144 }
eb0cf671
PL
145 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
146 free (cache);
327a7fd4
KP
147}
148
149void
eb0cf671 150FcGlobalCacheLoad (FcGlobalCache *cache,
03a212e5 151 FcStrSet *staleDirs,
eb0cf671 152 const FcChar8 *cache_file)
327a7fd4 153{
8245771d 154 char name_buf[8192];
eb0cf671
PL
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;
327a7fd4 159
03a212e5
PL
160 struct stat cache_stat, dir_stat;
161
162 if (stat ((char *) cache_file, &cache_stat) < 0)
163 return;
164
eb0cf671
PL
165 cache->fd = open ((char *) cache_file, O_RDONLY);
166 if (cache->fd == -1)
167 return;
327a7fd4 168
eb0cf671 169 cache->updated = FcFalse;
327a7fd4 170
eb0cf671
PL
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;
327a7fd4 176
eb0cf671 177 lseek (cache->fd, current_arch_start, SEEK_SET);
03a212e5
PL
178 FcCacheReadString (cache->fd, candidate_arch_machine_name,
179 sizeof (candidate_arch_machine_name));
180 if (strlen(candidate_arch_machine_name) == 0)
eb0cf671 181 goto bail0;
1b7be377 182
eb0cf671 183 while (1)
1b7be377 184 {
8cb4c56d
PL
185 off_t targ;
186
eb0cf671
PL
187 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
188 if (!strlen(name_buf))
1b7be377 189 break;
1b7be377 190
03a212e5
PL
191 if (stat ((char *) name_buf, &dir_stat) < 0 ||
192 dir_stat.st_mtime > cache_stat.st_mtime)
193 {
194 FcCache md;
195
8245771d 196 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
03a212e5
PL
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
eb0cf671
PL
202 d = malloc (sizeof (FcGlobalCacheDir));
203 if (!d)
204 goto bail1;
1b7be377 205
eb0cf671
PL
206 d->next = cache->dirs;
207 cache->dirs = d;
1b7be377 208
8245771d 209 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
eb0cf671
PL
210 d->ent = 0;
211 d->offset = lseek (cache->fd, 0, SEEK_CUR);
8cb4c56d
PL
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;
1b7be377 217 }
eb0cf671 218 return;
1b7be377 219
eb0cf671
PL
220 bail1:
221 for (d = cache->dirs; d; d = next)
1b7be377 222 {
eb0cf671
PL
223 next = d->next;
224 free (d);
1b7be377 225 }
eb0cf671
PL
226 cache->dirs = 0;
227 bail0:
eb0cf671
PL
228 close (cache->fd);
229 cache->fd = -1;
230 return;
1b7be377
PL
231}
232
1b7be377 233FcBool
8245771d 234FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
1b7be377 235{
eb0cf671 236 FcGlobalCacheDir *d;
03a212e5 237 FcBool ret = FcFalse;
1b7be377 238
eb0cf671 239 if (cache->fd == -1)
1b7be377 240 return FcFalse;
1b7be377 241
eb0cf671 242 for (d = cache->dirs; d; d = d->next)
1b7be377 243 {
03a212e5 244 if (strncmp (d->name, dir, strlen(dir)) == 0)
1b7be377 245 {
eb0cf671
PL
246 lseek (cache->fd, d->offset, SEEK_SET);
247 if (!FcDirCacheConsume (cache->fd, set))
248 return FcFalse;
03a212e5
PL
249 if (strcmp (d->name, dir) == 0)
250 ret = FcTrue;
1b7be377 251 }
1b7be377 252 }
1b7be377 253
03a212e5 254 return ret;
1b7be377
PL
255}
256
eb0cf671
PL
257FcBool
258FcGlobalCacheUpdate (FcGlobalCache *cache,
8245771d 259 const char *name,
eb0cf671 260 FcFontSet *set)
1b7be377 261{
eb0cf671 262 FcGlobalCacheDir * d;
1b7be377 263
eb0cf671
PL
264 if (!set->nfont)
265 return FcTrue;
1b7be377 266
eb0cf671 267 for (d = cache->dirs; d; d = d->next)
1b7be377 268 {
eb0cf671 269 if (strcmp(d->name, name) == 0)
1b7be377 270 break;
24330d27 271 }
24330d27 272
eb0cf671 273 if (!d)
24330d27 274 {
eb0cf671
PL
275 d = malloc (sizeof (FcGlobalCacheDir));
276 if (!d)
277 return FcFalse;
278 d->next = cache->dirs;
279 cache->dirs = d;
24330d27 280 }
24330d27 281
eb0cf671 282 cache->updated = FcTrue;
24330d27 283
8245771d 284 d->name = (char *)FcStrCopy ((FcChar8 *)name);
eb0cf671
PL
285 d->ent = FcDirCacheProduce (set, &d->metadata);
286 d->offset = 0;
287 return FcTrue;
24330d27
KP
288}
289
290FcBool
327a7fd4
KP
291FcGlobalCacheSave (FcGlobalCache *cache,
292 const FcChar8 *cache_file)
24330d27 293{
eb0cf671 294 int fd;
327a7fd4 295 FcGlobalCacheDir *dir;
327a7fd4 296 FcAtomic *atomic;
eb0cf671
PL
297 off_t current_arch_start = 0, truncate_to;
298 char * current_arch_machine_name, * header;
24330d27 299
eb0cf671 300 if (!cache->updated)
24330d27
KP
301 return FcTrue;
302
daeed6e0 303#if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
a391da8f
KP
304 /* Set-UID programs can't safely update the cache */
305 if (getuid () != geteuid ())
306 return FcFalse;
daeed6e0 307#endif
a391da8f 308
134f6011
KP
309 atomic = FcAtomicCreate (cache_file);
310 if (!atomic)
24330d27 311 goto bail0;
134f6011 312 if (!FcAtomicLock (atomic))
24330d27 313 goto bail1;
eb0cf671
PL
314 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
315 S_IRUSR | S_IWUSR);
316 if (fd == -1)
24330d27
KP
317 goto bail2;
318
eb0cf671
PL
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
03a212e5
PL
331 header = malloc (10 + strlen (current_arch_machine_name));
332 if (!header)
333 goto bail1;
03a212e5 334
8cb4c56d 335 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
eb0cf671 336 for (dir = cache->dirs; dir; dir = dir->next)
24330d27 337 {
eb0cf671
PL
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;
eb0cf671 344
8cb4c56d
PL
345 sprintf (header, "%8x ", (int)truncate_to);
346 strcat (header, current_arch_machine_name);
347 if (!FcCacheWriteString (fd, header))
348 goto bail1;
349
eb0cf671
PL
350 for (dir = cache->dirs; dir; dir = dir->next)
351 {
5e678e94
PL
352 if (dir->ent)
353 {
354 FcCacheWriteString (fd, dir->name);
355 write (fd, &dir->metadata, sizeof(FcCache));
f6ee3db5 356 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
5e678e94
PL
357 write (fd, dir->ent, dir->metadata.count);
358 free (dir->ent);
359 }
24330d27 360 }
eb0cf671 361 FcCacheWriteString (fd, "");
24330d27 362
eb0cf671 363 if (close (fd) == -1)
24330d27
KP
364 goto bail3;
365
134f6011 366 if (!FcAtomicReplaceOrig (atomic))
24330d27
KP
367 goto bail3;
368
134f6011
KP
369 FcAtomicUnlock (atomic);
370 FcAtomicDestroy (atomic);
371
24330d27
KP
372 cache->updated = FcFalse;
373 return FcTrue;
374
24330d27 375bail3:
134f6011 376 FcAtomicDeleteNew (atomic);
24330d27 377bail2:
134f6011 378 FcAtomicUnlock (atomic);
24330d27 379bail1:
134f6011 380 FcAtomicDestroy (atomic);
24330d27
KP
381bail0:
382 return FcFalse;
383}
384
eb0cf671 385#define PAGESIZE 8192
212c9f43 386/*
eb0cf671
PL
387 * Find the next presumably-mmapable offset after the supplied file
388 * position.
212c9f43 389 */
4262e0b3
PL
390static int
391FcCacheNextOffset(off_t w)
179c3995 392{
212c9f43
PL
393 if (w % PAGESIZE == 0)
394 return w;
395 else
396 return ((w / PAGESIZE)+1)*PAGESIZE;
179c3995
KP
397}
398
212c9f43
PL
399/* return the address of the segment for the provided arch,
400 * or -1 if arch not found */
401static off_t
402FcCacheSkipToArch (int fd, const char * arch)
403{
2dbe7597
PL
404 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
405 char * candidate_arch;
212c9f43
PL
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
8cb4c56d
PL
413 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
414 return -1;
415
eb0cf671 416 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
2dbe7597 417 sizeof (candidate_arch_machine_name_count)) == 0)
5e678e94 418 return -1;
2dbe7597
PL
419 if (!strlen(candidate_arch_machine_name_count))
420 return -1;
421 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
8cb4c56d
PL
422
423 if (!bs || bs < strlen (candidate_arch_machine_name_count))
424 return -1;
425
2dbe7597 426 candidate_arch++; /* skip leading space */
212c9f43 427
2dbe7597 428 if (strcmp (candidate_arch, arch)==0)
5e678e94 429 return current_arch_start;
212c9f43
PL
430 current_arch_start += bs;
431 }
432
5e678e94 433 return -1;
212c9f43
PL
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. */
212c9f43
PL
439static FcBool
440FcCacheMoveDown (int fd, off_t start)
441{
eb0cf671 442 char * buf = malloc (8192);
2dbe7597 443 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
4262e0b3 444 long bs;
212c9f43
PL
445 int c, bytes_skipped;
446
447 if (!buf)
448 return FcFalse;
449
450 lseek (fd, start, SEEK_SET);
eb0cf671 451 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43
PL
452 sizeof (candidate_arch_machine_name)) == 0)
453 goto done;
2dbe7597 454 if (!strlen(candidate_arch_machine_name))
212c9f43
PL
455 goto done;
456
2dbe7597 457 bs = strtol(candidate_arch_machine_name, 0, 16);
212c9f43
PL
458 if (bs == 0)
459 goto done;
460
461 bytes_skipped = 0;
462 do
463 {
464 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
eb0cf671 465 if ((c = read (fd, buf, 8192)) <= 0)
212c9f43
PL
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
1b7be377
PL
484FcBool
485FcDirCacheValid (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
4262e0b3 510static int
1b7be377
PL
511FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
512 FcStrList *list, FcFontSet * set)
4262e0b3 513{
4262e0b3
PL
514 int ret = 0;
515 FcChar8 *dir;
516 FcChar8 *file, *base;
517 FcStrSet *subdirs;
518 FcStrList *sublist;
519 struct stat statb;
520
521 /*
03a212e5 522 * Read in the results from 'list'.
4262e0b3
PL
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 }
2304e38f 576 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
4262e0b3 577 {
1b7be377 578 if (FcDebug () & FC_DBG_FONTSET)
03a212e5 579 printf ("cache scan dir %s\n", dir);
2304e38f 580
1b7be377
PL
581 FcDirScanConfig (set, subdirs, cache,
582 config->blanks, dir, FcFalse, config);
4262e0b3
PL
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 }
1b7be377 593 ret += FcCacheReadDirs (config, cache, sublist, set);
4262e0b3
PL
594 free (file);
595 }
596 FcStrListDone (list);
597 return ret;
598}
599
600FcFontSet *
1b7be377 601FcCacheRead (FcConfig *config, FcGlobalCache * cache)
4262e0b3
PL
602{
603 FcFontSet * s = FcFontSetCreate();
604 if (!s)
605 return 0;
606
1b7be377 607 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
4262e0b3
PL
608 goto bail;
609
610 return s;
611
612 bail:
613 FcFontSetDestroy (s);
614 return 0;
615}
616
212c9f43 617/* read serialized state from the cache file */
eb0cf671 618static FcBool
2304e38f 619FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
212c9f43 620{
8245771d 621 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
4262e0b3 622 int fd;
212c9f43 623 char * current_arch_machine_name;
2dbe7597 624 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
212c9f43 625 off_t current_arch_start = 0;
8245771d 626 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
212c9f43 627
4262e0b3
PL
628 if (!cache_file)
629 goto bail;
212c9f43 630
eb0cf671 631 current_arch_machine_name = FcCacheMachineSignature();
4262e0b3 632 fd = open(cache_file, O_RDONLY);
212c9f43 633 if (fd == -1)
2dbe7597 634 goto bail;
212c9f43 635
212c9f43
PL
636 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
637 if (current_arch_start < 0)
4262e0b3 638 goto bail1;
212c9f43
PL
639
640 lseek (fd, current_arch_start, SEEK_SET);
eb0cf671 641 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43 642 sizeof (candidate_arch_machine_name)) == 0)
4262e0b3 643 goto bail1;
2304e38f
PL
644
645 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
8245771d 646 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
2304e38f 647
eb0cf671
PL
648 if (!FcDirCacheConsume (fd, set))
649 goto bail1;
650
651 close(fd);
652 free (cache_file);
653 return FcTrue;
654
655 bail1:
2304e38f 656 close (fd);
eb0cf671
PL
657 bail:
658 free (cache_file);
659 return FcFalse;
660}
661
662static FcBool
663FcDirCacheConsume (int fd, FcFontSet *set)
664{
665 FcCache metadata;
666 void * current_dir_block;
fd77c154 667 off_t pos;
212c9f43 668
212c9f43
PL
669 read(fd, &metadata, sizeof(FcCache));
670 if (metadata.magic != FC_CACHE_MAGIC)
eb0cf671 671 return FcFalse;
212c9f43 672
2dbe7597 673 if (!metadata.count)
eb0cf671 674 return FcTrue;
2dbe7597 675
fd77c154 676 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
2dbe7597
PL
677 current_dir_block = mmap (0, metadata.count,
678 PROT_READ, MAP_SHARED, fd, pos);
679 if (current_dir_block == MAP_FAILED)
eb0cf671 680 return FcFalse;
2dbe7597
PL
681
682 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
eb0cf671
PL
683 return FcFalse;
684
212c9f43 685 return FcTrue;
eb0cf671
PL
686}
687
688static void *
689FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
690{
691 void * current_dir_block, * final_dir_block;
8245771d 692 static unsigned int rand_state = 0;
eb0cf671
PL
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;
212c9f43 723
4262e0b3 724 bail:
eb0cf671
PL
725 free (current_dir_block);
726 return 0;
212c9f43
PL
727}
728
729/* write serialized state to the cache file */
730FcBool
2304e38f 731FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
212c9f43 732{
4262e0b3 733 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
2304e38f 734 int fd, i;
212c9f43
PL
735 FcCache metadata;
736 off_t current_arch_start = 0, truncate_to;
8245771d 737
2dbe7597 738 char * current_arch_machine_name, * header;
eb0cf671 739 void * current_dir_block;
212c9f43 740
4262e0b3
PL
741 if (!cache_file)
742 goto bail;
743
eb0cf671 744 current_dir_block = FcDirCacheProduce (set, &metadata);
212c9f43 745
eb0cf671 746 if (!metadata.count)
4262e0b3 747 {
8245771d 748 unlink ((char *)cache_file);
4262e0b3
PL
749 free (cache_file);
750 return FcTrue;
751 }
752
4262e0b3
PL
753 if (!current_dir_block)
754 goto bail;
212c9f43 755
4262e0b3
PL
756 if (FcDebug () & FC_DBG_CACHE)
757 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
758
8245771d 759 fd = open((char *)cache_file, O_RDWR | O_CREAT, 0666);
212c9f43 760 if (fd == -1)
eb0cf671 761 goto bail0;
212c9f43 762
eb0cf671 763 current_arch_machine_name = FcCacheMachineSignature ();
212c9f43
PL
764 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
765 if (current_arch_start < 0)
4262e0b3 766 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
212c9f43
PL
767
768 if (!FcCacheMoveDown(fd, current_arch_start))
8cb4c56d 769 goto bail0;
212c9f43
PL
770
771 current_arch_start = lseek(fd, 0, SEEK_CUR);
772 if (ftruncate (fd, current_arch_start) == -1)
8cb4c56d 773 goto bail0;
212c9f43 774
212c9f43 775 /* now write the address of the next offset */
eb0cf671 776 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start;
2dbe7597 777 header = malloc (10 + strlen (current_arch_machine_name));
eb0cf671 778 if (!header)
8cb4c56d 779 goto bail0;
2dbe7597
PL
780 sprintf (header, "%8x ", (int)truncate_to);
781 strcat (header, current_arch_machine_name);
eb0cf671 782 if (!FcCacheWriteString (fd, header))
4262e0b3 783 goto bail1;
212c9f43 784
2304e38f 785 for (i = 0; i < dirs->size; i++)
8245771d 786 FcCacheWriteString (fd, (char *)dirs->strs[i]);
2304e38f
PL
787 FcCacheWriteString (fd, "");
788
4262e0b3
PL
789 write (fd, &metadata, sizeof(FcCache));
790 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
eb0cf671 791 write (fd, current_dir_block, metadata.count);
5e678e94 792 free (current_dir_block);
4262e0b3 793
2dbe7597 794 /* this actually serves to pad out the cache file, if needed */
212c9f43 795 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
4262e0b3 796 goto bail1;
212c9f43
PL
797
798 close(fd);
799 return FcTrue;
800
4262e0b3 801 bail1:
0230c9f8 802 free (header);
eb0cf671
PL
803 bail0:
804 free (current_dir_block);
4262e0b3 805 bail:
8245771d 806 unlink ((char *)cache_file);
4262e0b3 807 free (cache_file);
212c9f43
PL
808 return FcFalse;
809}
810
2dbe7597 811static char *
eb0cf671 812FcCacheMachineSignature ()
2dbe7597
PL
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
4262e0b3
PL
845static int banks_ptr = 0, banks_alloc = 0;
846static int * bankId = 0;
847
eb0cf671 848static FcBool
4262e0b3
PL
849FcCacheHaveBank (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
863int
864FcCacheBankToIndex (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
2dbe7597 877 if (banks_ptr >= banks_alloc)
4262e0b3 878 {
2dbe7597 879 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
4262e0b3
PL
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}