/work/workdir/UnpackedTarball/fontconfig/src/fccache.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2000 Keith Packard |
3 | | * Copyright © 2005 Patrick Lam |
4 | | * |
5 | | * Permission to use, copy, modify, distribute, and sell this software and its |
6 | | * documentation for any purpose is hereby granted without fee, provided that |
7 | | * the above copyright notice appear in all copies and that both that |
8 | | * copyright notice and this permission notice appear in supporting |
9 | | * documentation, and that the name of the author(s) not be used in |
10 | | * advertising or publicity pertaining to distribution of the software without |
11 | | * specific, written prior permission. The authors make no |
12 | | * representations about the suitability of this software for any purpose. It |
13 | | * is provided "as is" without express or implied warranty. |
14 | | * |
15 | | * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
16 | | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
17 | | * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
18 | | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
19 | | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
20 | | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
21 | | * PERFORMANCE OF THIS SOFTWARE. |
22 | | */ |
23 | | #include "fcint.h" |
24 | | |
25 | | #include "fcarch.h" |
26 | | #include "fcmd5.h" |
27 | | |
28 | | #include <fcntl.h> |
29 | | #include <stdio.h> |
30 | | #include <stdlib.h> |
31 | | #ifdef HAVE_DIRENT_H |
32 | | # include <dirent.h> |
33 | | #endif |
34 | | #include <limits.h> |
35 | | #include <string.h> |
36 | | #include <sys/stat.h> |
37 | | #include <sys/types.h> |
38 | | |
39 | | #ifndef _WIN32 |
40 | | # include <sys/time.h> |
41 | | #else |
42 | | # include <winsock2.h> /* for struct timeval */ |
43 | | #endif |
44 | | |
45 | | #include <assert.h> |
46 | | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
47 | | # include <sys/mman.h> |
48 | | # include <unistd.h> |
49 | | #endif |
50 | | #if defined(_WIN32) |
51 | | # include <sys/locking.h> |
52 | | #endif |
53 | | |
54 | | #ifndef O_BINARY |
55 | 107 | # define O_BINARY 0 |
56 | | #endif |
57 | | |
58 | | FcBool |
59 | | FcDirCacheCreateUUID (FcChar8 *dir, |
60 | | FcBool force, |
61 | | FcConfig *config) |
62 | 0 | { |
63 | 0 | return FcTrue; |
64 | 0 | } |
65 | | |
66 | | FcBool |
67 | | FcDirCacheDeleteUUID (const FcChar8 *dir, |
68 | | FcConfig *config) |
69 | 213 | { |
70 | 213 | FcBool ret = FcTrue; |
71 | 213 | #ifndef _WIN32 |
72 | 213 | const FcChar8 *sysroot; |
73 | 213 | FcChar8 *target, *d; |
74 | 213 | struct stat statb; |
75 | 213 | struct timeval times[2]; |
76 | | |
77 | 213 | config = FcConfigReference (config); |
78 | 213 | if (!config) |
79 | 0 | return FcFalse; |
80 | 213 | sysroot = FcConfigGetSysRoot (config); |
81 | 213 | if (sysroot) |
82 | 0 | d = FcStrBuildFilename (sysroot, dir, NULL); |
83 | 213 | else |
84 | 213 | d = FcStrBuildFilename (dir, NULL); |
85 | 213 | if (FcStat (d, &statb) != 0) { |
86 | 212 | ret = FcFalse; |
87 | 212 | goto bail; |
88 | 212 | } |
89 | 1 | target = FcStrBuildFilename (d, ".uuid", NULL); |
90 | 1 | ret = unlink ((char *)target) == 0; |
91 | 1 | if (ret) { |
92 | 0 | times[0].tv_sec = statb.st_atime; |
93 | 0 | times[1].tv_sec = statb.st_mtime; |
94 | 0 | # ifdef HAVE_STRUCT_STAT_ST_MTIM |
95 | 0 | times[0].tv_usec = statb.st_atim.tv_nsec / 1000; |
96 | 0 | times[1].tv_usec = statb.st_mtim.tv_nsec / 1000; |
97 | | # else |
98 | | times[0].tv_usec = 0; |
99 | | times[1].tv_usec = 0; |
100 | | # endif |
101 | 0 | if (utimes ((const char *)d, times) != 0) { |
102 | 0 | fprintf (stderr, "Unable to revert mtime: %s\n", d); |
103 | 0 | } |
104 | 0 | } |
105 | 1 | FcStrFree (target); |
106 | 213 | bail: |
107 | 213 | FcStrFree (d); |
108 | 213 | FcConfigDestroy (config); |
109 | 213 | #endif |
110 | | |
111 | 213 | return ret; |
112 | 1 | } |
113 | | |
114 | | #define CACHEBASE_LEN (1 + 36 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX)) |
115 | | |
116 | | static FcBool |
117 | | FcCacheIsMmapSafe (int fd) |
118 | 105 | { |
119 | 105 | enum { |
120 | 105 | MMAP_NOT_INITIALIZED = 0, |
121 | 105 | MMAP_USE, |
122 | 105 | MMAP_DONT_USE, |
123 | 105 | MMAP_CHECK_FS, |
124 | 105 | } status; |
125 | 105 | static void *static_status; |
126 | | |
127 | 105 | status = (intptr_t)fc_atomic_ptr_get (&static_status); |
128 | | |
129 | 105 | if (status == MMAP_NOT_INITIALIZED) { |
130 | 105 | const char *env = getenv ("FONTCONFIG_USE_MMAP"); |
131 | 105 | FcBool use; |
132 | 105 | if (env && FcNameBool ((const FcChar8 *)env, &use)) |
133 | 0 | status = use ? MMAP_USE : MMAP_DONT_USE; |
134 | 105 | else |
135 | 105 | status = MMAP_CHECK_FS; |
136 | 105 | (void)fc_atomic_ptr_cmpexch (&static_status, NULL, (void *)(intptr_t)status); |
137 | 105 | } |
138 | | |
139 | 105 | if (status == MMAP_CHECK_FS) |
140 | 105 | return FcIsFsMmapSafe (fd); |
141 | 0 | else |
142 | 0 | return status == MMAP_USE; |
143 | 105 | } |
144 | | |
145 | | static const char bin2hex[] = { '0', '1', '2', '3', |
146 | | '4', '5', '6', '7', |
147 | | '8', '9', 'a', 'b', |
148 | | 'c', 'd', 'e', 'f' }; |
149 | | |
150 | | static FcChar8 * |
151 | | FcDirCacheBasenameMD5 (FcConfig *config, const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN]) |
152 | 108 | { |
153 | 108 | FcChar8 *mapped_dir = NULL; |
154 | 108 | unsigned char hash[16]; |
155 | 108 | FcChar8 *hex_hash, *key = NULL; |
156 | 108 | int cnt; |
157 | 108 | struct MD5Context ctx; |
158 | 108 | const FcChar8 *salt, *orig_dir = NULL; |
159 | | |
160 | 108 | salt = FcConfigMapSalt (config, dir); |
161 | | /* Obtain a path where "dir" is mapped to. |
162 | | * In case: |
163 | | * <remap-dir as-path="/usr/share/fonts">/run/host/fonts</remap-dir> |
164 | | * |
165 | | * FcConfigMapFontPath (config, "/run/host/fonts") will returns "/usr/share/fonts". |
166 | | */ |
167 | 108 | mapped_dir = FcConfigMapFontPath (config, dir); |
168 | 108 | if (mapped_dir) { |
169 | 0 | orig_dir = dir; |
170 | 0 | dir = mapped_dir; |
171 | 0 | } |
172 | 108 | if (salt) { |
173 | 0 | size_t dl = strlen ((const char *)dir); |
174 | 0 | size_t sl = strlen ((const char *)salt); |
175 | |
|
176 | 0 | key = (FcChar8 *)malloc (dl + sl + 1); |
177 | 0 | memcpy (key, dir, dl); |
178 | 0 | memcpy (key + dl, salt, sl + 1); |
179 | 0 | key[dl + sl] = 0; |
180 | 0 | if (!orig_dir) |
181 | 0 | orig_dir = dir; |
182 | 0 | dir = key; |
183 | 0 | } |
184 | 108 | MD5Init (&ctx); |
185 | 108 | MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *)dir)); |
186 | | |
187 | 108 | MD5Final (hash, &ctx); |
188 | | |
189 | 108 | if (key) |
190 | 0 | FcStrFree (key); |
191 | | |
192 | 108 | hex_hash = cache_base; |
193 | 1.83k | for (cnt = 0; cnt < 16; ++cnt) { |
194 | 1.72k | hex_hash[2 * cnt] = bin2hex[hash[cnt] >> 4]; |
195 | 1.72k | hex_hash[2 * cnt + 1] = bin2hex[hash[cnt] & 0xf]; |
196 | 1.72k | } |
197 | 108 | hex_hash[2 * cnt] = 0; |
198 | 108 | strcat ((char *)cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX); |
199 | 108 | if (FcDebug() & FC_DBG_CACHE) { |
200 | 0 | printf ("cache: %s (dir: %s%s%s%s%s%s)\n", cache_base, orig_dir ? orig_dir : dir, mapped_dir ? " (mapped to " : "", mapped_dir ? (char *)mapped_dir : "", mapped_dir ? ")" : "", salt ? ", salt: " : "", salt ? (char *)salt : ""); |
201 | 0 | } |
202 | | |
203 | 108 | if (mapped_dir) |
204 | 0 | FcStrFree (mapped_dir); |
205 | | |
206 | 108 | return cache_base; |
207 | 108 | } |
208 | | |
209 | | #ifndef _WIN32 |
210 | | static FcChar8 * |
211 | | FcDirCacheBasenameUUID (FcConfig *config, const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN]) |
212 | 1 | { |
213 | 1 | FcChar8 *target, *fuuid; |
214 | 1 | const FcChar8 *sysroot = FcConfigGetSysRoot (config); |
215 | 1 | int fd; |
216 | | |
217 | | /* We don't need to apply remapping here. because .uuid was created at that very directory |
218 | | * to determine the cache name no matter where it was mapped to. |
219 | | */ |
220 | 1 | cache_base[0] = 0; |
221 | 1 | if (sysroot) |
222 | 0 | target = FcStrBuildFilename (sysroot, dir, NULL); |
223 | 1 | else |
224 | 1 | target = FcStrdup (dir); |
225 | 1 | fuuid = FcStrBuildFilename (target, ".uuid", NULL); |
226 | 1 | if ((fd = FcOpen ((char *)fuuid, O_RDONLY)) != -1) { |
227 | 0 | char suuid[37]; |
228 | 0 | ssize_t len; |
229 | |
|
230 | 0 | memset (suuid, 0, sizeof (suuid)); |
231 | 0 | len = read (fd, suuid, 36); |
232 | 0 | suuid[36] = 0; |
233 | 0 | close (fd); |
234 | 0 | if (len < 0) |
235 | 0 | goto bail; |
236 | 0 | cache_base[0] = '/'; |
237 | 0 | strcpy ((char *)&cache_base[1], suuid); |
238 | 0 | strcat ((char *)cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX); |
239 | 0 | if (FcDebug() & FC_DBG_CACHE) { |
240 | 0 | printf ("cache fallbacks to: %s (dir: %s)\n", cache_base, dir); |
241 | 0 | } |
242 | 0 | } |
243 | 1 | bail: |
244 | 1 | FcStrFree (fuuid); |
245 | 1 | FcStrFree (target); |
246 | | |
247 | 1 | return cache_base; |
248 | 1 | } |
249 | | #endif |
250 | | |
251 | | FcBool |
252 | | FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config) |
253 | 0 | { |
254 | 0 | FcChar8 *cache_hashed = NULL; |
255 | 0 | FcChar8 cache_base[CACHEBASE_LEN]; |
256 | 0 | #ifndef _WIN32 |
257 | 0 | FcChar8 uuid_cache_base[CACHEBASE_LEN]; |
258 | 0 | #endif |
259 | 0 | FcStrList *list; |
260 | 0 | FcChar8 *cache_dir; |
261 | 0 | const FcChar8 *sysroot; |
262 | 0 | FcBool ret = FcTrue; |
263 | |
|
264 | 0 | config = FcConfigReference (config); |
265 | 0 | if (!config) |
266 | 0 | return FcFalse; |
267 | 0 | sysroot = FcConfigGetSysRoot (config); |
268 | |
|
269 | 0 | FcDirCacheBasenameMD5 (config, dir, cache_base); |
270 | 0 | #ifndef _WIN32 |
271 | 0 | FcDirCacheBasenameUUID (config, dir, uuid_cache_base); |
272 | 0 | #endif |
273 | |
|
274 | 0 | list = FcStrListCreate (config->cacheDirs); |
275 | 0 | if (!list) { |
276 | 0 | ret = FcFalse; |
277 | 0 | goto bail; |
278 | 0 | } |
279 | | |
280 | 0 | while ((cache_dir = FcStrListNext (list))) { |
281 | 0 | if (sysroot) |
282 | 0 | cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL); |
283 | 0 | else |
284 | 0 | cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL); |
285 | 0 | if (!cache_hashed) |
286 | 0 | break; |
287 | 0 | (void)unlink ((char *)cache_hashed); |
288 | 0 | FcStrFree (cache_hashed); |
289 | 0 | #ifndef _WIN32 |
290 | 0 | if (uuid_cache_base[0] != 0) { |
291 | 0 | if (sysroot) |
292 | 0 | cache_hashed = FcStrBuildFilename (sysroot, cache_dir, uuid_cache_base, NULL); |
293 | 0 | else |
294 | 0 | cache_hashed = FcStrBuildFilename (cache_dir, uuid_cache_base, NULL); |
295 | 0 | if (!cache_hashed) |
296 | 0 | break; |
297 | 0 | (void)unlink ((char *)cache_hashed); |
298 | 0 | FcStrFree (cache_hashed); |
299 | 0 | } |
300 | 0 | #endif |
301 | 0 | } |
302 | 0 | FcStrListDone (list); |
303 | 0 | FcDirCacheDeleteUUID (dir, config); |
304 | | /* return FcFalse if something went wrong */ |
305 | 0 | if (cache_dir) |
306 | 0 | ret = FcFalse; |
307 | 0 | bail: |
308 | 0 | FcConfigDestroy (config); |
309 | |
|
310 | 0 | return ret; |
311 | 0 | } |
312 | | |
313 | | static int |
314 | | FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat) |
315 | 106 | { |
316 | 106 | int fd; |
317 | | |
318 | | #ifdef _WIN32 |
319 | | if (FcStat (cache_file, file_stat) < 0) |
320 | | return -1; |
321 | | #endif |
322 | 106 | fd = FcOpen ((char *)cache_file, O_RDONLY | O_BINARY); |
323 | 106 | if (fd < 0) |
324 | 1 | return fd; |
325 | 105 | #ifndef _WIN32 |
326 | 105 | if (fstat (fd, file_stat) < 0) { |
327 | 0 | close (fd); |
328 | 0 | return -1; |
329 | 0 | } |
330 | 105 | #endif |
331 | 105 | return fd; |
332 | 105 | } |
333 | | |
334 | | /* |
335 | | * Look for a cache file for the specified dir. Attempt |
336 | | * to use each one we find, stopping when the callback |
337 | | * indicates success |
338 | | */ |
339 | | static FcBool |
340 | | FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, |
341 | | FcBool (*callback) (FcConfig *config, int fd, struct stat *fd_stat, |
342 | | struct stat *dir_stat, struct timeval *cache_mtime, void *closure), |
343 | | void *closure, FcChar8 **cache_file_ret) |
344 | 318 | { |
345 | 318 | int fd = -1; |
346 | 318 | FcChar8 cache_base[CACHEBASE_LEN]; |
347 | 318 | FcStrList *list; |
348 | 318 | FcChar8 *cache_dir, *d; |
349 | 318 | struct stat file_stat, dir_stat; |
350 | 318 | FcBool ret = FcFalse; |
351 | 318 | const FcChar8 *sysroot = FcConfigGetSysRoot (config); |
352 | 318 | struct timeval latest_mtime = (struct timeval){ 0 }; |
353 | | |
354 | 318 | if (sysroot) |
355 | 0 | d = FcStrBuildFilename (sysroot, dir, NULL); |
356 | 318 | else |
357 | 318 | d = FcStrdup (dir); |
358 | 318 | if (FcStatChecksum (d, &dir_stat) < 0) { |
359 | 212 | FcStrFree (d); |
360 | 212 | return FcFalse; |
361 | 212 | } |
362 | 106 | FcStrFree (d); |
363 | | |
364 | 106 | FcDirCacheBasenameMD5 (config, dir, cache_base); |
365 | | |
366 | 106 | list = FcStrListCreate (config->cacheDirs); |
367 | 106 | if (!list) |
368 | 0 | return FcFalse; |
369 | | |
370 | 212 | while ((cache_dir = FcStrListNext (list))) { |
371 | 106 | FcChar8 *cache_hashed; |
372 | 106 | #ifndef _WIN32 |
373 | 106 | FcBool retried = FcFalse; |
374 | 106 | #endif |
375 | | |
376 | 106 | if (sysroot) |
377 | 0 | cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL); |
378 | 106 | else |
379 | 106 | cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL); |
380 | 106 | if (!cache_hashed) |
381 | 0 | break; |
382 | 106 | #ifndef _WIN32 |
383 | 106 | retry: |
384 | 106 | #endif |
385 | 106 | fd = FcDirCacheOpenFile (cache_hashed, &file_stat); |
386 | 106 | if (fd >= 0) { |
387 | 105 | ret = (*callback) (config, fd, &file_stat, &dir_stat, &latest_mtime, closure); |
388 | 105 | close (fd); |
389 | 105 | if (ret) { |
390 | 105 | if (cache_file_ret) { |
391 | 0 | if (*cache_file_ret) |
392 | 0 | FcStrFree (*cache_file_ret); |
393 | 0 | *cache_file_ret = cache_hashed; |
394 | 0 | } else |
395 | 105 | FcStrFree (cache_hashed); |
396 | 105 | } else |
397 | 0 | FcStrFree (cache_hashed); |
398 | 105 | } |
399 | 1 | #ifndef _WIN32 |
400 | 1 | else if (!retried) { |
401 | 1 | FcChar8 uuid_cache_base[CACHEBASE_LEN]; |
402 | | |
403 | 1 | retried = FcTrue; |
404 | 1 | FcDirCacheBasenameUUID (config, dir, uuid_cache_base); |
405 | 1 | if (uuid_cache_base[0] != 0) { |
406 | 0 | FcStrFree (cache_hashed); |
407 | 0 | if (sysroot) |
408 | 0 | cache_hashed = FcStrBuildFilename (sysroot, cache_dir, uuid_cache_base, NULL); |
409 | 0 | else |
410 | 0 | cache_hashed = FcStrBuildFilename (cache_dir, uuid_cache_base, NULL); |
411 | 0 | if (!cache_hashed) |
412 | 0 | break; |
413 | 0 | goto retry; |
414 | 0 | } else |
415 | 1 | FcStrFree (cache_hashed); |
416 | 1 | } |
417 | 0 | #endif |
418 | 0 | else |
419 | 0 | FcStrFree (cache_hashed); |
420 | 106 | } |
421 | 106 | FcStrListDone (list); |
422 | | |
423 | 106 | if (closure) |
424 | 106 | return !!(*((FcCache **)closure) != NULL); |
425 | 0 | return ret; |
426 | 106 | } |
427 | | |
428 | 107 | #define FC_CACHE_MIN_MMAP 1024 |
429 | | |
430 | | /* |
431 | | * Skip list element, make sure the 'next' pointer is the last thing |
432 | | * in the structure, it will be allocated large enough to hold all |
433 | | * of the necessary pointers |
434 | | */ |
435 | | |
436 | | typedef struct _FcCacheSkip FcCacheSkip; |
437 | | |
438 | | struct _FcCacheSkip { |
439 | | FcCache *cache; |
440 | | FcRef ref; |
441 | | intptr_t size; |
442 | | void *allocated; |
443 | | dev_t cache_dev; |
444 | | ino_t cache_ino; |
445 | | time_t cache_mtime; |
446 | | long cache_mtime_nano; |
447 | | FcCacheSkip *next[1]; |
448 | | }; |
449 | | |
450 | | /* |
451 | | * The head of the skip list; pointers for every possible level |
452 | | * in the skip list, plus the largest level in the list |
453 | | */ |
454 | | |
455 | 151 | #define FC_CACHE_MAX_LEVEL 16 |
456 | | |
457 | | /* Protected by cache_lock below */ |
458 | | static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL]; |
459 | | static int fcCacheMaxLevel; |
460 | | |
461 | | static FcMutex *cache_lock; |
462 | | |
463 | | static void |
464 | | lock_cache (void) |
465 | 12.7k | { |
466 | 12.7k | FcMutex *lock; |
467 | 12.7k | retry: |
468 | 12.7k | lock = fc_atomic_ptr_get (&cache_lock); |
469 | 12.7k | if (!lock) { |
470 | 106 | lock = (FcMutex *)malloc (sizeof (FcMutex)); |
471 | 106 | FcMutexInit (lock); |
472 | 106 | if (!fc_atomic_ptr_cmpexch (&cache_lock, NULL, lock)) { |
473 | 0 | FcMutexFinish (lock); |
474 | 0 | free (lock); |
475 | 0 | goto retry; |
476 | 0 | } |
477 | | |
478 | 106 | FcMutexLock (lock); |
479 | | /* Initialize random state */ |
480 | 106 | FcRandom(); |
481 | 106 | return; |
482 | 106 | } |
483 | 12.6k | FcMutexLock (lock); |
484 | 12.6k | } |
485 | | |
486 | | static void |
487 | | unlock_cache (void) |
488 | 12.7k | { |
489 | 12.7k | FcMutex *lock; |
490 | 12.7k | lock = fc_atomic_ptr_get (&cache_lock); |
491 | 12.7k | FcMutexUnlock (lock); |
492 | 12.7k | } |
493 | | |
494 | | static void |
495 | | free_lock (void) |
496 | 0 | { |
497 | 0 | FcMutex *lock; |
498 | 0 | lock = fc_atomic_ptr_get (&cache_lock); |
499 | 0 | if (lock && fc_atomic_ptr_cmpexch (&cache_lock, lock, NULL)) { |
500 | 0 | FcMutexFinish (lock); |
501 | 0 | free (lock); |
502 | 0 | } |
503 | 0 | } |
504 | | |
505 | | /* |
506 | | * Generate a random level number, distributed |
507 | | * so that each level is 1/4 as likely as the one before |
508 | | * |
509 | | * Note that level numbers run 1 <= level <= MAX_LEVEL |
510 | | */ |
511 | | static int |
512 | | random_level (void) |
513 | 106 | { |
514 | | /* tricky bit -- each bit is '1' 75% of the time */ |
515 | 106 | long int bits = FcRandom() | FcRandom(); |
516 | 106 | int level = 0; |
517 | | |
518 | 151 | while (++level < FC_CACHE_MAX_LEVEL) { |
519 | 151 | if (bits & 1) |
520 | 106 | break; |
521 | 45 | bits >>= 1; |
522 | 45 | } |
523 | 106 | return level; |
524 | 106 | } |
525 | | |
526 | | /* |
527 | | * Insert cache into the list |
528 | | */ |
529 | | static FcBool |
530 | | FcCacheInsert (FcCache *cache, struct stat *cache_stat) |
531 | 106 | { |
532 | 106 | FcCacheSkip **update[FC_CACHE_MAX_LEVEL]; |
533 | 106 | FcCacheSkip *s, **next; |
534 | 106 | int i, level; |
535 | | |
536 | 106 | lock_cache(); |
537 | | |
538 | | /* |
539 | | * Find links along each chain |
540 | | */ |
541 | 106 | next = fcCacheChains; |
542 | 106 | for (i = fcCacheMaxLevel; --i >= 0;) { |
543 | 0 | for (; (s = next[i]); next = s->next) |
544 | 0 | if (s->cache > cache) |
545 | 0 | break; |
546 | 0 | update[i] = &next[i]; |
547 | 0 | } |
548 | | |
549 | | /* |
550 | | * Create new list element |
551 | | */ |
552 | 106 | level = random_level(); |
553 | 106 | if (level > fcCacheMaxLevel) { |
554 | 106 | level = fcCacheMaxLevel + 1; |
555 | 106 | update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel]; |
556 | 106 | fcCacheMaxLevel = level; |
557 | 106 | } |
558 | | |
559 | 106 | s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *)); |
560 | 106 | if (!s) { |
561 | 0 | unlock_cache(); |
562 | 0 | return FcFalse; |
563 | 0 | } |
564 | | |
565 | 106 | s->cache = cache; |
566 | 106 | s->size = cache->size; |
567 | 106 | s->allocated = NULL; |
568 | 106 | FcRefInit (&s->ref, 1); |
569 | 106 | if (cache_stat) { |
570 | 105 | s->cache_dev = cache_stat->st_dev; |
571 | 105 | s->cache_ino = cache_stat->st_ino; |
572 | 105 | s->cache_mtime = cache_stat->st_mtime; |
573 | 105 | #ifdef HAVE_STRUCT_STAT_ST_MTIM |
574 | 105 | s->cache_mtime_nano = cache_stat->st_mtim.tv_nsec; |
575 | | #else |
576 | | s->cache_mtime_nano = 0; |
577 | | #endif |
578 | 105 | } else { |
579 | 1 | s->cache_dev = 0; |
580 | 1 | s->cache_ino = 0; |
581 | 1 | s->cache_mtime = 0; |
582 | 1 | s->cache_mtime_nano = 0; |
583 | 1 | } |
584 | | |
585 | | /* |
586 | | * Insert into all fcCacheChains |
587 | | */ |
588 | 212 | for (i = 0; i < level; i++) { |
589 | 106 | s->next[i] = *update[i]; |
590 | 106 | *update[i] = s; |
591 | 106 | } |
592 | | |
593 | 106 | unlock_cache(); |
594 | 106 | return FcTrue; |
595 | 106 | } |
596 | | |
597 | | static FcCacheSkip * |
598 | | FcCacheFindByAddrUnlocked (void *object) |
599 | 12.5k | { |
600 | 12.5k | int i; |
601 | 12.5k | FcCacheSkip **next = fcCacheChains; |
602 | 12.5k | FcCacheSkip *s; |
603 | | |
604 | 12.5k | if (!object) |
605 | 0 | return NULL; |
606 | | |
607 | | /* |
608 | | * Walk chain pointers one level at a time |
609 | | */ |
610 | 25.0k | for (i = fcCacheMaxLevel; --i >= 0;) |
611 | 12.5k | while (next[i] && (char *)object >= ((char *)next[i]->cache + next[i]->size)) |
612 | 0 | next = next[i]->next; |
613 | | /* |
614 | | * Here we are |
615 | | */ |
616 | 12.5k | s = next[0]; |
617 | 12.5k | if (s && (char *)object < ((char *)s->cache + s->size)) |
618 | 12.5k | return s; |
619 | 0 | return NULL; |
620 | 12.5k | } |
621 | | |
622 | | static FcCacheSkip * |
623 | | FcCacheFindByAddr (void *object) |
624 | 6.91k | { |
625 | 6.91k | FcCacheSkip *ret; |
626 | 6.91k | lock_cache(); |
627 | 6.91k | ret = FcCacheFindByAddrUnlocked (object); |
628 | 6.91k | unlock_cache(); |
629 | 6.91k | return ret; |
630 | 6.91k | } |
631 | | |
632 | | static void |
633 | | FcCacheRemoveUnlocked (FcCache *cache) |
634 | 0 | { |
635 | 0 | FcCacheSkip **update[FC_CACHE_MAX_LEVEL]; |
636 | 0 | FcCacheSkip *s, **next; |
637 | 0 | int i; |
638 | 0 | void *allocated; |
639 | | |
640 | | /* |
641 | | * Find links along each chain |
642 | | */ |
643 | 0 | next = fcCacheChains; |
644 | 0 | for (i = fcCacheMaxLevel; --i >= 0;) { |
645 | 0 | for (; (s = next[i]); next = s->next) |
646 | 0 | if (s->cache >= cache) |
647 | 0 | break; |
648 | 0 | update[i] = &next[i]; |
649 | 0 | } |
650 | 0 | s = next[0]; |
651 | 0 | for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++) |
652 | 0 | *update[i] = s->next[i]; |
653 | 0 | while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL) |
654 | 0 | fcCacheMaxLevel--; |
655 | |
|
656 | 0 | if (s) { |
657 | 0 | allocated = s->allocated; |
658 | 0 | while (allocated) { |
659 | | /* First element in allocated chunk is the free list */ |
660 | 0 | next = *(void **)allocated; |
661 | 0 | free (allocated); |
662 | 0 | allocated = next; |
663 | 0 | } |
664 | 0 | free (s); |
665 | 0 | } |
666 | 0 | } |
667 | | |
668 | | static FcCache * |
669 | | FcCacheFindByStat (struct stat *cache_stat) |
670 | 105 | { |
671 | 105 | FcCacheSkip *s; |
672 | | |
673 | 105 | lock_cache(); |
674 | 105 | for (s = fcCacheChains[0]; s; s = s->next[0]) |
675 | 0 | if (s->cache_dev == cache_stat->st_dev && |
676 | 0 | s->cache_ino == cache_stat->st_ino && |
677 | 0 | s->cache_mtime == cache_stat->st_mtime) { |
678 | 0 | #ifdef HAVE_STRUCT_STAT_ST_MTIM |
679 | 0 | if (s->cache_mtime_nano != cache_stat->st_mtim.tv_nsec) |
680 | 0 | continue; |
681 | 0 | #endif |
682 | 0 | FcRefInc (&s->ref); |
683 | 0 | unlock_cache(); |
684 | 0 | return s->cache; |
685 | 0 | } |
686 | 105 | unlock_cache(); |
687 | 105 | return NULL; |
688 | 105 | } |
689 | | |
690 | | static void |
691 | | FcDirCacheDisposeUnlocked (FcCache *cache) |
692 | 0 | { |
693 | 0 | FcCacheRemoveUnlocked (cache); |
694 | |
|
695 | 0 | switch (cache->magic) { |
696 | 0 | case FC_CACHE_MAGIC_ALLOC: |
697 | 0 | free (cache); |
698 | 0 | break; |
699 | 0 | case FC_CACHE_MAGIC_MMAP: |
700 | 0 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
701 | 0 | munmap (cache, cache->size); |
702 | | #elif defined(_WIN32) |
703 | | UnmapViewOfFile (cache); |
704 | | #endif |
705 | 0 | break; |
706 | 0 | } |
707 | 0 | } |
708 | | |
709 | | void |
710 | | FcCacheObjectReference (void *object) |
711 | 6.80k | { |
712 | 6.80k | FcCacheSkip *skip = FcCacheFindByAddr (object); |
713 | | |
714 | 6.80k | if (skip) |
715 | 6.80k | FcRefInc (&skip->ref); |
716 | 6.80k | } |
717 | | |
718 | | void |
719 | | FcCacheObjectDereference (void *object) |
720 | 5.61k | { |
721 | 5.61k | FcCacheSkip *skip; |
722 | | |
723 | 5.61k | lock_cache(); |
724 | 5.61k | skip = FcCacheFindByAddrUnlocked (object); |
725 | 5.61k | if (skip) { |
726 | 5.61k | if (FcRefDec (&skip->ref) == 1) |
727 | 0 | FcDirCacheDisposeUnlocked (skip->cache); |
728 | 5.61k | } |
729 | 5.61k | unlock_cache(); |
730 | 5.61k | } |
731 | | |
732 | | void * |
733 | | FcCacheAllocate (FcCache *cache, size_t len) |
734 | 0 | { |
735 | 0 | FcCacheSkip *skip; |
736 | 0 | void *allocated = NULL; |
737 | |
|
738 | 0 | lock_cache(); |
739 | 0 | skip = FcCacheFindByAddrUnlocked (cache); |
740 | 0 | if (skip) { |
741 | 0 | void *chunk = malloc (sizeof (void *) + len); |
742 | 0 | if (chunk) { |
743 | | /* First element in allocated chunk is the free list */ |
744 | 0 | *(void **)chunk = skip->allocated; |
745 | 0 | skip->allocated = chunk; |
746 | | /* Return the rest */ |
747 | 0 | allocated = ((FcChar8 *)chunk) + sizeof (void *); |
748 | 0 | } |
749 | 0 | } |
750 | 0 | unlock_cache(); |
751 | 0 | return allocated; |
752 | 0 | } |
753 | | |
754 | | void |
755 | | FcCacheFini (void) |
756 | 0 | { |
757 | 0 | int i; |
758 | 0 | FcBool res = FcTrue; |
759 | |
|
760 | 0 | for (i = 0; i < FC_CACHE_MAX_LEVEL; i++) { |
761 | 0 | if (fcCacheChains[i] != NULL) { |
762 | 0 | res = FcFalse; |
763 | 0 | if (FcDebug() & FC_DBG_CACHE) { |
764 | 0 | FcCacheSkip *s = fcCacheChains[i]; |
765 | 0 | fprintf (stderr, "Fontconfig error: not freed %p (dir: %s, refcount %" FC_ATOMIC_INT_FORMAT ")\n", s->cache, FcCacheDir (s->cache), s->ref.count); |
766 | 0 | } |
767 | 0 | } |
768 | 0 | } |
769 | 0 | if (res) |
770 | 0 | free_lock(); |
771 | 0 | } |
772 | | |
773 | | static FcBool |
774 | | FcCacheTimeValid (FcConfig *config, FcCache *cache, struct stat *dir_stat) |
775 | 105 | { |
776 | 105 | struct stat dir_static; |
777 | 105 | FcBool fnano = FcTrue; |
778 | | |
779 | 105 | if (!dir_stat) { |
780 | 0 | const FcChar8 *sysroot = FcConfigGetSysRoot (config); |
781 | 0 | FcChar8 *d; |
782 | |
|
783 | 0 | if (sysroot) |
784 | 0 | d = FcStrBuildFilename (sysroot, FcCacheDir (cache), NULL); |
785 | 0 | else |
786 | 0 | d = FcStrdup (FcCacheDir (cache)); |
787 | 0 | if (FcStatChecksum (d, &dir_static) < 0) { |
788 | 0 | FcStrFree (d); |
789 | 0 | return FcFalse; |
790 | 0 | } |
791 | 0 | FcStrFree (d); |
792 | 0 | dir_stat = &dir_static; |
793 | 0 | } |
794 | 105 | #ifdef HAVE_STRUCT_STAT_ST_MTIM |
795 | 105 | fnano = (cache->checksum_nano == dir_stat->st_mtim.tv_nsec); |
796 | 105 | if (FcDebug() & FC_DBG_CACHE) |
797 | 0 | printf ("FcCacheTimeValid dir \"%s\" cache checksum %d.%ld dir checksum %d.%ld\n", |
798 | 0 | FcCacheDir (cache), cache->checksum, (long)cache->checksum_nano, (int)dir_stat->st_mtime, dir_stat->st_mtim.tv_nsec); |
799 | | #else |
800 | | if (FcDebug() & FC_DBG_CACHE) |
801 | | printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n", |
802 | | FcCacheDir (cache), cache->checksum, (int)dir_stat->st_mtime); |
803 | | #endif |
804 | | |
805 | 105 | return dir_stat->st_mtime == 0 || (cache->checksum == (int)dir_stat->st_mtime && fnano); |
806 | 105 | } |
807 | | |
808 | | static FcBool |
809 | | FcCacheOffsetsValid (FcCache *cache) |
810 | 105 | { |
811 | 105 | char *base = (char *)cache; |
812 | 105 | char *end = base + cache->size; |
813 | 105 | intptr_t *dirs; |
814 | 105 | FcFontSet *fs; |
815 | 105 | int i, j; |
816 | | |
817 | 105 | if (cache->dir < 0 || cache->dir > cache->size - sizeof (intptr_t) || |
818 | 105 | memchr (base + cache->dir, '\0', cache->size - cache->dir) == NULL) |
819 | 0 | return FcFalse; |
820 | | |
821 | 105 | if (cache->dirs < 0 || cache->dirs >= cache->size || |
822 | 105 | cache->dirs_count < 0 || |
823 | 105 | cache->dirs_count > (cache->size - cache->dirs) / sizeof (intptr_t)) |
824 | 0 | return FcFalse; |
825 | | |
826 | 105 | dirs = FcCacheDirs (cache); |
827 | 105 | if (dirs) { |
828 | 105 | for (i = 0; i < cache->dirs_count; i++) { |
829 | 0 | FcChar8 *dir; |
830 | |
|
831 | 0 | if (dirs[i] < 0 || |
832 | 0 | dirs[i] > end - (char *)dirs - sizeof (intptr_t)) |
833 | 0 | return FcFalse; |
834 | | |
835 | 0 | dir = FcOffsetToPtr (dirs, dirs[i], FcChar8); |
836 | 0 | if (memchr (dir, '\0', end - (char *)dir) == NULL) |
837 | 0 | return FcFalse; |
838 | 0 | } |
839 | 105 | } |
840 | | |
841 | 105 | if (cache->set < 0 || cache->set > cache->size - sizeof (FcFontSet)) |
842 | 0 | return FcFalse; |
843 | | |
844 | 105 | fs = FcCacheSet (cache); |
845 | 105 | if (fs) { |
846 | 105 | if (fs->nfont > (end - (char *)fs) / sizeof (FcPattern)) |
847 | 0 | return FcFalse; |
848 | | |
849 | 105 | if (!FcIsEncodedOffset (fs->fonts)) |
850 | 0 | return FcFalse; |
851 | | |
852 | 1.47k | for (i = 0; i < fs->nfont; i++) { |
853 | 1.36k | FcPattern *font = FcFontSetFont (fs, i); |
854 | 1.36k | FcPatternElt *e; |
855 | 1.36k | FcValueListPtr l; |
856 | 1.36k | char *last_offset; |
857 | 1.36k | const FcLangSet *ls; |
858 | 1.36k | const FcRange *r; |
859 | 1.36k | const FcChar8 *s; |
860 | | |
861 | 1.36k | if ((char *)font < base || |
862 | 1.36k | (char *)font > end - sizeof (FcFontSet) || |
863 | 1.36k | font->elts_offset < 0 || |
864 | 1.36k | font->elts_offset > end - (char *)font || |
865 | 1.36k | font->num > (end - (char *)font - font->elts_offset) / sizeof (FcPatternElt) || |
866 | 1.36k | !FcRefIsConst (&font->ref)) |
867 | 0 | return FcFalse; |
868 | | |
869 | 1.36k | e = FcPatternElts (font); |
870 | 1.36k | if (e->values != 0 && !FcIsEncodedOffset (e->values)) |
871 | 0 | return FcFalse; |
872 | | |
873 | 39.9k | for (j = 0; j < font->num; j++) { |
874 | 38.5k | last_offset = (char *)font + font->elts_offset; |
875 | 77.0k | for (l = FcPatternEltValues (&e[j]); l; l = FcValueListNext (l)) { |
876 | 38.5k | if ((char *)l < last_offset || (char *)l > end - sizeof (*l) || |
877 | 38.5k | (l->next != NULL && !FcIsEncodedOffset (l->next))) |
878 | 0 | return FcFalse; |
879 | 38.5k | switch (l->value.type) { |
880 | 0 | case FcTypeVoid: |
881 | 5.88k | case FcTypeInteger: |
882 | 8.61k | case FcTypeDouble: |
883 | 19.5k | case FcTypeBool: |
884 | 19.5k | case FcTypeMatrix: |
885 | 19.5k | case FcTypeFTFace: |
886 | 19.5k | break; /* nop */ |
887 | 16.2k | case FcTypeString: |
888 | 16.2k | s = FcValueString (&l->value); |
889 | 16.2k | if ((intptr_t)s < (intptr_t)&e[j] || |
890 | 16.2k | (intptr_t)&l->value > (intptr_t)end - sizeof (*l) || |
891 | 16.2k | !FcIsEncodedOffset (l->value.u.s)) { |
892 | 0 | if (FcDebug() & FC_DBG_CACHE) { |
893 | 0 | fprintf (stderr, "Fontconfig warning: invalid cache: broken string pointer\n"); |
894 | 0 | } |
895 | 0 | return FcFalse; |
896 | 0 | } |
897 | 16.2k | break; |
898 | 16.2k | case FcTypeCharSet: |
899 | | /* FcCharSet might be re-used by FcCharSetFindFrozen |
900 | | * which would means a pointer might be out of Elts |
901 | | */ |
902 | 1.36k | if ((intptr_t)&l->value > (intptr_t)end - sizeof (*l) || |
903 | 1.36k | !FcIsEncodedOffset (l->value.u.c)) { |
904 | 0 | if (FcDebug() & FC_DBG_CACHE) { |
905 | 0 | fprintf (stderr, "Fontconfig warning: invalid cache: broken charset\n"); |
906 | 0 | } |
907 | 0 | return FcFalse; |
908 | 0 | } |
909 | 1.36k | break; |
910 | 1.36k | case FcTypeLangSet: |
911 | 1.36k | ls = FcValueLangSet (&l->value); |
912 | 1.36k | if ((intptr_t)ls < (intptr_t)&e[j] || |
913 | 1.36k | (intptr_t)&l->value > (intptr_t)end - sizeof (*l) || |
914 | 1.36k | !FcIsEncodedOffset (l->value.u.l)) { |
915 | 0 | if (FcDebug() & FC_DBG_CACHE) { |
916 | 0 | fprintf (stderr, "Fontconfig warning: invalid cache: broken langset\n"); |
917 | 0 | } |
918 | 0 | return FcFalse; |
919 | 0 | } |
920 | | /* ls->extra isn't serialized. it must be null */ |
921 | 1.36k | if (ls->extra != NULL) { |
922 | 0 | if (FcDebug() & FC_DBG_CACHE) { |
923 | 0 | fprintf (stderr, "Fontconfig warning: invalid cache: broken langset\n"); |
924 | 0 | } |
925 | 0 | return FcFalse; |
926 | 0 | } |
927 | 1.36k | break; |
928 | 1.36k | case FcTypeRange: |
929 | 0 | r = FcValueRange (&l->value); |
930 | 0 | if ((intptr_t)r < (intptr_t)&e[j] || |
931 | 0 | (intptr_t)&l->value > (intptr_t)end - sizeof (*l) || |
932 | 0 | !FcIsEncodedOffset (l->value.u.r)) { |
933 | 0 | if (FcDebug() & FC_DBG_CACHE) { |
934 | 0 | fprintf (stderr, "Fontconfig warning: invalid cache: broken range\n"); |
935 | 0 | } |
936 | 0 | return FcFalse; |
937 | 0 | } |
938 | 0 | break; |
939 | 0 | default: |
940 | | /* just ignore unknown object type for upper-compatible in the future */ |
941 | 0 | if (FcDebug() & FC_DBG_CACHEV) { |
942 | 0 | fprintf (stderr, "Fontconfig warning: invalid cache: unknown object type in pattern: %d\n", l->value.type); |
943 | 0 | } |
944 | 0 | break; |
945 | 38.5k | } |
946 | 38.5k | last_offset = (char *)l + 1; |
947 | 38.5k | } |
948 | 38.5k | } |
949 | 1.36k | } |
950 | 105 | } |
951 | | |
952 | 105 | return FcTrue; |
953 | 105 | } |
954 | | |
955 | | /* |
956 | | * Map a cache file into memory |
957 | | */ |
958 | | static FcCache * |
959 | | FcDirCacheMapFd (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat) |
960 | 105 | { |
961 | 105 | FcCache *cache; |
962 | 105 | FcBool allocated = FcFalse; |
963 | | |
964 | 105 | if (fd_stat->st_size > INTPTR_MAX || |
965 | 105 | fd_stat->st_size < (int)sizeof (FcCache)) |
966 | 0 | return NULL; |
967 | 105 | cache = FcCacheFindByStat (fd_stat); |
968 | 105 | if (cache) { |
969 | 0 | if (FcCacheTimeValid (config, cache, dir_stat)) |
970 | 0 | return cache; |
971 | 0 | FcDirCacheUnload (cache); |
972 | 0 | cache = NULL; |
973 | 0 | } |
974 | | |
975 | | /* |
976 | | * Large cache files are mmap'ed, smaller cache files are read. This |
977 | | * balances the system cost of mmap against per-process memory usage. |
978 | | */ |
979 | 105 | if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP) { |
980 | 105 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
981 | 105 | cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0); |
982 | 105 | # if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED) |
983 | 105 | posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED); |
984 | 105 | # endif |
985 | 105 | if (cache == MAP_FAILED) |
986 | 0 | cache = NULL; |
987 | | #elif defined(_WIN32) |
988 | | { |
989 | | HANDLE hFileMap; |
990 | | |
991 | | cache = NULL; |
992 | | hFileMap = CreateFileMapping ((HANDLE)_get_osfhandle (fd), NULL, |
993 | | PAGE_READONLY, 0, 0, NULL); |
994 | | if (hFileMap != NULL) { |
995 | | cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, |
996 | | fd_stat->st_size); |
997 | | CloseHandle (hFileMap); |
998 | | } |
999 | | } |
1000 | | #endif |
1001 | 105 | } |
1002 | 105 | if (!cache) { |
1003 | 0 | cache = malloc (fd_stat->st_size); |
1004 | 0 | if (!cache) |
1005 | 0 | return NULL; |
1006 | | |
1007 | 0 | if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size) { |
1008 | 0 | free (cache); |
1009 | 0 | return NULL; |
1010 | 0 | } |
1011 | 0 | allocated = FcTrue; |
1012 | 0 | } |
1013 | 105 | if (cache->magic != FC_CACHE_MAGIC_MMAP || |
1014 | 105 | cache->version < FC_CACHE_VERSION_NUMBER || |
1015 | 105 | cache->size != (intptr_t)fd_stat->st_size || |
1016 | 105 | !FcCacheOffsetsValid (cache) || |
1017 | 105 | !FcCacheTimeValid (config, cache, dir_stat) || |
1018 | 105 | !FcCacheInsert (cache, fd_stat)) { |
1019 | 0 | if (allocated) |
1020 | 0 | free (cache); |
1021 | 0 | else { |
1022 | 0 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
1023 | 0 | munmap (cache, fd_stat->st_size); |
1024 | | #elif defined(_WIN32) |
1025 | | UnmapViewOfFile (cache); |
1026 | | #endif |
1027 | 0 | } |
1028 | 0 | return NULL; |
1029 | 0 | } |
1030 | | |
1031 | | /* Mark allocated caches so they're freed rather than unmapped */ |
1032 | 105 | if (allocated) |
1033 | 0 | cache->magic = FC_CACHE_MAGIC_ALLOC; |
1034 | | |
1035 | 105 | return cache; |
1036 | 105 | } |
1037 | | |
1038 | | void |
1039 | | FcDirCacheReference (FcCache *cache, int nref) |
1040 | 106 | { |
1041 | 106 | FcCacheSkip *skip = FcCacheFindByAddr (cache); |
1042 | | |
1043 | 106 | if (skip) |
1044 | 106 | FcRefAdd (&skip->ref, nref); |
1045 | 106 | } |
1046 | | |
1047 | | void |
1048 | | FcDirCacheUnload (FcCache *cache) |
1049 | 106 | { |
1050 | 106 | FcCacheObjectDereference (cache); |
1051 | 106 | } |
1052 | | |
1053 | | static FcBool |
1054 | | FcDirCacheMapHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, struct timeval *latest_cache_mtime, void *closure) |
1055 | 105 | { |
1056 | 105 | FcCache *cache = FcDirCacheMapFd (config, fd, fd_stat, dir_stat); |
1057 | 105 | struct timeval cache_mtime, zero_mtime = { 0, 0 }, dir_mtime; |
1058 | | |
1059 | 105 | if (!cache) |
1060 | 0 | return FcFalse; |
1061 | 105 | cache_mtime.tv_sec = fd_stat->st_mtime; |
1062 | 105 | dir_mtime.tv_sec = dir_stat->st_mtime; |
1063 | 105 | #ifdef HAVE_STRUCT_STAT_ST_MTIM |
1064 | 105 | cache_mtime.tv_usec = fd_stat->st_mtim.tv_nsec / 1000; |
1065 | 105 | dir_mtime.tv_usec = dir_stat->st_mtim.tv_nsec / 1000; |
1066 | | #else |
1067 | | cache_mtime.tv_usec = 0; |
1068 | | dir_mtime.tv_usec = 0; |
1069 | | #endif |
1070 | | /* special take care of OSTree */ |
1071 | 105 | if (!timercmp (&zero_mtime, &dir_mtime, !=)) { |
1072 | 0 | if (!timercmp (&zero_mtime, &cache_mtime, !=)) { |
1073 | 0 | if (*((FcCache **)closure)) |
1074 | 0 | FcDirCacheUnload (*((FcCache **)closure)); |
1075 | 0 | } else if (*((FcCache **)closure) && !timercmp (&zero_mtime, latest_cache_mtime, !=)) { |
1076 | 0 | FcDirCacheUnload (cache); |
1077 | 0 | return FcFalse; |
1078 | 0 | } else if (timercmp (latest_cache_mtime, &cache_mtime, <)) { |
1079 | 0 | if (*((FcCache **)closure)) |
1080 | 0 | FcDirCacheUnload (*((FcCache **)closure)); |
1081 | 0 | } |
1082 | 105 | } else if (timercmp (latest_cache_mtime, &cache_mtime, <)) { |
1083 | 105 | if (*((FcCache **)closure)) |
1084 | 0 | FcDirCacheUnload (*((FcCache **)closure)); |
1085 | 105 | } else { |
1086 | 0 | FcDirCacheUnload (cache); |
1087 | 0 | return FcFalse; |
1088 | 0 | } |
1089 | 105 | latest_cache_mtime->tv_sec = cache_mtime.tv_sec; |
1090 | 105 | latest_cache_mtime->tv_usec = cache_mtime.tv_usec; |
1091 | 105 | *((FcCache **)closure) = cache; |
1092 | 105 | return FcTrue; |
1093 | 105 | } |
1094 | | |
1095 | | FcCache * |
1096 | | FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file) |
1097 | 318 | { |
1098 | 318 | FcCache *cache = NULL; |
1099 | | |
1100 | 318 | config = FcConfigReference (config); |
1101 | 318 | if (!config) |
1102 | 0 | return NULL; |
1103 | 318 | if (!FcDirCacheProcess (config, dir, |
1104 | 318 | FcDirCacheMapHelper, |
1105 | 318 | &cache, cache_file)) |
1106 | 213 | cache = NULL; |
1107 | | |
1108 | 318 | FcConfigDestroy (config); |
1109 | | |
1110 | 318 | return cache; |
1111 | 318 | } |
1112 | | |
1113 | | FcCache * |
1114 | | FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) |
1115 | 0 | { |
1116 | 0 | int fd; |
1117 | 0 | FcCache *cache = NULL; |
1118 | 0 | struct stat my_file_stat; |
1119 | 0 | FcConfig *config; |
1120 | |
|
1121 | 0 | if (!file_stat) |
1122 | 0 | file_stat = &my_file_stat; |
1123 | 0 | config = FcConfigReference (NULL); |
1124 | 0 | if (!config) |
1125 | 0 | return NULL; |
1126 | 0 | fd = FcDirCacheOpenFile (cache_file, file_stat); |
1127 | 0 | if (fd >= 0) { |
1128 | 0 | cache = FcDirCacheMapFd (config, fd, file_stat, NULL); |
1129 | 0 | close (fd); |
1130 | 0 | } |
1131 | 0 | FcConfigDestroy (config); |
1132 | |
|
1133 | 0 | return cache; |
1134 | 0 | } |
1135 | | |
1136 | | static int |
1137 | | FcDirChecksum (struct stat *statb) |
1138 | 1 | { |
1139 | 1 | int ret = (int)statb->st_mtime; |
1140 | 1 | char *endptr; |
1141 | 1 | char *source_date_epoch; |
1142 | 1 | unsigned long long epoch; |
1143 | | |
1144 | 1 | source_date_epoch = getenv ("SOURCE_DATE_EPOCH"); |
1145 | 1 | if (source_date_epoch) { |
1146 | 0 | errno = 0; |
1147 | 0 | epoch = strtoull (source_date_epoch, &endptr, 10); |
1148 | |
|
1149 | 0 | if (endptr == source_date_epoch) |
1150 | 0 | fprintf (stderr, |
1151 | 0 | "Fontconfig: SOURCE_DATE_EPOCH invalid\n"); |
1152 | 0 | else if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0)) || (errno != 0 && epoch == 0)) |
1153 | 0 | fprintf (stderr, |
1154 | 0 | "Fontconfig: SOURCE_DATE_EPOCH: strtoull: %s: %" FC_UINT64_FORMAT "\n", |
1155 | 0 | strerror (errno), epoch); |
1156 | 0 | else if (*endptr != '\0') |
1157 | 0 | fprintf (stderr, |
1158 | 0 | "Fontconfig: SOURCE_DATE_EPOCH has trailing garbage\n"); |
1159 | 0 | else if (epoch > ULONG_MAX) |
1160 | 0 | fprintf (stderr, |
1161 | 0 | "Fontconfig: SOURCE_DATE_EPOCH must be <= %lu but saw: %" FC_UINT64_FORMAT "\n", |
1162 | 0 | ULONG_MAX, epoch); |
1163 | 0 | else if (epoch < ret) |
1164 | | /* Only override if directory is newer */ |
1165 | 0 | ret = (int)epoch; |
1166 | 0 | } |
1167 | | |
1168 | 1 | return ret; |
1169 | 1 | } |
1170 | | |
1171 | | static int64_t |
1172 | | FcDirChecksumNano (struct stat *statb) |
1173 | 1 | { |
1174 | 1 | #ifdef HAVE_STRUCT_STAT_ST_MTIM |
1175 | | /* No nanosecond component to parse */ |
1176 | 1 | if (getenv ("SOURCE_DATE_EPOCH")) |
1177 | 0 | return 0; |
1178 | 1 | return statb->st_mtim.tv_nsec; |
1179 | | #else |
1180 | | return 0; |
1181 | | #endif |
1182 | 1 | } |
1183 | | |
1184 | | /* |
1185 | | * Validate a cache file by reading the header and checking |
1186 | | * the magic number and the size field |
1187 | | */ |
1188 | | static FcBool |
1189 | | FcDirCacheValidateHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, struct timeval *latest_cache_mtime, void *closure FC_UNUSED) |
1190 | 0 | { |
1191 | 0 | FcBool ret = FcTrue; |
1192 | 0 | FcCache c; |
1193 | |
|
1194 | 0 | if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache)) |
1195 | 0 | ret = FcFalse; |
1196 | 0 | else if (c.magic != FC_CACHE_MAGIC_MMAP) |
1197 | 0 | ret = FcFalse; |
1198 | 0 | else if (c.version < FC_CACHE_VERSION_NUMBER) |
1199 | 0 | ret = FcFalse; |
1200 | 0 | else if (fd_stat->st_size != c.size) |
1201 | 0 | ret = FcFalse; |
1202 | 0 | else if (c.checksum != FcDirChecksum (dir_stat)) |
1203 | 0 | ret = FcFalse; |
1204 | 0 | #ifdef HAVE_STRUCT_STAT_ST_MTIM |
1205 | 0 | else if (c.checksum_nano != FcDirChecksumNano (dir_stat)) |
1206 | 0 | ret = FcFalse; |
1207 | 0 | #endif |
1208 | 0 | return ret; |
1209 | 0 | } |
1210 | | |
1211 | | static FcBool |
1212 | | FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config) |
1213 | 0 | { |
1214 | 0 | return FcDirCacheProcess (config, dir, |
1215 | 0 | FcDirCacheValidateHelper, |
1216 | 0 | NULL, NULL); |
1217 | 0 | } |
1218 | | |
1219 | | FcBool |
1220 | | FcDirCacheValid (const FcChar8 *dir) |
1221 | 0 | { |
1222 | 0 | FcConfig *config; |
1223 | 0 | FcBool ret; |
1224 | |
|
1225 | 0 | config = FcConfigReference (NULL); |
1226 | 0 | if (!config) |
1227 | 0 | return FcFalse; |
1228 | | |
1229 | 0 | ret = FcDirCacheValidConfig (dir, config); |
1230 | 0 | FcConfigDestroy (config); |
1231 | |
|
1232 | 0 | return ret; |
1233 | 0 | } |
1234 | | |
1235 | | /* |
1236 | | * Build a cache structure from the given contents |
1237 | | */ |
1238 | | FcCache * |
1239 | | FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs) |
1240 | 1 | { |
1241 | 1 | FcSerialize *serialize = FcSerializeCreate(); |
1242 | 1 | FcCache *cache; |
1243 | 1 | int i; |
1244 | 1 | FcChar8 *dir_serialize; |
1245 | 1 | intptr_t *dirs_serialize; |
1246 | 1 | FcFontSet *set_serialize; |
1247 | | |
1248 | 1 | if (!serialize) |
1249 | 0 | return NULL; |
1250 | | /* |
1251 | | * Space for cache structure |
1252 | | */ |
1253 | 1 | FcSerializeReserve (serialize, sizeof (FcCache)); |
1254 | | /* |
1255 | | * Directory name |
1256 | | */ |
1257 | 1 | if (!FcStrSerializeAlloc (serialize, dir)) |
1258 | 0 | goto bail1; |
1259 | | /* |
1260 | | * Subdirs |
1261 | | */ |
1262 | 1 | FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *)); |
1263 | 1 | for (i = 0; i < dirs->num; i++) |
1264 | 0 | if (!FcStrSerializeAlloc (serialize, dirs->strs[i])) |
1265 | 0 | goto bail1; |
1266 | | |
1267 | | /* |
1268 | | * Patterns |
1269 | | */ |
1270 | 1 | if (!FcFontSetSerializeAlloc (serialize, set)) |
1271 | 0 | goto bail1; |
1272 | | |
1273 | | /* Serialize layout complete. Now allocate space and fill it */ |
1274 | 1 | cache = malloc (serialize->size); |
1275 | 1 | if (!cache) |
1276 | 0 | goto bail1; |
1277 | | /* shut up valgrind */ |
1278 | 1 | memset (cache, 0, serialize->size); |
1279 | | |
1280 | 1 | serialize->linear = cache; |
1281 | | |
1282 | 1 | cache->magic = FC_CACHE_MAGIC_ALLOC; |
1283 | 1 | cache->version = FC_CACHE_VERSION_NUMBER; |
1284 | 1 | cache->size = serialize->size; |
1285 | 1 | cache->checksum = FcDirChecksum (dir_stat); |
1286 | 1 | cache->checksum_nano = FcDirChecksumNano (dir_stat); |
1287 | | |
1288 | | /* |
1289 | | * Serialize directory name |
1290 | | */ |
1291 | 1 | dir_serialize = FcStrSerialize (serialize, dir); |
1292 | 1 | if (!dir_serialize) |
1293 | 0 | goto bail2; |
1294 | 1 | cache->dir = FcPtrToOffset (cache, dir_serialize); |
1295 | | |
1296 | | /* |
1297 | | * Serialize sub dirs |
1298 | | */ |
1299 | 1 | dirs_serialize = FcSerializePtr (serialize, dirs); |
1300 | 1 | if (!dirs_serialize) |
1301 | 0 | goto bail2; |
1302 | 1 | cache->dirs = FcPtrToOffset (cache, dirs_serialize); |
1303 | 1 | cache->dirs_count = dirs->num; |
1304 | 1 | for (i = 0; i < dirs->num; i++) { |
1305 | 0 | FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]); |
1306 | 0 | if (!d_serialize) |
1307 | 0 | goto bail2; |
1308 | 0 | dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize); |
1309 | 0 | } |
1310 | | |
1311 | | /* |
1312 | | * Serialize font set |
1313 | | */ |
1314 | 1 | set_serialize = FcFontSetSerialize (serialize, set); |
1315 | 1 | if (!set_serialize) |
1316 | 0 | goto bail2; |
1317 | 1 | cache->set = FcPtrToOffset (cache, set_serialize); |
1318 | | |
1319 | 1 | FcSerializeDestroy (serialize); |
1320 | | |
1321 | 1 | FcCacheInsert (cache, NULL); |
1322 | | |
1323 | 1 | return cache; |
1324 | | |
1325 | 0 | bail2: |
1326 | 0 | free (cache); |
1327 | 0 | bail1: |
1328 | 0 | FcSerializeDestroy (serialize); |
1329 | 0 | return NULL; |
1330 | 0 | } |
1331 | | |
1332 | | FcCache * |
1333 | | FcDirCacheRebuild (FcCache *cache, struct stat *dir_stat, FcStrSet *dirs) |
1334 | 0 | { |
1335 | 0 | FcCache *newp; |
1336 | 0 | FcFontSet *set = FcFontSetDeserialize (FcCacheSet (cache)); |
1337 | 0 | const FcChar8 *dir = FcCacheDir (cache); |
1338 | |
|
1339 | 0 | newp = FcDirCacheBuild (set, dir, dir_stat, dirs); |
1340 | 0 | FcFontSetDestroy (set); |
1341 | |
|
1342 | 0 | return newp; |
1343 | 0 | } |
1344 | | |
1345 | | /* write serialized state to the cache file */ |
1346 | | FcBool |
1347 | | FcDirCacheWrite (FcCache *cache, FcConfig *config) |
1348 | 1 | { |
1349 | 1 | FcChar8 *dir = FcCacheDir (cache); |
1350 | 1 | FcChar8 cache_base[CACHEBASE_LEN]; |
1351 | 1 | FcChar8 *cache_hashed; |
1352 | 1 | int fd; |
1353 | 1 | FcAtomic *atomic; |
1354 | 1 | FcStrList *list; |
1355 | 1 | FcChar8 *cache_dir = NULL; |
1356 | 1 | FcChar8 *test_dir, *d = NULL; |
1357 | 1 | FcCacheSkip *skip; |
1358 | 1 | struct stat cache_stat; |
1359 | 1 | unsigned int magic; |
1360 | 1 | int written; |
1361 | 1 | const FcChar8 *sysroot = FcConfigGetSysRoot (config); |
1362 | 1 | FcStrSet *cpath; |
1363 | | |
1364 | | /* |
1365 | | * Write it to the first directory in the list which is writable |
1366 | | */ |
1367 | | |
1368 | 1 | cpath = FcStrSetCreateEx (FCSS_GROW_BY_64); |
1369 | 1 | if (!cpath) |
1370 | 0 | return FcFalse; |
1371 | 1 | list = FcStrListCreate (config->cacheDirs); |
1372 | 1 | if (!list) { |
1373 | 0 | FcStrSetDestroy (cpath); |
1374 | 0 | return FcFalse; |
1375 | 0 | } |
1376 | 1 | while ((test_dir = FcStrListNext (list))) { |
1377 | 1 | if (d) |
1378 | 0 | FcStrFree (d); |
1379 | 1 | if (sysroot) |
1380 | 0 | d = FcStrBuildFilename (sysroot, test_dir, NULL); |
1381 | 1 | else |
1382 | 1 | d = FcStrCopyFilename (test_dir); |
1383 | | |
1384 | 1 | if (access ((char *)d, W_OK) == 0) { |
1385 | 0 | cache_dir = FcStrCopyFilename (d); |
1386 | 0 | break; |
1387 | 1 | } else { |
1388 | | /* |
1389 | | * If the directory doesn't exist, try to create it |
1390 | | */ |
1391 | 1 | if (access ((char *)d, F_OK) == -1) { |
1392 | 1 | if (FcMakeDirectory (d)) { |
1393 | 1 | cache_dir = FcStrCopyFilename (d); |
1394 | | /* Create CACHEDIR.TAG */ |
1395 | 1 | FcDirCacheCreateTagFile (d); |
1396 | 1 | break; |
1397 | 1 | } |
1398 | 1 | } |
1399 | | /* |
1400 | | * Otherwise, try making it writable |
1401 | | */ |
1402 | 0 | else if (chmod ((char *)d, 0755) == 0) { |
1403 | 0 | cache_dir = FcStrCopyFilename (d); |
1404 | | /* Try to create CACHEDIR.TAG too */ |
1405 | 0 | FcDirCacheCreateTagFile (d); |
1406 | 0 | break; |
1407 | 0 | } |
1408 | | /* Record a path that was supposed to be a cache directory */ |
1409 | 0 | FcStrSetAdd (cpath, d); |
1410 | 0 | } |
1411 | 1 | } |
1412 | 1 | if (!test_dir) { |
1413 | 0 | FcStrList *l; |
1414 | 0 | FcChar8 *s; |
1415 | |
|
1416 | 0 | l = FcStrListCreate (cpath); |
1417 | 0 | fprintf (stderr, "\nFontconfig error: No writable cache directories\n"); |
1418 | 0 | while ((s = FcStrListNext (l))) { |
1419 | 0 | fprintf (stderr, "\t%s\n", s); |
1420 | 0 | } |
1421 | 0 | FcStrListDone (l); |
1422 | 0 | } |
1423 | 1 | if (d) |
1424 | 1 | FcStrFree (d); |
1425 | 1 | FcStrSetDestroy (cpath); |
1426 | 1 | FcStrListDone (list); |
1427 | 1 | if (!cache_dir) |
1428 | 0 | return FcFalse; |
1429 | | |
1430 | 1 | FcDirCacheBasenameMD5 (config, dir, cache_base); |
1431 | 1 | cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL); |
1432 | 1 | FcStrFree (cache_dir); |
1433 | 1 | if (!cache_hashed) |
1434 | 0 | return FcFalse; |
1435 | | |
1436 | 1 | if (FcDebug() & FC_DBG_CACHE) |
1437 | 0 | printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n", |
1438 | 0 | dir, cache_hashed); |
1439 | | |
1440 | 1 | atomic = FcAtomicCreate ((FcChar8 *)cache_hashed); |
1441 | 1 | if (!atomic) |
1442 | 0 | goto bail1; |
1443 | | |
1444 | 1 | if (!FcAtomicLock (atomic)) |
1445 | 0 | goto bail3; |
1446 | | |
1447 | 1 | fd = FcOpen ((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666); |
1448 | 1 | if (fd == -1) |
1449 | 0 | goto bail4; |
1450 | | |
1451 | | /* Temporarily switch magic to MMAP while writing to file */ |
1452 | 1 | magic = cache->magic; |
1453 | 1 | if (magic != FC_CACHE_MAGIC_MMAP) |
1454 | 1 | cache->magic = FC_CACHE_MAGIC_MMAP; |
1455 | | |
1456 | | /* |
1457 | | * Write cache contents to file |
1458 | | */ |
1459 | 1 | written = write (fd, cache, cache->size); |
1460 | | |
1461 | | /* Switch magic back */ |
1462 | 1 | if (magic != FC_CACHE_MAGIC_MMAP) |
1463 | 1 | cache->magic = magic; |
1464 | | |
1465 | 1 | if (written != cache->size) { |
1466 | 0 | perror ("write cache"); |
1467 | 0 | goto bail5; |
1468 | 0 | } |
1469 | | |
1470 | 1 | close (fd); |
1471 | 1 | if (!FcAtomicReplaceOrig (atomic)) |
1472 | 0 | goto bail4; |
1473 | | |
1474 | | /* If the file is small, update the cache chain entry such that the |
1475 | | * new cache file is not read again. If it's large, we don't do that |
1476 | | * such that we reload it, using mmap, which is shared across processes. |
1477 | | */ |
1478 | 1 | if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat)) { |
1479 | 0 | lock_cache(); |
1480 | 0 | if ((skip = FcCacheFindByAddrUnlocked (cache))) { |
1481 | 0 | skip->cache_dev = cache_stat.st_dev; |
1482 | 0 | skip->cache_ino = cache_stat.st_ino; |
1483 | 0 | skip->cache_mtime = cache_stat.st_mtime; |
1484 | 0 | #ifdef HAVE_STRUCT_STAT_ST_MTIM |
1485 | 0 | skip->cache_mtime_nano = cache_stat.st_mtim.tv_nsec; |
1486 | | #else |
1487 | | skip->cache_mtime_nano = 0; |
1488 | | #endif |
1489 | 0 | } |
1490 | 0 | unlock_cache(); |
1491 | 0 | } |
1492 | | |
1493 | 1 | FcStrFree (cache_hashed); |
1494 | 1 | FcAtomicUnlock (atomic); |
1495 | 1 | FcAtomicDestroy (atomic); |
1496 | 1 | return FcTrue; |
1497 | | |
1498 | 0 | bail5: |
1499 | 0 | close (fd); |
1500 | 0 | bail4: |
1501 | 0 | FcAtomicUnlock (atomic); |
1502 | 0 | bail3: |
1503 | 0 | FcAtomicDestroy (atomic); |
1504 | 0 | bail1: |
1505 | 0 | FcStrFree (cache_hashed); |
1506 | 0 | return FcFalse; |
1507 | 0 | } |
1508 | | |
1509 | | FcBool |
1510 | | FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose) |
1511 | 0 | { |
1512 | 0 | DIR *d; |
1513 | 0 | struct dirent *ent; |
1514 | 0 | FcChar8 *dir; |
1515 | 0 | FcBool ret = FcTrue; |
1516 | 0 | FcBool remove; |
1517 | 0 | FcCache *cache; |
1518 | 0 | struct stat target_stat; |
1519 | 0 | const FcChar8 *sysroot; |
1520 | 0 | FcConfig *config; |
1521 | |
|
1522 | 0 | config = FcConfigReference (NULL); |
1523 | 0 | if (!config) |
1524 | 0 | return FcFalse; |
1525 | | /* FIXME: this API needs to support non-current FcConfig */ |
1526 | 0 | sysroot = FcConfigGetSysRoot (config); |
1527 | 0 | if (sysroot) |
1528 | 0 | dir = FcStrBuildFilename (sysroot, cache_dir, NULL); |
1529 | 0 | else |
1530 | 0 | dir = FcStrCopyFilename (cache_dir); |
1531 | 0 | if (!dir) { |
1532 | 0 | fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir); |
1533 | 0 | ret = FcFalse; |
1534 | 0 | goto bail; |
1535 | 0 | } |
1536 | 0 | if (access ((char *)dir, W_OK) != 0) { |
1537 | 0 | if (verbose || FcDebug() & FC_DBG_CACHE) |
1538 | 0 | printf ("%s: not cleaning %s cache directory\n", dir, |
1539 | 0 | access ((char *)dir, F_OK) == 0 ? "unwritable" : "non-existent"); |
1540 | 0 | goto bail0; |
1541 | 0 | } |
1542 | 0 | if (verbose || FcDebug() & FC_DBG_CACHE) |
1543 | 0 | printf ("%s: cleaning cache directory\n", dir); |
1544 | 0 | d = opendir ((char *)dir); |
1545 | 0 | if (!d) { |
1546 | 0 | perror ((char *)dir); |
1547 | 0 | ret = FcFalse; |
1548 | 0 | goto bail0; |
1549 | 0 | } |
1550 | 0 | while ((ent = readdir (d))) { |
1551 | 0 | FcChar8 *file_name; |
1552 | 0 | const FcChar8 *target_dir; |
1553 | |
|
1554 | 0 | if (ent->d_name[0] == '.') |
1555 | 0 | continue; |
1556 | | /* skip cache files for different architectures and */ |
1557 | | /* files which are not cache files at all */ |
1558 | 0 | if (strlen (ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) || |
1559 | 0 | strcmp (ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX)) |
1560 | 0 | continue; |
1561 | | |
1562 | 0 | file_name = FcStrBuildFilename (dir, (FcChar8 *)ent->d_name, NULL); |
1563 | 0 | if (!file_name) { |
1564 | 0 | fprintf (stderr, "Fontconfig error: %s: allocation failure\n", dir); |
1565 | 0 | ret = FcFalse; |
1566 | 0 | break; |
1567 | 0 | } |
1568 | 0 | remove = FcFalse; |
1569 | 0 | cache = FcDirCacheLoadFile (file_name, NULL); |
1570 | 0 | if (!cache) { |
1571 | 0 | if (verbose || FcDebug() & FC_DBG_CACHE) |
1572 | 0 | printf ("%s: invalid cache file: %s\n", dir, ent->d_name); |
1573 | 0 | remove = FcTrue; |
1574 | 0 | } else { |
1575 | 0 | FcChar8 *s; |
1576 | |
|
1577 | 0 | target_dir = FcCacheDir (cache); |
1578 | 0 | if (sysroot) |
1579 | 0 | s = FcStrBuildFilename (sysroot, target_dir, NULL); |
1580 | 0 | else |
1581 | 0 | s = FcStrdup (target_dir); |
1582 | 0 | if (stat ((char *)s, &target_stat) < 0) { |
1583 | 0 | if (verbose || FcDebug() & FC_DBG_CACHE) |
1584 | 0 | printf ("%s: %s: missing directory: %s \n", |
1585 | 0 | dir, ent->d_name, s); |
1586 | 0 | remove = FcTrue; |
1587 | 0 | } |
1588 | 0 | FcDirCacheUnload (cache); |
1589 | 0 | FcStrFree (s); |
1590 | 0 | } |
1591 | 0 | if (remove) { |
1592 | 0 | if (unlink ((char *)file_name) < 0) { |
1593 | 0 | perror ((char *)file_name); |
1594 | 0 | ret = FcFalse; |
1595 | 0 | } |
1596 | 0 | } |
1597 | 0 | FcStrFree (file_name); |
1598 | 0 | } |
1599 | |
|
1600 | 0 | closedir (d); |
1601 | 0 | bail0: |
1602 | 0 | FcStrFree (dir); |
1603 | 0 | bail: |
1604 | 0 | FcConfigDestroy (config); |
1605 | |
|
1606 | 0 | return ret; |
1607 | 0 | } |
1608 | | |
1609 | | int |
1610 | | FcDirCacheLock (const FcChar8 *dir, |
1611 | | FcConfig *config) |
1612 | 1 | { |
1613 | 1 | FcChar8 *cache_hashed = NULL; |
1614 | 1 | FcChar8 cache_base[CACHEBASE_LEN]; |
1615 | 1 | FcStrList *list; |
1616 | 1 | FcChar8 *cache_dir; |
1617 | 1 | const FcChar8 *sysroot = FcConfigGetSysRoot (config); |
1618 | 1 | int fd = -1; |
1619 | | |
1620 | 1 | FcDirCacheBasenameMD5 (config, dir, cache_base); |
1621 | 1 | list = FcStrListCreate (config->cacheDirs); |
1622 | 1 | if (!list) |
1623 | 0 | return -1; |
1624 | | |
1625 | 2 | while ((cache_dir = FcStrListNext (list))) { |
1626 | 1 | if (sysroot) |
1627 | 0 | cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL); |
1628 | 1 | else |
1629 | 1 | cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL); |
1630 | 1 | if (!cache_hashed) |
1631 | 0 | break; |
1632 | 1 | fd = FcOpen ((const char *)cache_hashed, O_RDWR); |
1633 | 1 | FcStrFree (cache_hashed); |
1634 | | /* No caches in that directory. simply retry with another one */ |
1635 | 1 | if (fd != -1) { |
1636 | | #if defined(_WIN32) |
1637 | | if (_locking (fd, _LK_LOCK, 1) == -1) |
1638 | | goto bail; |
1639 | | #else |
1640 | 0 | struct flock fl; |
1641 | |
|
1642 | 0 | fl.l_type = F_WRLCK; |
1643 | 0 | fl.l_whence = SEEK_SET; |
1644 | 0 | fl.l_start = 0; |
1645 | 0 | fl.l_len = 0; |
1646 | 0 | fl.l_pid = getpid(); |
1647 | 0 | if (fcntl (fd, F_SETLKW, &fl) == -1) |
1648 | 0 | goto bail; |
1649 | 0 | #endif |
1650 | 0 | break; |
1651 | 0 | } |
1652 | 1 | } |
1653 | 1 | FcStrListDone (list); |
1654 | 1 | return fd; |
1655 | 0 | bail: |
1656 | 0 | FcStrListDone (list); |
1657 | 0 | if (fd != -1) |
1658 | 0 | close (fd); |
1659 | 0 | return -1; |
1660 | 1 | } |
1661 | | |
1662 | | void |
1663 | | FcDirCacheUnlock (int fd) |
1664 | 1 | { |
1665 | 1 | if (fd != -1) { |
1666 | | #if defined(_WIN32) |
1667 | | _locking (fd, _LK_UNLCK, 1); |
1668 | | #else |
1669 | 0 | struct flock fl; |
1670 | |
|
1671 | 0 | fl.l_type = F_UNLCK; |
1672 | 0 | fl.l_whence = SEEK_SET; |
1673 | 0 | fl.l_start = 0; |
1674 | 0 | fl.l_len = 0; |
1675 | 0 | fl.l_pid = getpid(); |
1676 | 0 | fcntl (fd, F_SETLK, &fl); |
1677 | 0 | #endif |
1678 | 0 | close (fd); |
1679 | 0 | } |
1680 | 1 | } |
1681 | | |
1682 | | /* |
1683 | | * Hokey little macro trick to permit the definitions of C functions |
1684 | | * with the same name as CPP macros |
1685 | | */ |
1686 | | #define args1(x) (x) |
1687 | | #define args2(x, y) (x, y) |
1688 | | |
1689 | | const FcChar8 * |
1690 | | FcCacheDir args1 (const FcCache *c) |
1691 | 0 | { |
1692 | 0 | return FcCacheDir (c); |
1693 | 0 | } |
1694 | | |
1695 | | FcFontSet * |
1696 | | FcCacheCopySet args1 (const FcCache *c) |
1697 | 0 | { |
1698 | 0 | FcFontSet *old = FcCacheSet (c); |
1699 | 0 | FcFontSet *newp = FcFontSetCreate(); |
1700 | 0 | int i; |
1701 | |
|
1702 | 0 | if (!newp) |
1703 | 0 | return NULL; |
1704 | 0 | for (i = 0; i < old->nfont; i++) { |
1705 | 0 | FcPattern *font = FcFontSetFont (old, i); |
1706 | |
|
1707 | 0 | FcPatternReference (font); |
1708 | 0 | if (!FcFontSetAdd (newp, font)) { |
1709 | 0 | FcFontSetDestroy (newp); |
1710 | 0 | return NULL; |
1711 | 0 | } |
1712 | 0 | } |
1713 | 0 | return newp; |
1714 | 0 | } |
1715 | | |
1716 | | const FcChar8 * |
1717 | | FcCacheSubdir args2 (const FcCache *c, int i) |
1718 | 0 | { |
1719 | 0 | return FcCacheSubdir (c, i); |
1720 | 0 | } |
1721 | | |
1722 | | int |
1723 | | FcCacheNumSubdir args1 (const FcCache *c) |
1724 | 0 | { |
1725 | 0 | return c->dirs_count; |
1726 | 0 | } |
1727 | | |
1728 | | int |
1729 | | FcCacheNumFont args1 (const FcCache *c) |
1730 | 0 | { |
1731 | 0 | return FcCacheSet (c)->nfont; |
1732 | 0 | } |
1733 | | |
1734 | | FcBool |
1735 | | FcDirCacheCreateTagFile (const FcChar8 *cache_dir) |
1736 | 1 | { |
1737 | 1 | FcChar8 *cache_tag; |
1738 | 1 | int fd; |
1739 | 1 | FILE *fp; |
1740 | 1 | FcAtomic *atomic; |
1741 | 1 | static const FcChar8 cache_tag_contents[] = |
1742 | 1 | "Signature: 8a477f597d28d172789f06886806bc55\n" |
1743 | 1 | "# This file is a cache directory tag created by fontconfig.\n" |
1744 | 1 | "# For information about cache directory tags, see:\n" |
1745 | 1 | "# http://www.brynosaurus.com/cachedir/\n"; |
1746 | 1 | static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1; |
1747 | 1 | FcBool ret = FcFalse; |
1748 | | |
1749 | 1 | if (!cache_dir) |
1750 | 0 | return FcFalse; |
1751 | | |
1752 | 1 | if (access ((char *)cache_dir, W_OK) == 0) { |
1753 | | /* Create CACHEDIR.TAG */ |
1754 | 1 | cache_tag = FcStrBuildFilename (cache_dir, "CACHEDIR.TAG", NULL); |
1755 | 1 | if (!cache_tag) |
1756 | 0 | return FcFalse; |
1757 | 1 | atomic = FcAtomicCreate ((FcChar8 *)cache_tag); |
1758 | 1 | if (!atomic) |
1759 | 0 | goto bail1; |
1760 | 1 | if (!FcAtomicLock (atomic)) |
1761 | 0 | goto bail2; |
1762 | 1 | fd = FcOpen ((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644); |
1763 | 1 | if (fd == -1) |
1764 | 0 | goto bail3; |
1765 | 1 | fp = fdopen (fd, "wb"); |
1766 | 1 | if (fp == NULL) |
1767 | 0 | goto bail3; |
1768 | | |
1769 | 1 | fwrite (cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp); |
1770 | 1 | fclose (fp); |
1771 | | |
1772 | 1 | if (!FcAtomicReplaceOrig (atomic)) |
1773 | 0 | goto bail3; |
1774 | | |
1775 | 1 | ret = FcTrue; |
1776 | 1 | bail3: |
1777 | 1 | FcAtomicUnlock (atomic); |
1778 | 1 | bail2: |
1779 | 1 | FcAtomicDestroy (atomic); |
1780 | 1 | bail1: |
1781 | 1 | FcStrFree (cache_tag); |
1782 | 1 | } |
1783 | | |
1784 | 1 | if (FcDebug() & FC_DBG_CACHE) { |
1785 | 0 | if (ret) |
1786 | 0 | printf ("Created CACHEDIR.TAG at %s\n", cache_dir); |
1787 | 0 | else |
1788 | 0 | printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir); |
1789 | 0 | } |
1790 | | |
1791 | 1 | return ret; |
1792 | 1 | } |
1793 | | |
1794 | | void |
1795 | | FcCacheCreateTagFile (FcConfig *config) |
1796 | 0 | { |
1797 | 0 | FcChar8 *cache_dir = NULL, *d = NULL; |
1798 | 0 | FcStrList *list; |
1799 | 0 | const FcChar8 *sysroot; |
1800 | |
|
1801 | 0 | config = FcConfigReference (config); |
1802 | 0 | if (!config) |
1803 | 0 | return; |
1804 | 0 | sysroot = FcConfigGetSysRoot (config); |
1805 | |
|
1806 | 0 | list = FcConfigGetCacheDirs (config); |
1807 | 0 | if (!list) |
1808 | 0 | goto bail; |
1809 | | |
1810 | 0 | while ((cache_dir = FcStrListNext (list))) { |
1811 | 0 | if (d) |
1812 | 0 | FcStrFree (d); |
1813 | 0 | if (sysroot) |
1814 | 0 | d = FcStrBuildFilename (sysroot, cache_dir, NULL); |
1815 | 0 | else |
1816 | 0 | d = FcStrCopyFilename (cache_dir); |
1817 | 0 | if (FcDirCacheCreateTagFile (d)) |
1818 | 0 | break; |
1819 | 0 | } |
1820 | 0 | if (d) |
1821 | 0 | FcStrFree (d); |
1822 | 0 | FcStrListDone (list); |
1823 | 0 | bail: |
1824 | 0 | FcConfigDestroy (config); |
1825 | 0 | } |
1826 | | |
1827 | | #define __fccache__ |
1828 | | #include "fcaliastail.h" |
1829 | | #undef __fccache__ |