2 * fontconfig/fc-cache/fc-cache.c
4 * Copyright © 2002 Keith Packard
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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.
25 #include "../src/fcarch.h"
31 #define HAVE_GETOPT_LONG 1
36 #include <fontconfig/fontconfig.h>
40 #include <sys/types.h>
50 #define sleep(x) Sleep((x) * 1000)
61 #ifndef HAVE_GETOPT_LONG
62 #define HAVE_GETOPT_LONG 0
69 const struct option longopts[] = {
71 {"really-force", 0, 0, 'r'},
73 {"system-only", 0, 0, 's'},
74 {"version", 0, 0, 'V'},
75 {"verbose", 0, 0, 'v'},
82 extern int optind, opterr, optopt;
87 usage (char *program, int error)
89 FILE *file = error ? stderr : stdout;
91 fprintf (file, "usage: %s [-frRsvVh] [--force|--really-force] [--root <root>] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
94 fprintf (file, "usage: %s [-frRsvVh] [dirs]\n",
97 fprintf (file, "Build font information caches in [dirs]\n"
98 "(all directories in font configuration by default).\n");
101 fprintf (file, " -f, --force scan directories with apparently valid caches\n");
102 fprintf (file, " -r, --really-force erase all existing caches, then rescan\n");
103 fprintf (file, " -R, --root <root> change to <root> before loading files\n");
104 fprintf (file, " -s, --system-only scan system-wide directories only\n");
105 fprintf (file, " -v, --verbose display status information while busy\n");
106 fprintf (file, " -V, --version display font config version and exit\n");
107 fprintf (file, " -h, --help display this help and exit\n");
109 fprintf (file, " -f (force) scan directories with apparently valid caches\n");
110 fprintf (file, " -r, (really force) erase all existing caches, then rescan\n");
111 fprintf (file, " -R <root> (root) change to <root> before loading files\n");
112 fprintf (file, " -s (system) scan system-wide directories only\n");
113 fprintf (file, " -v (verbose) display status information while busy\n");
114 fprintf (file, " -V (version) display font config version and exit\n");
115 fprintf (file, " -h (help) display this help and exit\n");
120 static FcStrSet *processed_dirs;
123 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
135 * Now scan all of the directories into separate databases
136 * and write out the results
138 while ((dir = FcStrListNext (list)))
142 printf ("%s: ", dir);
149 printf ("skipping, no such directory\n");
153 if (FcStrSetMember (processed_dirs, dir))
156 printf ("skipping, looped directory detected\n");
160 if (FcStat (config, dir, &statb) == -1)
166 printf ("skipping, no such directory\n");
169 fprintf (stderr, "\"%s\": ", dir);
177 if (!S_ISDIR (statb.st_mode))
179 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
184 FcDirCacheUnlink (dir, config);
189 cache = FcDirCacheLoad (dir, config, NULL);
197 cache = FcDirCacheRead (dir, FcTrue, config);
200 fprintf (stderr, "%s: error scanning\n", dir);
209 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
210 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
215 printf ("caching, new cache contents: %d fonts, %d dirs\n",
216 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
218 if (!FcDirCacheValid (dir))
220 fprintf (stderr, "%s: failed to write cache\n", dir);
221 (void) FcDirCacheUnlink (dir, config);
226 subdirs = FcStrSetCreate ();
229 fprintf (stderr, "%s: Can't create subdir set\n", dir);
231 FcDirCacheUnload (cache);
234 for (i = 0; i < FcCacheNumSubdir (cache); i++)
235 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
237 FcDirCacheUnload (cache);
239 sublist = FcStrListCreate (subdirs);
240 FcStrSetDestroy (subdirs);
243 fprintf (stderr, "%s: Can't create subdir list\n", dir);
247 FcStrSetAdd (processed_dirs, dir);
248 ret += scanDirs (sublist, config, force, really_force, verbose, changed);
250 FcStrListDone (list);
255 cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
264 struct stat target_stat;
266 fullDir = FcConfigGetRootPlus (config, dir);
272 if (access ((char *) checkDir, W_OK) != 0)
275 printf ("%s: not cleaning %s cache directory\n", dir,
276 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
280 printf ("%s: cleaning cache directory\n", dir);
281 d = opendir ((char *) checkDir);
284 perror ((char *) dir);
288 while ((ent = readdir (d)))
291 const FcChar8 *target_dir;
293 if (ent->d_name[0] == '.')
295 /* skip cache files for different architectures and */
296 /* files which are not cache files at all */
297 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
298 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
301 file_name = FcStrPathPlus (dir, (const FcChar8 *) ent->d_name, NULL);
304 fprintf (stderr, "%s: allocation failure\n", dir);
309 cache = FcDirCacheLoadFile2 (file_name, config, NULL);
313 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
318 target_dir = FcCacheDir (cache);
319 if (FcStat (config, target_dir, &target_stat) < 0)
322 printf ("%s: %s: missing directory: %s \n",
323 dir, ent->d_name, target_dir);
329 FcChar8 *unlink_file = FcConfigGetRootPlus (config, file_name);
331 unlink_file = file_name;
332 if (unlink ((char *) unlink_file) < 0)
334 perror ((char *) unlink_file);
337 if (unlink_file != file_name)
338 FcStrFree (unlink_file);
340 FcDirCacheUnload (cache);
341 FcStrFree (file_name);
352 cleanCacheDirectories (FcConfig *config, FcBool verbose)
354 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
360 while ((cache_dir = FcStrListNext (cache_dirs)))
362 if (!cleanCacheDirectory (config, cache_dir, verbose))
368 FcStrListDone (cache_dirs);
373 main (int argc, char **argv)
377 FcBool verbose = FcFalse;
378 FcBool force = FcFalse;
379 FcBool really_force = FcFalse;
380 FcBool systemOnly = FcFalse;
386 #if HAVE_GETOPT_LONG || HAVE_GETOPT
390 while ((c = getopt_long (argc, argv, "frR:sVvh", longopts, NULL)) != -1)
392 while ((c = getopt (argc, argv, "frR:sVvh")) != -1)
397 really_force = FcTrue;
409 fprintf (stderr, "fontconfig version %d.%d.%d\n",
410 FC_MAJOR, FC_MINOR, FC_REVISION);
427 FcConfigEnableHome (FcFalse);
428 config = FcInitLoadConfig ();
431 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
434 FcConfigSetCurrent (config);
435 FcConfigSetRoot (config, (const FcChar8 *) rootDir);
439 dirs = FcStrSetCreate ();
442 fprintf (stderr, "%s: Can't create list of directories\n",
448 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
450 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
455 list = FcStrListCreate (dirs);
456 FcStrSetDestroy (dirs);
459 list = FcConfigGetConfigDirs (config);
461 if ((processed_dirs = FcStrSetCreate()) == NULL) {
462 fprintf(stderr, "Cannot malloc\n");
467 ret = scanDirs (list, config, force, really_force, verbose, &changed);
469 FcStrSetDestroy (processed_dirs);
471 cleanCacheDirectories (config, verbose);
474 * Now we need to sleep a second (or two, to be extra sure), to make
475 * sure that timestamps for changes after this run of fc-cache are later
476 * then any timestamps we wrote. We don't use gettimeofday() because
477 * sleep(3) can't be interrupted by a signal here -- this isn't in the
478 * library, and there aren't any signals flying around here.
480 FcConfigDestroy (config);
485 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");