]>
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, char *program, 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 | struct stat statb; | |
139 | FcBool was_valid; | |
140 | ||
141 | /* | |
142 | * Now scan all of the directories into separate databases | |
143 | * and write out the results | |
144 | */ | |
145 | while ((dir = FcStrListNext (list))) | |
146 | { | |
147 | if (verbose) | |
148 | { | |
149 | printf ("%s: \"%s\": ", program, dir); | |
150 | fflush (stdout); | |
151 | } | |
152 | ||
153 | if (!dir) | |
154 | { | |
155 | if (verbose) | |
156 | printf ("skipping, no such directory\n"); | |
157 | continue; | |
158 | } | |
159 | ||
160 | if (FcStrSetMember (processed_dirs, dir)) | |
161 | { | |
162 | if (verbose) | |
163 | printf ("skipping, looped directory detected\n"); | |
164 | continue; | |
165 | } | |
166 | ||
167 | set = FcFontSetCreate (); | |
168 | if (!set) | |
169 | { | |
170 | fprintf (stderr, "%s: Can't create font set\n", dir); | |
171 | ret++; | |
172 | continue; | |
173 | } | |
174 | subdirs = FcStrSetCreate (); | |
175 | if (!subdirs) | |
176 | { | |
177 | fprintf (stderr, "%s: Can't create directory set\n", dir); | |
178 | ret++; | |
179 | FcFontSetDestroy (set); | |
180 | continue; | |
181 | } | |
182 | ||
183 | if (access ((char *) dir, W_OK) < 0) | |
184 | { | |
185 | switch (errno) { | |
186 | case ENOENT: | |
187 | case ENOTDIR: | |
188 | if (verbose) | |
189 | printf ("skipping, no such directory\n"); | |
190 | FcFontSetDestroy (set); | |
191 | FcStrSetDestroy (subdirs); | |
192 | continue; | |
193 | case EACCES: | |
194 | case EROFS: | |
195 | /* That's ok, caches go to /var anyway. */ | |
196 | /* Ideally we'd do an access on the hashed_name. */ | |
197 | /* But we hid that behind an abstraction barrier. */ | |
198 | break; | |
199 | default: | |
200 | fprintf (stderr, "\"%s\": ", dir); | |
201 | perror (""); | |
202 | ret++; | |
203 | ||
204 | FcFontSetDestroy (set); | |
205 | FcStrSetDestroy (subdirs); | |
206 | continue; | |
207 | } | |
208 | } | |
209 | if (stat ((char *) dir, &statb) == -1) | |
210 | { | |
211 | fprintf (stderr, "\"%s\": ", dir); | |
212 | perror (""); | |
213 | FcFontSetDestroy (set); | |
214 | FcStrSetDestroy (subdirs); | |
215 | ret++; | |
216 | continue; | |
217 | } | |
218 | if (!S_ISDIR (statb.st_mode)) | |
219 | { | |
220 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
221 | FcFontSetDestroy (set); | |
222 | FcStrSetDestroy (subdirs); | |
223 | continue; | |
224 | } | |
225 | ||
226 | if (really_force) | |
227 | FcDirCacheUnlink (dir, config); | |
228 | ||
229 | if (!force) | |
230 | was_valid = FcDirCacheValid (dir); | |
231 | ||
232 | if (!FcDirScanConfig (set, subdirs, FcConfigGetBlanks (config), dir, force, config)) | |
233 | { | |
234 | fprintf (stderr, "%s: error scanning\n", dir); | |
235 | FcFontSetDestroy (set); | |
236 | FcStrSetDestroy (subdirs); | |
237 | ret++; | |
238 | continue; | |
239 | } | |
240 | if (!force && was_valid) | |
241 | { | |
242 | if (verbose) | |
243 | printf ("skipping, %d fonts, %d dirs\n", | |
244 | set->nfont, nsubdirs(subdirs)); | |
245 | } | |
246 | else | |
247 | { | |
248 | if (verbose) | |
249 | printf ("caching, %d fonts, %d dirs\n", | |
250 | set->nfont, nsubdirs (subdirs)); | |
251 | ||
252 | if (!FcDirCacheValid (dir)) | |
253 | { | |
254 | fprintf (stderr, "%s: failed to write cache\n", dir); | |
255 | (void) FcDirCacheUnlink (dir, config); | |
256 | ret++; | |
257 | } | |
258 | } | |
259 | FcFontSetDestroy (set); | |
260 | sublist = FcStrListCreate (subdirs); | |
261 | FcStrSetDestroy (subdirs); | |
262 | if (!sublist) | |
263 | { | |
264 | fprintf (stderr, "%s: Can't create subdir list\n", dir); | |
265 | ret++; | |
266 | continue; | |
267 | } | |
268 | FcStrSetAdd (processed_dirs, dir); | |
269 | ret += scanDirs (sublist, config, program, force, really_force, verbose); | |
270 | } | |
271 | FcStrListDone (list); | |
272 | return ret; | |
273 | } | |
274 | ||
275 | FcCache * | |
276 | FcCacheFileMap (const FcChar8 *file, struct stat *file_stat) | |
277 | { | |
278 | FcCache *cache; | |
279 | int fd; | |
280 | ||
281 | fd = open (file, O_RDONLY | O_BINARY); | |
282 | if (fd < 0) | |
283 | return NULL; | |
284 | if (fstat (fd, file_stat) < 0) { | |
285 | close (fd); | |
286 | return NULL; | |
287 | } | |
288 | if (FcDirCacheLoad (fd, file_stat->st_size, &cache)) { | |
289 | close (fd); | |
290 | return cache; | |
291 | } | |
292 | close (fd); | |
293 | return NULL; | |
294 | } | |
295 | ||
296 | static FcBool | |
297 | cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) | |
298 | { | |
299 | DIR *d; | |
300 | struct dirent *ent; | |
301 | char *dir_base; | |
302 | FcBool ret = FcTrue; | |
303 | FcBool remove; | |
304 | FcCache *cache; | |
305 | struct stat file_stat; | |
306 | struct stat target_stat; | |
307 | ||
308 | dir_base = FcStrPlus (dir, "/"); | |
309 | if (access ((char *) dir, W_OK|X_OK) != 0) | |
310 | { | |
311 | if (verbose) | |
312 | printf ("%s: skipping unwritable cache directory\n", dir); | |
313 | return FcTrue; | |
314 | } | |
315 | d = opendir (dir); | |
316 | if (!d) | |
317 | { | |
318 | perror (dir); | |
319 | return FcFalse; | |
320 | } | |
321 | while ((ent = readdir (d))) | |
322 | { | |
323 | FcChar8 *file_name; | |
324 | FcChar8 *target_dir; | |
325 | ||
326 | if (ent->d_name[0] == '.') | |
327 | continue; | |
328 | file_name = FcStrPlus (dir_base, ent->d_name); | |
329 | if (!file_name) | |
330 | { | |
331 | fprintf (stderr, "%s: allocation failure\n", dir); | |
332 | ret = FcFalse; | |
333 | break; | |
334 | } | |
335 | cache = FcCacheFileMap (file_name, &file_stat); | |
336 | if (!cache) | |
337 | { | |
338 | fprintf (stderr, "%s: invalid cache file: %s\n", dir, ent->d_name); | |
339 | FcStrFree (file_name); | |
340 | ret = FcFalse; | |
341 | continue; | |
342 | } | |
343 | target_dir = FcCacheDir (cache); | |
344 | remove = FcFalse; | |
345 | if (stat (target_dir, &target_stat) < 0) | |
346 | { | |
347 | if (verbose) | |
348 | printf ("%s: %s: missing directory: %s \n", | |
349 | dir, ent->d_name, target_dir); | |
350 | remove = FcTrue; | |
351 | } | |
352 | else if (target_stat.st_mtime > file_stat.st_mtime) | |
353 | { | |
354 | if (verbose) | |
355 | printf ("%s: %s: cache outdated: %s\n", | |
356 | dir, ent->d_name, target_dir); | |
357 | remove = FcTrue; | |
358 | } | |
359 | if (remove) | |
360 | { | |
361 | if (unlink (file_name) < 0) | |
362 | { | |
363 | perror (file_name); | |
364 | ret = FcFalse; | |
365 | } | |
366 | } | |
367 | FcStrFree (file_name); | |
368 | } | |
369 | ||
370 | closedir (d); | |
371 | return ret; | |
372 | } | |
373 | ||
374 | static FcBool | |
375 | cleanCacheDirectories (FcConfig *config, FcBool verbose) | |
376 | { | |
377 | FcStrList *cache_dirs = FcConfigGetCacheDirs (config); | |
378 | FcChar8 *cache_dir; | |
379 | FcBool ret = FcTrue; | |
380 | ||
381 | if (!cache_dirs) | |
382 | return FcFalse; | |
383 | while ((cache_dir = FcStrListNext (cache_dirs))) | |
384 | { | |
385 | if (!cleanCacheDirectory (config, cache_dir, verbose)) | |
386 | { | |
387 | ret = FcFalse; | |
388 | break; | |
389 | } | |
390 | } | |
391 | FcStrListDone (cache_dirs); | |
392 | return ret; | |
393 | } | |
394 | ||
395 | int | |
396 | main (int argc, char **argv) | |
397 | { | |
398 | FcStrSet *dirs; | |
399 | FcStrList *list; | |
400 | FcBool verbose = FcFalse; | |
401 | FcBool force = FcFalse; | |
402 | FcBool really_force = FcFalse; | |
403 | FcBool systemOnly = FcFalse; | |
404 | FcConfig *config; | |
405 | int i; | |
406 | int ret; | |
407 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
408 | int c; | |
409 | ||
410 | #if HAVE_GETOPT_LONG | |
411 | while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) | |
412 | #else | |
413 | while ((c = getopt (argc, argv, "frsVv?")) != -1) | |
414 | #endif | |
415 | { | |
416 | switch (c) { | |
417 | case 'r': | |
418 | really_force = FcTrue; | |
419 | /* fall through */ | |
420 | case 'f': | |
421 | force = FcTrue; | |
422 | break; | |
423 | case 's': | |
424 | systemOnly = FcTrue; | |
425 | break; | |
426 | case 'V': | |
427 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
428 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
429 | exit (0); | |
430 | case 'v': | |
431 | verbose = FcTrue; | |
432 | break; | |
433 | default: | |
434 | usage (argv[0]); | |
435 | } | |
436 | } | |
437 | i = optind; | |
438 | #else | |
439 | i = 1; | |
440 | #endif | |
441 | ||
442 | if (systemOnly) | |
443 | FcConfigEnableHome (FcFalse); | |
444 | config = FcInitLoadConfig (); | |
445 | if (!config) | |
446 | { | |
447 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); | |
448 | return 1; | |
449 | } | |
450 | FcConfigSetCurrent (config); | |
451 | ||
452 | if (argv[i]) | |
453 | { | |
454 | dirs = FcStrSetCreate (); | |
455 | if (!dirs) | |
456 | { | |
457 | fprintf (stderr, "%s: Can't create list of directories\n", | |
458 | argv[0]); | |
459 | return 1; | |
460 | } | |
461 | while (argv[i]) | |
462 | { | |
463 | if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i])) | |
464 | { | |
465 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); | |
466 | return 1; | |
467 | } | |
468 | i++; | |
469 | } | |
470 | list = FcStrListCreate (dirs); | |
471 | FcStrSetDestroy (dirs); | |
472 | } | |
473 | else | |
474 | list = FcConfigGetConfigDirs (config); | |
475 | ||
476 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
477 | fprintf(stderr, "Cannot malloc\n"); | |
478 | return 1; | |
479 | } | |
480 | ||
481 | ret = scanDirs (list, config, argv[0], force, really_force, verbose); | |
482 | ||
483 | FcStrSetDestroy (processed_dirs); | |
484 | ||
485 | cleanCacheDirectories (config, verbose); | |
486 | ||
487 | /* | |
488 | * Now we need to sleep a second (or two, to be extra sure), to make | |
489 | * sure that timestamps for changes after this run of fc-cache are later | |
490 | * then any timestamps we wrote. We don't use gettimeofday() because | |
491 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
492 | * library, and there aren't any signals flying around here. | |
493 | */ | |
494 | FcConfigDestroy (config); | |
495 | sleep (2); | |
496 | if (verbose) | |
497 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
498 | return ret; | |
499 | } |