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