]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
Stop trampling the directory name when writing out caches. (with Mike
[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>
ea44e218 28#include <string.h>
212c9f43
PL
29#include <sys/mman.h>
30#include <sys/utsname.h>
fd77c154
PL
31#include <sys/types.h>
32#include <unistd.h>
24330d27 33#include "fcint.h"
7fd7221e 34#include <unistd.h>
24330d27 35
2dbe7597 36#define ENDIAN_TEST 0x12345678
7fd7221e 37#define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
2dbe7597 38
ea44e218 39static int
df3efc11 40FcDirCacheOpen (const FcChar8 * dir);
ea44e218
PL
41
42static char *
43FcDirCacheHashName (char * cache_file, int collisions);
44
eb0cf671 45static off_t
c60ec7cc 46FcCacheSkipToArch (int fd, const char * arch);
1b7be377 47
eb0cf671 48static FcBool
1d879de2 49FcCacheCopyOld (int fd, int fd_orig, off_t start);
1b7be377 50
eb0cf671
PL
51static void *
52FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
1b7be377
PL
53
54static FcBool
cd9bca69 55FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config);
1b7be377 56
eb0cf671
PL
57static int
58FcCacheNextOffset(off_t w);
1b7be377 59
eb0cf671
PL
60static char *
61FcCacheMachineSignature (void);
1b7be377
PL
62
63static FcBool
eb0cf671 64FcCacheHaveBank (int bank);
1b7be377 65
e77c1718
PL
66static void
67FcCacheAddBankDir (int bank, const char * dir);
68
ea44e218
PL
69struct MD5Context {
70 FcChar32 buf[4];
71 FcChar32 bits[2];
72 unsigned char in[64];
73};
74
75static void MD5Init(struct MD5Context *ctx);
76static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
77static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
78static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
79
eb0cf671 80#define FC_DBG_CACHE_REF 1024
1b7be377 81
8245771d
PL
82static char *
83FcCacheReadString (int fd, char *dest, int len)
24330d27 84{
212c9f43 85 FcChar8 c;
ccb3e93b 86 FcBool escape;
ccb3e93b
KP
87 int size;
88 int i;
24330d27 89
24330d27 90 if (len == 0)
03a212e5 91 return 0;
24330d27 92
ccb3e93b
KP
93 size = len;
94 i = 0;
24330d27 95 escape = FcFalse;
212c9f43 96 while (read (fd, &c, 1) == 1)
24330d27
KP
97 {
98 if (!escape)
99 {
100 switch (c) {
101 case '"':
ccb3e93b
KP
102 c = '\0';
103 break;
24330d27
KP
104 case '\\':
105 escape = FcTrue;
106 continue;
107 }
108 }
ccb3e93b
KP
109 if (i == size)
110 {
212c9f43
PL
111 dest[i++] = 0;
112 return dest;
ccb3e93b 113 }
212c9f43 114 dest[i++] = c;
ccb3e93b 115 if (c == '\0')
212c9f43 116 return dest;
24330d27
KP
117 escape = FcFalse;
118 }
ccb3e93b 119 return 0;
24330d27
KP
120}
121
ea44e218
PL
122static void
123FcCacheSkipString (int fd)
124{
125 FcChar8 c;
126 FcBool escape;
127
128 escape = FcFalse;
129 while (read (fd, &c, 1) == 1)
130 {
131 if (!escape)
132 {
133 switch (c) {
134 case '"':
135 c = '\0';
136 break;
137 case '\\':
138 escape = FcTrue;
139 continue;
140 }
141 }
142 if (c == '\0')
143 return;
144 escape = FcFalse;
145 }
146 return;
147}
148
24330d27 149static FcBool
8245771d 150FcCacheWriteString (int fd, const char *chars)
327a7fd4 151{
212c9f43 152 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
327a7fd4 153 return FcFalse;
327a7fd4
KP
154 return FcTrue;
155}
156
eb0cf671
PL
157static void
158FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
1b7be377 159{
5e678e94
PL
160 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
161 free (d->name);
162 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
163 free (d);
1b7be377
PL
164}
165
eb0cf671
PL
166FcGlobalCache *
167FcGlobalCacheCreate (void)
1b7be377 168{
eb0cf671 169 FcGlobalCache *cache;
1b7be377 170
eb0cf671
PL
171 cache = malloc (sizeof (FcGlobalCache));
172 if (!cache)
173 return 0;
174 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
175 cache->dirs = 0;
176 cache->updated = FcFalse;
177 cache->fd = -1;
178 return cache;
1b7be377
PL
179}
180
eb0cf671
PL
181void
182FcGlobalCacheDestroy (FcGlobalCache *cache)
327a7fd4 183{
eb0cf671 184 FcGlobalCacheDir *d, *next;
327a7fd4 185
eb0cf671 186 for (d = cache->dirs; d; d = next)
327a7fd4 187 {
eb0cf671
PL
188 next = d->next;
189 FcGlobalCacheDirDestroy (d);
327a7fd4 190 }
eb0cf671
PL
191 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
192 free (cache);
327a7fd4
KP
193}
194
195void
eb0cf671 196FcGlobalCacheLoad (FcGlobalCache *cache,
03a212e5 197 FcStrSet *staleDirs,
2b25f00c
PL
198 const FcChar8 *cache_file,
199 FcConfig *config)
327a7fd4 200{
ea44e218 201 char name_buf[FC_MAX_FILE_LEN];
eb0cf671 202 FcGlobalCacheDir *d, *next;
2b25f00c 203 FcFileTime config_time = FcConfigModifiedTime (config);
eb0cf671
PL
204 char * current_arch_machine_name;
205 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
206 off_t current_arch_start;
327a7fd4 207
03a212e5
PL
208 struct stat cache_stat, dir_stat;
209
210 if (stat ((char *) cache_file, &cache_stat) < 0)
211 return;
212
eb0cf671
PL
213 cache->fd = open ((char *) cache_file, O_RDONLY);
214 if (cache->fd == -1)
215 return;
327a7fd4 216
eb0cf671 217 cache->updated = FcFalse;
327a7fd4 218
c60ec7cc
PL
219 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
220 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
221 return;
222
eb0cf671
PL
223 current_arch_machine_name = FcCacheMachineSignature ();
224 current_arch_start = FcCacheSkipToArch(cache->fd,
c60ec7cc 225 current_arch_machine_name);
eb0cf671 226 if (current_arch_start < 0)
e58b50e8 227 goto bail_and_destroy;
327a7fd4 228
eb0cf671 229 lseek (cache->fd, current_arch_start, SEEK_SET);
03a212e5
PL
230 FcCacheReadString (cache->fd, candidate_arch_machine_name,
231 sizeof (candidate_arch_machine_name));
232 if (strlen(candidate_arch_machine_name) == 0)
e58b50e8 233 goto bail_and_destroy;
1b7be377 234
eb0cf671 235 while (1)
1b7be377 236 {
8cb4c56d
PL
237 off_t targ;
238
eb0cf671
PL
239 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
240 if (!strlen(name_buf))
1b7be377 241 break;
1b7be377 242
9090cb9e
PL
243 /* Directory must be older than the global cache file; also
244 cache must be newer than the config file. */
03a212e5 245 if (stat ((char *) name_buf, &dir_stat) < 0 ||
2b25f00c 246 dir_stat.st_mtime > cache_stat.st_mtime ||
9090cb9e 247 (config_time.set && cache_stat.st_mtime < config_time.time))
03a212e5
PL
248 {
249 FcCache md;
250
8245771d 251 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
03a212e5
PL
252 read (cache->fd, &md, sizeof (FcCache));
253 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
254 continue;
255 }
256
eb0cf671
PL
257 d = malloc (sizeof (FcGlobalCacheDir));
258 if (!d)
259 goto bail1;
1b7be377 260
eb0cf671
PL
261 d->next = cache->dirs;
262 cache->dirs = d;
1b7be377 263
8245771d 264 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
eb0cf671
PL
265 d->ent = 0;
266 d->offset = lseek (cache->fd, 0, SEEK_CUR);
8cb4c56d
PL
267 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
268 goto bail1;
269 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
270 if (lseek (cache->fd, targ, SEEK_SET) != targ)
271 goto bail1;
1b7be377 272 }
eb0cf671 273 return;
1b7be377 274
eb0cf671
PL
275 bail1:
276 for (d = cache->dirs; d; d = next)
1b7be377 277 {
eb0cf671
PL
278 next = d->next;
279 free (d);
1b7be377 280 }
eb0cf671 281 cache->dirs = 0;
e58b50e8
PL
282
283 close (cache->fd);
284 cache->fd = -1;
285 return;
286
287 bail_and_destroy:
eb0cf671
PL
288 close (cache->fd);
289 cache->fd = -1;
e58b50e8
PL
290
291 if (stat ((char *) cache_file, &cache_stat) == 0)
292 unlink ((char *)cache_file);
293
eb0cf671 294 return;
e58b50e8 295
1b7be377
PL
296}
297
1b7be377 298FcBool
8245771d 299FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
1b7be377 300{
eb0cf671 301 FcGlobalCacheDir *d;
03a212e5 302 FcBool ret = FcFalse;
1b7be377 303
eb0cf671 304 if (cache->fd == -1)
1b7be377 305 return FcFalse;
1b7be377 306
8a0b0ed6 307 dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir);
eb0cf671 308 for (d = cache->dirs; d; d = d->next)
1b7be377 309 {
03a212e5 310 if (strncmp (d->name, dir, strlen(dir)) == 0)
1b7be377 311 {
eb0cf671 312 lseek (cache->fd, d->offset, SEEK_SET);
cd9bca69 313 if (!FcDirCacheConsume (cache->fd, dir, set, config))
eb0cf671 314 return FcFalse;
03a212e5
PL
315 if (strcmp (d->name, dir) == 0)
316 ret = FcTrue;
1b7be377 317 }
1b7be377 318 }
1b7be377 319
03a212e5 320 return ret;
1b7be377
PL
321}
322
eb0cf671
PL
323FcBool
324FcGlobalCacheUpdate (FcGlobalCache *cache,
8245771d 325 const char *name,
8a0b0ed6
PL
326 FcFontSet *set,
327 FcConfig *config)
1b7be377 328{
eb0cf671 329 FcGlobalCacheDir * d;
1b7be377 330
8a0b0ed6 331 name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)name);
eb0cf671 332 for (d = cache->dirs; d; d = d->next)
1b7be377 333 {
eb0cf671 334 if (strcmp(d->name, name) == 0)
1b7be377 335 break;
24330d27 336 }
24330d27 337
eb0cf671 338 if (!d)
24330d27 339 {
eb0cf671
PL
340 d = malloc (sizeof (FcGlobalCacheDir));
341 if (!d)
342 return FcFalse;
343 d->next = cache->dirs;
344 cache->dirs = d;
24330d27 345 }
24330d27 346
eb0cf671 347 cache->updated = FcTrue;
24330d27 348
8245771d 349 d->name = (char *)FcStrCopy ((FcChar8 *)name);
eb0cf671
PL
350 d->ent = FcDirCacheProduce (set, &d->metadata);
351 d->offset = 0;
352 return FcTrue;
24330d27
KP
353}
354
355FcBool
327a7fd4 356FcGlobalCacheSave (FcGlobalCache *cache,
8a0b0ed6
PL
357 const FcChar8 *cache_file,
358 FcConfig *config)
24330d27 359{
1d879de2 360 int fd, fd_orig;
327a7fd4 361 FcGlobalCacheDir *dir;
327a7fd4 362 FcAtomic *atomic;
eb0cf671
PL
363 off_t current_arch_start = 0, truncate_to;
364 char * current_arch_machine_name, * header;
24330d27 365
eb0cf671 366 if (!cache->updated)
24330d27
KP
367 return FcTrue;
368
daeed6e0 369#if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
a391da8f
KP
370 /* Set-UID programs can't safely update the cache */
371 if (getuid () != geteuid ())
372 return FcFalse;
daeed6e0 373#endif
a391da8f 374
134f6011
KP
375 atomic = FcAtomicCreate (cache_file);
376 if (!atomic)
1d879de2
PL
377 return FcFalse;
378
134f6011 379 if (!FcAtomicLock (atomic))
24330d27 380 goto bail1;
eb0cf671
PL
381 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
382 S_IRUSR | S_IWUSR);
383 if (fd == -1)
24330d27 384 goto bail2;
f468f568 385 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
24330d27 386
1d879de2
PL
387 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
388
eb0cf671 389 current_arch_machine_name = FcCacheMachineSignature ();
1d879de2
PL
390 if (fd_orig == -1)
391 current_arch_start = 0;
392 else
393 current_arch_start = FcCacheSkipToArch (fd_orig,
c60ec7cc 394 current_arch_machine_name);
1d879de2 395
eb0cf671 396 if (current_arch_start < 0)
f468f568
PL
397 {
398 off_t i = lseek(fd_orig, 0, SEEK_END);
399 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
400 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
401 current_arch_start = FcCacheNextOffset (i);
402 }
eb0cf671 403
1d879de2
PL
404 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
405 goto bail3;
406
407 close (fd_orig);
408 fd_orig = -1;
eb0cf671
PL
409
410 current_arch_start = lseek(fd, 0, SEEK_CUR);
411 if (ftruncate (fd, current_arch_start) == -1)
1d879de2 412 goto bail3;
eb0cf671 413
03a212e5
PL
414 header = malloc (10 + strlen (current_arch_machine_name));
415 if (!header)
1d879de2 416 goto bail3;
03a212e5 417
8cb4c56d 418 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
eb0cf671 419 for (dir = cache->dirs; dir; dir = dir->next)
24330d27 420 {
eb0cf671
PL
421 truncate_to += strlen(dir->name) + 1;
422 truncate_to += sizeof (FcCache);
1c5b6345 423 truncate_to = FcCacheNextOffset (truncate_to);
eb0cf671
PL
424 truncate_to += dir->metadata.count;
425 }
426 truncate_to -= current_arch_start;
eb0cf671 427
8cb4c56d
PL
428 sprintf (header, "%8x ", (int)truncate_to);
429 strcat (header, current_arch_machine_name);
430 if (!FcCacheWriteString (fd, header))
1d879de2 431 goto bail4;
8cb4c56d 432
eb0cf671
PL
433 for (dir = cache->dirs; dir; dir = dir->next)
434 {
c60ec7cc 435 if (dir->name)
5e678e94 436 {
8a0b0ed6
PL
437 const char * d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
438
439 FcCacheWriteString (fd, d);
5e678e94 440 write (fd, &dir->metadata, sizeof(FcCache));
f6ee3db5 441 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
5e678e94
PL
442 write (fd, dir->ent, dir->metadata.count);
443 free (dir->ent);
444 }
24330d27 445 }
eb0cf671 446 FcCacheWriteString (fd, "");
24330d27 447
eb0cf671 448 if (close (fd) == -1)
1d879de2 449 goto bail25;
24330d27 450
134f6011 451 if (!FcAtomicReplaceOrig (atomic))
1d879de2 452 goto bail25;
24330d27 453
134f6011
KP
454 FcAtomicUnlock (atomic);
455 FcAtomicDestroy (atomic);
456
24330d27
KP
457 cache->updated = FcFalse;
458 return FcTrue;
459
1d879de2
PL
460 bail4:
461 free (header);
462 bail3:
463 if (fd_orig != -1)
464 close (fd_orig);
465
466 close (fd);
467 bail25:
134f6011 468 FcAtomicDeleteNew (atomic);
1d879de2 469 bail2:
134f6011 470 FcAtomicUnlock (atomic);
1d879de2 471 bail1:
134f6011 472 FcAtomicDestroy (atomic);
24330d27
KP
473 return FcFalse;
474}
475
212c9f43 476/*
eb0cf671
PL
477 * Find the next presumably-mmapable offset after the supplied file
478 * position.
212c9f43 479 */
4262e0b3
PL
480static int
481FcCacheNextOffset(off_t w)
179c3995 482{
7fd7221e
PL
483 static long pagesize = -1;
484 if (pagesize == -1)
485 pagesize = sysconf(_SC_PAGESIZE);
486 if (w % pagesize == 0)
212c9f43
PL
487 return w;
488 else
7fd7221e 489 return ((w / pagesize)+1)*pagesize;
179c3995
KP
490}
491
212c9f43
PL
492/* return the address of the segment for the provided arch,
493 * or -1 if arch not found */
494static off_t
c60ec7cc 495FcCacheSkipToArch (int fd, const char * arch)
212c9f43 496{
2dbe7597
PL
497 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
498 char * candidate_arch;
212c9f43
PL
499 off_t current_arch_start = 0;
500
ea44e218 501 lseek (fd, 0, SEEK_SET);
c60ec7cc 502 FcCacheSkipString (fd);
ea44e218
PL
503 current_arch_start = lseek (fd, 0, SEEK_CUR);
504
212c9f43
PL
505 /* skip arches that are not the current arch */
506 while (1)
507 {
508 long bs;
509
8cb4c56d
PL
510 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
511 return -1;
512
eb0cf671 513 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
2dbe7597 514 sizeof (candidate_arch_machine_name_count)) == 0)
5e678e94 515 return -1;
2dbe7597
PL
516 if (!strlen(candidate_arch_machine_name_count))
517 return -1;
518 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
8cb4c56d 519
13cdf607 520 // count = 0 should probably be distinguished from the !bs condition
8cb4c56d
PL
521 if (!bs || bs < strlen (candidate_arch_machine_name_count))
522 return -1;
523
2dbe7597 524 candidate_arch++; /* skip leading space */
212c9f43 525
2dbe7597 526 if (strcmp (candidate_arch, arch)==0)
5e678e94 527 return current_arch_start;
212c9f43
PL
528 current_arch_start += bs;
529 }
530
5e678e94 531 return -1;
212c9f43
PL
532}
533
534/* Cuts out the segment at the file pointer (moves everything else
535 * down to cover it), and leaves the file pointer at the end of the
536 * file. */
212c9f43 537static FcBool
1d879de2 538FcCacheCopyOld (int fd, int fd_orig, off_t start)
212c9f43 539{
eb0cf671 540 char * buf = malloc (8192);
2dbe7597 541 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
4262e0b3 542 long bs;
212c9f43 543 int c, bytes_skipped;
1d879de2 544 off_t loc;
212c9f43
PL
545
546 if (!buf)
547 return FcFalse;
548
1d879de2
PL
549 loc = 0;
550 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
551 do
552 {
553 int b = 8192;
554 if (loc + b > start)
555 b = start - loc;
556
557 if ((c = read (fd_orig, buf, b)) <= 0)
558 break;
559 if (write (fd, buf, c) < 0)
560 goto bail;
561
562 loc += c;
563 }
564 while (c > 0);
565
212c9f43 566 lseek (fd, start, SEEK_SET);
eb0cf671 567 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43
PL
568 sizeof (candidate_arch_machine_name)) == 0)
569 goto done;
2dbe7597 570 if (!strlen(candidate_arch_machine_name))
212c9f43
PL
571 goto done;
572
2dbe7597 573 bs = strtol(candidate_arch_machine_name, 0, 16);
212c9f43
PL
574 if (bs == 0)
575 goto done;
576
577 bytes_skipped = 0;
578 do
579 {
580 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
eb0cf671 581 if ((c = read (fd, buf, 8192)) <= 0)
212c9f43
PL
582 break;
583 lseek (fd, start+bytes_skipped, SEEK_SET);
584 if (write (fd, buf, c) < 0)
585 goto bail;
586 bytes_skipped += c;
587 }
588 while (c > 0);
589 lseek (fd, start+bytes_skipped, SEEK_SET);
590
591 done:
592 free (buf);
593 return FcTrue;
594
595 bail:
596 free (buf);
597 return FcFalse;
598}
599
55c8fa4f 600/* Does not check that the cache has the appropriate arch section. */
ec760b17
PL
601/* Also, this can be fooled if the original location has a stale
602 * cache, and the hashed location has an up-to-date cache. Oh well,
603 * sucks to be you in that case! */
1b7be377
PL
604FcBool
605FcDirCacheValid (const FcChar8 *dir)
606{
1b7be377 607 struct stat file_stat, dir_stat;
ea44e218 608 int fd;
1b7be377
PL
609
610 if (stat ((char *) dir, &dir_stat) < 0)
1b7be377 611 return FcFalse;
6bf23804 612
df3efc11 613 fd = FcDirCacheOpen (dir);
ea44e218
PL
614
615 if (fd < 0)
616 goto bail;
617 if (fstat (fd, &file_stat) < 0)
df3efc11 618 goto bail;
ea44e218
PL
619
620 close (fd);
ea44e218 621
55c8fa4f
PL
622 /*
623 * If the directory has been modified more recently than
624 * the cache file, the cache is not valid
625 */
2b25f00c 626 if (dir_stat.st_mtime > file_stat.st_mtime)
55c8fa4f 627 return FcFalse;
2b25f00c 628
55c8fa4f 629 return FcTrue;
ea44e218 630
ea44e218 631 bail:
df3efc11 632 close (fd);
ea44e218 633 return FcFalse;
55c8fa4f
PL
634}
635
636/* Assumes that the cache file in 'dir' exists.
637 * Checks that the cache has the appropriate arch section. */
638FcBool
639FcDirCacheHasCurrentArch (const FcChar8 *dir)
640{
55c8fa4f
PL
641 int fd;
642 off_t current_arch_start;
643 char *current_arch_machine_name;
644
df3efc11 645 fd = FcDirCacheOpen (dir);
ea44e218
PL
646 if (fd < 0)
647 goto bail;
648
649 current_arch_machine_name = FcCacheMachineSignature();
c60ec7cc 650 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
6bf23804
PL
651 close (fd);
652
653 if (current_arch_start < 0)
654 return FcFalse;
55c8fa4f
PL
655
656 return FcTrue;
ea44e218
PL
657
658 bail:
ea44e218 659 return FcFalse;
55c8fa4f 660}
6bf23804 661
55c8fa4f 662FcBool
8a0b0ed6 663FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
55c8fa4f 664{
8a0b0ed6 665 char *cache_file;
6f9fcb51 666 char *cache_hashed = 0;
3bfae75d 667 int fd, collisions;
15d7bd0a 668 struct stat cache_stat;
3bfae75d 669 char name_buf[FC_MAX_FILE_LEN];
55c8fa4f 670
8a0b0ed6
PL
671 dir = FcConfigNormalizeFontDir (config, dir);
672 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
df3efc11
PL
673 if (!cache_file)
674 return FcFalse;
675
3bfae75d 676 /* First remove normal cache file. */
15d7bd0a
PL
677 if (stat ((char *) cache_file, &cache_stat) == 0 &&
678 unlink ((char *)cache_file) != 0)
3bfae75d
PL
679 goto bail;
680
681 /* Next remove any applicable hashed files. */
682 fd = -1; collisions = 0;
683 do
1178b569 684 {
6f9fcb51
PL
685 if (cache_hashed)
686 FcStrFree ((FcChar8 *)cache_hashed);
687
3bfae75d
PL
688 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
689 if (!cache_hashed)
690 goto bail;
691
692 if (fd > 0)
693 close (fd);
694 fd = open(cache_hashed, O_RDONLY);
695 if (fd == -1)
696 {
697 FcStrFree ((FcChar8 *)cache_file);
698 return FcTrue;
699 }
700
701 FcCacheReadString (fd, name_buf, sizeof (name_buf));
702 if (!strlen(name_buf))
6f9fcb51
PL
703 {
704 FcStrFree ((FcChar8 *)cache_hashed);
3bfae75d 705 goto bail;
6f9fcb51 706 }
3bfae75d
PL
707 } while (strcmp (name_buf, cache_file) != 0);
708
709 FcStrFree ((FcChar8 *)cache_file);
710 close (fd);
711
712 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
713 unlink ((char *)cache_hashed) != 0)
714 {
715 FcStrFree ((FcChar8 *)cache_hashed);
716 goto bail;
1178b569 717 }
55c8fa4f 718
3bfae75d 719 FcStrFree ((FcChar8 *)cache_hashed);
1b7be377 720 return FcTrue;
3bfae75d
PL
721
722 bail:
723 FcStrFree ((FcChar8 *)cache_file);
724 return FcFalse;
1b7be377
PL
725}
726
4262e0b3 727static int
1b7be377
PL
728FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
729 FcStrList *list, FcFontSet * set)
4262e0b3 730{
4262e0b3
PL
731 int ret = 0;
732 FcChar8 *dir;
733 FcChar8 *file, *base;
734 FcStrSet *subdirs;
735 FcStrList *sublist;
736 struct stat statb;
737
738 /*
03a212e5 739 * Read in the results from 'list'.
4262e0b3
PL
740 */
741 while ((dir = FcStrListNext (list)))
742 {
9fad72ab
PL
743 if (!FcConfigAcceptFilename (config, dir))
744 continue;
745
4262e0b3
PL
746 /* freed below */
747 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
748 if (!file)
749 return FcFalse;
750
751 strcpy ((char *) file, (char *) dir);
752 strcat ((char *) file, "/");
753 base = file + strlen ((char *) file);
754
755 subdirs = FcStrSetCreate ();
756 if (!subdirs)
757 {
758 fprintf (stderr, "Can't create directory set\n");
759 ret++;
760 free (file);
761 continue;
762 }
763
764 if (access ((char *) dir, X_OK) < 0)
765 {
766 switch (errno) {
767 case ENOENT:
768 case ENOTDIR:
769 case EACCES:
770 break;
771 default:
772 fprintf (stderr, "\"%s\": ", dir);
773 perror ("");
774 ret++;
775 }
776 FcStrSetDestroy (subdirs);
777 free (file);
778 continue;
779 }
780 if (stat ((char *) dir, &statb) == -1)
781 {
782 fprintf (stderr, "\"%s\": ", dir);
783 perror ("");
784 FcStrSetDestroy (subdirs);
785 ret++;
786 free (file);
787 continue;
788 }
789 if (!S_ISDIR (statb.st_mode))
790 {
791 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
792 FcStrSetDestroy (subdirs);
793 free (file);
794 continue;
795 }
cd9bca69 796 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir, config))
4262e0b3 797 {
1b7be377 798 if (FcDebug () & FC_DBG_FONTSET)
03a212e5 799 printf ("cache scan dir %s\n", dir);
2304e38f 800
1b7be377
PL
801 FcDirScanConfig (set, subdirs, cache,
802 config->blanks, dir, FcFalse, config);
4262e0b3
PL
803 }
804 sublist = FcStrListCreate (subdirs);
805 FcStrSetDestroy (subdirs);
806 if (!sublist)
807 {
808 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
809 ret++;
810 free (file);
811 continue;
812 }
1b7be377 813 ret += FcCacheReadDirs (config, cache, sublist, set);
4262e0b3
PL
814 free (file);
815 }
816 FcStrListDone (list);
817 return ret;
818}
819
820FcFontSet *
1b7be377 821FcCacheRead (FcConfig *config, FcGlobalCache * cache)
4262e0b3
PL
822{
823 FcFontSet * s = FcFontSetCreate();
824 if (!s)
825 return 0;
826
1b7be377 827 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
4262e0b3
PL
828 goto bail;
829
830 return s;
831
832 bail:
833 FcFontSetDestroy (s);
834 return 0;
835}
836
ea44e218
PL
837static const char bin2hex[] = { '0', '1', '2', '3',
838 '4', '5', '6', '7',
839 '8', '9', 'a', 'b',
840 'c', 'd', 'e', 'f' };
841
842static char *
843FcDirCacheHashName (char * cache_file, int collisions)
844{
845 unsigned char hash[16], hex_hash[33];
846 char *cache_hashed;
847 unsigned char uscore = '_';
848 int cnt, i;
849 FcChar8 *tmp;
850 struct MD5Context ctx;
851
852 MD5Init (&ctx);
853 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
854
855 for (i = 0; i < collisions; i++)
856 MD5Update (&ctx, &uscore, 1);
857
858 MD5Final (hash, &ctx);
859
860 for (cnt = 0; cnt < 16; ++cnt)
861 {
862 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
863 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
864 }
865 hex_hash[32] = 0;
866
867 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
868 if (!tmp)
869 return 0;
870
83b67390 871 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
ea44e218
PL
872 free (tmp);
873
874 return cache_hashed;
875}
876
877/* Opens the hashed name for cache_file.
878 * This would fail in the unlikely event of a collision and subsequent
879 * removal of the file which originally caused the collision. */
880static int
df3efc11 881FcDirCacheOpen (const FcChar8 *dir)
ea44e218 882{
df3efc11 883 FcBool found;
ea44e218 884 int fd = -1, collisions = 0;
df3efc11 885 char *cache_file, *cache_hashed;
ea44e218 886 char name_buf[FC_MAX_FILE_LEN];
df3efc11
PL
887 struct stat dir_stat;
888
889 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
890 if (!cache_file)
891 return -1;
ea44e218 892
ec760b17
PL
893 fd = open(cache_file, O_RDONLY);
894 if (fd != -1)
895 return fd;
896
df3efc11
PL
897 if (stat ((char *)dir, &dir_stat) == -1)
898 return -1;
899
8a0b0ed6 900 found = FcFalse;
ea44e218
PL
901 do
902 {
df3efc11
PL
903 struct stat c;
904 FcChar8 * name_buf_dir;
905
ea44e218
PL
906 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
907 if (!cache_hashed)
575a37b7
PL
908 {
909 FcStrFree ((FcChar8 *)cache_file);
ea44e218 910 return -1;
575a37b7 911 }
ea44e218
PL
912
913 if (fd > 0)
914 close (fd);
915 fd = open(cache_hashed, O_RDONLY);
3bfae75d
PL
916 FcStrFree ((FcChar8 *)cache_hashed);
917
ea44e218 918 if (fd == -1)
575a37b7
PL
919 {
920 FcStrFree ((FcChar8 *)cache_file);
ea44e218 921 return -1;
575a37b7 922 }
ea44e218
PL
923 FcCacheReadString (fd, name_buf, sizeof (name_buf));
924 if (!strlen(name_buf))
925 goto bail;
df3efc11
PL
926
927 name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
928 if (stat ((char *)name_buf_dir, &c) == -1)
929 {
930 FcStrFree (name_buf_dir);
931 continue;
932 }
933 FcStrFree (name_buf_dir);
575a37b7 934 found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
df3efc11 935 } while (!found);
575a37b7 936 FcStrFree ((FcChar8 *)cache_file);
ea44e218
PL
937 return fd;
938
939 bail:
575a37b7 940 FcStrFree ((FcChar8 *)cache_file);
ea44e218
PL
941 close (fd);
942 return -1;
943}
944
212c9f43 945/* read serialized state from the cache file */
2eb84374 946FcBool
cd9bca69 947FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
212c9f43 948{
ea44e218
PL
949 int fd;
950 char *current_arch_machine_name;
951 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
952 off_t current_arch_start = 0;
953 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
212c9f43 954
df3efc11 955 fd = FcDirCacheOpen (dir);
ea44e218
PL
956 if (fd < 0)
957 goto bail;
212c9f43 958
ea44e218
PL
959 current_arch_machine_name = FcCacheMachineSignature();
960 current_arch_start = FcCacheSkipToArch(fd,
c60ec7cc 961 current_arch_machine_name);
212c9f43 962 if (current_arch_start < 0)
4262e0b3 963 goto bail1;
212c9f43
PL
964
965 lseek (fd, current_arch_start, SEEK_SET);
eb0cf671 966 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43 967 sizeof (candidate_arch_machine_name)) == 0)
4262e0b3 968 goto bail1;
2304e38f
PL
969
970 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
8245771d 971 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
2304e38f 972
cd9bca69 973 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
eb0cf671
PL
974 goto bail1;
975
976 close(fd);
eb0cf671
PL
977 return FcTrue;
978
979 bail1:
2304e38f 980 close (fd);
eb0cf671 981 bail:
eb0cf671
PL
982 return FcFalse;
983}
984
985static FcBool
cd9bca69 986FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
eb0cf671
PL
987{
988 FcCache metadata;
989 void * current_dir_block;
fd77c154 990 off_t pos;
212c9f43 991
212c9f43
PL
992 read(fd, &metadata, sizeof(FcCache));
993 if (metadata.magic != FC_CACHE_MAGIC)
eb0cf671 994 return FcFalse;
212c9f43 995
2dbe7597 996 if (!metadata.count)
05a98eaf
PL
997 {
998 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
999 lseek (fd, pos, SEEK_SET);
eadadf48
PL
1000 if (config)
1001 FcConfigAddFontDir (config, (FcChar8 *)dir);
eb0cf671 1002 return FcTrue;
05a98eaf 1003 }
2dbe7597 1004
fd77c154 1005 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
2dbe7597
PL
1006 current_dir_block = mmap (0, metadata.count,
1007 PROT_READ, MAP_SHARED, fd, pos);
c60ec7cc 1008 lseek (fd, pos+metadata.count, SEEK_SET);
2dbe7597 1009 if (current_dir_block == MAP_FAILED)
eb0cf671 1010 return FcFalse;
eb0cf671 1011
e77c1718 1012 FcCacheAddBankDir (metadata.bank, dir);
cd9bca69
PL
1013 if (config)
1014 FcConfigAddFontDir (config, (FcChar8 *)dir);
e77c1718 1015
b8948e85
PL
1016 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
1017 return FcFalse;
1018
212c9f43 1019 return FcTrue;
eb0cf671
PL
1020}
1021
1022static void *
1023FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1024{
1025 void * current_dir_block, * final_dir_block;
8245771d 1026 static unsigned int rand_state = 0;
8e351527 1027 int bank, needed_bytes_no_align;
eb0cf671
PL
1028
1029 if (!rand_state)
1030 rand_state = time(0L);
1031 bank = rand_r(&rand_state);
1032
1033 while (FcCacheHaveBank(bank))
1034 bank = rand_r(&rand_state);
1035
1036 memset (metadata, 0, sizeof(FcCache));
1037 FcFontSetNewBank();
8e351527
PL
1038 needed_bytes_no_align = FcFontSetNeededBytes (set);
1039 metadata->count = needed_bytes_no_align +
7fd7221e 1040 FcFontSetNeededBytesAlign ();
eb0cf671
PL
1041 metadata->magic = FC_CACHE_MAGIC;
1042 metadata->bank = bank;
1043
8e351527
PL
1044 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1045 {
1046 /* no, you don't really need to write any bytes at all. */
1047 metadata->count = 0;
eb0cf671 1048 return 0;
8e351527 1049 }
eb0cf671
PL
1050
1051 current_dir_block = malloc (metadata->count);
1052 if (!current_dir_block)
1053 goto bail;
8e351527
PL
1054 // shut up valgrind
1055 memset (current_dir_block, 0, metadata->count);
eb0cf671
PL
1056 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1057
8e351527 1058 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
eb0cf671
PL
1059 goto bail;
1060
1061 if (!FcFontSetSerialize (bank, set))
1062 goto bail;
1063
1064 return current_dir_block;
212c9f43 1065
4262e0b3 1066 bail:
eb0cf671
PL
1067 free (current_dir_block);
1068 return 0;
212c9f43
PL
1069}
1070
1071/* write serialized state to the cache file */
1072FcBool
2304e38f 1073FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
212c9f43 1074{
ea44e218 1075 char *cache_file;
3bfae75d 1076 char *cache_hashed;
1d879de2
PL
1077 int fd, fd_orig, i, dirs_count;
1078 FcAtomic *atomic;
1079 FcCache metadata;
1080 off_t current_arch_start = 0, truncate_to;
ea44e218
PL
1081 char name_buf[FC_MAX_FILE_LEN];
1082 int collisions;
8245771d 1083
1d879de2 1084 char *current_arch_machine_name, * header;
ea44e218 1085 void *current_dir_block = 0;
212c9f43 1086
cd9bca69
PL
1087 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1088 if (!dir)
1089 return FcFalse;
1090
ea44e218 1091 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
4262e0b3
PL
1092 if (!cache_file)
1093 goto bail;
1094
ea44e218
PL
1095 /* Ensure that we're not trampling a cache for some other dir. */
1096 /* This is slightly different from FcDirCacheOpen, since it
1097 * needs the filename, not the file descriptor. */
1098 fd = -1; collisions = 0;
1099 do
1100 {
3bfae75d
PL
1101 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1102 if (!cache_hashed)
ea44e218
PL
1103 goto bail0;
1104
1105 if (fd > 0)
1106 close (fd);
3bfae75d 1107 fd = open(cache_hashed, O_RDONLY);
ea44e218
PL
1108 if (fd == -1)
1109 break;
1110 FcCacheReadString (fd, name_buf, sizeof (name_buf));
1111 close (fd);
1112
1113 if (!strlen(name_buf))
1114 break;
1115
1116 if (strcmp (name_buf, cache_file) != 0)
1117 continue;
1118 } while (0);
1119
eb0cf671 1120 current_dir_block = FcDirCacheProduce (set, &metadata);
212c9f43 1121
a9698bed 1122 if (metadata.count && !current_dir_block)
ea44e218 1123 goto bail1;
212c9f43 1124
4262e0b3
PL
1125 if (FcDebug () & FC_DBG_CACHE)
1126 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1127
3bfae75d 1128 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1d879de2 1129 if (!atomic)
ea44e218 1130 goto bail1;
212c9f43 1131
1d879de2 1132 if (!FcAtomicLock (atomic))
ec760b17
PL
1133 {
1134 /* Now try rewriting the original version of the file. */
1135 FcAtomicDestroy (atomic);
1d879de2 1136
3bfae75d 1137 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
ec760b17
PL
1138 fd_orig = open (cache_file, O_RDONLY);
1139 if (fd_orig == -1)
1140 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1141
1142 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1143 if (fd == -1)
1144 goto bail2;
1145 }
1146
1147 /* In all cases, try opening the real location of the cache file first. */
1148 /* (even if that's not atomic.) */
1149 fd_orig = open (cache_file, O_RDONLY);
1150 if (fd_orig == -1)
1151 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1d879de2
PL
1152
1153 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1154 if (fd == -1)
ec760b17 1155 goto bail3;
ea44e218
PL
1156
1157 FcCacheWriteString (fd, cache_file);
1d879de2 1158
eb0cf671 1159 current_arch_machine_name = FcCacheMachineSignature ();
1d879de2
PL
1160 current_arch_start = 0;
1161
1162 if (fd_orig != -1)
1163 current_arch_start =
c60ec7cc 1164 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1d879de2 1165
212c9f43 1166 if (current_arch_start < 0)
f468f568
PL
1167 {
1168 off_t i = lseek(fd_orig, 0, SEEK_END);
1169 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
1170 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
1171 current_arch_start = FcCacheNextOffset (i);
1172 }
212c9f43 1173
1d879de2 1174 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
ea44e218 1175 goto bail4;
1d879de2
PL
1176
1177 if (fd_orig != -1)
1178 close (fd_orig);
212c9f43
PL
1179
1180 current_arch_start = lseek(fd, 0, SEEK_CUR);
1181 if (ftruncate (fd, current_arch_start) == -1)
ea44e218 1182 goto bail4;
212c9f43 1183
6aee8c6f
PL
1184 /* allocate space for subdir names in this block */
1185 dirs_count = 0;
1186 for (i = 0; i < dirs->size; i++)
1187 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1188 dirs_count ++;
1189
212c9f43 1190 /* now write the address of the next offset */
6aee8c6f 1191 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
2dbe7597 1192 header = malloc (10 + strlen (current_arch_machine_name));
eb0cf671 1193 if (!header)
ea44e218 1194 goto bail4;
2dbe7597
PL
1195 sprintf (header, "%8x ", (int)truncate_to);
1196 strcat (header, current_arch_machine_name);
eb0cf671 1197 if (!FcCacheWriteString (fd, header))
ea44e218 1198 goto bail5;
212c9f43 1199
2304e38f 1200 for (i = 0; i < dirs->size; i++)
8245771d 1201 FcCacheWriteString (fd, (char *)dirs->strs[i]);
2304e38f
PL
1202 FcCacheWriteString (fd, "");
1203
4262e0b3 1204 write (fd, &metadata, sizeof(FcCache));
a9698bed
PL
1205 if (metadata.count)
1206 {
1207 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1208 write (fd, current_dir_block, metadata.count);
1209 free (current_dir_block);
1210 }
4262e0b3 1211
2dbe7597 1212 /* this actually serves to pad out the cache file, if needed */
212c9f43 1213 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
ea44e218 1214 goto bail5;
212c9f43 1215
6f9fcb51 1216 free (header);
212c9f43 1217 close(fd);
1d879de2 1218 if (!FcAtomicReplaceOrig(atomic))
ea44e218 1219 goto bail5;
3bfae75d 1220 FcStrFree ((FcChar8 *)cache_hashed);
575a37b7 1221 FcStrFree ((FcChar8 *)cache_file);
1d879de2
PL
1222 FcAtomicUnlock (atomic);
1223 FcAtomicDestroy (atomic);
212c9f43
PL
1224 return FcTrue;
1225
ea44e218 1226 bail5:
0230c9f8 1227 free (header);
ea44e218 1228 bail4:
1d879de2 1229 close (fd);
ea44e218 1230 bail3:
1d879de2 1231 FcAtomicUnlock (atomic);
ea44e218 1232 bail2:
1d879de2 1233 FcAtomicDestroy (atomic);
ea44e218 1234 bail1:
3bfae75d 1235 FcStrFree ((FcChar8 *)cache_hashed);
eb0cf671 1236 bail0:
8245771d 1237 unlink ((char *)cache_file);
575a37b7 1238 FcStrFree ((FcChar8 *)cache_file);
1d879de2
PL
1239 if (current_dir_block)
1240 free (current_dir_block);
1241 bail:
212c9f43
PL
1242 return FcFalse;
1243}
1244
2dbe7597 1245static char *
eb0cf671 1246FcCacheMachineSignature ()
2dbe7597
PL
1247{
1248 static char buf[MACHINE_SIGNATURE_SIZE];
7fd7221e 1249 int32_t magic = ENDIAN_TEST;
2dbe7597
PL
1250 char * m = (char *)&magic;
1251
1252 sprintf (buf, "%2x%2x%2x%2x "
1253 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
7fd7221e 1254 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
2dbe7597 1255 m[0], m[1], m[2], m[3],
cd310911
PL
1256 (unsigned int)sizeof (char),
1257 (unsigned int)sizeof (char *),
1258 (unsigned int)sizeof (int),
1259 (unsigned int)sizeof (FcPattern),
1260 (unsigned int)sizeof (FcPatternEltPtr),
1261 (unsigned int)sizeof (struct _FcPatternElt *),
1262 (unsigned int)sizeof (FcPatternElt),
1263 (unsigned int)sizeof (FcObjectPtr),
1264 (unsigned int)sizeof (FcValueListPtr),
1265 (unsigned int)sizeof (FcValue),
1266 (unsigned int)sizeof (FcValueBinding),
1267 (unsigned int)sizeof (struct _FcValueList *),
1268 (unsigned int)sizeof (FcCharSet),
1269 (unsigned int)sizeof (FcCharLeaf **),
1270 (unsigned int)sizeof (FcChar16 *),
1271 (unsigned int)sizeof (FcChar16),
1272 (unsigned int)sizeof (FcCharLeaf),
1273 (unsigned int)sizeof (FcChar32),
7fd7221e
PL
1274 (unsigned int)sizeof (FcCache),
1275 (unsigned int)sysconf(_SC_PAGESIZE));
2dbe7597
PL
1276
1277 return buf;
1278}
1279
4262e0b3 1280static int banks_ptr = 0, banks_alloc = 0;
b8948e85 1281int * _fcBankId = 0, * _fcBankIdx = 0;
e77c1718 1282static const char ** bankDirs = 0;
4262e0b3 1283
eb0cf671 1284static FcBool
4262e0b3
PL
1285FcCacheHaveBank (int bank)
1286{
1287 int i;
1288
1289 if (bank < FC_BANK_FIRST)
1290 return FcTrue;
1291
1292 for (i = 0; i < banks_ptr; i++)
b8948e85 1293 if (_fcBankId[i] == bank)
4262e0b3
PL
1294 return FcTrue;
1295
1296 return FcFalse;
1297}
1298
1299int
b8948e85 1300FcCacheBankToIndexMTF (int bank)
4262e0b3 1301{
751932dd 1302 int i, j;
4262e0b3
PL
1303
1304 for (i = 0; i < banks_ptr; i++)
b8948e85 1305 if (_fcBankId[_fcBankIdx[i]] == bank)
751932dd 1306 {
b8948e85 1307 int t = _fcBankIdx[i];
751932dd
PL
1308
1309 for (j = i; j > 0; j--)
b8948e85
PL
1310 _fcBankIdx[j] = _fcBankIdx[j-1];
1311 _fcBankIdx[0] = t;
751932dd
PL
1312 return t;
1313 }
4262e0b3 1314
2dbe7597 1315 if (banks_ptr >= banks_alloc)
4262e0b3 1316 {
751932dd 1317 int * b, * bidx;
e77c1718
PL
1318 const char ** bds;
1319
b8948e85 1320 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
4262e0b3
PL
1321 if (!b)
1322 return -1;
b8948e85 1323 _fcBankId = b;
751932dd 1324
b8948e85 1325 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
751932dd
PL
1326 if (!bidx)
1327 return -1;
b8948e85 1328 _fcBankIdx = bidx;
751932dd 1329
e77c1718
PL
1330 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1331 if (!bds)
1332 return -1;
1333 bankDirs = bds;
1334
4262e0b3
PL
1335 banks_alloc += 4;
1336 }
1337
1338 i = banks_ptr++;
b8948e85
PL
1339 _fcBankId[i] = bank;
1340 _fcBankIdx[i] = i;
4262e0b3
PL
1341 return i;
1342}
e77c1718
PL
1343
1344static void
1345FcCacheAddBankDir (int bank, const char * dir)
1346{
b8948e85 1347 int bi = FcCacheBankToIndexMTF (bank);
e77c1718
PL
1348
1349 if (bi < 0)
1350 return;
1351
1352 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1353}
1354
1355const char *
1356FcCacheFindBankDir (int bank)
1357{
1358 int bi = FcCacheBankToIndex (bank);
1359 return bankDirs[bi];
1360}
1361
ea44e218
PL
1362/*
1363 * This code implements the MD5 message-digest algorithm.
1364 * The algorithm is due to Ron Rivest. This code was
1365 * written by Colin Plumb in 1993, no copyright is claimed.
1366 * This code is in the public domain; do with it what you wish.
1367 *
1368 * Equivalent code is available from RSA Data Security, Inc.
1369 * This code has been tested against that, and is equivalent,
1370 * except that you don't need to include two pages of legalese
1371 * with every copy.
1372 *
1373 * To compute the message digest of a chunk of bytes, declare an
1374 * MD5Context structure, pass it to MD5Init, call MD5Update as
1375 * needed on buffers full of bytes, and then call MD5Final, which
1376 * will fill a supplied 16-byte array with the digest.
1377 */
1378
1379#ifndef HIGHFIRST
1380#define byteReverse(buf, len) /* Nothing */
1381#else
1382/*
1383 * Note: this code is harmless on little-endian machines.
1384 */
1385void byteReverse(unsigned char *buf, unsigned longs)
1386{
1387 FcChar32 t;
1388 do {
1389 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1390 ((unsigned) buf[1] << 8 | buf[0]);
1391 *(FcChar32 *) buf = t;
1392 buf += 4;
1393 } while (--longs);
1394}
1395#endif
1396
1397/*
1398 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1399 * initialization constants.
1400 */
1401static void MD5Init(struct MD5Context *ctx)
1402{
1403 ctx->buf[0] = 0x67452301;
1404 ctx->buf[1] = 0xefcdab89;
1405 ctx->buf[2] = 0x98badcfe;
1406 ctx->buf[3] = 0x10325476;
1407
1408 ctx->bits[0] = 0;
1409 ctx->bits[1] = 0;
1410}
1411
1412/*
1413 * Update context to reflect the concatenation of another buffer full
1414 * of bytes.
1415 */
1416static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1417{
1418 FcChar32 t;
1419
1420 /* Update bitcount */
1421
1422 t = ctx->bits[0];
1423 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1424 ctx->bits[1]++; /* Carry from low to high */
1425 ctx->bits[1] += len >> 29;
1426
1427 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1428
1429 /* Handle any leading odd-sized chunks */
1430
1431 if (t) {
1432 unsigned char *p = (unsigned char *) ctx->in + t;
1433
1434 t = 64 - t;
1435 if (len < t) {
1436 memcpy(p, buf, len);
1437 return;
1438 }
1439 memcpy(p, buf, t);
1440 byteReverse(ctx->in, 16);
1441 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1442 buf += t;
1443 len -= t;
1444 }
1445 /* Process data in 64-byte chunks */
1446
1447 while (len >= 64) {
1448 memcpy(ctx->in, buf, 64);
1449 byteReverse(ctx->in, 16);
1450 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1451 buf += 64;
1452 len -= 64;
1453 }
1454
1455 /* Handle any remaining bytes of data. */
1456
1457 memcpy(ctx->in, buf, len);
1458}
1459
1460/*
1461 * Final wrapup - pad to 64-byte boundary with the bit pattern
1462 * 1 0* (64-bit count of bits processed, MSB-first)
1463 */
1464static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1465{
1466 unsigned count;
1467 unsigned char *p;
1468
1469 /* Compute number of bytes mod 64 */
1470 count = (ctx->bits[0] >> 3) & 0x3F;
1471
1472 /* Set the first char of padding to 0x80. This is safe since there is
1473 always at least one byte free */
1474 p = ctx->in + count;
1475 *p++ = 0x80;
1476
1477 /* Bytes of padding needed to make 64 bytes */
1478 count = 64 - 1 - count;
1479
1480 /* Pad out to 56 mod 64 */
1481 if (count < 8) {
1482 /* Two lots of padding: Pad the first block to 64 bytes */
1483 memset(p, 0, count);
1484 byteReverse(ctx->in, 16);
1485 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1486
1487 /* Now fill the next block with 56 bytes */
1488 memset(ctx->in, 0, 56);
1489 } else {
1490 /* Pad block to 56 bytes */
1491 memset(p, 0, count - 8);
1492 }
1493 byteReverse(ctx->in, 14);
1494
1495 /* Append length in bits and transform */
1496 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1497 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1498
1499 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1500 byteReverse((unsigned char *) ctx->buf, 4);
1501 memcpy(digest, ctx->buf, 16);
1502 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1503}
1504
1505
1506/* The four core functions - F1 is optimized somewhat */
1507
1508/* #define F1(x, y, z) (x & y | ~x & z) */
1509#define F1(x, y, z) (z ^ (x & (y ^ z)))
1510#define F2(x, y, z) F1(z, x, y)
1511#define F3(x, y, z) (x ^ y ^ z)
1512#define F4(x, y, z) (y ^ (x | ~z))
1513
1514/* This is the central step in the MD5 algorithm. */
1515#define MD5STEP(f, w, x, y, z, data, s) \
1516 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1517
1518/*
1519 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1520 * reflect the addition of 16 longwords of new data. MD5Update blocks
1521 * the data and converts bytes into longwords for this routine.
1522 */
1523static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1524{
1525 register FcChar32 a, b, c, d;
1526
1527 a = buf[0];
1528 b = buf[1];
1529 c = buf[2];
1530 d = buf[3];
1531
1532 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1533 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1534 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1535 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1536 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1537 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1538 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1539 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1540 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1541 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1542 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1543 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1544 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1545 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1546 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1547 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1548
1549 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1550 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1551 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1552 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1553 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1554 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1555 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1556 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1557 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1558 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1559 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1560 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1561 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1562 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1563 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1564 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1565
1566 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1567 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1568 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1569 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1570 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1571 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1572 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1573 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1574 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1575 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1576 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1577 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1578 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1579 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1580 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1581 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1582
1583 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1584 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1585 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1586 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1587 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1588 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1589 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1590 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1591 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1592 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1593 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1594 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1595 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1596 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1597 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1598 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1599
1600 buf[0] += a;
1601 buf[1] += b;
1602 buf[2] += c;
1603 buf[3] += d;
1604}