]>
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 | |
d2c01029 | 131 | scanDirs (FcStrList *list, FcConfig *config, char *program, 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; | |
0c35c0fa | 138 | struct stat statb; |
2d3387fd | 139 | FcBool was_valid; |
179c3995 KP |
140 | |
141 | /* | |
142 | * Now scan all of the directories into separate databases | |
143 | * and write out the results | |
144 | */ | |
db50cbda | 145 | while ((dir = FcStrListNext (list))) |
179c3995 KP |
146 | { |
147 | if (verbose) | |
148 | { | |
db50cbda | 149 | printf ("%s: \"%s\": ", program, dir); |
179c3995 KP |
150 | fflush (stdout); |
151 | } | |
275cf6cd PL |
152 | |
153 | if (!dir) | |
154 | { | |
155 | if (verbose) | |
156 | printf ("skipping, no such directory\n"); | |
157 | continue; | |
158 | } | |
159 | ||
660acf8f PL |
160 | if (FcStrSetMember (processed_dirs, dir)) |
161 | { | |
162 | if (verbose) | |
163 | printf ("skipping, looped directory detected\n"); | |
164 | continue; | |
165 | } | |
275cf6cd | 166 | |
179c3995 KP |
167 | set = FcFontSetCreate (); |
168 | if (!set) | |
169 | { | |
2d3387fd | 170 | fprintf (stderr, "%s: Can't create font set\n", dir); |
179c3995 KP |
171 | ret++; |
172 | continue; | |
173 | } | |
174 | subdirs = FcStrSetCreate (); | |
175 | if (!subdirs) | |
176 | { | |
2d3387fd | 177 | fprintf (stderr, "%s: Can't create directory set\n", dir); |
179c3995 | 178 | ret++; |
5c1853cd | 179 | FcFontSetDestroy (set); |
179c3995 KP |
180 | continue; |
181 | } | |
0c35c0fa | 182 | |
565a919e | 183 | if (access ((char *) dir, W_OK) < 0) |
0c35c0fa | 184 | { |
565a919e KP |
185 | switch (errno) { |
186 | case ENOENT: | |
187 | case ENOTDIR: | |
5d43e799 | 188 | if (verbose) |
565a919e | 189 | printf ("skipping, no such directory\n"); |
719f4b84 PL |
190 | FcFontSetDestroy (set); |
191 | FcStrSetDestroy (subdirs); | |
192 | continue; | |
565a919e KP |
193 | case EACCES: |
194 | case EROFS: | |
719f4b84 PL |
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. */ | |
565a919e KP |
198 | break; |
199 | default: | |
0c35c0fa KP |
200 | fprintf (stderr, "\"%s\": ", dir); |
201 | perror (""); | |
202 | ret++; | |
719f4b84 PL |
203 | |
204 | FcFontSetDestroy (set); | |
205 | FcStrSetDestroy (subdirs); | |
206 | continue; | |
0c35c0fa | 207 | } |
0c35c0fa | 208 | } |
565a919e KP |
209 | if (stat ((char *) dir, &statb) == -1) |
210 | { | |
211 | fprintf (stderr, "\"%s\": ", dir); | |
212 | perror (""); | |
5c1853cd KP |
213 | FcFontSetDestroy (set); |
214 | FcStrSetDestroy (subdirs); | |
565a919e KP |
215 | ret++; |
216 | continue; | |
217 | } | |
0c35c0fa KP |
218 | if (!S_ISDIR (statb.st_mode)) |
219 | { | |
220 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
5c1853cd KP |
221 | FcFontSetDestroy (set); |
222 | FcStrSetDestroy (subdirs); | |
0c35c0fa KP |
223 | continue; |
224 | } | |
d2c01029 PL |
225 | |
226 | if (really_force) | |
227 | FcDirCacheUnlink (dir, config); | |
228 | ||
2d3387fd | 229 | if (!force) |
f57783d2 | 230 | was_valid = FcDirCacheValid (dir); |
2d3387fd | 231 | |
af180c40 | 232 | if (!FcDirScanConfig (set, subdirs, FcConfigGetBlanks (config), dir, force, config)) |
179c3995 | 233 | { |
2d3387fd | 234 | fprintf (stderr, "%s: error scanning\n", dir); |
5c1853cd KP |
235 | FcFontSetDestroy (set); |
236 | FcStrSetDestroy (subdirs); | |
179c3995 KP |
237 | ret++; |
238 | continue; | |
239 | } | |
2d3387fd | 240 | if (!force && was_valid) |
179c3995 KP |
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)); | |
4262e0b3 | 251 | |
f57783d2 | 252 | if (!FcDirCacheValid (dir)) |
179c3995 | 253 | { |
2d3387fd KP |
254 | fprintf (stderr, "%s: failed to write cache\n", dir); |
255 | (void) FcDirCacheUnlink (dir, config); | |
179c3995 KP |
256 | ret++; |
257 | } | |
258 | } | |
259 | FcFontSetDestroy (set); | |
260 | sublist = FcStrListCreate (subdirs); | |
5c1853cd | 261 | FcStrSetDestroy (subdirs); |
179c3995 KP |
262 | if (!sublist) |
263 | { | |
2d3387fd | 264 | fprintf (stderr, "%s: Can't create subdir list\n", dir); |
179c3995 KP |
265 | ret++; |
266 | continue; | |
267 | } | |
660acf8f | 268 | FcStrSetAdd (processed_dirs, dir); |
d2c01029 | 269 | ret += scanDirs (sublist, config, program, force, really_force, verbose); |
179c3995 KP |
270 | } |
271 | FcStrListDone (list); | |
272 | return ret; | |
273 | } | |
274 | ||
d8ab9e6c KP |
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 | ||
179c3995 KP |
395 | int |
396 | main (int argc, char **argv) | |
397 | { | |
398 | FcStrSet *dirs; | |
399 | FcStrList *list; | |
400 | FcBool verbose = FcFalse; | |
401 | FcBool force = FcFalse; | |
d2c01029 | 402 | FcBool really_force = FcFalse; |
ff3f1f98 | 403 | FcBool systemOnly = FcFalse; |
179c3995 | 404 | FcConfig *config; |
24330d27 | 405 | int i; |
179c3995 | 406 | int ret; |
24330d27 KP |
407 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
408 | int c; | |
409 | ||
410 | #if HAVE_GETOPT_LONG | |
d2c01029 | 411 | while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) |
24330d27 | 412 | #else |
d2c01029 | 413 | while ((c = getopt (argc, argv, "frsVv?")) != -1) |
24330d27 KP |
414 | #endif |
415 | { | |
416 | switch (c) { | |
d2c01029 PL |
417 | case 'r': |
418 | really_force = FcTrue; | |
419 | /* fall through */ | |
179c3995 KP |
420 | case 'f': |
421 | force = FcTrue; | |
422 | break; | |
ff3f1f98 KP |
423 | case 's': |
424 | systemOnly = FcTrue; | |
425 | break; | |
24330d27 KP |
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': | |
179c3995 | 431 | verbose = FcTrue; |
24330d27 KP |
432 | break; |
433 | default: | |
434 | usage (argv[0]); | |
435 | } | |
436 | } | |
437 | i = optind; | |
438 | #else | |
439 | i = 1; | |
440 | #endif | |
441 | ||
ff3f1f98 KP |
442 | if (systemOnly) |
443 | FcConfigEnableHome (FcFalse); | |
5e678e94 | 444 | config = FcInitLoadConfig (); |
179c3995 | 445 | if (!config) |
24330d27 | 446 | { |
179c3995 | 447 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); |
24330d27 KP |
448 | return 1; |
449 | } | |
df3efc11 | 450 | FcConfigSetCurrent (config); |
212c9f43 | 451 | |
24330d27 | 452 | if (argv[i]) |
24330d27 | 453 | { |
179c3995 KP |
454 | dirs = FcStrSetCreate (); |
455 | if (!dirs) | |
24330d27 | 456 | { |
179c3995 KP |
457 | fprintf (stderr, "%s: Can't create list of directories\n", |
458 | argv[0]); | |
459 | return 1; | |
24330d27 | 460 | } |
179c3995 | 461 | while (argv[i]) |
24330d27 | 462 | { |
179c3995 | 463 | if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i])) |
24330d27 | 464 | { |
179c3995 KP |
465 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); |
466 | return 1; | |
24330d27 | 467 | } |
179c3995 | 468 | i++; |
24330d27 | 469 | } |
179c3995 KP |
470 | list = FcStrListCreate (dirs); |
471 | FcStrSetDestroy (dirs); | |
24330d27 | 472 | } |
179c3995 KP |
473 | else |
474 | list = FcConfigGetConfigDirs (config); | |
660acf8f PL |
475 | |
476 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
477 | fprintf(stderr, "Cannot malloc\n"); | |
478 | return 1; | |
479 | } | |
480 | ||
d2c01029 | 481 | ret = scanDirs (list, config, argv[0], force, really_force, verbose); |
660acf8f PL |
482 | |
483 | FcStrSetDestroy (processed_dirs); | |
484 | ||
d8ab9e6c KP |
485 | cleanCacheDirectories (config, verbose); |
486 | ||
54560b01 KP |
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 | */ | |
716ac8b8 | 494 | FcConfigDestroy (config); |
54560b01 | 495 | sleep (2); |
24330d27 KP |
496 | if (verbose) |
497 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
498 | return ret; | |
499 | } |