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