]>
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 | 40 | static FcBool |
1d879de2 | 41 | FcCacheCopyOld (int fd, int fd_orig, 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 | 48 | |
2eb84374 | 49 | 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 | 62 | |
8245771d PL |
63 | static char * |
64 | FcCacheReadString (int fd, char *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 | |
8245771d | 104 | FcCacheWriteString (int fd, const char *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 | { |
8245771d | 154 | char name_buf[8192]; |
eb0cf671 PL |
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) | |
e58b50e8 | 175 | goto bail_and_destroy; |
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) | |
e58b50e8 | 181 | goto bail_and_destroy; |
1b7be377 | 182 | |
eb0cf671 | 183 | while (1) |
1b7be377 | 184 | { |
8cb4c56d PL |
185 | off_t targ; |
186 | ||
eb0cf671 PL |
187 | FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)); |
188 | if (!strlen(name_buf)) | |
1b7be377 | 189 | break; |
1b7be377 | 190 | |
03a212e5 PL |
191 | if (stat ((char *) name_buf, &dir_stat) < 0 || |
192 | dir_stat.st_mtime > cache_stat.st_mtime) | |
193 | { | |
194 | FcCache md; | |
195 | ||
8245771d | 196 | FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf)); |
03a212e5 PL |
197 | read (cache->fd, &md, sizeof (FcCache)); |
198 | lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET); | |
199 | continue; | |
200 | } | |
201 | ||
eb0cf671 PL |
202 | d = malloc (sizeof (FcGlobalCacheDir)); |
203 | if (!d) | |
204 | goto bail1; | |
1b7be377 | 205 | |
eb0cf671 PL |
206 | d->next = cache->dirs; |
207 | cache->dirs = d; | |
1b7be377 | 208 | |
8245771d | 209 | d->name = (char *)FcStrCopy ((FcChar8 *)name_buf); |
eb0cf671 PL |
210 | d->ent = 0; |
211 | d->offset = lseek (cache->fd, 0, SEEK_CUR); | |
8cb4c56d PL |
212 | if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache)) |
213 | goto bail1; | |
214 | targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count; | |
215 | if (lseek (cache->fd, targ, SEEK_SET) != targ) | |
216 | goto bail1; | |
1b7be377 | 217 | } |
eb0cf671 | 218 | return; |
1b7be377 | 219 | |
eb0cf671 PL |
220 | bail1: |
221 | for (d = cache->dirs; d; d = next) | |
1b7be377 | 222 | { |
eb0cf671 PL |
223 | next = d->next; |
224 | free (d); | |
1b7be377 | 225 | } |
eb0cf671 | 226 | cache->dirs = 0; |
e58b50e8 PL |
227 | |
228 | close (cache->fd); | |
229 | cache->fd = -1; | |
230 | return; | |
231 | ||
232 | bail_and_destroy: | |
eb0cf671 PL |
233 | close (cache->fd); |
234 | cache->fd = -1; | |
e58b50e8 PL |
235 | |
236 | if (stat ((char *) cache_file, &cache_stat) == 0) | |
237 | unlink ((char *)cache_file); | |
238 | ||
eb0cf671 | 239 | return; |
e58b50e8 | 240 | |
1b7be377 PL |
241 | } |
242 | ||
1b7be377 | 243 | FcBool |
8245771d | 244 | FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config) |
1b7be377 | 245 | { |
eb0cf671 | 246 | FcGlobalCacheDir *d; |
03a212e5 | 247 | FcBool ret = FcFalse; |
1b7be377 | 248 | |
eb0cf671 | 249 | if (cache->fd == -1) |
1b7be377 | 250 | return FcFalse; |
1b7be377 | 251 | |
eb0cf671 | 252 | for (d = cache->dirs; d; d = d->next) |
1b7be377 | 253 | { |
03a212e5 | 254 | if (strncmp (d->name, dir, strlen(dir)) == 0) |
1b7be377 | 255 | { |
eb0cf671 PL |
256 | lseek (cache->fd, d->offset, SEEK_SET); |
257 | if (!FcDirCacheConsume (cache->fd, set)) | |
258 | return FcFalse; | |
03a212e5 PL |
259 | if (strcmp (d->name, dir) == 0) |
260 | ret = FcTrue; | |
1b7be377 | 261 | } |
1b7be377 | 262 | } |
1b7be377 | 263 | |
03a212e5 | 264 | return ret; |
1b7be377 PL |
265 | } |
266 | ||
eb0cf671 PL |
267 | FcBool |
268 | FcGlobalCacheUpdate (FcGlobalCache *cache, | |
8245771d | 269 | const char *name, |
eb0cf671 | 270 | FcFontSet *set) |
1b7be377 | 271 | { |
eb0cf671 | 272 | FcGlobalCacheDir * d; |
1b7be377 | 273 | |
eb0cf671 PL |
274 | if (!set->nfont) |
275 | return FcTrue; | |
1b7be377 | 276 | |
eb0cf671 | 277 | for (d = cache->dirs; d; d = d->next) |
1b7be377 | 278 | { |
eb0cf671 | 279 | if (strcmp(d->name, name) == 0) |
1b7be377 | 280 | break; |
24330d27 | 281 | } |
24330d27 | 282 | |
eb0cf671 | 283 | if (!d) |
24330d27 | 284 | { |
eb0cf671 PL |
285 | d = malloc (sizeof (FcGlobalCacheDir)); |
286 | if (!d) | |
287 | return FcFalse; | |
288 | d->next = cache->dirs; | |
289 | cache->dirs = d; | |
24330d27 | 290 | } |
24330d27 | 291 | |
eb0cf671 | 292 | cache->updated = FcTrue; |
24330d27 | 293 | |
8245771d | 294 | d->name = (char *)FcStrCopy ((FcChar8 *)name); |
eb0cf671 PL |
295 | d->ent = FcDirCacheProduce (set, &d->metadata); |
296 | d->offset = 0; | |
297 | return FcTrue; | |
24330d27 KP |
298 | } |
299 | ||
300 | FcBool | |
327a7fd4 KP |
301 | FcGlobalCacheSave (FcGlobalCache *cache, |
302 | const FcChar8 *cache_file) | |
24330d27 | 303 | { |
1d879de2 | 304 | int fd, fd_orig; |
327a7fd4 | 305 | FcGlobalCacheDir *dir; |
327a7fd4 | 306 | FcAtomic *atomic; |
eb0cf671 PL |
307 | off_t current_arch_start = 0, truncate_to; |
308 | char * current_arch_machine_name, * header; | |
24330d27 | 309 | |
eb0cf671 | 310 | if (!cache->updated) |
24330d27 KP |
311 | return FcTrue; |
312 | ||
daeed6e0 | 313 | #if defined (HAVE_GETUID) && defined (HAVE_GETEUID) |
a391da8f KP |
314 | /* Set-UID programs can't safely update the cache */ |
315 | if (getuid () != geteuid ()) | |
316 | return FcFalse; | |
daeed6e0 | 317 | #endif |
a391da8f | 318 | |
134f6011 KP |
319 | atomic = FcAtomicCreate (cache_file); |
320 | if (!atomic) | |
1d879de2 PL |
321 | return FcFalse; |
322 | ||
134f6011 | 323 | if (!FcAtomicLock (atomic)) |
24330d27 | 324 | goto bail1; |
eb0cf671 PL |
325 | fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT, |
326 | S_IRUSR | S_IWUSR); | |
327 | if (fd == -1) | |
24330d27 KP |
328 | goto bail2; |
329 | ||
1d879de2 PL |
330 | fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY); |
331 | ||
eb0cf671 | 332 | current_arch_machine_name = FcCacheMachineSignature (); |
1d879de2 PL |
333 | if (fd_orig == -1) |
334 | current_arch_start = 0; | |
335 | else | |
336 | current_arch_start = FcCacheSkipToArch (fd_orig, | |
337 | current_arch_machine_name); | |
338 | ||
eb0cf671 | 339 | if (current_arch_start < 0) |
649cc361 | 340 | current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END)); |
eb0cf671 | 341 | |
1d879de2 PL |
342 | if (!FcCacheCopyOld(fd, fd_orig, current_arch_start)) |
343 | goto bail3; | |
344 | ||
345 | close (fd_orig); | |
346 | fd_orig = -1; | |
eb0cf671 PL |
347 | |
348 | current_arch_start = lseek(fd, 0, SEEK_CUR); | |
349 | if (ftruncate (fd, current_arch_start) == -1) | |
1d879de2 | 350 | goto bail3; |
eb0cf671 | 351 | |
03a212e5 PL |
352 | header = malloc (10 + strlen (current_arch_machine_name)); |
353 | if (!header) | |
1d879de2 | 354 | goto bail3; |
03a212e5 | 355 | |
8cb4c56d | 356 | truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11; |
eb0cf671 | 357 | for (dir = cache->dirs; dir; dir = dir->next) |
24330d27 | 358 | { |
eb0cf671 PL |
359 | truncate_to += strlen(dir->name) + 1; |
360 | truncate_to += sizeof (FcCache); | |
361 | truncate_to = FcCacheNextOffset (current_arch_start + truncate_to); | |
362 | truncate_to += dir->metadata.count; | |
363 | } | |
364 | truncate_to -= current_arch_start; | |
eb0cf671 | 365 | |
8cb4c56d PL |
366 | sprintf (header, "%8x ", (int)truncate_to); |
367 | strcat (header, current_arch_machine_name); | |
368 | if (!FcCacheWriteString (fd, header)) | |
1d879de2 | 369 | goto bail4; |
8cb4c56d | 370 | |
eb0cf671 PL |
371 | for (dir = cache->dirs; dir; dir = dir->next) |
372 | { | |
5e678e94 PL |
373 | if (dir->ent) |
374 | { | |
375 | FcCacheWriteString (fd, dir->name); | |
376 | write (fd, &dir->metadata, sizeof(FcCache)); | |
f6ee3db5 | 377 | lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET); |
5e678e94 PL |
378 | write (fd, dir->ent, dir->metadata.count); |
379 | free (dir->ent); | |
380 | } | |
24330d27 | 381 | } |
eb0cf671 | 382 | FcCacheWriteString (fd, ""); |
24330d27 | 383 | |
eb0cf671 | 384 | if (close (fd) == -1) |
1d879de2 | 385 | goto bail25; |
24330d27 | 386 | |
134f6011 | 387 | if (!FcAtomicReplaceOrig (atomic)) |
1d879de2 | 388 | goto bail25; |
24330d27 | 389 | |
134f6011 KP |
390 | FcAtomicUnlock (atomic); |
391 | FcAtomicDestroy (atomic); | |
392 | ||
24330d27 KP |
393 | cache->updated = FcFalse; |
394 | return FcTrue; | |
395 | ||
1d879de2 PL |
396 | bail4: |
397 | free (header); | |
398 | bail3: | |
399 | if (fd_orig != -1) | |
400 | close (fd_orig); | |
401 | ||
402 | close (fd); | |
403 | bail25: | |
134f6011 | 404 | FcAtomicDeleteNew (atomic); |
1d879de2 | 405 | bail2: |
134f6011 | 406 | FcAtomicUnlock (atomic); |
1d879de2 | 407 | bail1: |
134f6011 | 408 | FcAtomicDestroy (atomic); |
24330d27 KP |
409 | return FcFalse; |
410 | } | |
411 | ||
eb0cf671 | 412 | #define PAGESIZE 8192 |
212c9f43 | 413 | /* |
eb0cf671 PL |
414 | * Find the next presumably-mmapable offset after the supplied file |
415 | * position. | |
212c9f43 | 416 | */ |
4262e0b3 PL |
417 | static int |
418 | FcCacheNextOffset(off_t w) | |
179c3995 | 419 | { |
212c9f43 PL |
420 | if (w % PAGESIZE == 0) |
421 | return w; | |
422 | else | |
423 | return ((w / PAGESIZE)+1)*PAGESIZE; | |
179c3995 KP |
424 | } |
425 | ||
212c9f43 PL |
426 | /* return the address of the segment for the provided arch, |
427 | * or -1 if arch not found */ | |
428 | static off_t | |
429 | FcCacheSkipToArch (int fd, const char * arch) | |
430 | { | |
2dbe7597 PL |
431 | char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9]; |
432 | char * candidate_arch; | |
212c9f43 PL |
433 | off_t current_arch_start = 0; |
434 | ||
435 | /* skip arches that are not the current arch */ | |
436 | while (1) | |
437 | { | |
438 | long bs; | |
439 | ||
8cb4c56d PL |
440 | if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start) |
441 | return -1; | |
442 | ||
eb0cf671 | 443 | if (FcCacheReadString (fd, candidate_arch_machine_name_count, |
2dbe7597 | 444 | sizeof (candidate_arch_machine_name_count)) == 0) |
5e678e94 | 445 | return -1; |
2dbe7597 PL |
446 | if (!strlen(candidate_arch_machine_name_count)) |
447 | return -1; | |
448 | bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16); | |
8cb4c56d | 449 | |
13cdf607 | 450 | // count = 0 should probably be distinguished from the !bs condition |
8cb4c56d PL |
451 | if (!bs || bs < strlen (candidate_arch_machine_name_count)) |
452 | return -1; | |
453 | ||
2dbe7597 | 454 | candidate_arch++; /* skip leading space */ |
212c9f43 | 455 | |
2dbe7597 | 456 | if (strcmp (candidate_arch, arch)==0) |
5e678e94 | 457 | return current_arch_start; |
212c9f43 PL |
458 | current_arch_start += bs; |
459 | } | |
460 | ||
5e678e94 | 461 | return -1; |
212c9f43 PL |
462 | } |
463 | ||
464 | /* Cuts out the segment at the file pointer (moves everything else | |
465 | * down to cover it), and leaves the file pointer at the end of the | |
466 | * file. */ | |
212c9f43 | 467 | static FcBool |
1d879de2 | 468 | FcCacheCopyOld (int fd, int fd_orig, off_t start) |
212c9f43 | 469 | { |
eb0cf671 | 470 | char * buf = malloc (8192); |
2dbe7597 | 471 | char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9]; |
4262e0b3 | 472 | long bs; |
212c9f43 | 473 | int c, bytes_skipped; |
1d879de2 | 474 | off_t loc; |
212c9f43 PL |
475 | |
476 | if (!buf) | |
477 | return FcFalse; | |
478 | ||
1d879de2 PL |
479 | loc = 0; |
480 | lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET); | |
481 | do | |
482 | { | |
483 | int b = 8192; | |
484 | if (loc + b > start) | |
485 | b = start - loc; | |
486 | ||
487 | if ((c = read (fd_orig, buf, b)) <= 0) | |
488 | break; | |
489 | if (write (fd, buf, c) < 0) | |
490 | goto bail; | |
491 | ||
492 | loc += c; | |
493 | } | |
494 | while (c > 0); | |
495 | ||
212c9f43 | 496 | lseek (fd, start, SEEK_SET); |
eb0cf671 | 497 | if (FcCacheReadString (fd, candidate_arch_machine_name, |
212c9f43 PL |
498 | sizeof (candidate_arch_machine_name)) == 0) |
499 | goto done; | |
2dbe7597 | 500 | if (!strlen(candidate_arch_machine_name)) |
212c9f43 PL |
501 | goto done; |
502 | ||
2dbe7597 | 503 | bs = strtol(candidate_arch_machine_name, 0, 16); |
212c9f43 PL |
504 | if (bs == 0) |
505 | goto done; | |
506 | ||
507 | bytes_skipped = 0; | |
508 | do | |
509 | { | |
510 | lseek (fd, start+bs+bytes_skipped, SEEK_SET); | |
eb0cf671 | 511 | if ((c = read (fd, buf, 8192)) <= 0) |
212c9f43 PL |
512 | break; |
513 | lseek (fd, start+bytes_skipped, SEEK_SET); | |
514 | if (write (fd, buf, c) < 0) | |
515 | goto bail; | |
516 | bytes_skipped += c; | |
517 | } | |
518 | while (c > 0); | |
519 | lseek (fd, start+bytes_skipped, SEEK_SET); | |
520 | ||
521 | done: | |
522 | free (buf); | |
523 | return FcTrue; | |
524 | ||
525 | bail: | |
526 | free (buf); | |
527 | return FcFalse; | |
528 | } | |
529 | ||
55c8fa4f | 530 | /* Does not check that the cache has the appropriate arch section. */ |
1b7be377 PL |
531 | FcBool |
532 | FcDirCacheValid (const FcChar8 *dir) | |
533 | { | |
534 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); | |
535 | struct stat file_stat, dir_stat; | |
536 | ||
537 | if (stat ((char *) dir, &dir_stat) < 0) | |
538 | { | |
539 | FcStrFree (cache_file); | |
540 | return FcFalse; | |
541 | } | |
542 | if (stat ((char *) cache_file, &file_stat) < 0) | |
543 | { | |
544 | FcStrFree (cache_file); | |
545 | return FcFalse; | |
546 | } | |
6bf23804 | 547 | |
55c8fa4f PL |
548 | FcStrFree (cache_file); |
549 | /* | |
550 | * If the directory has been modified more recently than | |
551 | * the cache file, the cache is not valid | |
552 | */ | |
553 | if (dir_stat.st_mtime - file_stat.st_mtime > 0) | |
554 | return FcFalse; | |
555 | return FcTrue; | |
556 | } | |
557 | ||
558 | /* Assumes that the cache file in 'dir' exists. | |
559 | * Checks that the cache has the appropriate arch section. */ | |
560 | FcBool | |
561 | FcDirCacheHasCurrentArch (const FcChar8 *dir) | |
562 | { | |
563 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); | |
564 | int fd; | |
565 | off_t current_arch_start; | |
566 | char *current_arch_machine_name; | |
567 | ||
6bf23804 | 568 | current_arch_machine_name = FcCacheMachineSignature(); |
55c8fa4f | 569 | fd = open ((char *)cache_file, O_RDONLY); |
6bf23804 PL |
570 | if (fd == -1) |
571 | return FcFalse; | |
572 | ||
573 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); | |
574 | close (fd); | |
575 | ||
576 | if (current_arch_start < 0) | |
577 | return FcFalse; | |
55c8fa4f PL |
578 | |
579 | return FcTrue; | |
580 | } | |
6bf23804 | 581 | |
55c8fa4f PL |
582 | FcBool |
583 | FcDirCacheUnlink (const FcChar8 *dir) | |
584 | { | |
585 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); | |
15d7bd0a | 586 | struct stat cache_stat; |
55c8fa4f | 587 | |
15d7bd0a PL |
588 | if (stat ((char *) cache_file, &cache_stat) == 0 && |
589 | unlink ((char *)cache_file) != 0) | |
1178b569 PL |
590 | { |
591 | FcStrFree (cache_file); | |
1b7be377 | 592 | return FcFalse; |
1178b569 | 593 | } |
55c8fa4f PL |
594 | |
595 | FcStrFree (cache_file); | |
1b7be377 PL |
596 | return FcTrue; |
597 | } | |
598 | ||
4262e0b3 | 599 | static int |
1b7be377 PL |
600 | FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache, |
601 | FcStrList *list, FcFontSet * set) | |
4262e0b3 | 602 | { |
4262e0b3 PL |
603 | int ret = 0; |
604 | FcChar8 *dir; | |
605 | FcChar8 *file, *base; | |
606 | FcStrSet *subdirs; | |
607 | FcStrList *sublist; | |
608 | struct stat statb; | |
609 | ||
610 | /* | |
03a212e5 | 611 | * Read in the results from 'list'. |
4262e0b3 PL |
612 | */ |
613 | while ((dir = FcStrListNext (list))) | |
614 | { | |
615 | /* freed below */ | |
616 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); | |
617 | if (!file) | |
618 | return FcFalse; | |
619 | ||
620 | strcpy ((char *) file, (char *) dir); | |
621 | strcat ((char *) file, "/"); | |
622 | base = file + strlen ((char *) file); | |
623 | ||
624 | subdirs = FcStrSetCreate (); | |
625 | if (!subdirs) | |
626 | { | |
627 | fprintf (stderr, "Can't create directory set\n"); | |
628 | ret++; | |
629 | free (file); | |
630 | continue; | |
631 | } | |
632 | ||
633 | if (access ((char *) dir, X_OK) < 0) | |
634 | { | |
635 | switch (errno) { | |
636 | case ENOENT: | |
637 | case ENOTDIR: | |
638 | case EACCES: | |
639 | break; | |
640 | default: | |
641 | fprintf (stderr, "\"%s\": ", dir); | |
642 | perror (""); | |
643 | ret++; | |
644 | } | |
645 | FcStrSetDestroy (subdirs); | |
646 | free (file); | |
647 | continue; | |
648 | } | |
649 | if (stat ((char *) dir, &statb) == -1) | |
650 | { | |
651 | fprintf (stderr, "\"%s\": ", dir); | |
652 | perror (""); | |
653 | FcStrSetDestroy (subdirs); | |
654 | ret++; | |
655 | free (file); | |
656 | continue; | |
657 | } | |
658 | if (!S_ISDIR (statb.st_mode)) | |
659 | { | |
660 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
661 | FcStrSetDestroy (subdirs); | |
662 | free (file); | |
663 | continue; | |
664 | } | |
2304e38f | 665 | if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir)) |
4262e0b3 | 666 | { |
1b7be377 | 667 | if (FcDebug () & FC_DBG_FONTSET) |
03a212e5 | 668 | printf ("cache scan dir %s\n", dir); |
2304e38f | 669 | |
1b7be377 PL |
670 | FcDirScanConfig (set, subdirs, cache, |
671 | config->blanks, dir, FcFalse, config); | |
4262e0b3 PL |
672 | } |
673 | sublist = FcStrListCreate (subdirs); | |
674 | FcStrSetDestroy (subdirs); | |
675 | if (!sublist) | |
676 | { | |
677 | fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); | |
678 | ret++; | |
679 | free (file); | |
680 | continue; | |
681 | } | |
1b7be377 | 682 | ret += FcCacheReadDirs (config, cache, sublist, set); |
4262e0b3 PL |
683 | free (file); |
684 | } | |
685 | FcStrListDone (list); | |
686 | return ret; | |
687 | } | |
688 | ||
689 | FcFontSet * | |
1b7be377 | 690 | FcCacheRead (FcConfig *config, FcGlobalCache * cache) |
4262e0b3 PL |
691 | { |
692 | FcFontSet * s = FcFontSetCreate(); | |
693 | if (!s) | |
694 | return 0; | |
695 | ||
1b7be377 | 696 | if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s)) |
4262e0b3 PL |
697 | goto bail; |
698 | ||
699 | return s; | |
700 | ||
701 | bail: | |
702 | FcFontSetDestroy (s); | |
703 | return 0; | |
704 | } | |
705 | ||
212c9f43 | 706 | /* read serialized state from the cache file */ |
2eb84374 | 707 | FcBool |
2304e38f | 708 | FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir) |
212c9f43 | 709 | { |
8245771d | 710 | char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); |
4262e0b3 | 711 | int fd; |
212c9f43 | 712 | char * current_arch_machine_name; |
2dbe7597 | 713 | char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; |
212c9f43 | 714 | off_t current_arch_start = 0; |
8245771d | 715 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; |
212c9f43 | 716 | |
4262e0b3 PL |
717 | if (!cache_file) |
718 | goto bail; | |
212c9f43 | 719 | |
eb0cf671 | 720 | current_arch_machine_name = FcCacheMachineSignature(); |
4262e0b3 | 721 | fd = open(cache_file, O_RDONLY); |
212c9f43 | 722 | if (fd == -1) |
2dbe7597 | 723 | goto bail; |
212c9f43 | 724 | |
212c9f43 PL |
725 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); |
726 | if (current_arch_start < 0) | |
4262e0b3 | 727 | goto bail1; |
212c9f43 PL |
728 | |
729 | lseek (fd, current_arch_start, SEEK_SET); | |
eb0cf671 | 730 | if (FcCacheReadString (fd, candidate_arch_machine_name, |
212c9f43 | 731 | sizeof (candidate_arch_machine_name)) == 0) |
4262e0b3 | 732 | goto bail1; |
2304e38f PL |
733 | |
734 | while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0) | |
8245771d | 735 | FcStrSetAdd (dirs, (FcChar8 *)subdirName); |
2304e38f | 736 | |
eb0cf671 PL |
737 | if (!FcDirCacheConsume (fd, set)) |
738 | goto bail1; | |
739 | ||
740 | close(fd); | |
741 | free (cache_file); | |
742 | return FcTrue; | |
743 | ||
744 | bail1: | |
2304e38f | 745 | close (fd); |
eb0cf671 PL |
746 | bail: |
747 | free (cache_file); | |
748 | return FcFalse; | |
749 | } | |
750 | ||
751 | static FcBool | |
752 | FcDirCacheConsume (int fd, FcFontSet *set) | |
753 | { | |
754 | FcCache metadata; | |
755 | void * current_dir_block; | |
fd77c154 | 756 | off_t pos; |
212c9f43 | 757 | |
212c9f43 PL |
758 | read(fd, &metadata, sizeof(FcCache)); |
759 | if (metadata.magic != FC_CACHE_MAGIC) | |
eb0cf671 | 760 | return FcFalse; |
212c9f43 | 761 | |
2dbe7597 | 762 | if (!metadata.count) |
eb0cf671 | 763 | return FcTrue; |
2dbe7597 | 764 | |
fd77c154 | 765 | pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)); |
2dbe7597 PL |
766 | current_dir_block = mmap (0, metadata.count, |
767 | PROT_READ, MAP_SHARED, fd, pos); | |
768 | if (current_dir_block == MAP_FAILED) | |
eb0cf671 | 769 | return FcFalse; |
2dbe7597 PL |
770 | |
771 | if (!FcFontSetUnserialize (metadata, set, current_dir_block)) | |
eb0cf671 PL |
772 | return FcFalse; |
773 | ||
212c9f43 | 774 | return FcTrue; |
eb0cf671 PL |
775 | } |
776 | ||
777 | static void * | |
778 | FcDirCacheProduce (FcFontSet *set, FcCache *metadata) | |
779 | { | |
780 | void * current_dir_block, * final_dir_block; | |
8245771d | 781 | static unsigned int rand_state = 0; |
eb0cf671 PL |
782 | int bank; |
783 | ||
784 | if (!rand_state) | |
785 | rand_state = time(0L); | |
786 | bank = rand_r(&rand_state); | |
787 | ||
788 | while (FcCacheHaveBank(bank)) | |
789 | bank = rand_r(&rand_state); | |
790 | ||
791 | memset (metadata, 0, sizeof(FcCache)); | |
792 | FcFontSetNewBank(); | |
793 | metadata->count = FcFontSetNeededBytes (set); | |
794 | metadata->magic = FC_CACHE_MAGIC; | |
795 | metadata->bank = bank; | |
796 | ||
797 | if (!metadata->count) /* not a failure, no fonts to write */ | |
798 | return 0; | |
799 | ||
800 | current_dir_block = malloc (metadata->count); | |
801 | if (!current_dir_block) | |
802 | goto bail; | |
803 | final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block); | |
804 | ||
805 | if ((char *)current_dir_block + metadata->count != final_dir_block) | |
806 | goto bail; | |
807 | ||
808 | if (!FcFontSetSerialize (bank, set)) | |
809 | goto bail; | |
810 | ||
811 | return current_dir_block; | |
212c9f43 | 812 | |
4262e0b3 | 813 | bail: |
eb0cf671 PL |
814 | free (current_dir_block); |
815 | return 0; | |
212c9f43 PL |
816 | } |
817 | ||
818 | /* write serialized state to the cache file */ | |
819 | FcBool | |
2304e38f | 820 | FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir) |
212c9f43 | 821 | { |
4262e0b3 | 822 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); |
1d879de2 PL |
823 | int fd, fd_orig, i, dirs_count; |
824 | FcAtomic *atomic; | |
825 | FcCache metadata; | |
826 | off_t current_arch_start = 0, truncate_to; | |
8245771d | 827 | |
1d879de2 PL |
828 | char *current_arch_machine_name, * header; |
829 | void *current_dir_block; | |
212c9f43 | 830 | |
4262e0b3 PL |
831 | if (!cache_file) |
832 | goto bail; | |
833 | ||
eb0cf671 | 834 | current_dir_block = FcDirCacheProduce (set, &metadata); |
212c9f43 | 835 | |
a9698bed | 836 | if (metadata.count && !current_dir_block) |
1d879de2 | 837 | goto bail0; |
212c9f43 | 838 | |
4262e0b3 PL |
839 | if (FcDebug () & FC_DBG_CACHE) |
840 | printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file); | |
841 | ||
1d879de2 PL |
842 | atomic = FcAtomicCreate (cache_file); |
843 | if (!atomic) | |
eb0cf671 | 844 | goto bail0; |
212c9f43 | 845 | |
1d879de2 PL |
846 | if (!FcAtomicLock (atomic)) |
847 | goto bail1; | |
848 | ||
849 | fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666); | |
850 | ||
851 | fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666); | |
852 | if (fd == -1) | |
853 | goto bail2; | |
854 | ||
eb0cf671 | 855 | current_arch_machine_name = FcCacheMachineSignature (); |
1d879de2 PL |
856 | current_arch_start = 0; |
857 | ||
858 | if (fd_orig != -1) | |
859 | current_arch_start = | |
860 | FcCacheSkipToArch(fd_orig, current_arch_machine_name); | |
861 | ||
212c9f43 | 862 | if (current_arch_start < 0) |
649cc361 | 863 | current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END)); |
212c9f43 | 864 | |
1d879de2 PL |
865 | if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start)) |
866 | goto bail3; | |
867 | ||
868 | if (fd_orig != -1) | |
869 | close (fd_orig); | |
212c9f43 PL |
870 | |
871 | current_arch_start = lseek(fd, 0, SEEK_CUR); | |
872 | if (ftruncate (fd, current_arch_start) == -1) | |
1d879de2 | 873 | goto bail3; |
212c9f43 | 874 | |
6aee8c6f PL |
875 | /* allocate space for subdir names in this block */ |
876 | dirs_count = 0; | |
877 | for (i = 0; i < dirs->size; i++) | |
878 | dirs_count += strlen((char *)dirs->strs[i]) + 1; | |
879 | dirs_count ++; | |
880 | ||
212c9f43 | 881 | /* now write the address of the next offset */ |
6aee8c6f | 882 | truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start; |
2dbe7597 | 883 | header = malloc (10 + strlen (current_arch_machine_name)); |
eb0cf671 | 884 | if (!header) |
1d879de2 | 885 | goto bail3; |
2dbe7597 PL |
886 | sprintf (header, "%8x ", (int)truncate_to); |
887 | strcat (header, current_arch_machine_name); | |
eb0cf671 | 888 | if (!FcCacheWriteString (fd, header)) |
1d879de2 | 889 | goto bail4; |
212c9f43 | 890 | |
2304e38f | 891 | for (i = 0; i < dirs->size; i++) |
8245771d | 892 | FcCacheWriteString (fd, (char *)dirs->strs[i]); |
2304e38f PL |
893 | FcCacheWriteString (fd, ""); |
894 | ||
4262e0b3 | 895 | write (fd, &metadata, sizeof(FcCache)); |
a9698bed PL |
896 | if (metadata.count) |
897 | { | |
898 | lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET); | |
899 | write (fd, current_dir_block, metadata.count); | |
900 | free (current_dir_block); | |
901 | } | |
4262e0b3 | 902 | |
2dbe7597 | 903 | /* this actually serves to pad out the cache file, if needed */ |
212c9f43 | 904 | if (ftruncate (fd, current_arch_start + truncate_to) == -1) |
1d879de2 | 905 | goto bail4; |
212c9f43 PL |
906 | |
907 | close(fd); | |
1d879de2 PL |
908 | if (!FcAtomicReplaceOrig(atomic)) |
909 | goto bail4; | |
910 | FcAtomicUnlock (atomic); | |
911 | FcAtomicDestroy (atomic); | |
212c9f43 PL |
912 | return FcTrue; |
913 | ||
1d879de2 | 914 | bail4: |
0230c9f8 | 915 | free (header); |
1d879de2 PL |
916 | bail3: |
917 | close (fd); | |
918 | bail2: | |
919 | FcAtomicUnlock (atomic); | |
920 | bail1: | |
921 | FcAtomicDestroy (atomic); | |
eb0cf671 | 922 | bail0: |
8245771d | 923 | unlink ((char *)cache_file); |
4262e0b3 | 924 | free (cache_file); |
1d879de2 PL |
925 | if (current_dir_block) |
926 | free (current_dir_block); | |
927 | bail: | |
212c9f43 PL |
928 | return FcFalse; |
929 | } | |
930 | ||
2dbe7597 | 931 | static char * |
eb0cf671 | 932 | FcCacheMachineSignature () |
2dbe7597 PL |
933 | { |
934 | static char buf[MACHINE_SIGNATURE_SIZE]; | |
935 | int magic = ENDIAN_TEST; | |
936 | char * m = (char *)&magic; | |
937 | ||
938 | sprintf (buf, "%2x%2x%2x%2x " | |
939 | "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x " | |
940 | "%4x %4x %4x %4x %4x %4x %4x\n", | |
941 | m[0], m[1], m[2], m[3], | |
cd310911 PL |
942 | (unsigned int)sizeof (char), |
943 | (unsigned int)sizeof (char *), | |
944 | (unsigned int)sizeof (int), | |
945 | (unsigned int)sizeof (FcPattern), | |
946 | (unsigned int)sizeof (FcPatternEltPtr), | |
947 | (unsigned int)sizeof (struct _FcPatternElt *), | |
948 | (unsigned int)sizeof (FcPatternElt), | |
949 | (unsigned int)sizeof (FcObjectPtr), | |
950 | (unsigned int)sizeof (FcValueListPtr), | |
951 | (unsigned int)sizeof (FcValue), | |
952 | (unsigned int)sizeof (FcValueBinding), | |
953 | (unsigned int)sizeof (struct _FcValueList *), | |
954 | (unsigned int)sizeof (FcCharSet), | |
955 | (unsigned int)sizeof (FcCharLeaf **), | |
956 | (unsigned int)sizeof (FcChar16 *), | |
957 | (unsigned int)sizeof (FcChar16), | |
958 | (unsigned int)sizeof (FcCharLeaf), | |
959 | (unsigned int)sizeof (FcChar32), | |
960 | (unsigned int)sizeof (FcCache)); | |
2dbe7597 PL |
961 | |
962 | return buf; | |
963 | } | |
964 | ||
4262e0b3 | 965 | static int banks_ptr = 0, banks_alloc = 0; |
751932dd | 966 | static int * bankId = 0, * bankIdx = 0; |
4262e0b3 | 967 | |
eb0cf671 | 968 | static FcBool |
4262e0b3 PL |
969 | FcCacheHaveBank (int bank) |
970 | { | |
971 | int i; | |
972 | ||
973 | if (bank < FC_BANK_FIRST) | |
974 | return FcTrue; | |
975 | ||
976 | for (i = 0; i < banks_ptr; i++) | |
977 | if (bankId[i] == bank) | |
978 | return FcTrue; | |
979 | ||
980 | return FcFalse; | |
981 | } | |
982 | ||
983 | int | |
984 | FcCacheBankToIndex (int bank) | |
985 | { | |
751932dd | 986 | int i, j; |
4262e0b3 PL |
987 | |
988 | for (i = 0; i < banks_ptr; i++) | |
751932dd PL |
989 | if (bankId[bankIdx[i]] == bank) |
990 | { | |
991 | int t = bankIdx[i]; | |
992 | ||
993 | for (j = i; j > 0; j--) | |
994 | bankIdx[j] = bankIdx[j-1]; | |
995 | bankIdx[0] = t; | |
996 | return t; | |
997 | } | |
4262e0b3 | 998 | |
2dbe7597 | 999 | if (banks_ptr >= banks_alloc) |
4262e0b3 | 1000 | { |
751932dd | 1001 | int * b, * bidx; |
2dbe7597 | 1002 | b = realloc (bankId, (banks_alloc + 4) * sizeof(int)); |
4262e0b3 PL |
1003 | if (!b) |
1004 | return -1; | |
4262e0b3 | 1005 | bankId = b; |
751932dd PL |
1006 | |
1007 | bidx = realloc (bankIdx, (banks_alloc + 4) * sizeof(int)); | |
1008 | if (!bidx) | |
1009 | return -1; | |
1010 | bankIdx = bidx; | |
1011 | ||
4262e0b3 PL |
1012 | banks_alloc += 4; |
1013 | } | |
1014 | ||
1015 | i = banks_ptr++; | |
1016 | bankId[i] = bank; | |
751932dd | 1017 | bankIdx[i] = i; |
4262e0b3 PL |
1018 | return i; |
1019 | } |