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>
40 #include <sys/types.h>
53 #define sleep(x) Sleep((x) * 1000)
59 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
61 #define dgettext(d, s) (s)
70 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
76 #ifndef HAVE_GETOPT_LONG
77 #define HAVE_GETOPT_LONG 0
84 const struct option longopts[] = {
85 {"error-on-no-fonts", 0, 0, 'E'},
87 {"really-force", 0, 0, 'r'},
88 {"sysroot", required_argument, 0, 'y'},
89 {"system-only", 0, 0, 's'},
90 {"version", 0, 0, 'V'},
91 {"verbose", 0, 0, 'v'},
98 extern int optind, opterr, optopt;
103 usage (char *program, int error)
105 FILE *file = error ? stderr : stdout;
107 fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [--error-on-no-fonts] [--force|--really-force] [--sysroot=SYSROOT] [--system-only] [--verbose] [--version] [--help] [dirs]\n"),
110 fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [dirs]\n"),
113 fprintf (file, _("Build font information caches in [dirs]\n"
114 "(all directories in font configuration by default).\n"));
115 fprintf (file, "\n");
117 fprintf (file, _(" -E, --error-on-no-fonts raise an error if no fonts in a directory\n"));
118 fprintf (file, _(" -f, --force scan directories with apparently valid caches\n"));
119 fprintf (file, _(" -r, --really-force erase all existing caches, then rescan\n"));
120 fprintf (file, _(" -s, --system-only scan system-wide directories only\n"));
121 fprintf (file, _(" -y, --sysroot=SYSROOT prepend SYSROOT to all paths for scanning\n"));
122 fprintf (file, _(" -v, --verbose display status information while busy\n"));
123 fprintf (file, _(" -V, --version display font config version and exit\n"));
124 fprintf (file, _(" -h, --help display this help and exit\n"));
126 fprintf (file, _(" -E (error-on-no-fonts)\n"));
127 fprintf (file, _(" raise an error if no fonts in a directory\n"));
128 fprintf (file, _(" -f (force) scan directories with apparently valid caches\n"));
129 fprintf (file, _(" -r, (really force) erase all existing caches, then rescan\n"));
130 fprintf (file, _(" -s (system) scan system-wide directories only\n"));
131 fprintf (file, _(" -y SYSROOT (sysroot) prepend SYSROOT to all paths for scanning\n"));
132 fprintf (file, _(" -v (verbose) display status information while busy\n"));
133 fprintf (file, _(" -V (version) display font config version and exit\n"));
134 fprintf (file, _(" -h (help) display this help and exit\n"));
139 static FcStrSet *processed_dirs;
142 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, FcBool error_on_no_fonts, int *changed)
150 FcBool was_valid, was_processed = FcFalse;
152 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
155 * Now scan all of the directories into separate databases
156 * and write out the results
158 while ((dir = FcStrListNext (list)))
163 printf ("[%s]", sysroot);
164 printf ("%s: ", dir);
168 if (FcStrSetMember (processed_dirs, dir))
171 printf (_("skipping, looped directory detected\n"));
175 FcChar8 *rooted_dir = NULL;
178 rooted_dir = FcStrPlus(sysroot, dir);
181 rooted_dir = FcStrCopy(dir);
184 if (stat ((char *) rooted_dir, &statb) == -1)
190 printf (_("skipping, no such directory\n"));
193 fprintf (stderr, "\"%s\": ", dir);
198 FcStrFree (rooted_dir);
203 FcStrFree(rooted_dir);
206 if (!S_ISDIR (statb.st_mode))
208 fprintf (stderr, _("\"%s\": not a directory, skipping\n"), dir);
211 was_processed = FcTrue;
215 FcDirCacheUnlink (dir, config);
221 cache = FcDirCacheLoad (dir, config, NULL);
229 cache = FcDirCacheRead (dir, FcTrue, config);
232 fprintf (stderr, _("\"%s\": scanning error\n"), dir);
241 printf (_("skipping, existing cache is valid: %d fonts, %d dirs\n"),
242 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
247 printf (_("caching, new cache contents: %d fonts, %d dirs\n"),
248 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
250 if (!FcDirCacheValid (dir))
252 fprintf (stderr, _("%s: failed to write cache\n"), dir);
253 (void) FcDirCacheUnlink (dir, config);
258 subdirs = FcStrSetCreate ();
261 fprintf (stderr, _("%s: Can't create subdir set\n"), dir);
263 FcDirCacheUnload (cache);
266 for (i = 0; i < FcCacheNumSubdir (cache); i++)
267 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
269 FcDirCacheUnload (cache);
271 sublist = FcStrListCreate (subdirs);
272 FcStrSetDestroy (subdirs);
275 fprintf (stderr, _("%s: Can't create subdir list\n"), dir);
279 FcStrSetAdd (processed_dirs, dir);
280 ret += scanDirs (sublist, config, force, really_force, verbose, error_on_no_fonts, changed);
281 FcStrListDone (sublist);
284 if (error_on_no_fonts && !was_processed)
290 cleanCacheDirectories (FcConfig *config, FcBool verbose)
292 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
298 while ((cache_dir = FcStrListNext (cache_dirs)))
300 if (!FcDirCacheClean (cache_dir, verbose))
306 FcStrListDone (cache_dirs);
311 main (int argc, char **argv)
315 FcBool verbose = FcFalse;
316 FcBool force = FcFalse;
317 FcBool really_force = FcFalse;
318 FcBool systemOnly = FcFalse;
319 FcBool error_on_no_fonts = FcFalse;
321 FcChar8 *sysroot = NULL;
325 #if HAVE_GETOPT_LONG || HAVE_GETOPT
328 setlocale (LC_ALL, "");
330 while ((c = getopt_long (argc, argv, "Efrsy:Vvh", longopts, NULL)) != -1)
332 while ((c = getopt (argc, argv, "Efrsy:Vvh")) != -1)
337 error_on_no_fonts = FcTrue;
340 really_force = FcTrue;
349 sysroot = FcStrCopy ((const FcChar8 *)optarg);
352 fprintf (stderr, "fontconfig version %d.%d.%d\n",
353 FC_MAJOR, FC_MINOR, FC_REVISION);
370 FcConfigEnableHome (FcFalse);
373 FcConfigSetSysRoot (NULL, sysroot);
375 config = FcConfigGetCurrent();
379 config = FcInitLoadConfig ();
383 fprintf (stderr, _("%s: Can't initialize font config library\n"), argv[0]);
386 FcConfigSetCurrent (config);
390 dirs = FcStrSetCreate ();
393 fprintf (stderr, _("%s: Can't create list of directories\n"),
399 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
401 fprintf (stderr, _("%s: Can't add directory\n"), argv[0]);
406 list = FcStrListCreate (dirs);
407 FcStrSetDestroy (dirs);
410 list = FcConfigGetFontDirs (config);
412 if ((processed_dirs = FcStrSetCreate()) == NULL) {
413 fprintf(stderr, _("Out of Memory\n"));
421 printf ("Font directories:\n");
422 while ((dir = FcStrListNext (list)))
424 printf ("\t%s\n", dir);
426 FcStrListFirst(list);
429 ret = scanDirs (list, config, force, really_force, verbose, error_on_no_fonts, &changed);
430 FcStrListDone (list);
433 * Try to create CACHEDIR.TAG anyway.
434 * This expects the fontconfig cache directory already exists.
435 * If it doesn't, it won't be simply created.
437 FcCacheCreateTagFile (config);
439 FcStrSetDestroy (processed_dirs);
441 cleanCacheDirectories (config, verbose);
443 FcConfigDestroy (config);
446 * Now we need to sleep a second (or two, to be extra sure), to make
447 * sure that timestamps for changes after this run of fc-cache are later
448 * then any timestamps we wrote. We don't use gettimeofday() because
449 * sleep(3) can't be interrupted by a signal here -- this isn't in the
450 * library, and there aren't any signals flying around here.
452 /* the resolution of mtime on FAT is 2 seconds */
456 printf ("%s: %s\n", argv[0], ret ? _("failed") : _("succeeded"));