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>
49 #define sleep(x) Sleep((x) * 1000)
55 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
57 #define dgettext(d, s) (s)
68 #ifndef HAVE_GETOPT_LONG
69 #define HAVE_GETOPT_LONG 0
76 const struct option longopts[] = {
77 {"error-on-no-fonts", 0, 0, 'E'},
79 {"really-force", 0, 0, 'r'},
80 {"sysroot", required_argument, 0, 'y'},
81 {"system-only", 0, 0, 's'},
82 {"version", 0, 0, 'V'},
83 {"verbose", 0, 0, 'v'},
90 extern int optind, opterr, optopt;
95 usage (char *program, int error)
97 FILE *file = error ? stderr : stdout;
99 fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [--error-on-no-fonts] [--force|--really-force] [--sysroot=SYSROOT] [--system-only] [--verbose] [--version] [--help] [dirs]\n"),
102 fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [dirs]\n"),
105 fprintf (file, _("Build font information caches in [dirs]\n"
106 "(all directories in font configuration by default).\n"));
107 fprintf (file, "\n");
109 fprintf (file, _(" -E, --error-on-no-fonts raise an error if no fonts in a directory\n"));
110 fprintf (file, _(" -f, --force scan directories with apparently valid caches\n"));
111 fprintf (file, _(" -r, --really-force erase all existing caches, then rescan\n"));
112 fprintf (file, _(" -s, --system-only scan system-wide directories only\n"));
113 fprintf (file, _(" -y, --sysroot=SYSROOT prepend SYSROOT to all paths for scanning\n"));
114 fprintf (file, _(" -v, --verbose display status information while busy\n"));
115 fprintf (file, _(" -V, --version display font config version and exit\n"));
116 fprintf (file, _(" -h, --help display this help and exit\n"));
118 fprintf (file, _(" -E (error-on-no-fonts)\n"));
119 fprintf (file, _(" raise an error if no fonts in a directory\n"));
120 fprintf (file, _(" -f (force) scan directories with apparently valid caches\n"));
121 fprintf (file, _(" -r, (really force) erase all existing caches, then rescan\n"));
122 fprintf (file, _(" -s (system) scan system-wide directories only\n"));
123 fprintf (file, _(" -y SYSROOT (sysroot) prepend SYSROOT to all paths for scanning\n"));
124 fprintf (file, _(" -v (verbose) display status information while busy\n"));
125 fprintf (file, _(" -V (version) display font config version and exit\n"));
126 fprintf (file, _(" -h (help) display this help and exit\n"));
131 static FcStrSet *processed_dirs;
134 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, FcBool error_on_no_fonts, int *changed)
142 FcBool was_valid, was_processed = FcFalse;
144 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
147 * Now scan all of the directories into separate databases
148 * and write out the results
150 while ((dir = FcStrListNext (list)))
155 printf ("[%s]", sysroot);
156 printf ("%s: ", dir);
160 if (FcStrSetMember (processed_dirs, dir))
163 printf (_("skipping, looped directory detected\n"));
167 if (stat ((char *) dir, &statb) == -1)
173 printf (_("skipping, no such directory\n"));
176 fprintf (stderr, "\"%s\": ", dir);
184 if (!S_ISDIR (statb.st_mode))
186 fprintf (stderr, _("\"%s\": not a directory, skipping\n"), dir);
189 was_processed = FcTrue;
193 FcDirCacheUnlink (dir, config);
194 FcDirCacheCreateUUID ((FcChar8 *) dir, FcTrue, config);
200 cache = FcDirCacheLoad (dir, config, NULL);
208 cache = FcDirCacheRead (dir, FcTrue, config);
211 fprintf (stderr, _("\"%s\": scanning error\n"), dir);
220 printf (_("skipping, existing cache is valid: %d fonts, %d dirs\n"),
221 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
226 printf (_("caching, new cache contents: %d fonts, %d dirs\n"),
227 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
229 if (!FcDirCacheValid (dir))
231 fprintf (stderr, _("%s: failed to write cache\n"), dir);
232 (void) FcDirCacheUnlink (dir, config);
237 subdirs = FcStrSetCreate ();
240 fprintf (stderr, _("%s: Can't create subdir set\n"), dir);
242 FcDirCacheUnload (cache);
245 for (i = 0; i < FcCacheNumSubdir (cache); i++)
246 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
248 FcDirCacheUnload (cache);
250 sublist = FcStrListCreate (subdirs);
251 FcStrSetDestroy (subdirs);
254 fprintf (stderr, _("%s: Can't create subdir list\n"), dir);
258 FcStrSetAdd (processed_dirs, dir);
259 ret += scanDirs (sublist, config, force, really_force, verbose, error_on_no_fonts, changed);
260 FcStrListDone (sublist);
262 if (error_on_no_fonts && !was_processed)
268 cleanCacheDirectories (FcConfig *config, FcBool verbose)
270 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
276 while ((cache_dir = FcStrListNext (cache_dirs)))
278 if (!FcDirCacheClean (cache_dir, verbose))
284 FcStrListDone (cache_dirs);
289 main (int argc, char **argv)
293 FcBool verbose = FcFalse;
294 FcBool force = FcFalse;
295 FcBool really_force = FcFalse;
296 FcBool systemOnly = FcFalse;
297 FcBool error_on_no_fonts = FcFalse;
299 FcChar8 *sysroot = NULL;
303 #if HAVE_GETOPT_LONG || HAVE_GETOPT
306 setlocale (LC_ALL, "");
308 while ((c = getopt_long (argc, argv, "Efrsy:Vvh", longopts, NULL)) != -1)
310 while ((c = getopt (argc, argv, "Efrsy:Vvh")) != -1)
315 error_on_no_fonts = FcTrue;
318 really_force = FcTrue;
327 sysroot = FcStrCopy ((const FcChar8 *)optarg);
330 fprintf (stderr, "fontconfig version %d.%d.%d\n",
331 FC_MAJOR, FC_MINOR, FC_REVISION);
348 FcConfigEnableHome (FcFalse);
351 FcConfigSetSysRoot (NULL, sysroot);
353 config = FcConfigGetCurrent();
357 config = FcInitLoadConfig ();
361 fprintf (stderr, _("%s: Can't initialize font config library\n"), argv[0]);
364 FcConfigSetCurrent (config);
368 dirs = FcStrSetCreate ();
371 fprintf (stderr, _("%s: Can't create list of directories\n"),
377 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
379 fprintf (stderr, _("%s: Can't add directory\n"), argv[0]);
384 list = FcStrListCreate (dirs);
385 FcStrSetDestroy (dirs);
388 list = FcConfigGetFontDirs (config);
390 if ((processed_dirs = FcStrSetCreate()) == NULL) {
391 fprintf(stderr, _("Out of Memory\n"));
396 ret = scanDirs (list, config, force, really_force, verbose, error_on_no_fonts, &changed);
397 FcStrListDone (list);
400 * Try to create CACHEDIR.TAG anyway.
401 * This expects the fontconfig cache directory already exists.
402 * If it doesn't, it won't be simply created.
404 FcCacheCreateTagFile (config);
406 FcStrSetDestroy (processed_dirs);
408 cleanCacheDirectories (config, verbose);
410 FcConfigDestroy (config);
413 * Now we need to sleep a second (or two, to be extra sure), to make
414 * sure that timestamps for changes after this run of fc-cache are later
415 * then any timestamps we wrote. We don't use gettimeofday() because
416 * sleep(3) can't be interrupted by a signal here -- this isn't in the
417 * library, and there aren't any signals flying around here.
419 /* the resolution of mtime on FAT is 2 seconds */
423 printf ("%s: %s\n", argv[0], ret ? _("failed") : _("succeeded"));