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