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[] = {
69 {"really-force", 0, 0, 'r'},
70 {"system-only", 0, 0, 's'},
71 {"version", 0, 0, 'V'},
72 {"verbose", 0, 0, 'v'},
79 extern int optind, opterr, optopt;
84 usage (char *program, int error)
86 FILE *file = error ? stderr : stdout;
88 fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
91 fprintf (file, "usage: %s [-frsvVh] [dirs]\n",
94 fprintf (file, "Build font information caches in [dirs]\n"
95 "(all directories in font configuration by default).\n");
98 fprintf (file, " -f, --force scan directories with apparently valid caches\n");
99 fprintf (file, " -r, --really-force erase all existing caches, then rescan\n");
100 fprintf (file, " -s, --system-only scan system-wide directories only\n");
101 fprintf (file, " -v, --verbose display status information while busy\n");
102 fprintf (file, " -V, --version display font config version and exit\n");
103 fprintf (file, " -h, --help display this help and exit\n");
105 fprintf (file, " -f (force) scan directories with apparently valid caches\n");
106 fprintf (file, " -r, (really force) erase all existing caches, then rescan\n");
107 fprintf (file, " -s (system) scan system-wide directories only\n");
108 fprintf (file, " -v (verbose) display status information while busy\n");
109 fprintf (file, " -V (version) display font config version and exit\n");
110 fprintf (file, " -h (help) display this help and exit\n");
115 static FcStrSet *processed_dirs;
118 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
130 * Now scan all of the directories into separate databases
131 * and write out the results
133 while ((dir = FcStrListNext (list)))
137 printf ("%s: ", dir);
144 printf ("skipping, no such directory\n");
148 if (FcStrSetMember (processed_dirs, dir))
151 printf ("skipping, looped directory detected\n");
155 if (stat ((char *) dir, &statb) == -1)
161 printf ("skipping, no such directory\n");
164 fprintf (stderr, "\"%s\": ", dir);
172 if (!S_ISDIR (statb.st_mode))
174 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
179 FcDirCacheUnlink (dir, config);
184 cache = FcDirCacheLoad (dir, config, NULL);
192 cache = FcDirCacheRead (dir, FcTrue, config);
195 fprintf (stderr, "%s: error scanning\n", dir);
204 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
205 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
210 printf ("caching, new cache contents: %d fonts, %d dirs\n",
211 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
213 if (!FcDirCacheValid (dir))
215 fprintf (stderr, "%s: failed to write cache\n", dir);
216 (void) FcDirCacheUnlink (dir, config);
221 subdirs = FcStrSetCreate ();
224 fprintf (stderr, "%s: Can't create subdir set\n", dir);
226 FcDirCacheUnload (cache);
229 for (i = 0; i < FcCacheNumSubdir (cache); i++)
230 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
232 FcDirCacheUnload (cache);
234 sublist = FcStrListCreate (subdirs);
235 FcStrSetDestroy (subdirs);
238 fprintf (stderr, "%s: Can't create subdir list\n", dir);
242 FcStrSetAdd (processed_dirs, dir);
243 ret += scanDirs (sublist, config, force, really_force, verbose, changed);
245 FcStrListDone (list);
250 cleanCacheDirectories (FcConfig *config, FcBool verbose)
252 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
258 while ((cache_dir = FcStrListNext (cache_dirs)))
260 if (!FcDirCacheClean (cache_dir, verbose))
266 FcStrListDone (cache_dirs);
271 main (int argc, char **argv)
275 FcBool verbose = FcFalse;
276 FcBool force = FcFalse;
277 FcBool really_force = FcFalse;
278 FcBool systemOnly = FcFalse;
283 #if HAVE_GETOPT_LONG || HAVE_GETOPT
287 while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
289 while ((c = getopt (argc, argv, "frsVvh")) != -1)
294 really_force = FcTrue;
303 fprintf (stderr, "fontconfig version %d.%d.%d\n",
304 FC_MAJOR, FC_MINOR, FC_REVISION);
321 FcConfigEnableHome (FcFalse);
322 config = FcInitLoadConfig ();
325 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
328 FcConfigSetCurrent (config);
332 dirs = FcStrSetCreate ();
335 fprintf (stderr, "%s: Can't create list of directories\n",
341 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
343 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
348 list = FcStrListCreate (dirs);
349 FcStrSetDestroy (dirs);
352 list = FcConfigGetConfigDirs (config);
354 if ((processed_dirs = FcStrSetCreate()) == NULL) {
355 fprintf(stderr, "Cannot malloc\n");
360 ret = scanDirs (list, config, force, really_force, verbose, &changed);
363 * Try to create CACHEDIR.TAG anyway.
364 * This expects the fontconfig cache directory already exists.
365 * If it doesn't, it won't be simply created.
367 FcCacheCreateTagFile (config);
369 FcStrSetDestroy (processed_dirs);
371 cleanCacheDirectories (config, verbose);
374 * Now we need to sleep a second (or two, to be extra sure), to make
375 * sure that timestamps for changes after this run of fc-cache are later
376 * then any timestamps we wrote. We don't use gettimeofday() because
377 * sleep(3) can't be interrupted by a signal here -- this isn't in the
378 * library, and there aren't any signals flying around here.
380 FcConfigDestroy (config);
385 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");