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)
59 #ifndef HAVE_GETOPT_LONG
60 #define HAVE_GETOPT_LONG 0
67 const struct option longopts[] = {
68 {"error-on-no-fonts", 0, 0, 'E'},
70 {"really-force", 0, 0, 'r'},
71 {"sysroot", required_argument, 0, 'y'},
72 {"system-only", 0, 0, 's'},
73 {"version", 0, 0, 'V'},
74 {"verbose", 0, 0, 'v'},
81 extern int optind, opterr, optopt;
86 usage (char *program, int error)
88 FILE *file = error ? stderr : stdout;
90 fprintf (file, "usage: %s [-EfrsvVh] [-y SYSROOT] [--error-on-no-fonts] [--force|--really-force] [--sysroot=SYSROOT] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
93 fprintf (file, "usage: %s [-EfrsvVh] [-y SYSROOT] [dirs]\n",
96 fprintf (file, "Build font information caches in [dirs]\n"
97 "(all directories in font configuration by default).\n");
100 fprintf (file, " -E, --error-on-no-fonts raise an error if no fonts in a directory\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, " -s, --system-only scan system-wide directories only\n");
104 fprintf (file, " -y, --sysroot=SYSROOT prepend SYSROOT to all paths for scanning\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, " -E (error-on-no-fonts)\n");
110 fprintf (file, " raise an error if no fonts in a directory\n");
111 fprintf (file, " -f (force) scan directories with apparently valid caches\n");
112 fprintf (file, " -r, (really force) erase all existing caches, then rescan\n");
113 fprintf (file, " -s (system) scan system-wide directories only\n");
114 fprintf (file, " -y SYSROOT (sysroot) prepend SYSROOT to all paths for scanning\n");
115 fprintf (file, " -v (verbose) display status information while busy\n");
116 fprintf (file, " -V (version) display font config version and exit\n");
117 fprintf (file, " -h (help) display this help and exit\n");
122 static FcStrSet *processed_dirs;
125 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, FcBool error_on_no_fonts, int *changed)
133 FcBool was_valid, was_processed = FcFalse;
135 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
138 * Now scan all of the directories into separate databases
139 * and write out the results
141 while ((dir = FcStrListNext (list)))
146 printf ("[%s]", sysroot);
147 printf ("%s: ", dir);
151 if (FcStrSetMember (processed_dirs, dir))
154 printf ("skipping, looped directory detected\n");
158 if (stat ((char *) dir, &statb) == -1)
164 printf ("skipping, no such directory\n");
167 fprintf (stderr, "\"%s\": ", dir);
175 if (!S_ISDIR (statb.st_mode))
177 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
180 was_processed = FcTrue;
183 FcDirCacheUnlink (dir, config);
188 cache = FcDirCacheLoad (dir, config, NULL);
196 cache = FcDirCacheRead (dir, FcTrue, config);
199 fprintf (stderr, "%s: error scanning\n", dir);
208 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
209 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
214 printf ("caching, new cache contents: %d fonts, %d dirs\n",
215 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
217 if (!FcDirCacheValid (dir))
219 fprintf (stderr, "%s: failed to write cache\n", dir);
220 (void) FcDirCacheUnlink (dir, config);
225 subdirs = FcStrSetCreate ();
228 fprintf (stderr, "%s: Can't create subdir set\n", dir);
230 FcDirCacheUnload (cache);
233 for (i = 0; i < FcCacheNumSubdir (cache); i++)
234 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
236 FcDirCacheUnload (cache);
238 sublist = FcStrListCreate (subdirs);
239 FcStrSetDestroy (subdirs);
242 fprintf (stderr, "%s: Can't create subdir list\n", dir);
246 FcStrSetAdd (processed_dirs, dir);
247 ret += scanDirs (sublist, config, force, really_force, verbose, error_on_no_fonts, changed);
248 FcStrListDone (sublist);
250 if (error_on_no_fonts && !was_processed)
256 cleanCacheDirectories (FcConfig *config, FcBool verbose)
258 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
264 while ((cache_dir = FcStrListNext (cache_dirs)))
266 if (!FcDirCacheClean (cache_dir, verbose))
272 FcStrListDone (cache_dirs);
277 main (int argc, char **argv)
281 FcBool verbose = FcFalse;
282 FcBool force = FcFalse;
283 FcBool really_force = FcFalse;
284 FcBool systemOnly = FcFalse;
285 FcBool error_on_no_fonts = FcFalse;
287 FcChar8 *sysroot = NULL;
291 #if HAVE_GETOPT_LONG || HAVE_GETOPT
295 while ((c = getopt_long (argc, argv, "Efrsy:Vvh", longopts, NULL)) != -1)
297 while ((c = getopt (argc, argv, "Efrsy:Vvh")) != -1)
302 error_on_no_fonts = FcTrue;
305 really_force = FcTrue;
314 sysroot = FcStrCopy ((const FcChar8 *)optarg);
317 fprintf (stderr, "fontconfig version %d.%d.%d\n",
318 FC_MAJOR, FC_MINOR, FC_REVISION);
335 FcConfigEnableHome (FcFalse);
338 FcConfigSetSysRoot (NULL, sysroot);
340 config = FcConfigGetCurrent();
344 config = FcInitLoadConfig ();
348 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
351 FcConfigSetCurrent (config);
355 dirs = FcStrSetCreate ();
358 fprintf (stderr, "%s: Can't create list of directories\n",
364 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
366 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
371 list = FcStrListCreate (dirs);
372 FcStrSetDestroy (dirs);
375 list = FcConfigGetConfigDirs (config);
377 if ((processed_dirs = FcStrSetCreate()) == NULL) {
378 fprintf(stderr, "Cannot malloc\n");
383 ret = scanDirs (list, config, force, really_force, verbose, error_on_no_fonts, &changed);
384 FcStrListDone (list);
387 * Try to create CACHEDIR.TAG anyway.
388 * This expects the fontconfig cache directory already exists.
389 * If it doesn't, it won't be simply created.
391 FcCacheCreateTagFile (config);
393 FcStrSetDestroy (processed_dirs);
395 cleanCacheDirectories (config, verbose);
397 FcConfigDestroy (config);
400 * Now we need to sleep a second (or two, to be extra sure), to make
401 * sure that timestamps for changes after this run of fc-cache are later
402 * then any timestamps we wrote. We don't use gettimeofday() because
403 * sleep(3) can't be interrupted by a signal here -- this isn't in the
404 * library, and there aren't any signals flying around here.
406 /* the resolution of mtime on FAT is 2 seconds */
410 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");