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