]>
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 | ||
0334e5a2 MF |
25 | #include "../fc-arch/fcarch.h" |
26 | ||
24330d27 KP |
27 | #ifdef HAVE_CONFIG_H |
28 | #include <config.h> | |
29 | #else | |
c4bd0638 MALF |
30 | #ifdef linux |
31 | #define HAVE_GETOPT_LONG 1 | |
32 | #endif | |
24330d27 KP |
33 | #define HAVE_GETOPT 1 |
34 | #endif | |
35 | ||
4984242e | 36 | #include <fontconfig/fontconfig.h> |
f045376c PL |
37 | #include <stdio.h> |
38 | #include <stdlib.h> | |
39 | #include <unistd.h> | |
40 | #include <sys/types.h> | |
41 | #include <sys/stat.h> | |
42 | #include <errno.h> | |
d8ab9e6c KP |
43 | #include <fcntl.h> |
44 | #include <dirent.h> | |
0334e5a2 | 45 | #include <string.h> |
f045376c | 46 | |
d6217cc6 PL |
47 | #if defined (_WIN32) |
48 | #define STRICT | |
49 | #include <windows.h> | |
50 | #define sleep(x) Sleep((x) * 1000) | |
51 | #undef STRICT | |
52 | #endif | |
53 | ||
d8ab9e6c KP |
54 | #ifndef O_BINARY |
55 | #define O_BINARY 0 | |
56 | #endif | |
57 | ||
c4bd0638 MALF |
58 | #ifndef HAVE_GETOPT |
59 | #define HAVE_GETOPT 0 | |
60 | #endif | |
61 | #ifndef HAVE_GETOPT_LONG | |
62 | #define HAVE_GETOPT_LONG 0 | |
63 | #endif | |
64 | ||
24330d27 | 65 | #if HAVE_GETOPT_LONG |
c4bd0638 | 66 | #undef _GNU_SOURCE |
24330d27 KP |
67 | #define _GNU_SOURCE |
68 | #include <getopt.h> | |
69 | const struct option longopts[] = { | |
179c3995 | 70 | {"force", 0, 0, 'f'}, |
d2c01029 | 71 | {"really-force", 0, 0, 'r'}, |
ff3f1f98 | 72 | {"system-only", 0, 0, 's'}, |
24330d27 KP |
73 | {"version", 0, 0, 'V'}, |
74 | {"verbose", 0, 0, 'v'}, | |
75 | {"help", 0, 0, '?'}, | |
76 | {NULL,0,0,0}, | |
77 | }; | |
78 | #else | |
79 | #if HAVE_GETOPT | |
80 | extern char *optarg; | |
81 | extern int optind, opterr, optopt; | |
82 | #endif | |
83 | #endif | |
84 | ||
65018b4a KP |
85 | static void |
86 | usage (char *program) | |
24330d27 | 87 | { |
86b12431 | 88 | #if HAVE_GETOPT_LONG |
d2c01029 | 89 | fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", |
86b12431 KP |
90 | program); |
91 | #else | |
d2c01029 | 92 | fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n", |
24330d27 | 93 | program); |
86b12431 | 94 | #endif |
24330d27 KP |
95 | fprintf (stderr, "Build font information caches in [dirs]\n" |
96 | "(all directories in font configuration by default).\n"); | |
97 | fprintf (stderr, "\n"); | |
86b12431 | 98 | #if HAVE_GETOPT_LONG |
80a7d664 | 99 | fprintf (stderr, " -f, --force scan directories with apparently valid caches\n"); |
d2c01029 | 100 | fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n"); |
ff3f1f98 | 101 | fprintf (stderr, " -s, --system-only scan system-wide directories only\n"); |
24330d27 KP |
102 | fprintf (stderr, " -v, --verbose display status information while busy\n"); |
103 | fprintf (stderr, " -V, --version display font config version and exit\n"); | |
104 | fprintf (stderr, " -?, --help display this help and exit\n"); | |
86b12431 KP |
105 | #else |
106 | fprintf (stderr, " -f (force) scan directories with apparently valid caches\n"); | |
d2c01029 | 107 | fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n"); |
86b12431 KP |
108 | fprintf (stderr, " -s (system) scan system-wide directories only\n"); |
109 | fprintf (stderr, " -v (verbose) display status information while busy\n"); | |
110 | fprintf (stderr, " -V (version) display font config version and exit\n"); | |
111 | fprintf (stderr, " -? (help) display this help and exit\n"); | |
112 | #endif | |
24330d27 KP |
113 | exit (1); |
114 | } | |
115 | ||
660acf8f PL |
116 | static FcStrSet *processed_dirs; |
117 | ||
179c3995 | 118 | static int |
bc5e487f | 119 | scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose) |
24330d27 | 120 | { |
4984242e KP |
121 | int ret = 0; |
122 | const FcChar8 *dir; | |
123 | FcStrSet *subdirs; | |
124 | FcStrList *sublist; | |
125 | FcCache *cache; | |
126 | struct stat statb; | |
127 | FcBool was_valid; | |
128 | int i; | |
179c3995 KP |
129 | |
130 | /* | |
131 | * Now scan all of the directories into separate databases | |
132 | * and write out the results | |
133 | */ | |
db50cbda | 134 | while ((dir = FcStrListNext (list))) |
179c3995 KP |
135 | { |
136 | if (verbose) | |
137 | { | |
bc5e487f | 138 | printf ("%s: ", dir); |
179c3995 KP |
139 | fflush (stdout); |
140 | } | |
275cf6cd PL |
141 | |
142 | if (!dir) | |
143 | { | |
144 | if (verbose) | |
145 | printf ("skipping, no such directory\n"); | |
146 | continue; | |
147 | } | |
148 | ||
660acf8f PL |
149 | if (FcStrSetMember (processed_dirs, dir)) |
150 | { | |
151 | if (verbose) | |
152 | printf ("skipping, looped directory detected\n"); | |
153 | continue; | |
154 | } | |
275cf6cd | 155 | |
e12f718f | 156 | if (stat ((char *) dir, &statb) == -1) |
0c35c0fa | 157 | { |
565a919e KP |
158 | switch (errno) { |
159 | case ENOENT: | |
160 | case ENOTDIR: | |
5d43e799 | 161 | if (verbose) |
565a919e | 162 | printf ("skipping, no such directory\n"); |
565a919e KP |
163 | break; |
164 | default: | |
0c35c0fa KP |
165 | fprintf (stderr, "\"%s\": ", dir); |
166 | perror (""); | |
167 | ret++; | |
e12f718f | 168 | break; |
0c35c0fa | 169 | } |
565a919e KP |
170 | continue; |
171 | } | |
e12f718f | 172 | |
0c35c0fa KP |
173 | if (!S_ISDIR (statb.st_mode)) |
174 | { | |
175 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
176 | continue; | |
177 | } | |
d2c01029 PL |
178 | |
179 | if (really_force) | |
180 | FcDirCacheUnlink (dir, config); | |
181 | ||
bc5e487f KP |
182 | cache = NULL; |
183 | was_valid = FcFalse; | |
184 | if (!force) { | |
185 | cache = FcDirCacheLoad (dir, config, NULL); | |
186 | if (cache) | |
187 | was_valid = FcTrue; | |
188 | } | |
2d3387fd | 189 | |
bc5e487f | 190 | if (!cache) |
179c3995 | 191 | { |
bc5e487f KP |
192 | cache = FcDirCacheRead (dir, FcTrue, config); |
193 | if (!cache) | |
194 | { | |
195 | fprintf (stderr, "%s: error scanning\n", dir); | |
196 | ret++; | |
197 | continue; | |
198 | } | |
179c3995 | 199 | } |
bc5e487f | 200 | |
bc5e487f | 201 | if (was_valid) |
179c3995 KP |
202 | { |
203 | if (verbose) | |
50124d1e | 204 | printf ("skipping, existing cache is valid: %d fonts, %d dirs\n", |
4984242e | 205 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); |
179c3995 KP |
206 | } |
207 | else | |
208 | { | |
209 | if (verbose) | |
50124d1e | 210 | printf ("caching, new cache contents: %d fonts, %d dirs\n", |
4984242e | 211 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); |
4262e0b3 | 212 | |
f57783d2 | 213 | if (!FcDirCacheValid (dir)) |
179c3995 | 214 | { |
2d3387fd KP |
215 | fprintf (stderr, "%s: failed to write cache\n", dir); |
216 | (void) FcDirCacheUnlink (dir, config); | |
179c3995 KP |
217 | ret++; |
218 | } | |
219 | } | |
bc5e487f KP |
220 | |
221 | subdirs = FcStrSetCreate (); | |
222 | if (!subdirs) | |
223 | { | |
224 | fprintf (stderr, "%s: Can't create subdir set\n", dir); | |
225 | ret++; | |
226 | FcDirCacheUnload (cache); | |
227 | continue; | |
228 | } | |
4984242e | 229 | for (i = 0; i < FcCacheNumSubdir (cache); i++) |
bc5e487f KP |
230 | FcStrSetAdd (subdirs, FcCacheSubdir (cache, i)); |
231 | ||
232 | FcDirCacheUnload (cache); | |
233 | ||
179c3995 | 234 | sublist = FcStrListCreate (subdirs); |
5c1853cd | 235 | FcStrSetDestroy (subdirs); |
179c3995 KP |
236 | if (!sublist) |
237 | { | |
2d3387fd | 238 | fprintf (stderr, "%s: Can't create subdir list\n", dir); |
179c3995 KP |
239 | ret++; |
240 | continue; | |
241 | } | |
660acf8f | 242 | FcStrSetAdd (processed_dirs, dir); |
bc5e487f | 243 | ret += scanDirs (sublist, config, force, really_force, verbose); |
179c3995 KP |
244 | } |
245 | FcStrListDone (list); | |
246 | return ret; | |
247 | } | |
248 | ||
d8ab9e6c KP |
249 | static FcBool |
250 | cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) | |
251 | { | |
252 | DIR *d; | |
253 | struct dirent *ent; | |
b190ad9d | 254 | FcChar8 *dir_base; |
d8ab9e6c KP |
255 | FcBool ret = FcTrue; |
256 | FcBool remove; | |
257 | FcCache *cache; | |
258 | struct stat file_stat; | |
259 | struct stat target_stat; | |
260 | ||
b190ad9d | 261 | dir_base = FcStrPlus (dir, (FcChar8 *) "/"); |
1741499e KP |
262 | if (!dir_base) |
263 | { | |
264 | fprintf (stderr, "%s: out of memory\n", dir); | |
265 | return FcFalse; | |
266 | } | |
23848903 | 267 | if (access ((char *) dir, W_OK) != 0) |
d8ab9e6c KP |
268 | { |
269 | if (verbose) | |
2b0d3d8a KP |
270 | printf ("%s: not cleaning %s cache directory\n", dir, |
271 | access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent"); | |
1741499e | 272 | FcStrFree (dir_base); |
d8ab9e6c KP |
273 | return FcTrue; |
274 | } | |
bc5e487f KP |
275 | if (verbose) |
276 | printf ("%s: cleaning cache directory\n", dir); | |
b190ad9d | 277 | d = opendir ((char *) dir); |
d8ab9e6c KP |
278 | if (!d) |
279 | { | |
b190ad9d | 280 | perror ((char *) dir); |
1741499e | 281 | FcStrFree (dir_base); |
d8ab9e6c KP |
282 | return FcFalse; |
283 | } | |
284 | while ((ent = readdir (d))) | |
285 | { | |
286 | FcChar8 *file_name; | |
4984242e | 287 | const FcChar8 *target_dir; |
d8ab9e6c KP |
288 | |
289 | if (ent->d_name[0] == '.') | |
290 | continue; | |
0334e5a2 MF |
291 | /* skip cache files for different architectures and */ |
292 | /* files which are not cache files at all */ | |
293 | if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) || | |
294 | strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX)) | |
295 | continue; | |
296 | ||
b190ad9d | 297 | file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name); |
d8ab9e6c KP |
298 | if (!file_name) |
299 | { | |
300 | fprintf (stderr, "%s: allocation failure\n", dir); | |
301 | ret = FcFalse; | |
302 | break; | |
303 | } | |
2a3e3c44 | 304 | remove = FcFalse; |
bc5e487f | 305 | cache = FcDirCacheLoadFile (file_name, &file_stat); |
d8ab9e6c | 306 | if (!cache) |
d8ab9e6c KP |
307 | { |
308 | if (verbose) | |
2a3e3c44 | 309 | printf ("%s: invalid cache file: %s\n", dir, ent->d_name); |
d8ab9e6c KP |
310 | remove = FcTrue; |
311 | } | |
2a3e3c44 | 312 | else |
d8ab9e6c | 313 | { |
2a3e3c44 KP |
314 | target_dir = FcCacheDir (cache); |
315 | if (stat ((char *) target_dir, &target_stat) < 0) | |
316 | { | |
317 | if (verbose) | |
318 | printf ("%s: %s: missing directory: %s \n", | |
319 | dir, ent->d_name, target_dir); | |
320 | remove = FcTrue; | |
321 | } | |
322 | else if (target_stat.st_mtime > file_stat.st_mtime) | |
323 | { | |
324 | if (verbose) | |
325 | printf ("%s: %s: cache outdated: %s\n", | |
326 | dir, ent->d_name, target_dir); | |
327 | remove = FcTrue; | |
328 | } | |
d8ab9e6c KP |
329 | } |
330 | if (remove) | |
331 | { | |
b190ad9d | 332 | if (unlink ((char *) file_name) < 0) |
d8ab9e6c | 333 | { |
b190ad9d | 334 | perror ((char *) file_name); |
d8ab9e6c KP |
335 | ret = FcFalse; |
336 | } | |
337 | } | |
1741499e | 338 | FcDirCacheUnload (cache); |
d8ab9e6c KP |
339 | FcStrFree (file_name); |
340 | } | |
341 | ||
342 | closedir (d); | |
1741499e | 343 | FcStrFree (dir_base); |
d8ab9e6c KP |
344 | return ret; |
345 | } | |
346 | ||
347 | static FcBool | |
348 | cleanCacheDirectories (FcConfig *config, FcBool verbose) | |
349 | { | |
350 | FcStrList *cache_dirs = FcConfigGetCacheDirs (config); | |
351 | FcChar8 *cache_dir; | |
352 | FcBool ret = FcTrue; | |
353 | ||
354 | if (!cache_dirs) | |
355 | return FcFalse; | |
356 | while ((cache_dir = FcStrListNext (cache_dirs))) | |
357 | { | |
358 | if (!cleanCacheDirectory (config, cache_dir, verbose)) | |
359 | { | |
360 | ret = FcFalse; | |
361 | break; | |
362 | } | |
363 | } | |
364 | FcStrListDone (cache_dirs); | |
365 | return ret; | |
366 | } | |
367 | ||
179c3995 KP |
368 | int |
369 | main (int argc, char **argv) | |
370 | { | |
371 | FcStrSet *dirs; | |
372 | FcStrList *list; | |
373 | FcBool verbose = FcFalse; | |
374 | FcBool force = FcFalse; | |
d2c01029 | 375 | FcBool really_force = FcFalse; |
ff3f1f98 | 376 | FcBool systemOnly = FcFalse; |
179c3995 | 377 | FcConfig *config; |
24330d27 | 378 | int i; |
179c3995 | 379 | int ret; |
24330d27 KP |
380 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
381 | int c; | |
382 | ||
383 | #if HAVE_GETOPT_LONG | |
d2c01029 | 384 | while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) |
24330d27 | 385 | #else |
d2c01029 | 386 | while ((c = getopt (argc, argv, "frsVv?")) != -1) |
24330d27 KP |
387 | #endif |
388 | { | |
389 | switch (c) { | |
d2c01029 PL |
390 | case 'r': |
391 | really_force = FcTrue; | |
392 | /* fall through */ | |
179c3995 KP |
393 | case 'f': |
394 | force = FcTrue; | |
395 | break; | |
ff3f1f98 KP |
396 | case 's': |
397 | systemOnly = FcTrue; | |
398 | break; | |
24330d27 KP |
399 | case 'V': |
400 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
401 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
402 | exit (0); | |
403 | case 'v': | |
179c3995 | 404 | verbose = FcTrue; |
24330d27 KP |
405 | break; |
406 | default: | |
407 | usage (argv[0]); | |
408 | } | |
409 | } | |
410 | i = optind; | |
411 | #else | |
412 | i = 1; | |
413 | #endif | |
414 | ||
ff3f1f98 KP |
415 | if (systemOnly) |
416 | FcConfigEnableHome (FcFalse); | |
5e678e94 | 417 | config = FcInitLoadConfig (); |
179c3995 | 418 | if (!config) |
24330d27 | 419 | { |
179c3995 | 420 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); |
24330d27 KP |
421 | return 1; |
422 | } | |
df3efc11 | 423 | FcConfigSetCurrent (config); |
212c9f43 | 424 | |
24330d27 | 425 | if (argv[i]) |
24330d27 | 426 | { |
179c3995 KP |
427 | dirs = FcStrSetCreate (); |
428 | if (!dirs) | |
24330d27 | 429 | { |
179c3995 KP |
430 | fprintf (stderr, "%s: Can't create list of directories\n", |
431 | argv[0]); | |
432 | return 1; | |
24330d27 | 433 | } |
179c3995 | 434 | while (argv[i]) |
24330d27 | 435 | { |
9b511b29 | 436 | if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i])) |
24330d27 | 437 | { |
179c3995 KP |
438 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); |
439 | return 1; | |
24330d27 | 440 | } |
179c3995 | 441 | i++; |
24330d27 | 442 | } |
179c3995 KP |
443 | list = FcStrListCreate (dirs); |
444 | FcStrSetDestroy (dirs); | |
24330d27 | 445 | } |
179c3995 KP |
446 | else |
447 | list = FcConfigGetConfigDirs (config); | |
660acf8f PL |
448 | |
449 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
450 | fprintf(stderr, "Cannot malloc\n"); | |
451 | return 1; | |
452 | } | |
453 | ||
bc5e487f | 454 | ret = scanDirs (list, config, force, really_force, verbose); |
660acf8f PL |
455 | |
456 | FcStrSetDestroy (processed_dirs); | |
457 | ||
d8ab9e6c KP |
458 | cleanCacheDirectories (config, verbose); |
459 | ||
54560b01 KP |
460 | /* |
461 | * Now we need to sleep a second (or two, to be extra sure), to make | |
462 | * sure that timestamps for changes after this run of fc-cache are later | |
463 | * then any timestamps we wrote. We don't use gettimeofday() because | |
464 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
465 | * library, and there aren't any signals flying around here. | |
466 | */ | |
716ac8b8 | 467 | FcConfigDestroy (config); |
54560b01 | 468 | sleep (2); |
24330d27 KP |
469 | if (verbose) |
470 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
471 | return ret; | |
472 | } |