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