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, FcBool recursive, int *changed, FcStrSet *updateDirs)
133 * Now scan all of the directories into separate databases
134 * and write out the results
136 while ((dir = FcStrListNext (list)))
141 printf ("Re-scanning %s: ", dir);
143 printf ("%s: ", dir);
147 if (recursive && FcStrSetMember (processed_dirs, dir))
150 printf ("skipping, looped directory detected\n");
154 if (stat ((char *) dir, &statb) == -1)
160 printf ("skipping, no such directory\n");
163 fprintf (stderr, "\"%s\": ", dir);
171 if (!S_ISDIR (statb.st_mode))
173 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
178 FcDirCacheUnlink (dir, config);
183 cache = FcDirCacheLoad (dir, config, NULL);
191 cache = FcDirCacheRead (dir, FcTrue, config);
194 fprintf (stderr, "%s: error scanning\n", dir);
203 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
204 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
209 printf ("caching, new cache contents: %d fonts, %d dirs\n",
210 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
212 if (!FcDirCacheValid (dir))
214 fprintf (stderr, "%s: failed to write cache\n", dir);
215 (void) FcDirCacheUnlink (dir, config);
222 subdirs = FcStrSetCreate ();
225 fprintf (stderr, "%s: Can't create subdir set\n", dir);
227 FcDirCacheUnload (cache);
230 for (i = 0; i < FcCacheNumSubdir (cache); i++)
231 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
232 if (updateDirs && FcCacheNumSubdir (cache) > 0)
233 FcStrSetAdd (updateDirs, dir);
235 FcDirCacheUnload (cache);
237 sublist = FcStrListCreate (subdirs);
238 FcStrSetDestroy (subdirs);
241 fprintf (stderr, "%s: Can't create subdir list\n", dir);
245 FcStrSetAdd (processed_dirs, dir);
246 ret += scanDirs (sublist, config, force, really_force, verbose, recursive, changed, updateDirs);
247 FcStrListDone (sublist);
250 FcDirCacheUnload (cache);
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)
279 FcStrSet *dirs, *updateDirs;
281 FcBool verbose = FcFalse;
282 FcBool force = FcFalse;
283 FcBool really_force = FcFalse;
284 FcBool systemOnly = FcFalse;
286 FcChar8 *sysroot = NULL;
290 #if HAVE_GETOPT_LONG || HAVE_GETOPT
294 while ((c = getopt_long (argc, argv, "frsy:Vvh", longopts, NULL)) != -1)
296 while ((c = getopt (argc, argv, "frsy:Vvh")) != -1)
301 really_force = FcTrue;
310 sysroot = FcStrCopy ((const FcChar8 *)optarg);
313 fprintf (stderr, "fontconfig version %d.%d.%d\n",
314 FC_MAJOR, FC_MINOR, FC_REVISION);
331 FcConfigEnableHome (FcFalse);
334 FcConfigSetSysRoot (NULL, sysroot);
336 config = FcConfigGetCurrent();
340 config = FcInitLoadConfig ();
344 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
347 FcConfigSetCurrent (config);
351 dirs = FcStrSetCreate ();
354 fprintf (stderr, "%s: Can't create list of directories\n",
360 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
362 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
367 list = FcStrListCreate (dirs);
368 FcStrSetDestroy (dirs);
371 list = FcConfigGetConfigDirs (config);
373 if ((processed_dirs = FcStrSetCreate()) == NULL) {
374 fprintf(stderr, "Cannot malloc\n");
378 updateDirs = FcStrSetCreate ();
380 ret = scanDirs (list, config, force, really_force, verbose, FcTrue, &changed, updateDirs);
381 /* Update the directory cache again to avoid the race condition as much as possible */
382 FcStrListDone (list);
383 list = FcStrListCreate (updateDirs);
386 ret += scanDirs (list, config, FcTrue, really_force, verbose, FcFalse, &changed, NULL);
387 FcStrListDone (list);
391 * Try to create CACHEDIR.TAG anyway.
392 * This expects the fontconfig cache directory already exists.
393 * If it doesn't, it won't be simply created.
395 FcCacheCreateTagFile (config);
397 FcStrSetDestroy (processed_dirs);
399 cleanCacheDirectories (config, verbose);
401 FcConfigDestroy (config);
404 * Now we need to sleep a second (or two, to be extra sure), to make
405 * sure that timestamps for changes after this run of fc-cache are later
406 * then any timestamps we wrote. We don't use gettimeofday() because
407 * sleep(3) can't be interrupted by a signal here -- this isn't in the
408 * library, and there aren't any signals flying around here.
410 /* the resolution of mtime on FAT is 2 seconds */
414 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");