]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
4bd4418a | 2 | * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2002 Keith Packard |
24330d27 KP |
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 | ||
24330d27 KP |
25 | #ifdef HAVE_CONFIG_H |
26 | #include <config.h> | |
27 | #else | |
c4bd0638 MALF |
28 | #ifdef linux |
29 | #define HAVE_GETOPT_LONG 1 | |
30 | #endif | |
24330d27 KP |
31 | #define HAVE_GETOPT 1 |
32 | #endif | |
33 | ||
d8ab9e6c | 34 | #include "fcint.h" |
f045376c PL |
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> | |
d8ab9e6c KP |
41 | #include <fcntl.h> |
42 | #include <dirent.h> | |
f045376c | 43 | |
d6217cc6 PL |
44 | #if defined (_WIN32) |
45 | #define STRICT | |
46 | #include <windows.h> | |
47 | #define sleep(x) Sleep((x) * 1000) | |
48 | #undef STRICT | |
49 | #endif | |
50 | ||
d8ab9e6c KP |
51 | #ifndef O_BINARY |
52 | #define O_BINARY 0 | |
53 | #endif | |
54 | ||
c4bd0638 MALF |
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 | ||
24330d27 | 62 | #if HAVE_GETOPT_LONG |
c4bd0638 | 63 | #undef _GNU_SOURCE |
24330d27 KP |
64 | #define _GNU_SOURCE |
65 | #include <getopt.h> | |
66 | const struct option longopts[] = { | |
179c3995 | 67 | {"force", 0, 0, 'f'}, |
d2c01029 | 68 | {"really-force", 0, 0, 'r'}, |
ff3f1f98 | 69 | {"system-only", 0, 0, 's'}, |
24330d27 KP |
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 | ||
65018b4a KP |
82 | static void |
83 | usage (char *program) | |
24330d27 | 84 | { |
86b12431 | 85 | #if HAVE_GETOPT_LONG |
d2c01029 | 86 | fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", |
86b12431 KP |
87 | program); |
88 | #else | |
d2c01029 | 89 | fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n", |
24330d27 | 90 | program); |
86b12431 | 91 | #endif |
24330d27 KP |
92 | fprintf (stderr, "Build font information caches in [dirs]\n" |
93 | "(all directories in font configuration by default).\n"); | |
94 | fprintf (stderr, "\n"); | |
86b12431 | 95 | #if HAVE_GETOPT_LONG |
80a7d664 | 96 | fprintf (stderr, " -f, --force scan directories with apparently valid caches\n"); |
d2c01029 | 97 | fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n"); |
ff3f1f98 | 98 | fprintf (stderr, " -s, --system-only scan system-wide directories only\n"); |
24330d27 KP |
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"); | |
86b12431 KP |
102 | #else |
103 | fprintf (stderr, " -f (force) scan directories with apparently valid caches\n"); | |
d2c01029 | 104 | fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n"); |
86b12431 KP |
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 | |
24330d27 KP |
110 | exit (1); |
111 | } | |
112 | ||
660acf8f PL |
113 | static FcStrSet *processed_dirs; |
114 | ||
179c3995 KP |
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 | |
bc5e487f | 131 | scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose) |
24330d27 KP |
132 | { |
133 | int ret = 0; | |
97293e07 | 134 | const FcChar8 *dir; |
24330d27 | 135 | FcFontSet *set; |
179c3995 KP |
136 | FcStrSet *subdirs; |
137 | FcStrList *sublist; | |
bc5e487f | 138 | FcCache *cache; |
0c35c0fa | 139 | struct stat statb; |
2d3387fd | 140 | FcBool was_valid; |
bc5e487f | 141 | int i; |
179c3995 KP |
142 | |
143 | /* | |
144 | * Now scan all of the directories into separate databases | |
145 | * and write out the results | |
146 | */ | |
db50cbda | 147 | while ((dir = FcStrListNext (list))) |
179c3995 KP |
148 | { |
149 | if (verbose) | |
150 | { | |
bc5e487f | 151 | printf ("%s: ", dir); |
179c3995 KP |
152 | fflush (stdout); |
153 | } | |
275cf6cd PL |
154 | |
155 | if (!dir) | |
156 | { | |
157 | if (verbose) | |
158 | printf ("skipping, no such directory\n"); | |
159 | continue; | |
160 | } | |
161 | ||
660acf8f PL |
162 | if (FcStrSetMember (processed_dirs, dir)) |
163 | { | |
164 | if (verbose) | |
165 | printf ("skipping, looped directory detected\n"); | |
166 | continue; | |
167 | } | |
275cf6cd | 168 | |
565a919e | 169 | if (access ((char *) dir, W_OK) < 0) |
0c35c0fa | 170 | { |
565a919e KP |
171 | switch (errno) { |
172 | case ENOENT: | |
173 | case ENOTDIR: | |
5d43e799 | 174 | if (verbose) |
565a919e | 175 | printf ("skipping, no such directory\n"); |
719f4b84 | 176 | continue; |
565a919e KP |
177 | case EACCES: |
178 | case EROFS: | |
719f4b84 PL |
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. */ | |
565a919e KP |
182 | break; |
183 | default: | |
0c35c0fa KP |
184 | fprintf (stderr, "\"%s\": ", dir); |
185 | perror (""); | |
186 | ret++; | |
719f4b84 | 187 | |
719f4b84 | 188 | continue; |
0c35c0fa | 189 | } |
0c35c0fa | 190 | } |
565a919e KP |
191 | if (stat ((char *) dir, &statb) == -1) |
192 | { | |
193 | fprintf (stderr, "\"%s\": ", dir); | |
194 | perror (""); | |
195 | ret++; | |
196 | continue; | |
197 | } | |
0c35c0fa KP |
198 | if (!S_ISDIR (statb.st_mode)) |
199 | { | |
200 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
201 | continue; | |
202 | } | |
d2c01029 PL |
203 | |
204 | if (really_force) | |
205 | FcDirCacheUnlink (dir, config); | |
206 | ||
bc5e487f KP |
207 | cache = NULL; |
208 | was_valid = FcFalse; | |
209 | if (!force) { | |
210 | cache = FcDirCacheLoad (dir, config, NULL); | |
211 | if (cache) | |
212 | was_valid = FcTrue; | |
213 | } | |
2d3387fd | 214 | |
bc5e487f | 215 | if (!cache) |
179c3995 | 216 | { |
bc5e487f KP |
217 | cache = FcDirCacheRead (dir, FcTrue, config); |
218 | if (!cache) | |
219 | { | |
220 | fprintf (stderr, "%s: error scanning\n", dir); | |
221 | ret++; | |
222 | continue; | |
223 | } | |
179c3995 | 224 | } |
bc5e487f KP |
225 | |
226 | set = FcCacheSet (cache); | |
227 | ||
228 | if (was_valid) | |
179c3995 KP |
229 | { |
230 | if (verbose) | |
231 | printf ("skipping, %d fonts, %d dirs\n", | |
bc5e487f | 232 | set->nfont, cache->dirs_count); |
179c3995 KP |
233 | } |
234 | else | |
235 | { | |
236 | if (verbose) | |
237 | printf ("caching, %d fonts, %d dirs\n", | |
bc5e487f | 238 | set->nfont, cache->dirs_count); |
4262e0b3 | 239 | |
f57783d2 | 240 | if (!FcDirCacheValid (dir)) |
179c3995 | 241 | { |
2d3387fd KP |
242 | fprintf (stderr, "%s: failed to write cache\n", dir); |
243 | (void) FcDirCacheUnlink (dir, config); | |
179c3995 KP |
244 | ret++; |
245 | } | |
246 | } | |
bc5e487f KP |
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 | ||
179c3995 | 261 | sublist = FcStrListCreate (subdirs); |
5c1853cd | 262 | FcStrSetDestroy (subdirs); |
179c3995 KP |
263 | if (!sublist) |
264 | { | |
2d3387fd | 265 | fprintf (stderr, "%s: Can't create subdir list\n", dir); |
179c3995 | 266 | ret++; |
bc5e487f | 267 | FcDirCacheUnload (cache); |
179c3995 KP |
268 | continue; |
269 | } | |
660acf8f | 270 | FcStrSetAdd (processed_dirs, dir); |
bc5e487f | 271 | ret += scanDirs (sublist, config, force, really_force, verbose); |
179c3995 KP |
272 | } |
273 | FcStrListDone (list); | |
274 | return ret; | |
275 | } | |
276 | ||
d8ab9e6c KP |
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, "/"); | |
1741499e KP |
290 | if (!dir_base) |
291 | { | |
292 | fprintf (stderr, "%s: out of memory\n", dir); | |
293 | return FcFalse; | |
294 | } | |
d8ab9e6c KP |
295 | if (access ((char *) dir, W_OK|X_OK) != 0) |
296 | { | |
297 | if (verbose) | |
bc5e487f | 298 | printf ("%s: not cleaning unwritable cache directory\n", dir); |
1741499e | 299 | FcStrFree (dir_base); |
d8ab9e6c KP |
300 | return FcTrue; |
301 | } | |
bc5e487f KP |
302 | if (verbose) |
303 | printf ("%s: cleaning cache directory\n", dir); | |
d8ab9e6c KP |
304 | d = opendir (dir); |
305 | if (!d) | |
306 | { | |
307 | perror (dir); | |
1741499e | 308 | FcStrFree (dir_base); |
d8ab9e6c KP |
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 | } | |
bc5e487f | 325 | cache = FcDirCacheLoadFile (file_name, &file_stat); |
d8ab9e6c KP |
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 | } | |
1741499e | 357 | FcDirCacheUnload (cache); |
d8ab9e6c KP |
358 | FcStrFree (file_name); |
359 | } | |
360 | ||
361 | closedir (d); | |
1741499e | 362 | FcStrFree (dir_base); |
d8ab9e6c KP |
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 | ||
179c3995 KP |
387 | int |
388 | main (int argc, char **argv) | |
389 | { | |
390 | FcStrSet *dirs; | |
391 | FcStrList *list; | |
392 | FcBool verbose = FcFalse; | |
393 | FcBool force = FcFalse; | |
d2c01029 | 394 | FcBool really_force = FcFalse; |
ff3f1f98 | 395 | FcBool systemOnly = FcFalse; |
179c3995 | 396 | FcConfig *config; |
24330d27 | 397 | int i; |
179c3995 | 398 | int ret; |
24330d27 KP |
399 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
400 | int c; | |
401 | ||
402 | #if HAVE_GETOPT_LONG | |
d2c01029 | 403 | while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) |
24330d27 | 404 | #else |
d2c01029 | 405 | while ((c = getopt (argc, argv, "frsVv?")) != -1) |
24330d27 KP |
406 | #endif |
407 | { | |
408 | switch (c) { | |
d2c01029 PL |
409 | case 'r': |
410 | really_force = FcTrue; | |
411 | /* fall through */ | |
179c3995 KP |
412 | case 'f': |
413 | force = FcTrue; | |
414 | break; | |
ff3f1f98 KP |
415 | case 's': |
416 | systemOnly = FcTrue; | |
417 | break; | |
24330d27 KP |
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': | |
179c3995 | 423 | verbose = FcTrue; |
24330d27 KP |
424 | break; |
425 | default: | |
426 | usage (argv[0]); | |
427 | } | |
428 | } | |
429 | i = optind; | |
430 | #else | |
431 | i = 1; | |
432 | #endif | |
433 | ||
ff3f1f98 KP |
434 | if (systemOnly) |
435 | FcConfigEnableHome (FcFalse); | |
5e678e94 | 436 | config = FcInitLoadConfig (); |
179c3995 | 437 | if (!config) |
24330d27 | 438 | { |
179c3995 | 439 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); |
24330d27 KP |
440 | return 1; |
441 | } | |
df3efc11 | 442 | FcConfigSetCurrent (config); |
212c9f43 | 443 | |
24330d27 | 444 | if (argv[i]) |
24330d27 | 445 | { |
179c3995 KP |
446 | dirs = FcStrSetCreate (); |
447 | if (!dirs) | |
24330d27 | 448 | { |
179c3995 KP |
449 | fprintf (stderr, "%s: Can't create list of directories\n", |
450 | argv[0]); | |
451 | return 1; | |
24330d27 | 452 | } |
179c3995 | 453 | while (argv[i]) |
24330d27 | 454 | { |
179c3995 | 455 | if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i])) |
24330d27 | 456 | { |
179c3995 KP |
457 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); |
458 | return 1; | |
24330d27 | 459 | } |
179c3995 | 460 | i++; |
24330d27 | 461 | } |
179c3995 KP |
462 | list = FcStrListCreate (dirs); |
463 | FcStrSetDestroy (dirs); | |
24330d27 | 464 | } |
179c3995 KP |
465 | else |
466 | list = FcConfigGetConfigDirs (config); | |
660acf8f PL |
467 | |
468 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
469 | fprintf(stderr, "Cannot malloc\n"); | |
470 | return 1; | |
471 | } | |
472 | ||
bc5e487f | 473 | ret = scanDirs (list, config, force, really_force, verbose); |
660acf8f PL |
474 | |
475 | FcStrSetDestroy (processed_dirs); | |
476 | ||
d8ab9e6c KP |
477 | cleanCacheDirectories (config, verbose); |
478 | ||
54560b01 KP |
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 | */ | |
716ac8b8 | 486 | FcConfigDestroy (config); |
54560b01 | 487 | sleep (2); |
24330d27 KP |
488 | if (verbose) |
489 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
490 | return ret; | |
491 | } |