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