]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * fontconfig/fc-cache/fc-cache.c | |
3 | * | |
4 | * Copyright © 2002 Keith Packard | |
5 | * | |
6 | * Permission to use, copy, modify, distribute, and sell this software and its | |
7 | * documentation for any purpose is hereby granted without fee, provided that | |
8 | * the above copyright notice appear in all copies and that both that | |
9 | * copyright notice and this permission notice appear in supporting | |
10 | * documentation, and that the name of the author(s) not be used in | |
11 | * advertising or publicity pertaining to distribution of the software without | |
12 | * specific, written prior permission. The authors make no | |
13 | * representations about the suitability of this software for any purpose. It | |
14 | * is provided "as is" without express or implied warranty. | |
15 | * | |
16 | * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
18 | * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
19 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
20 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
21 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
22 | * PERFORMANCE OF THIS SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "../src/fcarch.h" | |
26 | ||
27 | #ifdef HAVE_CONFIG_H | |
28 | #include <config.h> | |
29 | #else | |
30 | #ifdef linux | |
31 | #define HAVE_GETOPT_LONG 1 | |
32 | #endif | |
33 | #define HAVE_GETOPT 1 | |
34 | #endif | |
35 | ||
36 | #include <fontconfig/fontconfig.h> | |
37 | #include <stdio.h> | |
38 | #include <stdlib.h> | |
39 | #include <unistd.h> | |
40 | #include <sys/types.h> | |
41 | #include <sys/stat.h> | |
42 | #include <errno.h> | |
43 | #include <fcntl.h> | |
44 | #include <dirent.h> | |
45 | #include <string.h> | |
46 | ||
47 | #if defined (_WIN32) | |
48 | #define STRICT | |
49 | #include <windows.h> | |
50 | #define sleep(x) Sleep((x) * 1000) | |
51 | #undef STRICT | |
52 | #endif | |
53 | ||
54 | #ifndef O_BINARY | |
55 | #define O_BINARY 0 | |
56 | #endif | |
57 | ||
58 | #ifndef HAVE_GETOPT | |
59 | #define HAVE_GETOPT 0 | |
60 | #endif | |
61 | #ifndef HAVE_GETOPT_LONG | |
62 | #define HAVE_GETOPT_LONG 0 | |
63 | #endif | |
64 | ||
65 | #if HAVE_GETOPT_LONG | |
66 | #undef _GNU_SOURCE | |
67 | #define _GNU_SOURCE | |
68 | #include <getopt.h> | |
69 | const struct option longopts[] = { | |
70 | {"force", 0, 0, 'f'}, | |
71 | {"really-force", 0, 0, 'r'}, | |
72 | {"root", 1, 0, 'R'}, | |
73 | {"system-only", 0, 0, 's'}, | |
74 | {"version", 0, 0, 'V'}, | |
75 | {"verbose", 0, 0, 'v'}, | |
76 | {"help", 0, 0, 'h'}, | |
77 | {NULL,0,0,0}, | |
78 | }; | |
79 | #else | |
80 | #if HAVE_GETOPT | |
81 | extern char *optarg; | |
82 | extern int optind, opterr, optopt; | |
83 | #endif | |
84 | #endif | |
85 | ||
86 | static void | |
87 | usage (char *program, int error) | |
88 | { | |
89 | FILE *file = error ? stderr : stdout; | |
90 | #if HAVE_GETOPT_LONG | |
91 | fprintf (file, "usage: %s [-frRsvVh] [--force|--really-force] [--root <root>] [--system-only] [--verbose] [--version] [--help] [dirs]\n", | |
92 | program); | |
93 | #else | |
94 | fprintf (file, "usage: %s [-frRsvVh] [dirs]\n", | |
95 | program); | |
96 | #endif | |
97 | fprintf (file, "Build font information caches in [dirs]\n" | |
98 | "(all directories in font configuration by default).\n"); | |
99 | fprintf (file, "\n"); | |
100 | #if HAVE_GETOPT_LONG | |
101 | fprintf (file, " -f, --force scan directories with apparently valid caches\n"); | |
102 | fprintf (file, " -r, --really-force erase all existing caches, then rescan\n"); | |
103 | fprintf (file, " -R, --root <root> change to <root> before loading files\n"); | |
104 | fprintf (file, " -s, --system-only scan system-wide directories only\n"); | |
105 | fprintf (file, " -v, --verbose display status information while busy\n"); | |
106 | fprintf (file, " -V, --version display font config version and exit\n"); | |
107 | fprintf (file, " -h, --help display this help and exit\n"); | |
108 | #else | |
109 | fprintf (file, " -f (force) scan directories with apparently valid caches\n"); | |
110 | fprintf (file, " -r, (really force) erase all existing caches, then rescan\n"); | |
111 | fprintf (file, " -R <root> (root) change to <root> before loading files\n"); | |
112 | fprintf (file, " -s (system) scan system-wide directories only\n"); | |
113 | fprintf (file, " -v (verbose) display status information while busy\n"); | |
114 | fprintf (file, " -V (version) display font config version and exit\n"); | |
115 | fprintf (file, " -h (help) display this help and exit\n"); | |
116 | #endif | |
117 | exit (error); | |
118 | } | |
119 | ||
120 | static FcStrSet *processed_dirs; | |
121 | ||
122 | static int | |
123 | scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed) | |
124 | { | |
125 | int ret = 0; | |
126 | const FcChar8 *dir; | |
127 | FcStrSet *subdirs; | |
128 | FcStrList *sublist; | |
129 | FcCache *cache; | |
130 | struct stat statb; | |
131 | FcBool was_valid; | |
132 | int i; | |
133 | ||
134 | /* | |
135 | * Now scan all of the directories into separate databases | |
136 | * and write out the results | |
137 | */ | |
138 | while ((dir = FcStrListNext (list))) | |
139 | { | |
140 | if (verbose) | |
141 | { | |
142 | printf ("%s: ", dir); | |
143 | fflush (stdout); | |
144 | } | |
145 | ||
146 | if (!dir) | |
147 | { | |
148 | if (verbose) | |
149 | printf ("skipping, no such directory\n"); | |
150 | continue; | |
151 | } | |
152 | ||
153 | if (FcStrSetMember (processed_dirs, dir)) | |
154 | { | |
155 | if (verbose) | |
156 | printf ("skipping, looped directory detected\n"); | |
157 | continue; | |
158 | } | |
159 | ||
160 | if (FcStat (config, dir, &statb) == -1) | |
161 | { | |
162 | switch (errno) { | |
163 | case ENOENT: | |
164 | case ENOTDIR: | |
165 | if (verbose) | |
166 | printf ("skipping, no such directory\n"); | |
167 | break; | |
168 | default: | |
169 | fprintf (stderr, "\"%s\": ", dir); | |
170 | perror (""); | |
171 | ret++; | |
172 | break; | |
173 | } | |
174 | continue; | |
175 | } | |
176 | ||
177 | if (!S_ISDIR (statb.st_mode)) | |
178 | { | |
179 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
180 | continue; | |
181 | } | |
182 | ||
183 | if (really_force) | |
184 | FcDirCacheUnlink (dir, config); | |
185 | ||
186 | cache = NULL; | |
187 | was_valid = FcFalse; | |
188 | if (!force) { | |
189 | cache = FcDirCacheLoad (dir, config, NULL); | |
190 | if (cache) | |
191 | was_valid = FcTrue; | |
192 | } | |
193 | ||
194 | if (!cache) | |
195 | { | |
196 | (*changed)++; | |
197 | cache = FcDirCacheRead (dir, FcTrue, config); | |
198 | if (!cache) | |
199 | { | |
200 | fprintf (stderr, "%s: error scanning\n", dir); | |
201 | ret++; | |
202 | continue; | |
203 | } | |
204 | } | |
205 | ||
206 | if (was_valid) | |
207 | { | |
208 | if (verbose) | |
209 | printf ("skipping, existing cache is valid: %d fonts, %d dirs\n", | |
210 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); | |
211 | } | |
212 | else | |
213 | { | |
214 | if (verbose) | |
215 | printf ("caching, new cache contents: %d fonts, %d dirs\n", | |
216 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); | |
217 | ||
218 | if (!FcDirCacheValid (dir)) | |
219 | { | |
220 | fprintf (stderr, "%s: failed to write cache\n", dir); | |
221 | (void) FcDirCacheUnlink (dir, config); | |
222 | ret++; | |
223 | } | |
224 | } | |
225 | ||
226 | subdirs = FcStrSetCreate (); | |
227 | if (!subdirs) | |
228 | { | |
229 | fprintf (stderr, "%s: Can't create subdir set\n", dir); | |
230 | ret++; | |
231 | FcDirCacheUnload (cache); | |
232 | continue; | |
233 | } | |
234 | for (i = 0; i < FcCacheNumSubdir (cache); i++) | |
235 | FcStrSetAdd (subdirs, FcCacheSubdir (cache, i)); | |
236 | ||
237 | FcDirCacheUnload (cache); | |
238 | ||
239 | sublist = FcStrListCreate (subdirs); | |
240 | FcStrSetDestroy (subdirs); | |
241 | if (!sublist) | |
242 | { | |
243 | fprintf (stderr, "%s: Can't create subdir list\n", dir); | |
244 | ret++; | |
245 | continue; | |
246 | } | |
247 | FcStrSetAdd (processed_dirs, dir); | |
248 | ret += scanDirs (sublist, config, force, really_force, verbose, changed); | |
249 | } | |
250 | FcStrListDone (list); | |
251 | return ret; | |
252 | } | |
253 | ||
254 | static FcBool | |
255 | cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) | |
256 | { | |
257 | DIR *d; | |
258 | struct dirent *ent; | |
259 | FcChar8 *fullDir; | |
260 | FcChar8 *checkDir; | |
261 | FcBool ret = FcTrue; | |
262 | FcBool remove; | |
263 | FcCache *cache; | |
264 | struct stat target_stat; | |
265 | ||
266 | fullDir = FcConfigGetRootPlus (config, dir); | |
267 | if (fullDir) | |
268 | checkDir = fullDir; | |
269 | else | |
270 | checkDir = dir; | |
271 | ||
272 | if (access ((char *) checkDir, W_OK) != 0) | |
273 | { | |
274 | if (verbose) | |
275 | printf ("%s: not cleaning %s cache directory\n", dir, | |
276 | access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent"); | |
277 | goto done; | |
278 | } | |
279 | if (verbose) | |
280 | printf ("%s: cleaning cache directory\n", dir); | |
281 | d = opendir ((char *) checkDir); | |
282 | if (!d) | |
283 | { | |
284 | perror ((char *) dir); | |
285 | ret = FcFalse; | |
286 | goto done; | |
287 | } | |
288 | while ((ent = readdir (d))) | |
289 | { | |
290 | FcChar8 *file_name; | |
291 | const FcChar8 *target_dir; | |
292 | ||
293 | if (ent->d_name[0] == '.') | |
294 | continue; | |
295 | /* skip cache files for different architectures and */ | |
296 | /* files which are not cache files at all */ | |
297 | if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) || | |
298 | strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX)) | |
299 | continue; | |
300 | ||
301 | file_name = FcStrPathPlus (dir, (const FcChar8 *) ent->d_name, NULL); | |
302 | if (!file_name) | |
303 | { | |
304 | fprintf (stderr, "%s: allocation failure\n", dir); | |
305 | ret = FcFalse; | |
306 | break; | |
307 | } | |
308 | remove = FcFalse; | |
309 | cache = FcDirCacheLoadFile2 (file_name, config, NULL); | |
310 | if (!cache) | |
311 | { | |
312 | if (verbose) | |
313 | printf ("%s: invalid cache file: %s\n", dir, ent->d_name); | |
314 | remove = FcTrue; | |
315 | } | |
316 | else | |
317 | { | |
318 | target_dir = FcCacheDir (cache); | |
319 | if (FcStat (config, target_dir, &target_stat) < 0) | |
320 | { | |
321 | if (verbose) | |
322 | printf ("%s: %s: missing directory: %s \n", | |
323 | dir, ent->d_name, target_dir); | |
324 | remove = FcTrue; | |
325 | } | |
326 | } | |
327 | if (remove) | |
328 | { | |
329 | FcChar8 *unlink_file = FcConfigGetRootPlus (config, file_name); | |
330 | if (!unlink_file) | |
331 | unlink_file = file_name; | |
332 | if (unlink ((char *) unlink_file) < 0) | |
333 | { | |
334 | perror ((char *) unlink_file); | |
335 | ret = FcFalse; | |
336 | } | |
337 | if (unlink_file != file_name) | |
338 | FcStrFree (unlink_file); | |
339 | } | |
340 | FcDirCacheUnload (cache); | |
341 | FcStrFree (file_name); | |
342 | } | |
343 | ||
344 | closedir (d); | |
345 | done: | |
346 | if (fullDir) | |
347 | FcStrFree (fullDir); | |
348 | return ret; | |
349 | } | |
350 | ||
351 | static FcBool | |
352 | cleanCacheDirectories (FcConfig *config, FcBool verbose) | |
353 | { | |
354 | FcStrList *cache_dirs = FcConfigGetCacheDirs (config); | |
355 | FcChar8 *cache_dir; | |
356 | FcBool ret = FcTrue; | |
357 | ||
358 | if (!cache_dirs) | |
359 | return FcFalse; | |
360 | while ((cache_dir = FcStrListNext (cache_dirs))) | |
361 | { | |
362 | if (!cleanCacheDirectory (config, cache_dir, verbose)) | |
363 | { | |
364 | ret = FcFalse; | |
365 | break; | |
366 | } | |
367 | } | |
368 | FcStrListDone (cache_dirs); | |
369 | return ret; | |
370 | } | |
371 | ||
372 | int | |
373 | main (int argc, char **argv) | |
374 | { | |
375 | FcStrSet *dirs; | |
376 | FcStrList *list; | |
377 | FcBool verbose = FcFalse; | |
378 | FcBool force = FcFalse; | |
379 | FcBool really_force = FcFalse; | |
380 | FcBool systemOnly = FcFalse; | |
381 | FcConfig *config; | |
382 | const char *rootDir; | |
383 | int i; | |
384 | int changed; | |
385 | int ret; | |
386 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
387 | int c; | |
388 | ||
389 | #if HAVE_GETOPT_LONG | |
390 | while ((c = getopt_long (argc, argv, "frR:sVvh", longopts, NULL)) != -1) | |
391 | #else | |
392 | while ((c = getopt (argc, argv, "frR:sVvh")) != -1) | |
393 | #endif | |
394 | { | |
395 | switch (c) { | |
396 | case 'r': | |
397 | really_force = FcTrue; | |
398 | /* fall through */ | |
399 | case 'f': | |
400 | force = FcTrue; | |
401 | break; | |
402 | case 'R': | |
403 | rootDir = optarg; | |
404 | break; | |
405 | case 's': | |
406 | systemOnly = FcTrue; | |
407 | break; | |
408 | case 'V': | |
409 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
410 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
411 | exit (0); | |
412 | case 'v': | |
413 | verbose = FcTrue; | |
414 | break; | |
415 | case 'h': | |
416 | usage (argv[0], 0); | |
417 | default: | |
418 | usage (argv[0], 1); | |
419 | } | |
420 | } | |
421 | i = optind; | |
422 | #else | |
423 | i = 1; | |
424 | #endif | |
425 | ||
426 | if (systemOnly) | |
427 | FcConfigEnableHome (FcFalse); | |
428 | config = FcInitLoadConfig (); | |
429 | if (!config) | |
430 | { | |
431 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); | |
432 | return 1; | |
433 | } | |
434 | FcConfigSetCurrent (config); | |
435 | FcConfigSetRoot (config, (const FcChar8 *) rootDir); | |
436 | ||
437 | if (argv[i]) | |
438 | { | |
439 | dirs = FcStrSetCreate (); | |
440 | if (!dirs) | |
441 | { | |
442 | fprintf (stderr, "%s: Can't create list of directories\n", | |
443 | argv[0]); | |
444 | return 1; | |
445 | } | |
446 | while (argv[i]) | |
447 | { | |
448 | if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i])) | |
449 | { | |
450 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); | |
451 | return 1; | |
452 | } | |
453 | i++; | |
454 | } | |
455 | list = FcStrListCreate (dirs); | |
456 | FcStrSetDestroy (dirs); | |
457 | } | |
458 | else | |
459 | list = FcConfigGetConfigDirs (config); | |
460 | ||
461 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
462 | fprintf(stderr, "Cannot malloc\n"); | |
463 | return 1; | |
464 | } | |
465 | ||
466 | changed = 0; | |
467 | ret = scanDirs (list, config, force, really_force, verbose, &changed); | |
468 | ||
469 | FcStrSetDestroy (processed_dirs); | |
470 | ||
471 | cleanCacheDirectories (config, verbose); | |
472 | ||
473 | /* | |
474 | * Now we need to sleep a second (or two, to be extra sure), to make | |
475 | * sure that timestamps for changes after this run of fc-cache are later | |
476 | * then any timestamps we wrote. We don't use gettimeofday() because | |
477 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
478 | * library, and there aren't any signals flying around here. | |
479 | */ | |
480 | FcConfigDestroy (config); | |
481 | FcFini (); | |
482 | if (changed) | |
483 | sleep (2); | |
484 | if (verbose) | |
485 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
486 | return ret; | |
487 | } |