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