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