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 {"sysroot", 0, 0, 'y'},
71 {"system-only", 0, 0, 's'},
72 {"version", 0, 0, 'V'},
73 {"verbose", 0, 0, 'v'},
80 extern int optind, opterr, optopt;
85 usage (char *program, int error)
87 FILE *file = error ? stderr : stdout;
89 fprintf (file, "usage: %s [-frsvVh] [-y SYSROOT] [--force|--really-force] [--sysroot=SYSROOT] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
92 fprintf (file, "usage: %s [-frsvVh] [-y SYSROOT] [dirs]\n",
95 fprintf (file, "Build font information caches in [dirs]\n"
96 "(all directories in font configuration by default).\n");
99 fprintf (file, " -f, --force scan directories with apparently valid caches\n");
100 fprintf (file, " -r, --really-force erase all existing caches, then rescan\n");
101 fprintf (file, " -s, --system-only scan system-wide directories only\n");
102 fprintf (file, " -y, --sysroot=SYSROOT prepend SYSROOT to all paths for scanning\n");
103 fprintf (file, " -v, --verbose display status information while busy\n");
104 fprintf (file, " -V, --version display font config version and exit\n");
105 fprintf (file, " -h, --help display this help and exit\n");
107 fprintf (file, " -f (force) scan directories with apparently valid caches\n");
108 fprintf (file, " -r, (really force) erase all existing caches, then rescan\n");
109 fprintf (file, " -s (system) scan system-wide directories only\n");
110 fprintf (file, " -y SYSROOT (sysroot) prepend SYSROOT to all paths for scanning\n");
111 fprintf (file, " -v (verbose) display status information while busy\n");
112 fprintf (file, " -V (version) display font config version and exit\n");
113 fprintf (file, " -h (help) display this help and exit\n");
118 static FcStrSet *processed_dirs;
121 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
133 * Now scan all of the directories into separate databases
134 * and write out the results
136 while ((dir = FcStrListNext (list)))
140 printf ("%s: ", dir);
144 if (FcStrSetMember (processed_dirs, dir))
147 printf ("skipping, looped directory detected\n");
151 if (stat ((char *) dir, &statb) == -1)
157 printf ("skipping, no such directory\n");
160 fprintf (stderr, "\"%s\": ", dir);
168 if (!S_ISDIR (statb.st_mode))
170 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
175 FcDirCacheUnlink (dir, config);
180 cache = FcDirCacheLoad (dir, config, NULL);
188 cache = FcDirCacheRead (dir, FcTrue, config);
191 fprintf (stderr, "%s: error scanning\n", dir);
200 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
201 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
206 printf ("caching, new cache contents: %d fonts, %d dirs\n",
207 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
209 if (!FcDirCacheValid (dir))
211 fprintf (stderr, "%s: failed to write cache\n", dir);
212 (void) FcDirCacheUnlink (dir, config);
217 subdirs = FcStrSetCreate ();
220 fprintf (stderr, "%s: Can't create subdir set\n", dir);
222 FcDirCacheUnload (cache);
225 for (i = 0; i < FcCacheNumSubdir (cache); i++)
226 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
228 FcDirCacheUnload (cache);
230 sublist = FcStrListCreate (subdirs);
231 FcStrSetDestroy (subdirs);
234 fprintf (stderr, "%s: Can't create subdir list\n", dir);
238 FcStrSetAdd (processed_dirs, dir);
239 ret += scanDirs (sublist, config, force, really_force, verbose, changed);
241 FcStrListDone (list);
246 cleanCacheDirectories (FcConfig *config, FcBool verbose)
248 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
254 while ((cache_dir = FcStrListNext (cache_dirs)))
256 if (!FcDirCacheClean (cache_dir, verbose))
262 FcStrListDone (cache_dirs);
267 main (int argc, char **argv)
271 FcBool verbose = FcFalse;
272 FcBool force = FcFalse;
273 FcBool really_force = FcFalse;
274 FcBool systemOnly = FcFalse;
276 FcChar8 *sysroot = NULL;
280 #if HAVE_GETOPT_LONG || HAVE_GETOPT
284 while ((c = getopt_long (argc, argv, "frsy:Vvh", longopts, NULL)) != -1)
286 while ((c = getopt (argc, argv, "frsy:Vvh")) != -1)
291 really_force = FcTrue;
300 sysroot = FcStrCopy ((const FcChar8 *)optarg);
303 fprintf (stderr, "fontconfig version %d.%d.%d\n",
304 FC_MAJOR, FC_MINOR, FC_REVISION);
321 FcConfigEnableHome (FcFalse);
324 FcConfigSetSysRoot (NULL, sysroot);
326 config = FcConfigGetCurrent();
330 config = FcInitLoadConfig ();
334 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
337 FcConfigSetCurrent (config);
341 dirs = FcStrSetCreate ();
344 fprintf (stderr, "%s: Can't create list of directories\n",
350 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
352 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
357 list = FcStrListCreate (dirs);
358 FcStrSetDestroy (dirs);
361 list = FcConfigGetConfigDirs (config);
363 if ((processed_dirs = FcStrSetCreate()) == NULL) {
364 fprintf(stderr, "Cannot malloc\n");
369 ret = scanDirs (list, config, force, really_force, verbose, &changed);
372 * Try to create CACHEDIR.TAG anyway.
373 * This expects the fontconfig cache directory already exists.
374 * If it doesn't, it won't be simply created.
376 FcCacheCreateTagFile (config);
378 FcStrSetDestroy (processed_dirs);
380 cleanCacheDirectories (config, verbose);
383 * Now we need to sleep a second (or two, to be extra sure), to make
384 * sure that timestamps for changes after this run of fc-cache are later
385 * then any timestamps we wrote. We don't use gettimeofday() because
386 * sleep(3) can't be interrupted by a signal here -- this isn't in the
387 * library, and there aren't any signals flying around here.
389 FcConfigDestroy (config);
394 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");