]>
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, "/"); | |
290 | if (access ((char *) dir, W_OK|X_OK) != 0) | |
291 | { | |
292 | if (verbose) | |
bc5e487f | 293 | printf ("%s: not cleaning unwritable cache directory\n", dir); |
d8ab9e6c KP |
294 | return FcTrue; |
295 | } | |
bc5e487f KP |
296 | if (verbose) |
297 | printf ("%s: cleaning cache directory\n", dir); | |
d8ab9e6c KP |
298 | d = opendir (dir); |
299 | if (!d) | |
300 | { | |
301 | perror (dir); | |
302 | return FcFalse; | |
303 | } | |
304 | while ((ent = readdir (d))) | |
305 | { | |
306 | FcChar8 *file_name; | |
307 | FcChar8 *target_dir; | |
308 | ||
309 | if (ent->d_name[0] == '.') | |
310 | continue; | |
311 | file_name = FcStrPlus (dir_base, ent->d_name); | |
312 | if (!file_name) | |
313 | { | |
314 | fprintf (stderr, "%s: allocation failure\n", dir); | |
315 | ret = FcFalse; | |
316 | break; | |
317 | } | |
bc5e487f | 318 | cache = FcDirCacheLoadFile (file_name, &file_stat); |
d8ab9e6c KP |
319 | if (!cache) |
320 | { | |
321 | fprintf (stderr, "%s: invalid cache file: %s\n", dir, ent->d_name); | |
322 | FcStrFree (file_name); | |
323 | ret = FcFalse; | |
324 | continue; | |
325 | } | |
326 | target_dir = FcCacheDir (cache); | |
327 | remove = FcFalse; | |
328 | if (stat (target_dir, &target_stat) < 0) | |
329 | { | |
330 | if (verbose) | |
331 | printf ("%s: %s: missing directory: %s \n", | |
332 | dir, ent->d_name, target_dir); | |
333 | remove = FcTrue; | |
334 | } | |
335 | else if (target_stat.st_mtime > file_stat.st_mtime) | |
336 | { | |
337 | if (verbose) | |
338 | printf ("%s: %s: cache outdated: %s\n", | |
339 | dir, ent->d_name, target_dir); | |
340 | remove = FcTrue; | |
341 | } | |
342 | if (remove) | |
343 | { | |
344 | if (unlink (file_name) < 0) | |
345 | { | |
346 | perror (file_name); | |
347 | ret = FcFalse; | |
348 | } | |
349 | } | |
350 | FcStrFree (file_name); | |
351 | } | |
352 | ||
353 | closedir (d); | |
354 | return ret; | |
355 | } | |
356 | ||
357 | static FcBool | |
358 | cleanCacheDirectories (FcConfig *config, FcBool verbose) | |
359 | { | |
360 | FcStrList *cache_dirs = FcConfigGetCacheDirs (config); | |
361 | FcChar8 *cache_dir; | |
362 | FcBool ret = FcTrue; | |
363 | ||
364 | if (!cache_dirs) | |
365 | return FcFalse; | |
366 | while ((cache_dir = FcStrListNext (cache_dirs))) | |
367 | { | |
368 | if (!cleanCacheDirectory (config, cache_dir, verbose)) | |
369 | { | |
370 | ret = FcFalse; | |
371 | break; | |
372 | } | |
373 | } | |
374 | FcStrListDone (cache_dirs); | |
375 | return ret; | |
376 | } | |
377 | ||
179c3995 KP |
378 | int |
379 | main (int argc, char **argv) | |
380 | { | |
381 | FcStrSet *dirs; | |
382 | FcStrList *list; | |
383 | FcBool verbose = FcFalse; | |
384 | FcBool force = FcFalse; | |
d2c01029 | 385 | FcBool really_force = FcFalse; |
ff3f1f98 | 386 | FcBool systemOnly = FcFalse; |
179c3995 | 387 | FcConfig *config; |
24330d27 | 388 | int i; |
179c3995 | 389 | int ret; |
24330d27 KP |
390 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
391 | int c; | |
392 | ||
393 | #if HAVE_GETOPT_LONG | |
d2c01029 | 394 | while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) |
24330d27 | 395 | #else |
d2c01029 | 396 | while ((c = getopt (argc, argv, "frsVv?")) != -1) |
24330d27 KP |
397 | #endif |
398 | { | |
399 | switch (c) { | |
d2c01029 PL |
400 | case 'r': |
401 | really_force = FcTrue; | |
402 | /* fall through */ | |
179c3995 KP |
403 | case 'f': |
404 | force = FcTrue; | |
405 | break; | |
ff3f1f98 KP |
406 | case 's': |
407 | systemOnly = FcTrue; | |
408 | break; | |
24330d27 KP |
409 | case 'V': |
410 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
411 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
412 | exit (0); | |
413 | case 'v': | |
179c3995 | 414 | verbose = FcTrue; |
24330d27 KP |
415 | break; |
416 | default: | |
417 | usage (argv[0]); | |
418 | } | |
419 | } | |
420 | i = optind; | |
421 | #else | |
422 | i = 1; | |
423 | #endif | |
424 | ||
ff3f1f98 KP |
425 | if (systemOnly) |
426 | FcConfigEnableHome (FcFalse); | |
5e678e94 | 427 | config = FcInitLoadConfig (); |
179c3995 | 428 | if (!config) |
24330d27 | 429 | { |
179c3995 | 430 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); |
24330d27 KP |
431 | return 1; |
432 | } | |
df3efc11 | 433 | FcConfigSetCurrent (config); |
212c9f43 | 434 | |
24330d27 | 435 | if (argv[i]) |
24330d27 | 436 | { |
179c3995 KP |
437 | dirs = FcStrSetCreate (); |
438 | if (!dirs) | |
24330d27 | 439 | { |
179c3995 KP |
440 | fprintf (stderr, "%s: Can't create list of directories\n", |
441 | argv[0]); | |
442 | return 1; | |
24330d27 | 443 | } |
179c3995 | 444 | while (argv[i]) |
24330d27 | 445 | { |
179c3995 | 446 | if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i])) |
24330d27 | 447 | { |
179c3995 KP |
448 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); |
449 | return 1; | |
24330d27 | 450 | } |
179c3995 | 451 | i++; |
24330d27 | 452 | } |
179c3995 KP |
453 | list = FcStrListCreate (dirs); |
454 | FcStrSetDestroy (dirs); | |
24330d27 | 455 | } |
179c3995 KP |
456 | else |
457 | list = FcConfigGetConfigDirs (config); | |
660acf8f PL |
458 | |
459 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
460 | fprintf(stderr, "Cannot malloc\n"); | |
461 | return 1; | |
462 | } | |
463 | ||
bc5e487f | 464 | ret = scanDirs (list, config, force, really_force, verbose); |
660acf8f PL |
465 | |
466 | FcStrSetDestroy (processed_dirs); | |
467 | ||
d8ab9e6c KP |
468 | cleanCacheDirectories (config, verbose); |
469 | ||
54560b01 KP |
470 | /* |
471 | * Now we need to sleep a second (or two, to be extra sure), to make | |
472 | * sure that timestamps for changes after this run of fc-cache are later | |
473 | * then any timestamps we wrote. We don't use gettimeofday() because | |
474 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
475 | * library, and there aren't any signals flying around here. | |
476 | */ | |
716ac8b8 | 477 | FcConfigDestroy (config); |
54560b01 | 478 | sleep (2); |
24330d27 KP |
479 | if (verbose) |
480 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
481 | return ret; | |
482 | } |