]>
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 | ||
f045376c | 26 | #include "fcint.h" |
cf65c055 | 27 | #include "../fc-arch/fcarch.h" |
00f059e9 | 28 | #include <stdio.h> |
212c9f43 | 29 | #include <fcntl.h> |
4262e0b3 | 30 | #include <dirent.h> |
ea44e218 | 31 | #include <string.h> |
fd77c154 | 32 | #include <sys/types.h> |
d6217cc6 | 33 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
93f67dfc | 34 | # include <unistd.h> |
d6217cc6 | 35 | # include <sys/mman.h> |
93f67dfc PL |
36 | #elif defined(_WIN32) |
37 | # include <windows.h> | |
d6217cc6 | 38 | #endif |
24330d27 | 39 | |
2dbe7597 | 40 | #define ENDIAN_TEST 0x12345678 |
f2fb985c | 41 | #define MACHINE_SIGNATURE_SIZE (9 + 5*20 + 1) |
d6217cc6 PL |
42 | /* for when we don't have sysconf: */ |
43 | #define FC_HARDCODED_PAGESIZE 8192 | |
2dbe7597 | 44 | |
879af706 PL |
45 | #ifndef O_BINARY |
46 | #define O_BINARY 0 | |
47 | #endif | |
48 | ||
00f059e9 | 49 | static FILE * |
7410e40b | 50 | FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, FcChar8 **cache_path); |
ea44e218 | 51 | |
eb0cf671 PL |
52 | static void * |
53 | FcDirCacheProduce (FcFontSet *set, FcCache * metadata); | |
1b7be377 | 54 | |
00f059e9 | 55 | static off_t |
eb0cf671 | 56 | FcCacheNextOffset(off_t w); |
1b7be377 | 57 | |
1b7be377 | 58 | static FcBool |
eb0cf671 | 59 | FcCacheHaveBank (int bank); |
1b7be377 | 60 | |
e77c1718 PL |
61 | static void |
62 | FcCacheAddBankDir (int bank, const char * dir); | |
63 | ||
ea44e218 PL |
64 | struct MD5Context { |
65 | FcChar32 buf[4]; | |
66 | FcChar32 bits[2]; | |
67 | unsigned char in[64]; | |
68 | }; | |
69 | ||
70 | static void MD5Init(struct MD5Context *ctx); | |
71 | static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len); | |
72 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx); | |
73 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]); | |
74 | ||
eb0cf671 | 75 | #define FC_DBG_CACHE_REF 1024 |
1b7be377 | 76 | |
8245771d | 77 | static char * |
00f059e9 | 78 | FcCacheReadString (FILE *file, char *dest, int len) |
24330d27 | 79 | { |
00f059e9 KP |
80 | int c; |
81 | char *d = dest; | |
24330d27 | 82 | |
24330d27 | 83 | if (len == 0) |
03a212e5 | 84 | return 0; |
799157db | 85 | |
00f059e9 KP |
86 | while ((c = getc (file)) != EOF && len > 0) { |
87 | *d++ = c; | |
88 | if (c == '\0') | |
89 | return dest; | |
90 | len--; | |
24330d27 | 91 | } |
ccb3e93b | 92 | return 0; |
24330d27 KP |
93 | } |
94 | ||
95 | static FcBool | |
8245771d | 96 | FcCacheWriteString (int fd, const char *chars) |
327a7fd4 | 97 | { |
212c9f43 | 98 | if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1) |
327a7fd4 | 99 | return FcFalse; |
327a7fd4 KP |
100 | return FcTrue; |
101 | } | |
102 | ||
212c9f43 | 103 | /* |
eb0cf671 PL |
104 | * Find the next presumably-mmapable offset after the supplied file |
105 | * position. | |
212c9f43 | 106 | */ |
00f059e9 | 107 | static off_t |
4262e0b3 | 108 | FcCacheNextOffset(off_t w) |
179c3995 | 109 | { |
7fd7221e | 110 | static long pagesize = -1; |
c001a192 PL |
111 | |
112 | if (w == -1) | |
113 | return w; | |
114 | ||
7fd7221e | 115 | if (pagesize == -1) |
d6217cc6 | 116 | #if defined (HAVE_SYSCONF) |
7fd7221e | 117 | pagesize = sysconf(_SC_PAGESIZE); |
d6217cc6 PL |
118 | #else |
119 | pagesize = FC_HARDCODED_PAGESIZE; | |
120 | #endif | |
7fd7221e | 121 | if (w % pagesize == 0) |
212c9f43 PL |
122 | return w; |
123 | else | |
7fd7221e | 124 | return ((w / pagesize)+1)*pagesize; |
179c3995 KP |
125 | } |
126 | ||
55c8fa4f | 127 | /* Does not check that the cache has the appropriate arch section. */ |
1b7be377 | 128 | FcBool |
7410e40b | 129 | FcDirCacheValid (const FcChar8 *dir, FcConfig *config) |
1b7be377 | 130 | { |
00f059e9 | 131 | FILE *file; |
1b7be377 | 132 | |
00f059e9 | 133 | file = FcDirCacheOpen (config, dir, NULL); |
ea44e218 | 134 | |
af180c40 | 135 | if (file == NULL) |
c7490074 | 136 | return FcFalse; |
00f059e9 | 137 | fclose (file); |
ea44e218 | 138 | |
55c8fa4f PL |
139 | return FcTrue; |
140 | } | |
141 | ||
cf65c055 | 142 | #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX)) |
7410e40b PL |
143 | |
144 | static const char bin2hex[] = { '0', '1', '2', '3', | |
145 | '4', '5', '6', '7', | |
146 | '8', '9', 'a', 'b', | |
147 | 'c', 'd', 'e', 'f' }; | |
148 | ||
149 | static FcChar8 * | |
150 | FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN]) | |
55c8fa4f | 151 | { |
7410e40b PL |
152 | unsigned char hash[16]; |
153 | FcChar8 *hex_hash; | |
154 | int cnt; | |
155 | struct MD5Context ctx; | |
55c8fa4f | 156 | |
7410e40b PL |
157 | MD5Init (&ctx); |
158 | MD5Update (&ctx, (unsigned char *)dir, strlen ((char *) dir)); | |
3bfae75d | 159 | |
7410e40b PL |
160 | MD5Final (hash, &ctx); |
161 | ||
162 | cache_base[0] = '/'; | |
163 | hex_hash = cache_base + 1; | |
164 | for (cnt = 0; cnt < 16; ++cnt) | |
1178b569 | 165 | { |
7410e40b PL |
166 | hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4]; |
167 | hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf]; | |
168 | } | |
169 | hex_hash[2*cnt] = 0; | |
cf65c055 | 170 | strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX); |
6f9fcb51 | 171 | |
7410e40b PL |
172 | return cache_base; |
173 | } | |
3bfae75d | 174 | |
7410e40b PL |
175 | FcBool |
176 | FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config) | |
177 | { | |
7410e40b PL |
178 | FcChar8 *cache_hashed = NULL; |
179 | FcChar8 cache_base[CACHEBASE_LEN]; | |
180 | FcStrList *list; | |
181 | FcChar8 *cache_dir; | |
3bfae75d | 182 | |
7410e40b | 183 | FcDirCacheBasename (dir, cache_base); |
3bfae75d | 184 | |
7410e40b PL |
185 | list = FcStrListCreate (config->cacheDirs); |
186 | if (!list) | |
187 | return FcFalse; | |
188 | ||
189 | while ((cache_dir = FcStrListNext (list))) | |
3bfae75d | 190 | { |
7410e40b PL |
191 | cache_hashed = FcStrPlus (cache_dir, cache_base); |
192 | if (!cache_hashed) | |
193 | break; | |
00f059e9 | 194 | (void) unlink ((char *) cache_hashed); |
1178b569 | 195 | } |
7410e40b PL |
196 | FcStrListDone (list); |
197 | /* return FcFalse if something went wrong */ | |
198 | if (cache_dir) | |
199 | return FcFalse; | |
1b7be377 PL |
200 | return FcTrue; |
201 | } | |
202 | ||
4262e0b3 | 203 | static int |
00f059e9 | 204 | FcCacheReadDirs (FcConfig * config, |
8b413bb6 | 205 | FcStrList *list, FcFontSet * set, FcStrSet *processed_dirs) |
4262e0b3 | 206 | { |
4262e0b3 PL |
207 | int ret = 0; |
208 | FcChar8 *dir; | |
4262e0b3 PL |
209 | FcStrSet *subdirs; |
210 | FcStrList *sublist; | |
4262e0b3 PL |
211 | |
212 | /* | |
03a212e5 | 213 | * Read in the results from 'list'. |
4262e0b3 PL |
214 | */ |
215 | while ((dir = FcStrListNext (list))) | |
216 | { | |
9fad72ab PL |
217 | if (!FcConfigAcceptFilename (config, dir)) |
218 | continue; | |
219 | ||
fff5a5af PL |
220 | /* Skip this directory if already updated |
221 | * to avoid the looped directories via symlinks | |
5c3deb29 | 222 | * Clearly a dir not in fonts.conf shouldn't be globally cached. |
fff5a5af | 223 | */ |
5c3deb29 PL |
224 | |
225 | if (FcStrSetMember (processed_dirs, dir)) | |
226 | continue; | |
227 | if (!FcStrSetAdd (processed_dirs, dir)) | |
228 | continue; | |
4262e0b3 PL |
229 | |
230 | subdirs = FcStrSetCreate (); | |
231 | if (!subdirs) | |
232 | { | |
233 | fprintf (stderr, "Can't create directory set\n"); | |
234 | ret++; | |
4262e0b3 PL |
235 | continue; |
236 | } | |
237 | ||
00f059e9 KP |
238 | FcDirScanConfig (set, subdirs, |
239 | config->blanks, dir, FcFalse, config); | |
240 | ||
4262e0b3 PL |
241 | sublist = FcStrListCreate (subdirs); |
242 | FcStrSetDestroy (subdirs); | |
243 | if (!sublist) | |
244 | { | |
245 | fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); | |
246 | ret++; | |
4262e0b3 PL |
247 | continue; |
248 | } | |
00f059e9 | 249 | ret += FcCacheReadDirs (config, sublist, set, processed_dirs); |
4262e0b3 PL |
250 | } |
251 | FcStrListDone (list); | |
252 | return ret; | |
253 | } | |
254 | ||
255 | FcFontSet * | |
00f059e9 | 256 | FcCacheRead (FcConfig *config) |
4262e0b3 | 257 | { |
8b413bb6 PL |
258 | FcFontSet *s = FcFontSetCreate(); |
259 | FcStrSet *processed_dirs; | |
260 | ||
4262e0b3 PL |
261 | if (!s) |
262 | return 0; | |
263 | ||
8b413bb6 PL |
264 | processed_dirs = FcStrSetCreate(); |
265 | if (!processed_dirs) | |
4262e0b3 PL |
266 | goto bail; |
267 | ||
00f059e9 | 268 | if (FcCacheReadDirs (config, FcConfigGetConfigDirs (config), s, processed_dirs)) |
8b413bb6 PL |
269 | goto bail1; |
270 | ||
271 | FcStrSetDestroy (processed_dirs); | |
4262e0b3 PL |
272 | return s; |
273 | ||
8b413bb6 PL |
274 | bail1: |
275 | FcStrSetDestroy (processed_dirs); | |
4262e0b3 PL |
276 | bail: |
277 | FcFontSetDestroy (s); | |
278 | return 0; | |
279 | } | |
280 | ||
7410e40b PL |
281 | /* Opens the cache file for 'dir' for reading. |
282 | * This searches the list of cache dirs for the relevant cache file, | |
283 | * returning the first one found. | |
284 | */ | |
00f059e9 | 285 | static FILE * |
7410e40b | 286 | FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, FcChar8 **cache_path) |
ea44e218 | 287 | { |
00f059e9 | 288 | FILE *file = NULL; |
7410e40b PL |
289 | FcChar8 *cache_hashed = NULL; |
290 | FcChar8 cache_base[CACHEBASE_LEN]; | |
291 | FcStrList *list; | |
292 | FcChar8 *cache_dir; | |
d2f78684 KP |
293 | struct stat file_stat, dir_stat; |
294 | ||
295 | if (stat ((char *) dir, &dir_stat) < 0) | |
00f059e9 | 296 | return NULL; |
df3efc11 | 297 | |
7410e40b | 298 | FcDirCacheBasename (dir, cache_base); |
ea44e218 | 299 | |
7410e40b PL |
300 | list = FcStrListCreate (config->cacheDirs); |
301 | if (!list) | |
00f059e9 | 302 | return NULL; |
7410e40b PL |
303 | |
304 | while ((cache_dir = FcStrListNext (list))) | |
ea44e218 | 305 | { |
7410e40b PL |
306 | cache_hashed = FcStrPlus (cache_dir, cache_base); |
307 | if (!cache_hashed) | |
d00c3cb5 | 308 | break; |
00f059e9 KP |
309 | file = fopen((char *) cache_hashed, "rb"); |
310 | if (file != NULL) { | |
311 | if (fstat (fileno (file), &file_stat) >= 0 && | |
d2f78684 KP |
312 | dir_stat.st_mtime <= file_stat.st_mtime) |
313 | { | |
314 | break; | |
315 | } | |
00f059e9 KP |
316 | fclose (file); |
317 | file = NULL; | |
d2f78684 | 318 | } |
7410e40b | 319 | FcStrFree (cache_hashed); |
d2f78684 | 320 | cache_hashed = NULL; |
7410e40b PL |
321 | } |
322 | FcStrListDone (list); | |
df3efc11 | 323 | |
00f059e9 KP |
324 | if (file == NULL) |
325 | return NULL; | |
7410e40b PL |
326 | |
327 | if (cache_path) | |
328 | *cache_path = cache_hashed; | |
329 | else | |
330 | FcStrFree (cache_hashed); | |
ea44e218 | 331 | |
00f059e9 | 332 | return file; |
ea44e218 PL |
333 | } |
334 | ||
212c9f43 | 335 | /* read serialized state from the cache file */ |
2eb84374 | 336 | FcBool |
af180c40 KP |
337 | FcDirCacheConsume (FILE *file, FcFontSet *set, FcStrSet *dirs, |
338 | const FcChar8 *dir, char *dirname) | |
212c9f43 | 339 | { |
00f059e9 KP |
340 | FcCache metadata; |
341 | void *current_dir_block; | |
342 | char subdir_name[FC_MAX_FILE_LEN + 1 + 12 + 1]; | |
af180c40 | 343 | int i; |
2304e38f | 344 | |
00f059e9 | 345 | if (fread(&metadata, sizeof(FcCache), 1, file) != 1) |
af180c40 | 346 | goto bail; |
212c9f43 | 347 | if (metadata.magic != FC_CACHE_MAGIC) |
af180c40 | 348 | goto bail; |
c001a192 | 349 | |
af180c40 KP |
350 | /* skip directory name; it's just for fc-cat */ |
351 | if (!FcCacheReadString (file, subdir_name, sizeof (subdir_name))) | |
352 | goto bail; | |
353 | ||
354 | if (dirname) | |
355 | strcpy (dirname, subdir_name); | |
356 | ||
357 | for (i = 0; i < metadata.subdirs; i++) { | |
358 | if (!FcCacheReadString (file, subdir_name, sizeof (subdir_name))) | |
359 | goto bail; | |
360 | FcStrSetAdd (dirs, (FcChar8 *)subdir_name); | |
361 | } | |
c001a192 | 362 | |
00f059e9 KP |
363 | if (metadata.count) |
364 | { | |
365 | int fd = fileno (file); | |
d6217cc6 | 366 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
00f059e9 KP |
367 | current_dir_block = mmap (0, metadata.count, |
368 | PROT_READ, MAP_SHARED, fd, metadata.pos); | |
369 | if (current_dir_block == MAP_FAILED) | |
af180c40 | 370 | goto bail; |
93f67dfc PL |
371 | #elif defined(_WIN32) |
372 | { | |
00f059e9 | 373 | HANDLE hFileMap; |
93f67dfc | 374 | |
00f059e9 KP |
375 | hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL); |
376 | if (hFileMap == NULL) | |
af180c40 | 377 | goto bail; |
93f67dfc | 378 | |
00f059e9 KP |
379 | current_dir_block = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, metadata.pos, metadata.count); |
380 | if (current_dir_block == NULL) | |
381 | { | |
382 | CloseHandle (hFileMap); | |
af180c40 | 383 | goto bail; |
00f059e9 | 384 | } |
93f67dfc | 385 | } |
d6217cc6 | 386 | #else |
00f059e9 | 387 | if (lseek (fd, metatdata.pos, SEEK_SET) == -1) |
af180c40 | 388 | goto bail; |
d6217cc6 | 389 | |
00f059e9 KP |
390 | current_dir_block = malloc (metadata.count); |
391 | if (!current_dir_block) | |
af180c40 | 392 | goto bail; |
eb0cf671 | 393 | |
00f059e9 KP |
394 | /* could also use CreateMappedViewOfFile under MinGW... */ |
395 | if (read (fd, current_dir_block, metadata.count) != metadata.count) | |
396 | { | |
397 | free (current_dir_block); | |
af180c40 | 398 | goto bail; |
00f059e9 KP |
399 | } |
400 | #endif | |
401 | FcCacheAddBankDir (metadata.bank, (char *) dir); | |
402 | if (!FcFontSetUnserialize (&metadata, set, current_dir_block)) | |
af180c40 | 403 | goto bail; |
00f059e9 | 404 | } |
af180c40 KP |
405 | |
406 | return FcTrue; | |
407 | ||
408 | bail: | |
409 | return FcFalse; | |
410 | } | |
411 | ||
412 | FcBool | |
413 | FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, | |
414 | const FcChar8 *dir, FcConfig *config) | |
415 | { | |
416 | FILE *file; | |
417 | ||
418 | file = FcDirCacheOpen (config, dir, NULL); | |
419 | if (file == NULL) | |
420 | goto bail; | |
421 | ||
422 | if (!FcDirCacheConsume (file, set, dirs, dir, NULL)) | |
423 | goto bail1; | |
424 | ||
cd9bca69 PL |
425 | if (config) |
426 | FcConfigAddFontDir (config, (FcChar8 *)dir); | |
af180c40 KP |
427 | |
428 | fclose (file); | |
212c9f43 | 429 | return FcTrue; |
00f059e9 | 430 | |
af180c40 | 431 | bail1: |
00f059e9 | 432 | fclose (file); |
af180c40 | 433 | bail: |
d6217cc6 | 434 | return FcFalse; |
eb0cf671 | 435 | } |
af180c40 | 436 | |
eb0cf671 PL |
437 | |
438 | static void * | |
439 | FcDirCacheProduce (FcFontSet *set, FcCache *metadata) | |
440 | { | |
441 | void * current_dir_block, * final_dir_block; | |
8245771d | 442 | static unsigned int rand_state = 0; |
8e351527 | 443 | int bank, needed_bytes_no_align; |
eb0cf671 | 444 | |
d6217cc6 | 445 | #if defined (HAVE_RAND_R) |
eb0cf671 PL |
446 | if (!rand_state) |
447 | rand_state = time(0L); | |
448 | bank = rand_r(&rand_state); | |
449 | ||
450 | while (FcCacheHaveBank(bank)) | |
451 | bank = rand_r(&rand_state); | |
d6217cc6 PL |
452 | #else |
453 | if (!rand_state) | |
454 | { | |
455 | rand_state = 1; | |
456 | srand (time (0L)); | |
457 | } | |
458 | bank = rand(); | |
459 | ||
460 | while (FcCacheHaveBank(bank)) | |
461 | bank = rand(); | |
462 | #endif | |
eb0cf671 PL |
463 | |
464 | memset (metadata, 0, sizeof(FcCache)); | |
465 | FcFontSetNewBank(); | |
8e351527 PL |
466 | needed_bytes_no_align = FcFontSetNeededBytes (set); |
467 | metadata->count = needed_bytes_no_align + | |
7fd7221e | 468 | FcFontSetNeededBytesAlign (); |
eb0cf671 PL |
469 | metadata->magic = FC_CACHE_MAGIC; |
470 | metadata->bank = bank; | |
471 | ||
8e351527 PL |
472 | if (!needed_bytes_no_align) /* not a failure, no fonts to write */ |
473 | { | |
474 | /* no, you don't really need to write any bytes at all. */ | |
475 | metadata->count = 0; | |
eb0cf671 | 476 | return 0; |
8e351527 | 477 | } |
eb0cf671 PL |
478 | |
479 | current_dir_block = malloc (metadata->count); | |
480 | if (!current_dir_block) | |
481 | goto bail; | |
bb6b1993 | 482 | /* shut up valgrind */ |
8e351527 | 483 | memset (current_dir_block, 0, metadata->count); |
eb0cf671 PL |
484 | final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block); |
485 | ||
8e351527 | 486 | if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block) |
eb0cf671 PL |
487 | goto bail; |
488 | ||
489 | if (!FcFontSetSerialize (bank, set)) | |
490 | goto bail; | |
491 | ||
492 | return current_dir_block; | |
212c9f43 | 493 | |
4262e0b3 | 494 | bail: |
eb0cf671 PL |
495 | free (current_dir_block); |
496 | return 0; | |
212c9f43 PL |
497 | } |
498 | ||
7410e40b PL |
499 | static FcBool |
500 | FcMakeDirectory (const FcChar8 *dir) | |
501 | { | |
502 | FcChar8 *parent; | |
503 | FcBool ret; | |
504 | ||
505 | if (strlen ((char *) dir) == 0) | |
506 | return FcFalse; | |
507 | ||
508 | parent = FcStrDirname (dir); | |
509 | if (!parent) | |
510 | return FcFalse; | |
511 | if (access ((char *) parent, W_OK|X_OK) == 0) | |
512 | ret = mkdir ((char *) dir, 0777) == 0; | |
513 | else if (access ((char *) parent, F_OK) == -1) | |
514 | ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0777) == 0); | |
515 | else | |
516 | ret = FcFalse; | |
517 | FcStrFree (parent); | |
518 | return ret; | |
519 | } | |
520 | ||
212c9f43 PL |
521 | /* write serialized state to the cache file */ |
522 | FcBool | |
7410e40b | 523 | FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *config) |
212c9f43 | 524 | { |
7410e40b PL |
525 | FcChar8 cache_base[CACHEBASE_LEN]; |
526 | FcChar8 *cache_hashed; | |
00f059e9 | 527 | int fd, i; |
1d879de2 PL |
528 | FcAtomic *atomic; |
529 | FcCache metadata; | |
ea44e218 | 530 | void *current_dir_block = 0; |
00f059e9 | 531 | FcStrList *list; |
d2f78684 KP |
532 | FcChar8 *cache_dir = NULL; |
533 | FcChar8 *test_dir; | |
00f059e9 | 534 | off_t header_len; |
212c9f43 | 535 | |
7410e40b | 536 | /* |
d2f78684 | 537 | * Write it to the first directory in the list which is writable |
7410e40b | 538 | */ |
d2f78684 KP |
539 | |
540 | list = FcStrListCreate (config->cacheDirs); | |
541 | if (!list) | |
542 | return FcFalse; | |
543 | while ((test_dir = FcStrListNext (list))) { | |
544 | if (access ((char *) test_dir, W_OK|X_OK) == 0) | |
545 | { | |
546 | cache_dir = test_dir; | |
547 | break; | |
548 | } | |
549 | else | |
550 | { | |
7410e40b PL |
551 | /* |
552 | * If the directory doesn't exist, try to create it | |
553 | */ | |
d2f78684 KP |
554 | if (access ((char *) test_dir, F_OK) == -1) { |
555 | if (FcMakeDirectory (test_dir)) | |
556 | { | |
557 | cache_dir = test_dir; | |
7410e40b | 558 | break; |
d2f78684 | 559 | } |
7410e40b PL |
560 | } |
561 | } | |
7410e40b | 562 | } |
d2f78684 KP |
563 | FcStrListDone (list); |
564 | if (!cache_dir) | |
565 | return FcFalse; | |
566 | FcDirCacheBasename (dir, cache_base); | |
567 | cache_hashed = FcStrPlus (cache_dir, cache_base); | |
568 | if (!cache_hashed) | |
569 | return FcFalse; | |
ea44e218 | 570 | |
eb0cf671 | 571 | current_dir_block = FcDirCacheProduce (set, &metadata); |
212c9f43 | 572 | |
a9698bed | 573 | if (metadata.count && !current_dir_block) |
ea44e218 | 574 | goto bail1; |
212c9f43 | 575 | |
4262e0b3 | 576 | if (FcDebug () & FC_DBG_CACHE) |
c1c3ba06 KP |
577 | printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n", |
578 | dir, cache_hashed); | |
4262e0b3 | 579 | |
3bfae75d | 580 | atomic = FcAtomicCreate ((FcChar8 *)cache_hashed); |
1d879de2 | 581 | if (!atomic) |
ea44e218 | 582 | goto bail1; |
212c9f43 | 583 | |
1d879de2 | 584 | if (!FcAtomicLock (atomic)) |
c1c3ba06 | 585 | goto bail2; |
ec760b17 | 586 | |
879af706 | 587 | fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666); |
1d879de2 | 588 | if (fd == -1) |
ec760b17 | 589 | goto bail3; |
00f059e9 KP |
590 | |
591 | /* | |
592 | * Compute file header length -- the FcCache followed by the subdir names | |
593 | */ | |
594 | header_len = sizeof (FcCache); | |
af180c40 | 595 | header_len += strlen ((char *) dir) + 1; |
2304e38f | 596 | for (i = 0; i < dirs->size; i++) |
00f059e9 KP |
597 | header_len += strlen ((char *)dirs->strs[i]) + 1; |
598 | ||
599 | metadata.pos = FcCacheNextOffset (lseek (fd, 0, SEEK_CUR) + header_len); | |
600 | metadata.subdirs = dirs->size; | |
601 | ||
602 | /* | |
603 | * Write out the header | |
604 | */ | |
fff5a5af PL |
605 | if (write (fd, &metadata, sizeof(FcCache)) != sizeof(FcCache)) |
606 | { | |
68355f38 PL |
607 | perror("write metadata"); |
608 | goto bail5; | |
609 | } | |
00f059e9 | 610 | |
af180c40 KP |
611 | FcCacheWriteString (fd, (char *) dir); |
612 | ||
00f059e9 KP |
613 | for (i = 0; i < dirs->size; i++) |
614 | FcCacheWriteString (fd, (char *)dirs->strs[i]); | |
615 | ||
a9698bed PL |
616 | if (metadata.count) |
617 | { | |
00f059e9 | 618 | if (lseek (fd, metadata.pos, SEEK_SET) != metadata.pos) |
68355f38 PL |
619 | perror("lseek"); |
620 | else if (write (fd, current_dir_block, metadata.count) != | |
621 | metadata.count) | |
622 | perror("write current_dir_block"); | |
a9698bed | 623 | free (current_dir_block); |
f2fb985c | 624 | current_dir_block = 0; |
a9698bed | 625 | } |
4262e0b3 | 626 | |
212c9f43 | 627 | close(fd); |
1d879de2 | 628 | if (!FcAtomicReplaceOrig(atomic)) |
ae2aafe6 | 629 | goto bail3; |
3bfae75d | 630 | FcStrFree ((FcChar8 *)cache_hashed); |
1d879de2 PL |
631 | FcAtomicUnlock (atomic); |
632 | FcAtomicDestroy (atomic); | |
212c9f43 PL |
633 | return FcTrue; |
634 | ||
ea44e218 | 635 | bail5: |
1d879de2 | 636 | close (fd); |
ea44e218 | 637 | bail3: |
1d879de2 | 638 | FcAtomicUnlock (atomic); |
ea44e218 | 639 | bail2: |
1d879de2 | 640 | FcAtomicDestroy (atomic); |
ea44e218 | 641 | bail1: |
3bfae75d | 642 | FcStrFree ((FcChar8 *)cache_hashed); |
1d879de2 PL |
643 | if (current_dir_block) |
644 | free (current_dir_block); | |
212c9f43 PL |
645 | return FcFalse; |
646 | } | |
647 | ||
4262e0b3 | 648 | static int banks_ptr = 0, banks_alloc = 0; |
b8948e85 | 649 | int * _fcBankId = 0, * _fcBankIdx = 0; |
e77c1718 | 650 | static const char ** bankDirs = 0; |
4262e0b3 | 651 | |
eb0cf671 | 652 | static FcBool |
4262e0b3 PL |
653 | FcCacheHaveBank (int bank) |
654 | { | |
655 | int i; | |
656 | ||
657 | if (bank < FC_BANK_FIRST) | |
658 | return FcTrue; | |
659 | ||
660 | for (i = 0; i < banks_ptr; i++) | |
b8948e85 | 661 | if (_fcBankId[i] == bank) |
4262e0b3 PL |
662 | return FcTrue; |
663 | ||
664 | return FcFalse; | |
665 | } | |
666 | ||
667 | int | |
b8948e85 | 668 | FcCacheBankToIndexMTF (int bank) |
4262e0b3 | 669 | { |
751932dd | 670 | int i, j; |
4262e0b3 PL |
671 | |
672 | for (i = 0; i < banks_ptr; i++) | |
b8948e85 | 673 | if (_fcBankId[_fcBankIdx[i]] == bank) |
751932dd | 674 | { |
b8948e85 | 675 | int t = _fcBankIdx[i]; |
751932dd PL |
676 | |
677 | for (j = i; j > 0; j--) | |
b8948e85 PL |
678 | _fcBankIdx[j] = _fcBankIdx[j-1]; |
679 | _fcBankIdx[0] = t; | |
751932dd PL |
680 | return t; |
681 | } | |
4262e0b3 | 682 | |
2dbe7597 | 683 | if (banks_ptr >= banks_alloc) |
4262e0b3 | 684 | { |
751932dd | 685 | int * b, * bidx; |
e77c1718 PL |
686 | const char ** bds; |
687 | ||
b8948e85 | 688 | b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int)); |
4262e0b3 PL |
689 | if (!b) |
690 | return -1; | |
b8948e85 | 691 | _fcBankId = b; |
751932dd | 692 | |
b8948e85 | 693 | bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int)); |
751932dd PL |
694 | if (!bidx) |
695 | return -1; | |
b8948e85 | 696 | _fcBankIdx = bidx; |
751932dd | 697 | |
e77c1718 PL |
698 | bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *)); |
699 | if (!bds) | |
700 | return -1; | |
701 | bankDirs = bds; | |
702 | ||
4262e0b3 PL |
703 | banks_alloc += 4; |
704 | } | |
705 | ||
706 | i = banks_ptr++; | |
b8948e85 PL |
707 | _fcBankId[i] = bank; |
708 | _fcBankIdx[i] = i; | |
4262e0b3 PL |
709 | return i; |
710 | } | |
e77c1718 PL |
711 | |
712 | static void | |
713 | FcCacheAddBankDir (int bank, const char * dir) | |
714 | { | |
b8948e85 | 715 | int bi = FcCacheBankToIndexMTF (bank); |
e77c1718 PL |
716 | |
717 | if (bi < 0) | |
718 | return; | |
719 | ||
720 | bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir); | |
721 | } | |
722 | ||
723 | const char * | |
724 | FcCacheFindBankDir (int bank) | |
725 | { | |
726 | int bi = FcCacheBankToIndex (bank); | |
727 | return bankDirs[bi]; | |
728 | } | |
729 | ||
ea44e218 PL |
730 | /* |
731 | * This code implements the MD5 message-digest algorithm. | |
732 | * The algorithm is due to Ron Rivest. This code was | |
733 | * written by Colin Plumb in 1993, no copyright is claimed. | |
734 | * This code is in the public domain; do with it what you wish. | |
735 | * | |
736 | * Equivalent code is available from RSA Data Security, Inc. | |
737 | * This code has been tested against that, and is equivalent, | |
738 | * except that you don't need to include two pages of legalese | |
739 | * with every copy. | |
740 | * | |
741 | * To compute the message digest of a chunk of bytes, declare an | |
742 | * MD5Context structure, pass it to MD5Init, call MD5Update as | |
743 | * needed on buffers full of bytes, and then call MD5Final, which | |
744 | * will fill a supplied 16-byte array with the digest. | |
745 | */ | |
746 | ||
747 | #ifndef HIGHFIRST | |
748 | #define byteReverse(buf, len) /* Nothing */ | |
749 | #else | |
750 | /* | |
751 | * Note: this code is harmless on little-endian machines. | |
752 | */ | |
753 | void byteReverse(unsigned char *buf, unsigned longs) | |
754 | { | |
755 | FcChar32 t; | |
756 | do { | |
757 | t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | | |
758 | ((unsigned) buf[1] << 8 | buf[0]); | |
759 | *(FcChar32 *) buf = t; | |
760 | buf += 4; | |
761 | } while (--longs); | |
762 | } | |
763 | #endif | |
764 | ||
765 | /* | |
766 | * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious | |
767 | * initialization constants. | |
768 | */ | |
769 | static void MD5Init(struct MD5Context *ctx) | |
770 | { | |
771 | ctx->buf[0] = 0x67452301; | |
772 | ctx->buf[1] = 0xefcdab89; | |
773 | ctx->buf[2] = 0x98badcfe; | |
774 | ctx->buf[3] = 0x10325476; | |
775 | ||
776 | ctx->bits[0] = 0; | |
777 | ctx->bits[1] = 0; | |
778 | } | |
779 | ||
780 | /* | |
781 | * Update context to reflect the concatenation of another buffer full | |
782 | * of bytes. | |
783 | */ | |
784 | static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len) | |
785 | { | |
786 | FcChar32 t; | |
787 | ||
788 | /* Update bitcount */ | |
789 | ||
790 | t = ctx->bits[0]; | |
791 | if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t) | |
792 | ctx->bits[1]++; /* Carry from low to high */ | |
793 | ctx->bits[1] += len >> 29; | |
794 | ||
795 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | |
796 | ||
797 | /* Handle any leading odd-sized chunks */ | |
798 | ||
799 | if (t) { | |
800 | unsigned char *p = (unsigned char *) ctx->in + t; | |
801 | ||
802 | t = 64 - t; | |
803 | if (len < t) { | |
804 | memcpy(p, buf, len); | |
805 | return; | |
806 | } | |
807 | memcpy(p, buf, t); | |
808 | byteReverse(ctx->in, 16); | |
809 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
810 | buf += t; | |
811 | len -= t; | |
812 | } | |
813 | /* Process data in 64-byte chunks */ | |
814 | ||
815 | while (len >= 64) { | |
816 | memcpy(ctx->in, buf, 64); | |
817 | byteReverse(ctx->in, 16); | |
818 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
819 | buf += 64; | |
820 | len -= 64; | |
821 | } | |
822 | ||
823 | /* Handle any remaining bytes of data. */ | |
824 | ||
825 | memcpy(ctx->in, buf, len); | |
826 | } | |
827 | ||
828 | /* | |
829 | * Final wrapup - pad to 64-byte boundary with the bit pattern | |
830 | * 1 0* (64-bit count of bits processed, MSB-first) | |
831 | */ | |
832 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) | |
833 | { | |
834 | unsigned count; | |
835 | unsigned char *p; | |
836 | ||
837 | /* Compute number of bytes mod 64 */ | |
838 | count = (ctx->bits[0] >> 3) & 0x3F; | |
839 | ||
840 | /* Set the first char of padding to 0x80. This is safe since there is | |
841 | always at least one byte free */ | |
842 | p = ctx->in + count; | |
843 | *p++ = 0x80; | |
844 | ||
845 | /* Bytes of padding needed to make 64 bytes */ | |
846 | count = 64 - 1 - count; | |
847 | ||
848 | /* Pad out to 56 mod 64 */ | |
849 | if (count < 8) { | |
850 | /* Two lots of padding: Pad the first block to 64 bytes */ | |
851 | memset(p, 0, count); | |
852 | byteReverse(ctx->in, 16); | |
853 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
854 | ||
855 | /* Now fill the next block with 56 bytes */ | |
856 | memset(ctx->in, 0, 56); | |
857 | } else { | |
858 | /* Pad block to 56 bytes */ | |
859 | memset(p, 0, count - 8); | |
860 | } | |
861 | byteReverse(ctx->in, 14); | |
862 | ||
863 | /* Append length in bits and transform */ | |
864 | ((FcChar32 *) ctx->in)[14] = ctx->bits[0]; | |
865 | ((FcChar32 *) ctx->in)[15] = ctx->bits[1]; | |
866 | ||
867 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
868 | byteReverse((unsigned char *) ctx->buf, 4); | |
869 | memcpy(digest, ctx->buf, 16); | |
870 | memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ | |
871 | } | |
872 | ||
873 | ||
874 | /* The four core functions - F1 is optimized somewhat */ | |
875 | ||
876 | /* #define F1(x, y, z) (x & y | ~x & z) */ | |
877 | #define F1(x, y, z) (z ^ (x & (y ^ z))) | |
878 | #define F2(x, y, z) F1(z, x, y) | |
879 | #define F3(x, y, z) (x ^ y ^ z) | |
880 | #define F4(x, y, z) (y ^ (x | ~z)) | |
881 | ||
882 | /* This is the central step in the MD5 algorithm. */ | |
883 | #define MD5STEP(f, w, x, y, z, data, s) \ | |
884 | ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) | |
885 | ||
886 | /* | |
887 | * The core of the MD5 algorithm, this alters an existing MD5 hash to | |
888 | * reflect the addition of 16 longwords of new data. MD5Update blocks | |
889 | * the data and converts bytes into longwords for this routine. | |
890 | */ | |
891 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]) | |
892 | { | |
893 | register FcChar32 a, b, c, d; | |
894 | ||
895 | a = buf[0]; | |
896 | b = buf[1]; | |
897 | c = buf[2]; | |
898 | d = buf[3]; | |
899 | ||
900 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); | |
901 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); | |
902 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); | |
903 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); | |
904 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); | |
905 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); | |
906 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); | |
907 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); | |
908 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); | |
909 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); | |
910 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); | |
911 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); | |
912 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); | |
913 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); | |
914 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); | |
915 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); | |
916 | ||
917 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); | |
918 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); | |
919 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); | |
920 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); | |
921 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); | |
922 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); | |
923 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); | |
924 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); | |
925 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); | |
926 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); | |
927 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); | |
928 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); | |
929 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); | |
930 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); | |
931 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); | |
932 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); | |
933 | ||
934 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); | |
935 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); | |
936 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); | |
937 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); | |
938 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); | |
939 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); | |
940 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); | |
941 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); | |
942 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); | |
943 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); | |
944 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); | |
945 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); | |
946 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); | |
947 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); | |
948 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); | |
949 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); | |
950 | ||
951 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); | |
952 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); | |
953 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); | |
954 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); | |
955 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); | |
956 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); | |
957 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); | |
958 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); | |
959 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); | |
960 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); | |
961 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); | |
962 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); | |
963 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); | |
964 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); | |
965 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); | |
966 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); | |
967 | ||
968 | buf[0] += a; | |
969 | buf[1] += b; | |
970 | buf[2] += c; | |
971 | buf[3] += d; | |
972 | } |