]>
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 | 115 | static int |
bc5e487f | 116 | scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose) |
24330d27 | 117 | { |
4984242e KP |
118 | int ret = 0; |
119 | const FcChar8 *dir; | |
120 | FcStrSet *subdirs; | |
121 | FcStrList *sublist; | |
122 | FcCache *cache; | |
123 | struct stat statb; | |
124 | FcBool was_valid; | |
125 | int i; | |
179c3995 KP |
126 | |
127 | /* | |
128 | * Now scan all of the directories into separate databases | |
129 | * and write out the results | |
130 | */ | |
db50cbda | 131 | while ((dir = FcStrListNext (list))) |
179c3995 KP |
132 | { |
133 | if (verbose) | |
134 | { | |
bc5e487f | 135 | printf ("%s: ", dir); |
179c3995 KP |
136 | fflush (stdout); |
137 | } | |
275cf6cd PL |
138 | |
139 | if (!dir) | |
140 | { | |
141 | if (verbose) | |
142 | printf ("skipping, no such directory\n"); | |
143 | continue; | |
144 | } | |
145 | ||
660acf8f PL |
146 | if (FcStrSetMember (processed_dirs, dir)) |
147 | { | |
148 | if (verbose) | |
149 | printf ("skipping, looped directory detected\n"); | |
150 | continue; | |
151 | } | |
275cf6cd | 152 | |
565a919e | 153 | if (access ((char *) dir, W_OK) < 0) |
0c35c0fa | 154 | { |
565a919e KP |
155 | switch (errno) { |
156 | case ENOENT: | |
157 | case ENOTDIR: | |
5d43e799 | 158 | if (verbose) |
565a919e | 159 | printf ("skipping, no such directory\n"); |
719f4b84 | 160 | continue; |
565a919e KP |
161 | case EACCES: |
162 | case EROFS: | |
719f4b84 PL |
163 | /* That's ok, caches go to /var anyway. */ |
164 | /* Ideally we'd do an access on the hashed_name. */ | |
165 | /* But we hid that behind an abstraction barrier. */ | |
565a919e KP |
166 | break; |
167 | default: | |
0c35c0fa KP |
168 | fprintf (stderr, "\"%s\": ", dir); |
169 | perror (""); | |
170 | ret++; | |
719f4b84 | 171 | |
719f4b84 | 172 | continue; |
0c35c0fa | 173 | } |
0c35c0fa | 174 | } |
565a919e KP |
175 | if (stat ((char *) dir, &statb) == -1) |
176 | { | |
177 | fprintf (stderr, "\"%s\": ", dir); | |
178 | perror (""); | |
179 | ret++; | |
180 | continue; | |
181 | } | |
0c35c0fa KP |
182 | if (!S_ISDIR (statb.st_mode)) |
183 | { | |
184 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
185 | continue; | |
186 | } | |
d2c01029 PL |
187 | |
188 | if (really_force) | |
189 | FcDirCacheUnlink (dir, config); | |
190 | ||
bc5e487f KP |
191 | cache = NULL; |
192 | was_valid = FcFalse; | |
193 | if (!force) { | |
194 | cache = FcDirCacheLoad (dir, config, NULL); | |
195 | if (cache) | |
196 | was_valid = FcTrue; | |
197 | } | |
2d3387fd | 198 | |
bc5e487f | 199 | if (!cache) |
179c3995 | 200 | { |
bc5e487f KP |
201 | cache = FcDirCacheRead (dir, FcTrue, config); |
202 | if (!cache) | |
203 | { | |
204 | fprintf (stderr, "%s: error scanning\n", dir); | |
205 | ret++; | |
206 | continue; | |
207 | } | |
179c3995 | 208 | } |
bc5e487f | 209 | |
bc5e487f | 210 | if (was_valid) |
179c3995 KP |
211 | { |
212 | if (verbose) | |
213 | printf ("skipping, %d fonts, %d dirs\n", | |
4984242e | 214 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); |
179c3995 KP |
215 | } |
216 | else | |
217 | { | |
218 | if (verbose) | |
219 | printf ("caching, %d fonts, %d dirs\n", | |
4984242e | 220 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); |
4262e0b3 | 221 | |
f57783d2 | 222 | if (!FcDirCacheValid (dir)) |
179c3995 | 223 | { |
2d3387fd KP |
224 | fprintf (stderr, "%s: failed to write cache\n", dir); |
225 | (void) FcDirCacheUnlink (dir, config); | |
179c3995 KP |
226 | ret++; |
227 | } | |
228 | } | |
bc5e487f KP |
229 | |
230 | subdirs = FcStrSetCreate (); | |
231 | if (!subdirs) | |
232 | { | |
233 | fprintf (stderr, "%s: Can't create subdir set\n", dir); | |
234 | ret++; | |
235 | FcDirCacheUnload (cache); | |
236 | continue; | |
237 | } | |
4984242e | 238 | for (i = 0; i < FcCacheNumSubdir (cache); i++) |
bc5e487f KP |
239 | FcStrSetAdd (subdirs, FcCacheSubdir (cache, i)); |
240 | ||
241 | FcDirCacheUnload (cache); | |
242 | ||
179c3995 | 243 | sublist = FcStrListCreate (subdirs); |
5c1853cd | 244 | FcStrSetDestroy (subdirs); |
179c3995 KP |
245 | if (!sublist) |
246 | { | |
2d3387fd | 247 | fprintf (stderr, "%s: Can't create subdir list\n", dir); |
179c3995 KP |
248 | ret++; |
249 | continue; | |
250 | } | |
660acf8f | 251 | FcStrSetAdd (processed_dirs, dir); |
bc5e487f | 252 | ret += scanDirs (sublist, config, force, really_force, verbose); |
179c3995 KP |
253 | } |
254 | FcStrListDone (list); | |
255 | return ret; | |
256 | } | |
257 | ||
d8ab9e6c KP |
258 | static FcBool |
259 | cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) | |
260 | { | |
261 | DIR *d; | |
262 | struct dirent *ent; | |
b190ad9d | 263 | FcChar8 *dir_base; |
d8ab9e6c KP |
264 | FcBool ret = FcTrue; |
265 | FcBool remove; | |
266 | FcCache *cache; | |
267 | struct stat file_stat; | |
268 | struct stat target_stat; | |
269 | ||
b190ad9d | 270 | dir_base = FcStrPlus (dir, (FcChar8 *) "/"); |
1741499e KP |
271 | if (!dir_base) |
272 | { | |
273 | fprintf (stderr, "%s: out of memory\n", dir); | |
274 | return FcFalse; | |
275 | } | |
d8ab9e6c KP |
276 | if (access ((char *) dir, W_OK|X_OK) != 0) |
277 | { | |
278 | if (verbose) | |
bc5e487f | 279 | printf ("%s: not cleaning unwritable cache directory\n", dir); |
1741499e | 280 | FcStrFree (dir_base); |
d8ab9e6c KP |
281 | return FcTrue; |
282 | } | |
bc5e487f KP |
283 | if (verbose) |
284 | printf ("%s: cleaning cache directory\n", dir); | |
b190ad9d | 285 | d = opendir ((char *) dir); |
d8ab9e6c KP |
286 | if (!d) |
287 | { | |
b190ad9d | 288 | perror ((char *) dir); |
1741499e | 289 | FcStrFree (dir_base); |
d8ab9e6c KP |
290 | return FcFalse; |
291 | } | |
292 | while ((ent = readdir (d))) | |
293 | { | |
294 | FcChar8 *file_name; | |
4984242e | 295 | const FcChar8 *target_dir; |
d8ab9e6c KP |
296 | |
297 | if (ent->d_name[0] == '.') | |
298 | continue; | |
b190ad9d | 299 | file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name); |
d8ab9e6c KP |
300 | if (!file_name) |
301 | { | |
302 | fprintf (stderr, "%s: allocation failure\n", dir); | |
303 | ret = FcFalse; | |
304 | break; | |
305 | } | |
bc5e487f | 306 | cache = FcDirCacheLoadFile (file_name, &file_stat); |
d8ab9e6c KP |
307 | if (!cache) |
308 | { | |
309 | fprintf (stderr, "%s: invalid cache file: %s\n", dir, ent->d_name); | |
310 | FcStrFree (file_name); | |
311 | ret = FcFalse; | |
312 | continue; | |
313 | } | |
314 | target_dir = FcCacheDir (cache); | |
315 | remove = FcFalse; | |
b190ad9d | 316 | if (stat ((char *) target_dir, &target_stat) < 0) |
d8ab9e6c KP |
317 | { |
318 | if (verbose) | |
319 | printf ("%s: %s: missing directory: %s \n", | |
320 | dir, ent->d_name, target_dir); | |
321 | remove = FcTrue; | |
322 | } | |
323 | else if (target_stat.st_mtime > file_stat.st_mtime) | |
324 | { | |
325 | if (verbose) | |
326 | printf ("%s: %s: cache outdated: %s\n", | |
327 | dir, ent->d_name, target_dir); | |
328 | remove = FcTrue; | |
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 | } |