]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
Don't unlink the fonts.cache-2 file even if there's no data to write; just
[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 422
13cdf607 423 // count = 0 should probably be distinguished from the !bs condition
8cb4c56d
PL
424 if (!bs || bs < strlen (candidate_arch_machine_name_count))
425 return -1;
426
2dbe7597 427 candidate_arch++; /* skip leading space */
212c9f43 428
2dbe7597 429 if (strcmp (candidate_arch, arch)==0)
5e678e94 430 return current_arch_start;
212c9f43
PL
431 current_arch_start += bs;
432 }
433
5e678e94 434 return -1;
212c9f43
PL
435}
436
437/* Cuts out the segment at the file pointer (moves everything else
438 * down to cover it), and leaves the file pointer at the end of the
439 * file. */
212c9f43
PL
440static FcBool
441FcCacheMoveDown (int fd, off_t start)
442{
eb0cf671 443 char * buf = malloc (8192);
2dbe7597 444 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
4262e0b3 445 long bs;
212c9f43
PL
446 int c, bytes_skipped;
447
448 if (!buf)
449 return FcFalse;
450
451 lseek (fd, start, SEEK_SET);
eb0cf671 452 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43
PL
453 sizeof (candidate_arch_machine_name)) == 0)
454 goto done;
2dbe7597 455 if (!strlen(candidate_arch_machine_name))
212c9f43
PL
456 goto done;
457
2dbe7597 458 bs = strtol(candidate_arch_machine_name, 0, 16);
212c9f43
PL
459 if (bs == 0)
460 goto done;
461
462 bytes_skipped = 0;
463 do
464 {
465 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
eb0cf671 466 if ((c = read (fd, buf, 8192)) <= 0)
212c9f43
PL
467 break;
468 lseek (fd, start+bytes_skipped, SEEK_SET);
469 if (write (fd, buf, c) < 0)
470 goto bail;
471 bytes_skipped += c;
472 }
473 while (c > 0);
474 lseek (fd, start+bytes_skipped, SEEK_SET);
475
476 done:
477 free (buf);
478 return FcTrue;
479
480 bail:
481 free (buf);
482 return FcFalse;
483}
484
1b7be377
PL
485FcBool
486FcDirCacheValid (const FcChar8 *dir)
487{
488 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
489 struct stat file_stat, dir_stat;
490
491 if (stat ((char *) dir, &dir_stat) < 0)
492 {
493 FcStrFree (cache_file);
494 return FcFalse;
495 }
496 if (stat ((char *) cache_file, &file_stat) < 0)
497 {
498 FcStrFree (cache_file);
499 return FcFalse;
500 }
501 FcStrFree (cache_file);
502 /*
503 * If the directory has been modified more recently than
504 * the cache file, the cache is not valid
505 */
506 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
507 return FcFalse;
508 return FcTrue;
509}
510
4262e0b3 511static int
1b7be377
PL
512FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
513 FcStrList *list, FcFontSet * set)
4262e0b3 514{
4262e0b3
PL
515 int ret = 0;
516 FcChar8 *dir;
517 FcChar8 *file, *base;
518 FcStrSet *subdirs;
519 FcStrList *sublist;
520 struct stat statb;
521
522 /*
03a212e5 523 * Read in the results from 'list'.
4262e0b3
PL
524 */
525 while ((dir = FcStrListNext (list)))
526 {
527 /* freed below */
528 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
529 if (!file)
530 return FcFalse;
531
532 strcpy ((char *) file, (char *) dir);
533 strcat ((char *) file, "/");
534 base = file + strlen ((char *) file);
535
536 subdirs = FcStrSetCreate ();
537 if (!subdirs)
538 {
539 fprintf (stderr, "Can't create directory set\n");
540 ret++;
541 free (file);
542 continue;
543 }
544
545 if (access ((char *) dir, X_OK) < 0)
546 {
547 switch (errno) {
548 case ENOENT:
549 case ENOTDIR:
550 case EACCES:
551 break;
552 default:
553 fprintf (stderr, "\"%s\": ", dir);
554 perror ("");
555 ret++;
556 }
557 FcStrSetDestroy (subdirs);
558 free (file);
559 continue;
560 }
561 if (stat ((char *) dir, &statb) == -1)
562 {
563 fprintf (stderr, "\"%s\": ", dir);
564 perror ("");
565 FcStrSetDestroy (subdirs);
566 ret++;
567 free (file);
568 continue;
569 }
570 if (!S_ISDIR (statb.st_mode))
571 {
572 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
573 FcStrSetDestroy (subdirs);
574 free (file);
575 continue;
576 }
2304e38f 577 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
4262e0b3 578 {
1b7be377 579 if (FcDebug () & FC_DBG_FONTSET)
03a212e5 580 printf ("cache scan dir %s\n", dir);
2304e38f 581
1b7be377
PL
582 FcDirScanConfig (set, subdirs, cache,
583 config->blanks, dir, FcFalse, config);
4262e0b3
PL
584 }
585 sublist = FcStrListCreate (subdirs);
586 FcStrSetDestroy (subdirs);
587 if (!sublist)
588 {
589 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
590 ret++;
591 free (file);
592 continue;
593 }
1b7be377 594 ret += FcCacheReadDirs (config, cache, sublist, set);
4262e0b3
PL
595 free (file);
596 }
597 FcStrListDone (list);
598 return ret;
599}
600
601FcFontSet *
1b7be377 602FcCacheRead (FcConfig *config, FcGlobalCache * cache)
4262e0b3
PL
603{
604 FcFontSet * s = FcFontSetCreate();
605 if (!s)
606 return 0;
607
1b7be377 608 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
4262e0b3
PL
609 goto bail;
610
611 return s;
612
613 bail:
614 FcFontSetDestroy (s);
615 return 0;
616}
617
212c9f43 618/* read serialized state from the cache file */
eb0cf671 619static FcBool
2304e38f 620FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
212c9f43 621{
8245771d 622 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
4262e0b3 623 int fd;
212c9f43 624 char * current_arch_machine_name;
2dbe7597 625 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
212c9f43 626 off_t current_arch_start = 0;
8245771d 627 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
212c9f43 628
4262e0b3
PL
629 if (!cache_file)
630 goto bail;
212c9f43 631
eb0cf671 632 current_arch_machine_name = FcCacheMachineSignature();
4262e0b3 633 fd = open(cache_file, O_RDONLY);
212c9f43 634 if (fd == -1)
2dbe7597 635 goto bail;
212c9f43 636
212c9f43
PL
637 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
638 if (current_arch_start < 0)
4262e0b3 639 goto bail1;
212c9f43
PL
640
641 lseek (fd, current_arch_start, SEEK_SET);
eb0cf671 642 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43 643 sizeof (candidate_arch_machine_name)) == 0)
4262e0b3 644 goto bail1;
2304e38f
PL
645
646 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
8245771d 647 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
2304e38f 648
eb0cf671
PL
649 if (!FcDirCacheConsume (fd, set))
650 goto bail1;
651
652 close(fd);
653 free (cache_file);
654 return FcTrue;
655
656 bail1:
2304e38f 657 close (fd);
eb0cf671
PL
658 bail:
659 free (cache_file);
660 return FcFalse;
661}
662
663static FcBool
664FcDirCacheConsume (int fd, FcFontSet *set)
665{
666 FcCache metadata;
667 void * current_dir_block;
fd77c154 668 off_t pos;
212c9f43 669
212c9f43
PL
670 read(fd, &metadata, sizeof(FcCache));
671 if (metadata.magic != FC_CACHE_MAGIC)
eb0cf671 672 return FcFalse;
212c9f43 673
2dbe7597 674 if (!metadata.count)
eb0cf671 675 return FcTrue;
2dbe7597 676
fd77c154 677 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
2dbe7597
PL
678 current_dir_block = mmap (0, metadata.count,
679 PROT_READ, MAP_SHARED, fd, pos);
680 if (current_dir_block == MAP_FAILED)
eb0cf671 681 return FcFalse;
2dbe7597
PL
682
683 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
eb0cf671
PL
684 return FcFalse;
685
212c9f43 686 return FcTrue;
eb0cf671
PL
687}
688
689static void *
690FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
691{
692 void * current_dir_block, * final_dir_block;
8245771d 693 static unsigned int rand_state = 0;
eb0cf671
PL
694 int bank;
695
696 if (!rand_state)
697 rand_state = time(0L);
698 bank = rand_r(&rand_state);
699
700 while (FcCacheHaveBank(bank))
701 bank = rand_r(&rand_state);
702
703 memset (metadata, 0, sizeof(FcCache));
704 FcFontSetNewBank();
705 metadata->count = FcFontSetNeededBytes (set);
706 metadata->magic = FC_CACHE_MAGIC;
707 metadata->bank = bank;
708
709 if (!metadata->count) /* not a failure, no fonts to write */
710 return 0;
711
712 current_dir_block = malloc (metadata->count);
713 if (!current_dir_block)
714 goto bail;
715 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
716
717 if ((char *)current_dir_block + metadata->count != final_dir_block)
718 goto bail;
719
720 if (!FcFontSetSerialize (bank, set))
721 goto bail;
722
723 return current_dir_block;
212c9f43 724
4262e0b3 725 bail:
eb0cf671
PL
726 free (current_dir_block);
727 return 0;
212c9f43
PL
728}
729
730/* write serialized state to the cache file */
731FcBool
2304e38f 732FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
212c9f43 733{
4262e0b3 734 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
6aee8c6f 735 int fd, i, dirs_count;
212c9f43
PL
736 FcCache metadata;
737 off_t current_arch_start = 0, truncate_to;
8245771d 738
2dbe7597 739 char * current_arch_machine_name, * header;
eb0cf671 740 void * current_dir_block;
212c9f43 741
4262e0b3
PL
742 if (!cache_file)
743 goto bail;
744
eb0cf671 745 current_dir_block = FcDirCacheProduce (set, &metadata);
212c9f43 746
a9698bed 747 if (metadata.count && !current_dir_block)
4262e0b3 748 goto bail;
212c9f43 749
4262e0b3
PL
750 if (FcDebug () & FC_DBG_CACHE)
751 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
752
8245771d 753 fd = open((char *)cache_file, O_RDWR | O_CREAT, 0666);
212c9f43 754 if (fd == -1)
eb0cf671 755 goto bail0;
212c9f43 756
eb0cf671 757 current_arch_machine_name = FcCacheMachineSignature ();
212c9f43
PL
758 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
759 if (current_arch_start < 0)
4262e0b3 760 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
212c9f43
PL
761
762 if (!FcCacheMoveDown(fd, current_arch_start))
8cb4c56d 763 goto bail0;
212c9f43
PL
764
765 current_arch_start = lseek(fd, 0, SEEK_CUR);
766 if (ftruncate (fd, current_arch_start) == -1)
8cb4c56d 767 goto bail0;
212c9f43 768
6aee8c6f
PL
769 /* allocate space for subdir names in this block */
770 dirs_count = 0;
771 for (i = 0; i < dirs->size; i++)
772 dirs_count += strlen((char *)dirs->strs[i]) + 1;
773 dirs_count ++;
774
212c9f43 775 /* now write the address of the next offset */
6aee8c6f 776 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + 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 789 write (fd, &metadata, sizeof(FcCache));
a9698bed
PL
790 if (metadata.count)
791 {
792 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
793 write (fd, current_dir_block, metadata.count);
794 free (current_dir_block);
795 }
4262e0b3 796
2dbe7597 797 /* this actually serves to pad out the cache file, if needed */
212c9f43 798 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
4262e0b3 799 goto bail1;
212c9f43
PL
800
801 close(fd);
802 return FcTrue;
803
4262e0b3 804 bail1:
0230c9f8 805 free (header);
eb0cf671
PL
806 bail0:
807 free (current_dir_block);
4262e0b3 808 bail:
8245771d 809 unlink ((char *)cache_file);
4262e0b3 810 free (cache_file);
212c9f43
PL
811 return FcFalse;
812}
813
2dbe7597 814static char *
eb0cf671 815FcCacheMachineSignature ()
2dbe7597
PL
816{
817 static char buf[MACHINE_SIGNATURE_SIZE];
818 int magic = ENDIAN_TEST;
819 char * m = (char *)&magic;
820
821 sprintf (buf, "%2x%2x%2x%2x "
822 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
823 "%4x %4x %4x %4x %4x %4x %4x\n",
824 m[0], m[1], m[2], m[3],
825 sizeof (char),
826 sizeof (char *),
827 sizeof (int),
828 sizeof (FcPattern),
829 sizeof (FcPatternEltPtr),
830 sizeof (struct _FcPatternElt *),
831 sizeof (FcPatternElt),
832 sizeof (FcObjectPtr),
833 sizeof (FcValueListPtr),
834 sizeof (FcValue),
835 sizeof (FcValueBinding),
836 sizeof (struct _FcValueList *),
837 sizeof (FcCharSet),
838 sizeof (FcCharLeaf **),
839 sizeof (FcChar16 *),
840 sizeof (FcChar16),
841 sizeof (FcCharLeaf),
842 sizeof (FcChar32),
843 sizeof (FcCache));
844
845 return buf;
846}
847
4262e0b3
PL
848static int banks_ptr = 0, banks_alloc = 0;
849static int * bankId = 0;
850
eb0cf671 851static FcBool
4262e0b3
PL
852FcCacheHaveBank (int bank)
853{
854 int i;
855
856 if (bank < FC_BANK_FIRST)
857 return FcTrue;
858
859 for (i = 0; i < banks_ptr; i++)
860 if (bankId[i] == bank)
861 return FcTrue;
862
863 return FcFalse;
864}
865
866int
867FcCacheBankToIndex (int bank)
868{
869 static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1;
870 int i;
871 int * b;
872
873 if (bank == lastBank)
874 return lastIndex;
875
876 for (i = 0; i < banks_ptr; i++)
877 if (bankId[i] == bank)
878 return i;
879
2dbe7597 880 if (banks_ptr >= banks_alloc)
4262e0b3 881 {
2dbe7597 882 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
4262e0b3
PL
883 if (!b)
884 return -1;
885
886 bankId = b;
887 banks_alloc += 4;
888 }
889
890 i = banks_ptr++;
891 bankId[i] = bank;
892 return i;
893}