]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
317b8492 | 2 | * fontconfig/fc-cache/fc-cache.c |
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'}, | |
1439c8f2 | 75 | {"help", 0, 0, 'h'}, |
24330d27 KP |
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 | 85 | static void |
1439c8f2 | 86 | usage (char *program, int error) |
24330d27 | 87 | { |
1439c8f2 | 88 | FILE *file = error ? stderr : stdout; |
86b12431 | 89 | #if HAVE_GETOPT_LONG |
1439c8f2 | 90 | fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", |
86b12431 KP |
91 | program); |
92 | #else | |
1439c8f2 | 93 | fprintf (file, "usage: %s [-frsvVh] [dirs]\n", |
24330d27 | 94 | program); |
86b12431 | 95 | #endif |
1439c8f2 | 96 | fprintf (file, "Build font information caches in [dirs]\n" |
24330d27 | 97 | "(all directories in font configuration by default).\n"); |
1439c8f2 | 98 | fprintf (file, "\n"); |
86b12431 | 99 | #if HAVE_GETOPT_LONG |
1439c8f2 BE |
100 | fprintf (file, " -f, --force scan directories with apparently valid caches\n"); |
101 | fprintf (file, " -r, --really-force erase all existing caches, then rescan\n"); | |
102 | fprintf (file, " -s, --system-only scan system-wide directories only\n"); | |
103 | fprintf (file, " -v, --verbose display status information while busy\n"); | |
104 | fprintf (file, " -V, --version display font config version and exit\n"); | |
105 | fprintf (file, " -h, --help display this help and exit\n"); | |
86b12431 | 106 | #else |
1439c8f2 BE |
107 | fprintf (file, " -f (force) scan directories with apparently valid caches\n"); |
108 | fprintf (file, " -r, (really force) erase all existing caches, then rescan\n"); | |
109 | fprintf (file, " -s (system) scan system-wide directories only\n"); | |
110 | fprintf (file, " -v (verbose) display status information while busy\n"); | |
111 | fprintf (file, " -V (version) display font config version and exit\n"); | |
112 | fprintf (file, " -h (help) display this help and exit\n"); | |
86b12431 | 113 | #endif |
1439c8f2 | 114 | exit (error); |
24330d27 KP |
115 | } |
116 | ||
660acf8f PL |
117 | static FcStrSet *processed_dirs; |
118 | ||
179c3995 | 119 | static int |
bc5e487f | 120 | scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose) |
24330d27 | 121 | { |
4984242e KP |
122 | int ret = 0; |
123 | const FcChar8 *dir; | |
124 | FcStrSet *subdirs; | |
125 | FcStrList *sublist; | |
126 | FcCache *cache; | |
127 | struct stat statb; | |
128 | FcBool was_valid; | |
129 | int i; | |
179c3995 KP |
130 | |
131 | /* | |
132 | * Now scan all of the directories into separate databases | |
133 | * and write out the results | |
134 | */ | |
db50cbda | 135 | while ((dir = FcStrListNext (list))) |
179c3995 KP |
136 | { |
137 | if (verbose) | |
138 | { | |
bc5e487f | 139 | printf ("%s: ", dir); |
179c3995 KP |
140 | fflush (stdout); |
141 | } | |
275cf6cd PL |
142 | |
143 | if (!dir) | |
144 | { | |
145 | if (verbose) | |
146 | printf ("skipping, no such directory\n"); | |
147 | continue; | |
148 | } | |
149 | ||
660acf8f PL |
150 | if (FcStrSetMember (processed_dirs, dir)) |
151 | { | |
152 | if (verbose) | |
153 | printf ("skipping, looped directory detected\n"); | |
154 | continue; | |
155 | } | |
275cf6cd | 156 | |
e12f718f | 157 | if (stat ((char *) dir, &statb) == -1) |
0c35c0fa | 158 | { |
565a919e KP |
159 | switch (errno) { |
160 | case ENOENT: | |
161 | case ENOTDIR: | |
5d43e799 | 162 | if (verbose) |
565a919e | 163 | printf ("skipping, no such directory\n"); |
565a919e KP |
164 | break; |
165 | default: | |
0c35c0fa KP |
166 | fprintf (stderr, "\"%s\": ", dir); |
167 | perror (""); | |
168 | ret++; | |
e12f718f | 169 | break; |
0c35c0fa | 170 | } |
565a919e KP |
171 | continue; |
172 | } | |
e12f718f | 173 | |
0c35c0fa KP |
174 | if (!S_ISDIR (statb.st_mode)) |
175 | { | |
176 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
177 | continue; | |
178 | } | |
d2c01029 PL |
179 | |
180 | if (really_force) | |
181 | FcDirCacheUnlink (dir, config); | |
182 | ||
bc5e487f KP |
183 | cache = NULL; |
184 | was_valid = FcFalse; | |
185 | if (!force) { | |
186 | cache = FcDirCacheLoad (dir, config, NULL); | |
187 | if (cache) | |
188 | was_valid = FcTrue; | |
189 | } | |
2d3387fd | 190 | |
bc5e487f | 191 | if (!cache) |
179c3995 | 192 | { |
bc5e487f KP |
193 | cache = FcDirCacheRead (dir, FcTrue, config); |
194 | if (!cache) | |
195 | { | |
196 | fprintf (stderr, "%s: error scanning\n", dir); | |
197 | ret++; | |
198 | continue; | |
199 | } | |
179c3995 | 200 | } |
bc5e487f | 201 | |
bc5e487f | 202 | if (was_valid) |
179c3995 KP |
203 | { |
204 | if (verbose) | |
50124d1e | 205 | printf ("skipping, existing cache is valid: %d fonts, %d dirs\n", |
4984242e | 206 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); |
179c3995 KP |
207 | } |
208 | else | |
209 | { | |
210 | if (verbose) | |
50124d1e | 211 | printf ("caching, new cache contents: %d fonts, %d dirs\n", |
4984242e | 212 | FcCacheNumFont (cache), FcCacheNumSubdir (cache)); |
4262e0b3 | 213 | |
f57783d2 | 214 | if (!FcDirCacheValid (dir)) |
179c3995 | 215 | { |
2d3387fd KP |
216 | fprintf (stderr, "%s: failed to write cache\n", dir); |
217 | (void) FcDirCacheUnlink (dir, config); | |
179c3995 KP |
218 | ret++; |
219 | } | |
220 | } | |
bc5e487f KP |
221 | |
222 | subdirs = FcStrSetCreate (); | |
223 | if (!subdirs) | |
224 | { | |
225 | fprintf (stderr, "%s: Can't create subdir set\n", dir); | |
226 | ret++; | |
227 | FcDirCacheUnload (cache); | |
228 | continue; | |
229 | } | |
4984242e | 230 | for (i = 0; i < FcCacheNumSubdir (cache); i++) |
bc5e487f KP |
231 | FcStrSetAdd (subdirs, FcCacheSubdir (cache, i)); |
232 | ||
233 | FcDirCacheUnload (cache); | |
234 | ||
179c3995 | 235 | sublist = FcStrListCreate (subdirs); |
5c1853cd | 236 | FcStrSetDestroy (subdirs); |
179c3995 KP |
237 | if (!sublist) |
238 | { | |
2d3387fd | 239 | fprintf (stderr, "%s: Can't create subdir list\n", dir); |
179c3995 KP |
240 | ret++; |
241 | continue; | |
242 | } | |
660acf8f | 243 | FcStrSetAdd (processed_dirs, dir); |
bc5e487f | 244 | ret += scanDirs (sublist, config, force, really_force, verbose); |
179c3995 KP |
245 | } |
246 | FcStrListDone (list); | |
247 | return ret; | |
248 | } | |
249 | ||
d8ab9e6c KP |
250 | static FcBool |
251 | cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) | |
252 | { | |
253 | DIR *d; | |
254 | struct dirent *ent; | |
b190ad9d | 255 | FcChar8 *dir_base; |
d8ab9e6c KP |
256 | FcBool ret = FcTrue; |
257 | FcBool remove; | |
258 | FcCache *cache; | |
d8ab9e6c KP |
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; |
9b84ecff | 305 | cache = FcDirCacheLoadFile (file_name, NULL); |
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); | |
2a3e3c44 KP |
320 | remove = FcTrue; |
321 | } | |
d8ab9e6c KP |
322 | } |
323 | if (remove) | |
324 | { | |
b190ad9d | 325 | if (unlink ((char *) file_name) < 0) |
d8ab9e6c | 326 | { |
b190ad9d | 327 | perror ((char *) file_name); |
d8ab9e6c KP |
328 | ret = FcFalse; |
329 | } | |
330 | } | |
1741499e | 331 | FcDirCacheUnload (cache); |
d8ab9e6c KP |
332 | FcStrFree (file_name); |
333 | } | |
334 | ||
335 | closedir (d); | |
1741499e | 336 | FcStrFree (dir_base); |
d8ab9e6c KP |
337 | return ret; |
338 | } | |
339 | ||
340 | static FcBool | |
341 | cleanCacheDirectories (FcConfig *config, FcBool verbose) | |
342 | { | |
343 | FcStrList *cache_dirs = FcConfigGetCacheDirs (config); | |
344 | FcChar8 *cache_dir; | |
345 | FcBool ret = FcTrue; | |
346 | ||
347 | if (!cache_dirs) | |
348 | return FcFalse; | |
349 | while ((cache_dir = FcStrListNext (cache_dirs))) | |
350 | { | |
351 | if (!cleanCacheDirectory (config, cache_dir, verbose)) | |
352 | { | |
353 | ret = FcFalse; | |
354 | break; | |
355 | } | |
356 | } | |
357 | FcStrListDone (cache_dirs); | |
358 | return ret; | |
359 | } | |
360 | ||
179c3995 KP |
361 | int |
362 | main (int argc, char **argv) | |
363 | { | |
364 | FcStrSet *dirs; | |
365 | FcStrList *list; | |
366 | FcBool verbose = FcFalse; | |
367 | FcBool force = FcFalse; | |
d2c01029 | 368 | FcBool really_force = FcFalse; |
ff3f1f98 | 369 | FcBool systemOnly = FcFalse; |
179c3995 | 370 | FcConfig *config; |
24330d27 | 371 | int i; |
179c3995 | 372 | int ret; |
24330d27 KP |
373 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
374 | int c; | |
375 | ||
376 | #if HAVE_GETOPT_LONG | |
1439c8f2 | 377 | while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1) |
24330d27 | 378 | #else |
1439c8f2 | 379 | while ((c = getopt (argc, argv, "frsVvh")) != -1) |
24330d27 KP |
380 | #endif |
381 | { | |
382 | switch (c) { | |
d2c01029 PL |
383 | case 'r': |
384 | really_force = FcTrue; | |
385 | /* fall through */ | |
179c3995 KP |
386 | case 'f': |
387 | force = FcTrue; | |
388 | break; | |
ff3f1f98 KP |
389 | case 's': |
390 | systemOnly = FcTrue; | |
391 | break; | |
24330d27 KP |
392 | case 'V': |
393 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
394 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
395 | exit (0); | |
396 | case 'v': | |
179c3995 | 397 | verbose = FcTrue; |
24330d27 | 398 | break; |
1439c8f2 BE |
399 | case 'h': |
400 | usage (argv[0], 0); | |
24330d27 | 401 | default: |
1439c8f2 | 402 | usage (argv[0], 1); |
24330d27 KP |
403 | } |
404 | } | |
405 | i = optind; | |
406 | #else | |
407 | i = 1; | |
408 | #endif | |
409 | ||
ff3f1f98 KP |
410 | if (systemOnly) |
411 | FcConfigEnableHome (FcFalse); | |
5e678e94 | 412 | config = FcInitLoadConfig (); |
179c3995 | 413 | if (!config) |
24330d27 | 414 | { |
179c3995 | 415 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); |
24330d27 KP |
416 | return 1; |
417 | } | |
df3efc11 | 418 | FcConfigSetCurrent (config); |
212c9f43 | 419 | |
24330d27 | 420 | if (argv[i]) |
24330d27 | 421 | { |
179c3995 KP |
422 | dirs = FcStrSetCreate (); |
423 | if (!dirs) | |
24330d27 | 424 | { |
179c3995 KP |
425 | fprintf (stderr, "%s: Can't create list of directories\n", |
426 | argv[0]); | |
427 | return 1; | |
24330d27 | 428 | } |
179c3995 | 429 | while (argv[i]) |
24330d27 | 430 | { |
9b511b29 | 431 | if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i])) |
24330d27 | 432 | { |
179c3995 KP |
433 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); |
434 | return 1; | |
24330d27 | 435 | } |
179c3995 | 436 | i++; |
24330d27 | 437 | } |
179c3995 KP |
438 | list = FcStrListCreate (dirs); |
439 | FcStrSetDestroy (dirs); | |
24330d27 | 440 | } |
179c3995 KP |
441 | else |
442 | list = FcConfigGetConfigDirs (config); | |
660acf8f PL |
443 | |
444 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
445 | fprintf(stderr, "Cannot malloc\n"); | |
446 | return 1; | |
447 | } | |
448 | ||
bc5e487f | 449 | ret = scanDirs (list, config, force, really_force, verbose); |
660acf8f PL |
450 | |
451 | FcStrSetDestroy (processed_dirs); | |
452 | ||
d8ab9e6c KP |
453 | cleanCacheDirectories (config, verbose); |
454 | ||
54560b01 KP |
455 | /* |
456 | * Now we need to sleep a second (or two, to be extra sure), to make | |
457 | * sure that timestamps for changes after this run of fc-cache are later | |
458 | * then any timestamps we wrote. We don't use gettimeofday() because | |
459 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
460 | * library, and there aren't any signals flying around here. | |
461 | */ | |
716ac8b8 | 462 | FcConfigDestroy (config); |
c6c9400d | 463 | FcFini (); |
54560b01 | 464 | sleep (2); |
24330d27 KP |
465 | if (verbose) |
466 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
467 | return ret; | |
468 | } |