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.
29 #define HAVE_GETOPT_LONG 1
34 #include <fontconfig/fontconfig.h>
38 #include <sys/types.h>
48 #define sleep(x) Sleep((x) * 1000)
54 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
56 #define dgettext(d, s) (s)
67 #ifndef HAVE_GETOPT_LONG
68 #define HAVE_GETOPT_LONG 0
75 const struct option longopts[] = {
76 {"error-on-no-fonts", 0, 0, 'E'},
78 {"really-force", 0, 0, 'r'},
79 {"sysroot", required_argument, 0, 'y'},
80 {"system-only", 0, 0, 's'},
81 {"version", 0, 0, 'V'},
82 {"verbose", 0, 0, 'v'},
89 extern int optind, opterr, optopt;
94 usage (char *program, int error)
96 FILE *file = error ? stderr : stdout;
98 fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [--error-on-no-fonts] [--force|--really-force] [--sysroot=SYSROOT] [--system-only] [--verbose] [--version] [--help] [dirs]\n"),
101 fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [dirs]\n"),
104 fprintf (file, _("Build font information caches in [dirs]\n"
105 "(all directories in font configuration by default).\n"));
106 fprintf (file, "\n");
108 fprintf (file, _(" -E, --error-on-no-fonts raise an error if no fonts in a directory\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, _(" -s, --system-only scan system-wide directories only\n"));
112 fprintf (file, _(" -y, --sysroot=SYSROOT prepend SYSROOT to all paths for scanning\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"));
117 fprintf (file, _(" -E (error-on-no-fonts)\n"));
118 fprintf (file, _(" raise an error if no fonts in a directory\n"));
119 fprintf (file, _(" -f (force) scan directories with apparently valid caches\n"));
120 fprintf (file, _(" -r, (really force) erase all existing caches, then rescan\n"));
121 fprintf (file, _(" -s (system) scan system-wide directories only\n"));
122 fprintf (file, _(" -y SYSROOT (sysroot) prepend SYSROOT to all paths for scanning\n"));
123 fprintf (file, _(" -v (verbose) display status information while busy\n"));
124 fprintf (file, _(" -V (version) display font config version and exit\n"));
125 fprintf (file, _(" -h (help) display this help and exit\n"));
130 static FcStrSet *processed_dirs;
133 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, FcBool error_on_no_fonts, int *changed)
141 FcBool was_valid, was_processed = FcFalse;
143 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
146 * Now scan all of the directories into separate databases
147 * and write out the results
149 while ((dir = FcStrListNext (list)))
154 printf ("[%s]", sysroot);
155 printf ("%s: ", dir);
159 if (FcStrSetMember (processed_dirs, dir))
162 printf (_("skipping, looped directory detected\n"));
166 if (stat ((char *) dir, &statb) == -1)
172 printf (_("skipping, no such directory\n"));
175 fprintf (stderr, "\"%s\": ", dir);
183 if (!S_ISDIR (statb.st_mode))
185 fprintf (stderr, _("\"%s\": not a directory, skipping\n"), dir);
188 was_processed = FcTrue;
192 FcDirCacheUnlink (dir, config);
193 FcDirCacheCreateUUID ((FcChar8 *) dir, FcTrue, config);
199 cache = FcDirCacheLoad (dir, config, NULL);
207 cache = FcDirCacheRead (dir, FcTrue, config);
210 fprintf (stderr, _("\"%s\": scanning error\n"), dir);
219 printf (_("skipping, existing cache is valid: %d fonts, %d dirs\n"),
220 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
225 printf (_("caching, new cache contents: %d fonts, %d dirs\n"),
226 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
228 if (!FcDirCacheValid (dir))
230 fprintf (stderr, _("%s: failed to write cache\n"), dir);
231 (void) FcDirCacheUnlink (dir, config);
236 subdirs = FcStrSetCreate ();
239 fprintf (stderr, _("%s: Can't create subdir set\n"), dir);
241 FcDirCacheUnload (cache);
244 for (i = 0; i < FcCacheNumSubdir (cache); i++)
245 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
247 FcDirCacheUnload (cache);
249 sublist = FcStrListCreate (subdirs);
250 FcStrSetDestroy (subdirs);
253 fprintf (stderr, _("%s: Can't create subdir list\n"), dir);
257 FcStrSetAdd (processed_dirs, dir);
258 ret += scanDirs (sublist, config, force, really_force, verbose, error_on_no_fonts, changed);
259 FcStrListDone (sublist);
261 if (error_on_no_fonts && !was_processed)
267 cleanCacheDirectories (FcConfig *config, FcBool verbose)
269 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
275 while ((cache_dir = FcStrListNext (cache_dirs)))
277 if (!FcDirCacheClean (cache_dir, verbose))
283 FcStrListDone (cache_dirs);
288 main (int argc, char **argv)
292 FcBool verbose = FcFalse;
293 FcBool force = FcFalse;
294 FcBool really_force = FcFalse;
295 FcBool systemOnly = FcFalse;
296 FcBool error_on_no_fonts = FcFalse;
298 FcChar8 *sysroot = NULL;
302 #if HAVE_GETOPT_LONG || HAVE_GETOPT
306 while ((c = getopt_long (argc, argv, "Efrsy:Vvh", longopts, NULL)) != -1)
308 while ((c = getopt (argc, argv, "Efrsy:Vvh")) != -1)
313 error_on_no_fonts = FcTrue;
316 really_force = FcTrue;
325 sysroot = FcStrCopy ((const FcChar8 *)optarg);
328 fprintf (stderr, "fontconfig version %d.%d.%d\n",
329 FC_MAJOR, FC_MINOR, FC_REVISION);
346 FcConfigEnableHome (FcFalse);
349 FcConfigSetSysRoot (NULL, sysroot);
351 config = FcConfigGetCurrent();
355 config = FcInitLoadConfig ();
359 fprintf (stderr, _("%s: Can't initialize font config library\n"), argv[0]);
362 FcConfigSetCurrent (config);
366 dirs = FcStrSetCreate ();
369 fprintf (stderr, _("%s: Can't create list of directories\n"),
375 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
377 fprintf (stderr, _("%s: Can't add directory\n"), argv[0]);
382 list = FcStrListCreate (dirs);
383 FcStrSetDestroy (dirs);
386 list = FcConfigGetFontDirs (config);
388 if ((processed_dirs = FcStrSetCreate()) == NULL) {
389 fprintf(stderr, _("Out of Memory\n"));
394 ret = scanDirs (list, config, force, really_force, verbose, error_on_no_fonts, &changed);
395 FcStrListDone (list);
398 * Try to create CACHEDIR.TAG anyway.
399 * This expects the fontconfig cache directory already exists.
400 * If it doesn't, it won't be simply created.
402 FcCacheCreateTagFile (config);
404 FcStrSetDestroy (processed_dirs);
406 cleanCacheDirectories (config, verbose);
408 FcConfigDestroy (config);
411 * Now we need to sleep a second (or two, to be extra sure), to make
412 * sure that timestamps for changes after this run of fc-cache are later
413 * then any timestamps we wrote. We don't use gettimeofday() because
414 * sleep(3) can't be interrupted by a signal here -- this isn't in the
415 * library, and there aren't any signals flying around here.
417 /* the resolution of mtime on FAT is 2 seconds */
421 printf ("%s: %s\n", argv[0], ret ? _("failed") : _("succeeded"));