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