]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $ | |
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 Keith Packard not be used in | |
11 | * advertising or publicity pertaining to distribution of the software without | |
12 | * specific, written prior permission. Keith Packard makes 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 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
18 | * EVENT SHALL KEITH PACKARD 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 "../fc-arch/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 | {"system-only", 0, 0, 's'}, | |
73 | {"version", 0, 0, 'V'}, | |
74 | {"verbose", 0, 0, 'v'}, | |
75 | {"help", 0, 0, '?'}, | |
76 | {NULL,0,0,0}, | |
77 | }; | |
78 | #else | |
79 | #if HAVE_GETOPT | |
80 | extern char *optarg; | |
81 | extern int optind, opterr, optopt; | |
82 | #endif | |
83 | #endif | |
84 | ||
85 | static void | |
86 | usage (char *program) | |
87 | { | |
88 | #if HAVE_GETOPT_LONG | |
89 | fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", | |
90 | program); | |
91 | #else | |
92 | fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n", | |
93 | program); | |
94 | #endif | |
95 | fprintf (stderr, "Build font information caches in [dirs]\n" | |
96 | "(all directories in font configuration by default).\n"); | |
97 | fprintf (stderr, "\n"); | |
98 | #if HAVE_GETOPT_LONG | |
99 | fprintf (stderr, " -f, --force scan directories with apparently valid caches\n"); | |
100 | fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n"); | |
101 | fprintf (stderr, " -s, --system-only scan system-wide directories only\n"); | |
102 | fprintf (stderr, " -v, --verbose display status information while busy\n"); | |
103 | fprintf (stderr, " -V, --version display font config version and exit\n"); | |
104 | fprintf (stderr, " -?, --help display this help and exit\n"); | |
105 | #else | |
106 | fprintf (stderr, " -f (force) scan directories with apparently valid caches\n"); | |
107 | fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n"); | |
108 | fprintf (stderr, " -s (system) scan system-wide directories only\n"); | |
109 | fprintf (stderr, " -v (verbose) display status information while busy\n"); | |
110 | fprintf (stderr, " -V (version) display font config version and exit\n"); | |
111 | fprintf (stderr, " -? (help) display this help and exit\n"); | |
112 | #endif | |
113 | exit (1); | |
114 | } | |
115 | ||
116 | static FcStrSet *processed_dirs; | |
117 | ||
118 | static int | |
119 | scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose) | |
120 | { | |
121 | int ret = 0; | |
122 | const FcChar8 *dir; | |
123 | FcStrSet *subdirs; | |
124 | FcStrList *sublist; | |
125 | FcCache *cache; | |
126 | struct stat statb; | |
127 | FcBool was_valid; | |
128 | int i; | |
129 | ||
130 | /* | |
131 | * Now scan all of the directories into separate databases | |
132 | * and write out the results | |
133 | */ | |
134 | while ((dir = FcStrListNext (list))) | |
135 | { | |
136 | if (verbose) | |
137 | { | |
138 | printf ("%s: ", dir); | |
139 | fflush (stdout); | |
140 | } | |
141 | ||
142 | if (!dir) | |
143 | { | |
144 | if (verbose) | |
145 | printf ("skipping, no such directory\n"); | |
146 | continue; | |
147 | } | |
148 | ||
149 | if (FcStrSetMember (processed_dirs, dir)) | |
150 | { | |
151 | if (verbose) | |
152 | printf ("skipping, looped directory detected\n"); | |
153 | continue; | |
154 | } | |
155 | ||
156 | if (access ((char *) dir, W_OK) < 0) | |
157 | { | |
158 | switch (errno) { | |
159 | case ENOENT: | |
160 | case ENOTDIR: | |
161 | if (verbose) | |
162 | printf ("skipping, no such directory\n"); | |
163 | continue; | |
164 | case EACCES: | |
165 | case EROFS: | |
166 | /* That's ok, caches go to /var anyway. */ | |
167 | /* Ideally we'd do an access on the hashed_name. */ | |
168 | /* But we hid that behind an abstraction barrier. */ | |
169 | break; | |
170 | default: | |
171 | fprintf (stderr, "\"%s\": ", dir); | |
172 | perror (""); | |
173 | ret++; | |
174 | ||
175 | continue; | |
176 | } | |
177 | } | |
178 | if (stat ((char *) dir, &statb) == -1) | |
179 | { | |
180 | fprintf (stderr, "\"%s\": ", dir); | |
181 | perror (""); | |
182 | ret++; | |
183 | continue; | |
184 | } | |
185 | if (!S_ISDIR (statb.st_mode)) | |
186 | { | |
187 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
188 | continue; | |
189 | } | |
190 | ||
191 | if (really_force) | |
192 | FcDirCacheUnlink (dir, config); | |
193 | ||
194 | cache = NULL; | |
195 | was_valid = FcFalse; | |
196 | if (!force) { | |
197 | cache = FcDirCacheLoad (dir, config, NULL); | |
198 | if (cache) | |
199 | was_valid = FcTrue; | |
200 | } | |
201 | ||
202 | if (!cache) | |
203 | { | |
204 | cache = FcDirCacheRead (dir, FcTrue, config); | |
205 | if (!cache) | |
206 | { | |
207 | fprintf (stderr, "%s: error scanning\n", dir); | |
208 | ret++; | |
209 | continue; | |
210 | } | |
211 | } | |
212 | ||
213 | if (was_valid) | |
214 | { | |
215 | if (verbose) | |
216 | printf ("skipping, %d fonts, %d dirs\n", | |
217 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); | |
218 | } | |
219 | else | |
220 | { | |
221 | if (verbose) | |
222 | printf ("caching, %d fonts, %d dirs\n", | |
223 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); | |
224 | ||
225 | if (!FcDirCacheValid (dir)) | |
226 | { | |
227 | fprintf (stderr, "%s: failed to write cache\n", dir); | |
228 | (void) FcDirCacheUnlink (dir, config); | |
229 | ret++; | |
230 | } | |
231 | } | |
232 | ||
233 | subdirs = FcStrSetCreate (); | |
234 | if (!subdirs) | |
235 | { | |
236 | fprintf (stderr, "%s: Can't create subdir set\n", dir); | |
237 | ret++; | |
238 | FcDirCacheUnload (cache); | |
239 | continue; | |
240 | } | |
241 | for (i = 0; i < FcCacheNumSubdir (cache); i++) | |
242 | FcStrSetAdd (subdirs, FcCacheSubdir (cache, i)); | |
243 | ||
244 | FcDirCacheUnload (cache); | |
245 | ||
246 | sublist = FcStrListCreate (subdirs); | |
247 | FcStrSetDestroy (subdirs); | |
248 | if (!sublist) | |
249 | { | |
250 | fprintf (stderr, "%s: Can't create subdir list\n", dir); | |
251 | ret++; | |
252 | continue; | |
253 | } | |
254 | FcStrSetAdd (processed_dirs, dir); | |
255 | ret += scanDirs (sublist, config, force, really_force, verbose); | |
256 | } | |
257 | FcStrListDone (list); | |
258 | return ret; | |
259 | } | |
260 | ||
261 | static FcBool | |
262 | cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) | |
263 | { | |
264 | DIR *d; | |
265 | struct dirent *ent; | |
266 | FcChar8 *dir_base; | |
267 | FcBool ret = FcTrue; | |
268 | FcBool remove; | |
269 | FcCache *cache; | |
270 | struct stat file_stat; | |
271 | struct stat target_stat; | |
272 | ||
273 | dir_base = FcStrPlus (dir, (FcChar8 *) "/"); | |
274 | if (!dir_base) | |
275 | { | |
276 | fprintf (stderr, "%s: out of memory\n", dir); | |
277 | return FcFalse; | |
278 | } | |
279 | if (access ((char *) dir, W_OK|X_OK) != 0) | |
280 | { | |
281 | if (verbose) | |
282 | printf ("%s: not cleaning unwritable cache directory\n", dir); | |
283 | FcStrFree (dir_base); | |
284 | return FcTrue; | |
285 | } | |
286 | if (verbose) | |
287 | printf ("%s: cleaning cache directory\n", dir); | |
288 | d = opendir ((char *) dir); | |
289 | if (!d) | |
290 | { | |
291 | perror ((char *) dir); | |
292 | FcStrFree (dir_base); | |
293 | return FcFalse; | |
294 | } | |
295 | while ((ent = readdir (d))) | |
296 | { | |
297 | FcChar8 *file_name; | |
298 | const FcChar8 *target_dir; | |
299 | ||
300 | if (ent->d_name[0] == '.') | |
301 | continue; | |
302 | /* skip cache files for different architectures and */ | |
303 | /* files which are not cache files at all */ | |
304 | if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) || | |
305 | strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX)) | |
306 | continue; | |
307 | ||
308 | file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name); | |
309 | if (!file_name) | |
310 | { | |
311 | fprintf (stderr, "%s: allocation failure\n", dir); | |
312 | ret = FcFalse; | |
313 | break; | |
314 | } | |
315 | cache = FcDirCacheLoadFile (file_name, &file_stat); | |
316 | if (!cache) | |
317 | { | |
318 | fprintf (stderr, "%s: invalid cache file: %s\n", dir, ent->d_name); | |
319 | FcStrFree (file_name); | |
320 | ret = FcFalse; | |
321 | continue; | |
322 | } | |
323 | target_dir = FcCacheDir (cache); | |
324 | remove = FcFalse; | |
325 | if (stat ((char *) target_dir, &target_stat) < 0) | |
326 | { | |
327 | if (verbose) | |
328 | printf ("%s: %s: missing directory: %s \n", | |
329 | dir, ent->d_name, target_dir); | |
330 | remove = FcTrue; | |
331 | } | |
332 | else if (target_stat.st_mtime > file_stat.st_mtime) | |
333 | { | |
334 | if (verbose) | |
335 | printf ("%s: %s: cache outdated: %s\n", | |
336 | dir, ent->d_name, target_dir); | |
337 | remove = FcTrue; | |
338 | } | |
339 | if (remove) | |
340 | { | |
341 | if (unlink ((char *) file_name) < 0) | |
342 | { | |
343 | perror ((char *) file_name); | |
344 | ret = FcFalse; | |
345 | } | |
346 | } | |
347 | FcDirCacheUnload (cache); | |
348 | FcStrFree (file_name); | |
349 | } | |
350 | ||
351 | closedir (d); | |
352 | FcStrFree (dir_base); | |
353 | return ret; | |
354 | } | |
355 | ||
356 | static FcBool | |
357 | cleanCacheDirectories (FcConfig *config, FcBool verbose) | |
358 | { | |
359 | FcStrList *cache_dirs = FcConfigGetCacheDirs (config); | |
360 | FcChar8 *cache_dir; | |
361 | FcBool ret = FcTrue; | |
362 | ||
363 | if (!cache_dirs) | |
364 | return FcFalse; | |
365 | while ((cache_dir = FcStrListNext (cache_dirs))) | |
366 | { | |
367 | if (!cleanCacheDirectory (config, cache_dir, verbose)) | |
368 | { | |
369 | ret = FcFalse; | |
370 | break; | |
371 | } | |
372 | } | |
373 | FcStrListDone (cache_dirs); | |
374 | return ret; | |
375 | } | |
376 | ||
377 | int | |
378 | main (int argc, char **argv) | |
379 | { | |
380 | FcStrSet *dirs; | |
381 | FcStrList *list; | |
382 | FcBool verbose = FcFalse; | |
383 | FcBool force = FcFalse; | |
384 | FcBool really_force = FcFalse; | |
385 | FcBool systemOnly = FcFalse; | |
386 | FcConfig *config; | |
387 | int i; | |
388 | int ret; | |
389 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
390 | int c; | |
391 | ||
392 | #if HAVE_GETOPT_LONG | |
393 | while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) | |
394 | #else | |
395 | while ((c = getopt (argc, argv, "frsVv?")) != -1) | |
396 | #endif | |
397 | { | |
398 | switch (c) { | |
399 | case 'r': | |
400 | really_force = FcTrue; | |
401 | /* fall through */ | |
402 | case 'f': | |
403 | force = FcTrue; | |
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 | default: | |
416 | usage (argv[0]); | |
417 | } | |
418 | } | |
419 | i = optind; | |
420 | #else | |
421 | i = 1; | |
422 | #endif | |
423 | ||
424 | if (systemOnly) | |
425 | FcConfigEnableHome (FcFalse); | |
426 | config = FcInitLoadConfig (); | |
427 | if (!config) | |
428 | { | |
429 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); | |
430 | return 1; | |
431 | } | |
432 | FcConfigSetCurrent (config); | |
433 | ||
434 | if (argv[i]) | |
435 | { | |
436 | dirs = FcStrSetCreate (); | |
437 | if (!dirs) | |
438 | { | |
439 | fprintf (stderr, "%s: Can't create list of directories\n", | |
440 | argv[0]); | |
441 | return 1; | |
442 | } | |
443 | while (argv[i]) | |
444 | { | |
445 | if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i])) | |
446 | { | |
447 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); | |
448 | return 1; | |
449 | } | |
450 | i++; | |
451 | } | |
452 | list = FcStrListCreate (dirs); | |
453 | FcStrSetDestroy (dirs); | |
454 | } | |
455 | else | |
456 | list = FcConfigGetConfigDirs (config); | |
457 | ||
458 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
459 | fprintf(stderr, "Cannot malloc\n"); | |
460 | return 1; | |
461 | } | |
462 | ||
463 | ret = scanDirs (list, config, force, really_force, verbose); | |
464 | ||
465 | FcStrSetDestroy (processed_dirs); | |
466 | ||
467 | cleanCacheDirectories (config, verbose); | |
468 | ||
469 | /* | |
470 | * Now we need to sleep a second (or two, to be extra sure), to make | |
471 | * sure that timestamps for changes after this run of fc-cache are later | |
472 | * then any timestamps we wrote. We don't use gettimeofday() because | |
473 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
474 | * library, and there aren't any signals flying around here. | |
475 | */ | |
476 | FcConfigDestroy (config); | |
477 | sleep (2); | |
478 | if (verbose) | |
479 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
480 | return ret; | |
481 | } |