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