]>
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 |
24330d27 KP |
5 | * |
6 | * Permission to use, copy, modify, distribute, and sell this software and its | |
7 | * documentation for any purpose is hereby granted without fee, provided that | |
8 | * the above copyright notice appear in all copies and that both that | |
9 | * copyright notice and this permission notice appear in supporting | |
10 | * documentation, and that the name of Keith Packard not be used in | |
11 | * advertising or publicity pertaining to distribution of the software without | |
12 | * specific, written prior permission. Keith Packard makes no | |
13 | * representations about the suitability of this software for any purpose. It | |
14 | * is provided "as is" without express or implied warranty. | |
15 | * | |
16 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
18 | * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
19 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
20 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
21 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
22 | * PERFORMANCE OF THIS SOFTWARE. | |
23 | */ | |
24 | ||
212c9f43 | 25 | #include <fcntl.h> |
4262e0b3 | 26 | #include <dirent.h> |
212c9f43 PL |
27 | #include <sys/mman.h> |
28 | #include <sys/utsname.h> | |
fd77c154 PL |
29 | #include <sys/types.h> |
30 | #include <unistd.h> | |
24330d27 KP |
31 | #include "fcint.h" |
32 | ||
2dbe7597 PL |
33 | #define ENDIAN_TEST 0x12345678 |
34 | #define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1 | |
35 | ||
eb0cf671 PL |
36 | static off_t |
37 | FcCacheSkipToArch (int fd, const char * arch); | |
1b7be377 | 38 | |
eb0cf671 PL |
39 | static FcBool |
40 | FcCacheMoveDown (int fd, off_t start); | |
1b7be377 | 41 | |
eb0cf671 PL |
42 | static void * |
43 | FcDirCacheProduce (FcFontSet *set, FcCache * metadata); | |
1b7be377 PL |
44 | |
45 | static FcBool | |
eb0cf671 | 46 | FcDirCacheConsume (int fd, FcFontSet *set); |
1b7be377 PL |
47 | |
48 | static FcBool | |
2304e38f | 49 | FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir); |
1b7be377 | 50 | |
eb0cf671 PL |
51 | static int |
52 | FcCacheNextOffset(off_t w); | |
1b7be377 | 53 | |
eb0cf671 PL |
54 | static char * |
55 | FcCacheMachineSignature (void); | |
1b7be377 PL |
56 | |
57 | static FcBool | |
eb0cf671 | 58 | FcCacheHaveBank (int bank); |
1b7be377 | 59 | |
eb0cf671 | 60 | #define FC_DBG_CACHE_REF 1024 |
1b7be377 PL |
61 | |
62 | static FcChar8 * | |
eb0cf671 | 63 | FcCacheReadString (int fd, FcChar8 *dest, int len) |
24330d27 | 64 | { |
212c9f43 | 65 | FcChar8 c; |
ccb3e93b | 66 | FcBool escape; |
ccb3e93b KP |
67 | int size; |
68 | int i; | |
24330d27 | 69 | |
24330d27 KP |
70 | if (len == 0) |
71 | return FcFalse; | |
72 | ||
ccb3e93b KP |
73 | size = len; |
74 | i = 0; | |
24330d27 | 75 | escape = FcFalse; |
212c9f43 | 76 | while (read (fd, &c, 1) == 1) |
24330d27 KP |
77 | { |
78 | if (!escape) | |
79 | { | |
80 | switch (c) { | |
81 | case '"': | |
ccb3e93b KP |
82 | c = '\0'; |
83 | break; | |
24330d27 KP |
84 | case '\\': |
85 | escape = FcTrue; | |
86 | continue; | |
87 | } | |
88 | } | |
ccb3e93b KP |
89 | if (i == size) |
90 | { | |
212c9f43 PL |
91 | dest[i++] = 0; |
92 | return dest; | |
ccb3e93b | 93 | } |
212c9f43 | 94 | dest[i++] = c; |
ccb3e93b | 95 | if (c == '\0') |
212c9f43 | 96 | return dest; |
24330d27 KP |
97 | escape = FcFalse; |
98 | } | |
ccb3e93b | 99 | return 0; |
24330d27 KP |
100 | } |
101 | ||
102 | static FcBool | |
eb0cf671 | 103 | FcCacheWriteString (int fd, const FcChar8 *chars) |
327a7fd4 | 104 | { |
212c9f43 | 105 | if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1) |
327a7fd4 | 106 | return FcFalse; |
327a7fd4 KP |
107 | return FcTrue; |
108 | } | |
109 | ||
eb0cf671 PL |
110 | static void |
111 | FcGlobalCacheDirDestroy (FcGlobalCacheDir *d) | |
1b7be377 | 112 | { |
5e678e94 PL |
113 | FcMemFree (FC_MEM_STRING, strlen (d->name)+1); |
114 | free (d->name); | |
115 | FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir)); | |
116 | free (d); | |
1b7be377 PL |
117 | } |
118 | ||
eb0cf671 PL |
119 | FcGlobalCache * |
120 | FcGlobalCacheCreate (void) | |
1b7be377 | 121 | { |
eb0cf671 | 122 | FcGlobalCache *cache; |
1b7be377 | 123 | |
eb0cf671 PL |
124 | cache = malloc (sizeof (FcGlobalCache)); |
125 | if (!cache) | |
126 | return 0; | |
127 | FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache)); | |
128 | cache->dirs = 0; | |
129 | cache->updated = FcFalse; | |
130 | cache->fd = -1; | |
131 | return cache; | |
1b7be377 PL |
132 | } |
133 | ||
eb0cf671 PL |
134 | void |
135 | FcGlobalCacheDestroy (FcGlobalCache *cache) | |
327a7fd4 | 136 | { |
eb0cf671 | 137 | FcGlobalCacheDir *d, *next; |
327a7fd4 | 138 | |
eb0cf671 | 139 | for (d = cache->dirs; d; d = next) |
327a7fd4 | 140 | { |
eb0cf671 PL |
141 | next = d->next; |
142 | FcGlobalCacheDirDestroy (d); | |
327a7fd4 | 143 | } |
eb0cf671 PL |
144 | FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache)); |
145 | free (cache); | |
327a7fd4 KP |
146 | } |
147 | ||
148 | void | |
eb0cf671 PL |
149 | FcGlobalCacheLoad (FcGlobalCache *cache, |
150 | const FcChar8 *cache_file) | |
327a7fd4 | 151 | { |
eb0cf671 PL |
152 | FcChar8 name_buf[8192]; |
153 | FcGlobalCacheDir *d, *next; | |
154 | char * current_arch_machine_name; | |
155 | char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9]; | |
156 | off_t current_arch_start; | |
327a7fd4 | 157 | |
eb0cf671 PL |
158 | cache->fd = open ((char *) cache_file, O_RDONLY); |
159 | if (cache->fd == -1) | |
160 | return; | |
327a7fd4 | 161 | |
eb0cf671 | 162 | cache->updated = FcFalse; |
327a7fd4 | 163 | |
eb0cf671 PL |
164 | current_arch_machine_name = FcCacheMachineSignature (); |
165 | current_arch_start = FcCacheSkipToArch(cache->fd, | |
166 | current_arch_machine_name); | |
167 | if (current_arch_start < 0) | |
168 | goto bail0; | |
327a7fd4 | 169 | |
eb0cf671 PL |
170 | lseek (cache->fd, current_arch_start, SEEK_SET); |
171 | if (FcCacheReadString (cache->fd, candidate_arch_machine_name, | |
172 | sizeof (candidate_arch_machine_name)) == 0) | |
173 | goto bail0; | |
1b7be377 | 174 | |
eb0cf671 | 175 | while (1) |
1b7be377 | 176 | { |
eb0cf671 PL |
177 | FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)); |
178 | if (!strlen(name_buf)) | |
1b7be377 | 179 | break; |
1b7be377 | 180 | |
eb0cf671 PL |
181 | d = malloc (sizeof (FcGlobalCacheDir)); |
182 | if (!d) | |
183 | goto bail1; | |
1b7be377 | 184 | |
eb0cf671 PL |
185 | d->next = cache->dirs; |
186 | cache->dirs = d; | |
1b7be377 | 187 | |
eb0cf671 PL |
188 | d->name = FcStrCopy (name_buf); |
189 | d->ent = 0; | |
190 | d->offset = lseek (cache->fd, 0, SEEK_CUR); | |
191 | read (cache->fd, &d->metadata, sizeof (FcCache)); | |
192 | lseek (cache->fd, d->metadata.count, SEEK_CUR); | |
1b7be377 | 193 | } |
eb0cf671 | 194 | return; |
1b7be377 | 195 | |
eb0cf671 PL |
196 | bail1: |
197 | for (d = cache->dirs; d; d = next) | |
1b7be377 | 198 | { |
eb0cf671 PL |
199 | next = d->next; |
200 | free (d); | |
1b7be377 | 201 | } |
eb0cf671 PL |
202 | cache->dirs = 0; |
203 | bail0: | |
eb0cf671 PL |
204 | close (cache->fd); |
205 | cache->fd = -1; | |
206 | return; | |
1b7be377 PL |
207 | } |
208 | ||
1b7be377 | 209 | FcBool |
eb0cf671 | 210 | FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const FcChar8 *dir, FcConfig *config) |
1b7be377 | 211 | { |
eb0cf671 | 212 | FcGlobalCacheDir *d; |
1b7be377 | 213 | |
eb0cf671 | 214 | if (cache->fd == -1) |
1b7be377 | 215 | return FcFalse; |
1b7be377 | 216 | |
eb0cf671 | 217 | for (d = cache->dirs; d; d = d->next) |
1b7be377 | 218 | { |
eb0cf671 | 219 | if (strcmp (d->name, dir) == 0) |
1b7be377 | 220 | { |
eb0cf671 PL |
221 | lseek (cache->fd, d->offset, SEEK_SET); |
222 | if (!FcDirCacheConsume (cache->fd, set)) | |
223 | return FcFalse; | |
224 | return FcTrue; | |
1b7be377 | 225 | } |
1b7be377 | 226 | } |
1b7be377 | 227 | |
eb0cf671 | 228 | return FcFalse; |
1b7be377 PL |
229 | } |
230 | ||
eb0cf671 PL |
231 | FcBool |
232 | FcGlobalCacheUpdate (FcGlobalCache *cache, | |
233 | const FcChar8 *name, | |
234 | FcFontSet *set) | |
1b7be377 | 235 | { |
eb0cf671 | 236 | FcGlobalCacheDir * d; |
1b7be377 | 237 | |
eb0cf671 PL |
238 | if (!set->nfont) |
239 | return FcTrue; | |
1b7be377 | 240 | |
eb0cf671 | 241 | for (d = cache->dirs; d; d = d->next) |
1b7be377 | 242 | { |
eb0cf671 | 243 | if (strcmp(d->name, name) == 0) |
1b7be377 | 244 | break; |
24330d27 | 245 | } |
24330d27 | 246 | |
eb0cf671 | 247 | if (!d) |
24330d27 | 248 | { |
eb0cf671 PL |
249 | d = malloc (sizeof (FcGlobalCacheDir)); |
250 | if (!d) | |
251 | return FcFalse; | |
252 | d->next = cache->dirs; | |
253 | cache->dirs = d; | |
24330d27 | 254 | } |
24330d27 | 255 | |
eb0cf671 | 256 | cache->updated = FcTrue; |
24330d27 | 257 | |
eb0cf671 PL |
258 | d->name = FcStrCopy (name); |
259 | d->ent = FcDirCacheProduce (set, &d->metadata); | |
260 | d->offset = 0; | |
261 | return FcTrue; | |
24330d27 KP |
262 | } |
263 | ||
264 | FcBool | |
327a7fd4 KP |
265 | FcGlobalCacheSave (FcGlobalCache *cache, |
266 | const FcChar8 *cache_file) | |
24330d27 | 267 | { |
eb0cf671 | 268 | int fd; |
327a7fd4 | 269 | FcGlobalCacheDir *dir; |
327a7fd4 | 270 | FcAtomic *atomic; |
eb0cf671 PL |
271 | off_t current_arch_start = 0, truncate_to; |
272 | char * current_arch_machine_name, * header; | |
24330d27 | 273 | |
eb0cf671 | 274 | if (!cache->updated) |
24330d27 KP |
275 | return FcTrue; |
276 | ||
daeed6e0 | 277 | #if defined (HAVE_GETUID) && defined (HAVE_GETEUID) |
a391da8f KP |
278 | /* Set-UID programs can't safely update the cache */ |
279 | if (getuid () != geteuid ()) | |
280 | return FcFalse; | |
daeed6e0 | 281 | #endif |
a391da8f | 282 | |
134f6011 KP |
283 | atomic = FcAtomicCreate (cache_file); |
284 | if (!atomic) | |
24330d27 | 285 | goto bail0; |
134f6011 | 286 | if (!FcAtomicLock (atomic)) |
24330d27 | 287 | goto bail1; |
eb0cf671 PL |
288 | fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT, |
289 | S_IRUSR | S_IWUSR); | |
290 | if (fd == -1) | |
24330d27 KP |
291 | goto bail2; |
292 | ||
eb0cf671 PL |
293 | current_arch_machine_name = FcCacheMachineSignature (); |
294 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); | |
295 | if (current_arch_start < 0) | |
296 | current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END)); | |
297 | ||
298 | if (!FcCacheMoveDown(fd, current_arch_start)) | |
299 | goto bail2; | |
300 | ||
301 | current_arch_start = lseek(fd, 0, SEEK_CUR); | |
302 | if (ftruncate (fd, current_arch_start) == -1) | |
303 | goto bail2; | |
304 | ||
305 | truncate_to = current_arch_start; | |
306 | for (dir = cache->dirs; dir; dir = dir->next) | |
24330d27 | 307 | { |
eb0cf671 PL |
308 | truncate_to += strlen(dir->name) + 1; |
309 | truncate_to += sizeof (FcCache); | |
310 | truncate_to = FcCacheNextOffset (current_arch_start + truncate_to); | |
311 | truncate_to += dir->metadata.count; | |
312 | } | |
313 | truncate_to -= current_arch_start; | |
314 | header = malloc (10 + strlen (current_arch_machine_name)); | |
315 | if (!header) | |
316 | goto bail1; | |
317 | sprintf (header, "%8x ", (int)truncate_to); | |
318 | strcat (header, current_arch_machine_name); | |
319 | if (!FcCacheWriteString (fd, header)) | |
320 | goto bail1; | |
321 | ||
322 | for (dir = cache->dirs; dir; dir = dir->next) | |
323 | { | |
5e678e94 PL |
324 | if (dir->ent) |
325 | { | |
326 | FcCacheWriteString (fd, dir->name); | |
327 | write (fd, &dir->metadata, sizeof(FcCache)); | |
328 | lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET); | |
329 | write (fd, dir->ent, dir->metadata.count); | |
330 | free (dir->ent); | |
331 | } | |
24330d27 | 332 | } |
eb0cf671 | 333 | FcCacheWriteString (fd, ""); |
24330d27 | 334 | |
eb0cf671 | 335 | if (close (fd) == -1) |
24330d27 KP |
336 | goto bail3; |
337 | ||
134f6011 | 338 | if (!FcAtomicReplaceOrig (atomic)) |
24330d27 KP |
339 | goto bail3; |
340 | ||
134f6011 KP |
341 | FcAtomicUnlock (atomic); |
342 | FcAtomicDestroy (atomic); | |
343 | ||
24330d27 KP |
344 | cache->updated = FcFalse; |
345 | return FcTrue; | |
346 | ||
24330d27 | 347 | bail3: |
134f6011 | 348 | FcAtomicDeleteNew (atomic); |
24330d27 | 349 | bail2: |
134f6011 | 350 | FcAtomicUnlock (atomic); |
24330d27 | 351 | bail1: |
134f6011 | 352 | FcAtomicDestroy (atomic); |
24330d27 KP |
353 | bail0: |
354 | return FcFalse; | |
355 | } | |
356 | ||
eb0cf671 | 357 | #define PAGESIZE 8192 |
212c9f43 | 358 | /* |
eb0cf671 PL |
359 | * Find the next presumably-mmapable offset after the supplied file |
360 | * position. | |
212c9f43 | 361 | */ |
4262e0b3 PL |
362 | static int |
363 | FcCacheNextOffset(off_t w) | |
179c3995 | 364 | { |
212c9f43 PL |
365 | if (w % PAGESIZE == 0) |
366 | return w; | |
367 | else | |
368 | return ((w / PAGESIZE)+1)*PAGESIZE; | |
179c3995 KP |
369 | } |
370 | ||
212c9f43 PL |
371 | /* return the address of the segment for the provided arch, |
372 | * or -1 if arch not found */ | |
373 | static off_t | |
374 | FcCacheSkipToArch (int fd, const char * arch) | |
375 | { | |
2dbe7597 PL |
376 | char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9]; |
377 | char * candidate_arch; | |
212c9f43 PL |
378 | off_t current_arch_start = 0; |
379 | ||
380 | /* skip arches that are not the current arch */ | |
381 | while (1) | |
382 | { | |
383 | long bs; | |
384 | ||
385 | lseek (fd, current_arch_start, SEEK_SET); | |
eb0cf671 | 386 | if (FcCacheReadString (fd, candidate_arch_machine_name_count, |
2dbe7597 | 387 | sizeof (candidate_arch_machine_name_count)) == 0) |
5e678e94 | 388 | return -1; |
2dbe7597 PL |
389 | if (!strlen(candidate_arch_machine_name_count)) |
390 | return -1; | |
391 | bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16); | |
392 | candidate_arch++; /* skip leading space */ | |
212c9f43 | 393 | |
2dbe7597 | 394 | if (strcmp (candidate_arch, arch)==0) |
5e678e94 | 395 | return current_arch_start; |
212c9f43 PL |
396 | current_arch_start += bs; |
397 | } | |
398 | ||
5e678e94 | 399 | return -1; |
212c9f43 PL |
400 | } |
401 | ||
402 | /* Cuts out the segment at the file pointer (moves everything else | |
403 | * down to cover it), and leaves the file pointer at the end of the | |
404 | * file. */ | |
212c9f43 PL |
405 | static FcBool |
406 | FcCacheMoveDown (int fd, off_t start) | |
407 | { | |
eb0cf671 | 408 | char * buf = malloc (8192); |
2dbe7597 | 409 | char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9]; |
4262e0b3 | 410 | long bs; |
212c9f43 PL |
411 | int c, bytes_skipped; |
412 | ||
413 | if (!buf) | |
414 | return FcFalse; | |
415 | ||
416 | lseek (fd, start, SEEK_SET); | |
eb0cf671 | 417 | if (FcCacheReadString (fd, candidate_arch_machine_name, |
212c9f43 PL |
418 | sizeof (candidate_arch_machine_name)) == 0) |
419 | goto done; | |
2dbe7597 | 420 | if (!strlen(candidate_arch_machine_name)) |
212c9f43 PL |
421 | goto done; |
422 | ||
2dbe7597 | 423 | bs = strtol(candidate_arch_machine_name, 0, 16); |
212c9f43 PL |
424 | if (bs == 0) |
425 | goto done; | |
426 | ||
427 | bytes_skipped = 0; | |
428 | do | |
429 | { | |
430 | lseek (fd, start+bs+bytes_skipped, SEEK_SET); | |
eb0cf671 | 431 | if ((c = read (fd, buf, 8192)) <= 0) |
212c9f43 PL |
432 | break; |
433 | lseek (fd, start+bytes_skipped, SEEK_SET); | |
434 | if (write (fd, buf, c) < 0) | |
435 | goto bail; | |
436 | bytes_skipped += c; | |
437 | } | |
438 | while (c > 0); | |
439 | lseek (fd, start+bytes_skipped, SEEK_SET); | |
440 | ||
441 | done: | |
442 | free (buf); | |
443 | return FcTrue; | |
444 | ||
445 | bail: | |
446 | free (buf); | |
447 | return FcFalse; | |
448 | } | |
449 | ||
1b7be377 PL |
450 | FcBool |
451 | FcDirCacheValid (const FcChar8 *dir) | |
452 | { | |
453 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); | |
454 | struct stat file_stat, dir_stat; | |
455 | ||
456 | if (stat ((char *) dir, &dir_stat) < 0) | |
457 | { | |
458 | FcStrFree (cache_file); | |
459 | return FcFalse; | |
460 | } | |
461 | if (stat ((char *) cache_file, &file_stat) < 0) | |
462 | { | |
463 | FcStrFree (cache_file); | |
464 | return FcFalse; | |
465 | } | |
466 | FcStrFree (cache_file); | |
467 | /* | |
468 | * If the directory has been modified more recently than | |
469 | * the cache file, the cache is not valid | |
470 | */ | |
471 | if (dir_stat.st_mtime - file_stat.st_mtime > 0) | |
472 | return FcFalse; | |
473 | return FcTrue; | |
474 | } | |
475 | ||
4262e0b3 | 476 | static int |
1b7be377 PL |
477 | FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache, |
478 | FcStrList *list, FcFontSet * set) | |
4262e0b3 PL |
479 | { |
480 | DIR *d; | |
481 | struct dirent *e; | |
482 | int ret = 0; | |
483 | FcChar8 *dir; | |
484 | FcChar8 *file, *base; | |
485 | FcStrSet *subdirs; | |
486 | FcStrList *sublist; | |
487 | struct stat statb; | |
488 | ||
489 | /* | |
490 | * Now scan all of the directories into separate databases | |
491 | * and write out the results | |
492 | */ | |
493 | while ((dir = FcStrListNext (list))) | |
494 | { | |
495 | /* freed below */ | |
496 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); | |
497 | if (!file) | |
498 | return FcFalse; | |
499 | ||
500 | strcpy ((char *) file, (char *) dir); | |
501 | strcat ((char *) file, "/"); | |
502 | base = file + strlen ((char *) file); | |
503 | ||
504 | subdirs = FcStrSetCreate (); | |
505 | if (!subdirs) | |
506 | { | |
507 | fprintf (stderr, "Can't create directory set\n"); | |
508 | ret++; | |
509 | free (file); | |
510 | continue; | |
511 | } | |
512 | ||
513 | if (access ((char *) dir, X_OK) < 0) | |
514 | { | |
515 | switch (errno) { | |
516 | case ENOENT: | |
517 | case ENOTDIR: | |
518 | case EACCES: | |
519 | break; | |
520 | default: | |
521 | fprintf (stderr, "\"%s\": ", dir); | |
522 | perror (""); | |
523 | ret++; | |
524 | } | |
525 | FcStrSetDestroy (subdirs); | |
526 | free (file); | |
527 | continue; | |
528 | } | |
529 | if (stat ((char *) dir, &statb) == -1) | |
530 | { | |
531 | fprintf (stderr, "\"%s\": ", dir); | |
532 | perror (""); | |
533 | FcStrSetDestroy (subdirs); | |
534 | ret++; | |
535 | free (file); | |
536 | continue; | |
537 | } | |
538 | if (!S_ISDIR (statb.st_mode)) | |
539 | { | |
540 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
541 | FcStrSetDestroy (subdirs); | |
542 | free (file); | |
543 | continue; | |
544 | } | |
2304e38f | 545 | if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir)) |
4262e0b3 | 546 | { |
1b7be377 PL |
547 | if (FcDebug () & FC_DBG_FONTSET) |
548 | printf ("scan dir %s\n", dir); | |
2304e38f | 549 | |
1b7be377 PL |
550 | FcDirScanConfig (set, subdirs, cache, |
551 | config->blanks, dir, FcFalse, config); | |
4262e0b3 PL |
552 | } |
553 | sublist = FcStrListCreate (subdirs); | |
554 | FcStrSetDestroy (subdirs); | |
555 | if (!sublist) | |
556 | { | |
557 | fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); | |
558 | ret++; | |
559 | free (file); | |
560 | continue; | |
561 | } | |
1b7be377 | 562 | ret += FcCacheReadDirs (config, cache, sublist, set); |
4262e0b3 PL |
563 | free (file); |
564 | } | |
565 | FcStrListDone (list); | |
566 | return ret; | |
567 | } | |
568 | ||
569 | FcFontSet * | |
1b7be377 | 570 | FcCacheRead (FcConfig *config, FcGlobalCache * cache) |
4262e0b3 PL |
571 | { |
572 | FcFontSet * s = FcFontSetCreate(); | |
573 | if (!s) | |
574 | return 0; | |
575 | ||
1b7be377 | 576 | if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s)) |
4262e0b3 PL |
577 | goto bail; |
578 | ||
579 | return s; | |
580 | ||
581 | bail: | |
582 | FcFontSetDestroy (s); | |
583 | return 0; | |
584 | } | |
585 | ||
212c9f43 | 586 | /* read serialized state from the cache file */ |
eb0cf671 | 587 | static FcBool |
2304e38f | 588 | FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir) |
212c9f43 | 589 | { |
4262e0b3 PL |
590 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); |
591 | int fd; | |
212c9f43 | 592 | char * current_arch_machine_name; |
2dbe7597 | 593 | char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; |
212c9f43 | 594 | off_t current_arch_start = 0; |
2304e38f | 595 | FcChar8 subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; |
212c9f43 | 596 | |
4262e0b3 PL |
597 | if (!cache_file) |
598 | goto bail; | |
212c9f43 | 599 | |
eb0cf671 | 600 | current_arch_machine_name = FcCacheMachineSignature(); |
4262e0b3 | 601 | fd = open(cache_file, O_RDONLY); |
212c9f43 | 602 | if (fd == -1) |
2dbe7597 | 603 | goto bail; |
212c9f43 | 604 | |
212c9f43 PL |
605 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); |
606 | if (current_arch_start < 0) | |
4262e0b3 | 607 | goto bail1; |
212c9f43 PL |
608 | |
609 | lseek (fd, current_arch_start, SEEK_SET); | |
eb0cf671 | 610 | if (FcCacheReadString (fd, candidate_arch_machine_name, |
212c9f43 | 611 | sizeof (candidate_arch_machine_name)) == 0) |
4262e0b3 | 612 | goto bail1; |
2304e38f PL |
613 | |
614 | while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0) | |
615 | FcStrSetAdd (dirs, subdirName); | |
616 | ||
eb0cf671 PL |
617 | if (!FcDirCacheConsume (fd, set)) |
618 | goto bail1; | |
619 | ||
620 | close(fd); | |
621 | free (cache_file); | |
622 | return FcTrue; | |
623 | ||
624 | bail1: | |
2304e38f | 625 | close (fd); |
eb0cf671 PL |
626 | bail: |
627 | free (cache_file); | |
628 | return FcFalse; | |
629 | } | |
630 | ||
631 | static FcBool | |
632 | FcDirCacheConsume (int fd, FcFontSet *set) | |
633 | { | |
634 | FcCache metadata; | |
635 | void * current_dir_block; | |
fd77c154 | 636 | off_t pos; |
212c9f43 | 637 | |
212c9f43 PL |
638 | read(fd, &metadata, sizeof(FcCache)); |
639 | if (metadata.magic != FC_CACHE_MAGIC) | |
eb0cf671 | 640 | return FcFalse; |
212c9f43 | 641 | |
2dbe7597 | 642 | if (!metadata.count) |
eb0cf671 | 643 | return FcTrue; |
2dbe7597 | 644 | |
fd77c154 | 645 | pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)); |
2dbe7597 PL |
646 | current_dir_block = mmap (0, metadata.count, |
647 | PROT_READ, MAP_SHARED, fd, pos); | |
648 | if (current_dir_block == MAP_FAILED) | |
eb0cf671 | 649 | return FcFalse; |
2dbe7597 PL |
650 | |
651 | if (!FcFontSetUnserialize (metadata, set, current_dir_block)) | |
eb0cf671 PL |
652 | return FcFalse; |
653 | ||
212c9f43 | 654 | return FcTrue; |
eb0cf671 PL |
655 | } |
656 | ||
657 | static void * | |
658 | FcDirCacheProduce (FcFontSet *set, FcCache *metadata) | |
659 | { | |
660 | void * current_dir_block, * final_dir_block; | |
661 | static int rand_state = 0; | |
662 | int bank; | |
663 | ||
664 | if (!rand_state) | |
665 | rand_state = time(0L); | |
666 | bank = rand_r(&rand_state); | |
667 | ||
668 | while (FcCacheHaveBank(bank)) | |
669 | bank = rand_r(&rand_state); | |
670 | ||
671 | memset (metadata, 0, sizeof(FcCache)); | |
672 | FcFontSetNewBank(); | |
673 | metadata->count = FcFontSetNeededBytes (set); | |
674 | metadata->magic = FC_CACHE_MAGIC; | |
675 | metadata->bank = bank; | |
676 | ||
677 | if (!metadata->count) /* not a failure, no fonts to write */ | |
678 | return 0; | |
679 | ||
680 | current_dir_block = malloc (metadata->count); | |
681 | if (!current_dir_block) | |
682 | goto bail; | |
683 | final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block); | |
684 | ||
685 | if ((char *)current_dir_block + metadata->count != final_dir_block) | |
686 | goto bail; | |
687 | ||
688 | if (!FcFontSetSerialize (bank, set)) | |
689 | goto bail; | |
690 | ||
691 | return current_dir_block; | |
212c9f43 | 692 | |
4262e0b3 | 693 | bail: |
eb0cf671 PL |
694 | free (current_dir_block); |
695 | return 0; | |
212c9f43 PL |
696 | } |
697 | ||
698 | /* write serialized state to the cache file */ | |
699 | FcBool | |
2304e38f | 700 | FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir) |
212c9f43 | 701 | { |
4262e0b3 | 702 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); |
2304e38f | 703 | int fd, i; |
212c9f43 PL |
704 | FcCache metadata; |
705 | off_t current_arch_start = 0, truncate_to; | |
2dbe7597 | 706 | char * current_arch_machine_name, * header; |
eb0cf671 | 707 | void * current_dir_block; |
212c9f43 | 708 | |
4262e0b3 PL |
709 | if (!cache_file) |
710 | goto bail; | |
711 | ||
eb0cf671 | 712 | current_dir_block = FcDirCacheProduce (set, &metadata); |
212c9f43 | 713 | |
eb0cf671 | 714 | if (!metadata.count) |
4262e0b3 PL |
715 | { |
716 | unlink (cache_file); | |
717 | free (cache_file); | |
718 | return FcTrue; | |
719 | } | |
720 | ||
4262e0b3 PL |
721 | if (!current_dir_block) |
722 | goto bail; | |
212c9f43 | 723 | |
4262e0b3 PL |
724 | if (FcDebug () & FC_DBG_CACHE) |
725 | printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file); | |
726 | ||
727 | fd = open(cache_file, O_RDWR | O_CREAT, 0666); | |
212c9f43 | 728 | if (fd == -1) |
eb0cf671 | 729 | goto bail0; |
212c9f43 | 730 | |
eb0cf671 | 731 | current_arch_machine_name = FcCacheMachineSignature (); |
212c9f43 PL |
732 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); |
733 | if (current_arch_start < 0) | |
4262e0b3 | 734 | current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END)); |
212c9f43 PL |
735 | |
736 | if (!FcCacheMoveDown(fd, current_arch_start)) | |
0230c9f8 | 737 | goto bail1; |
212c9f43 PL |
738 | |
739 | current_arch_start = lseek(fd, 0, SEEK_CUR); | |
740 | if (ftruncate (fd, current_arch_start) == -1) | |
0230c9f8 | 741 | goto bail1; |
212c9f43 | 742 | |
212c9f43 | 743 | /* now write the address of the next offset */ |
eb0cf671 | 744 | truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start; |
2dbe7597 | 745 | header = malloc (10 + strlen (current_arch_machine_name)); |
eb0cf671 PL |
746 | if (!header) |
747 | goto bail1; | |
2dbe7597 PL |
748 | sprintf (header, "%8x ", (int)truncate_to); |
749 | strcat (header, current_arch_machine_name); | |
eb0cf671 | 750 | if (!FcCacheWriteString (fd, header)) |
4262e0b3 | 751 | goto bail1; |
212c9f43 | 752 | |
2304e38f PL |
753 | for (i = 0; i < dirs->size; i++) |
754 | FcCacheWriteString (fd, dirs->strs[i]); | |
755 | FcCacheWriteString (fd, ""); | |
756 | ||
4262e0b3 PL |
757 | write (fd, &metadata, sizeof(FcCache)); |
758 | lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET); | |
eb0cf671 | 759 | write (fd, current_dir_block, metadata.count); |
5e678e94 | 760 | free (current_dir_block); |
4262e0b3 | 761 | |
2dbe7597 | 762 | /* this actually serves to pad out the cache file, if needed */ |
212c9f43 | 763 | if (ftruncate (fd, current_arch_start + truncate_to) == -1) |
4262e0b3 | 764 | goto bail1; |
212c9f43 PL |
765 | |
766 | close(fd); | |
767 | return FcTrue; | |
768 | ||
4262e0b3 | 769 | bail1: |
0230c9f8 | 770 | free (header); |
eb0cf671 PL |
771 | bail0: |
772 | free (current_dir_block); | |
4262e0b3 PL |
773 | bail: |
774 | unlink (cache_file); | |
775 | free (cache_file); | |
212c9f43 PL |
776 | return FcFalse; |
777 | } | |
778 | ||
2dbe7597 | 779 | static char * |
eb0cf671 | 780 | FcCacheMachineSignature () |
2dbe7597 PL |
781 | { |
782 | static char buf[MACHINE_SIGNATURE_SIZE]; | |
783 | int magic = ENDIAN_TEST; | |
784 | char * m = (char *)&magic; | |
785 | ||
786 | sprintf (buf, "%2x%2x%2x%2x " | |
787 | "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x " | |
788 | "%4x %4x %4x %4x %4x %4x %4x\n", | |
789 | m[0], m[1], m[2], m[3], | |
790 | sizeof (char), | |
791 | sizeof (char *), | |
792 | sizeof (int), | |
793 | sizeof (FcPattern), | |
794 | sizeof (FcPatternEltPtr), | |
795 | sizeof (struct _FcPatternElt *), | |
796 | sizeof (FcPatternElt), | |
797 | sizeof (FcObjectPtr), | |
798 | sizeof (FcValueListPtr), | |
799 | sizeof (FcValue), | |
800 | sizeof (FcValueBinding), | |
801 | sizeof (struct _FcValueList *), | |
802 | sizeof (FcCharSet), | |
803 | sizeof (FcCharLeaf **), | |
804 | sizeof (FcChar16 *), | |
805 | sizeof (FcChar16), | |
806 | sizeof (FcCharLeaf), | |
807 | sizeof (FcChar32), | |
808 | sizeof (FcCache)); | |
809 | ||
810 | return buf; | |
811 | } | |
812 | ||
4262e0b3 PL |
813 | static int banks_ptr = 0, banks_alloc = 0; |
814 | static int * bankId = 0; | |
815 | ||
eb0cf671 | 816 | static FcBool |
4262e0b3 PL |
817 | FcCacheHaveBank (int bank) |
818 | { | |
819 | int i; | |
820 | ||
821 | if (bank < FC_BANK_FIRST) | |
822 | return FcTrue; | |
823 | ||
824 | for (i = 0; i < banks_ptr; i++) | |
825 | if (bankId[i] == bank) | |
826 | return FcTrue; | |
827 | ||
828 | return FcFalse; | |
829 | } | |
830 | ||
831 | int | |
832 | FcCacheBankToIndex (int bank) | |
833 | { | |
834 | static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1; | |
835 | int i; | |
836 | int * b; | |
837 | ||
838 | if (bank == lastBank) | |
839 | return lastIndex; | |
840 | ||
841 | for (i = 0; i < banks_ptr; i++) | |
842 | if (bankId[i] == bank) | |
843 | return i; | |
844 | ||
2dbe7597 | 845 | if (banks_ptr >= banks_alloc) |
4262e0b3 | 846 | { |
2dbe7597 | 847 | b = realloc (bankId, (banks_alloc + 4) * sizeof(int)); |
4262e0b3 PL |
848 | if (!b) |
849 | return -1; | |
850 | ||
851 | bankId = b; | |
852 | banks_alloc += 4; | |
853 | } | |
854 | ||
855 | i = banks_ptr++; | |
856 | bankId[i] = bank; | |
857 | return i; | |
858 | } |