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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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.
25 #include "../fc-arch/fcarch.h"
31 #define HAVE_GETOPT_LONG 1
36 #include <fontconfig/fontconfig.h>
40 #include <sys/types.h>
50 #define sleep(x) Sleep((x) * 1000)
61 #ifndef HAVE_GETOPT_LONG
62 #define HAVE_GETOPT_LONG 0
69 const struct option longopts[] = {
71 {"really-force", 0, 0, 'r'},
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 [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
93 fprintf (file, "usage: %s [-frsvVh] [dirs]\n",
96 fprintf (file, "Build font information caches in [dirs]\n"
97 "(all directories in font configuration by default).\n");
100 fprintf (file, " -f, --force scan directories with apparently valid caches\n");
101 fprintf (file, " -r, --really-force erase all existing caches, then rescan\n");
102 fprintf (file, " -s, --system-only scan system-wide directories only\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, " -v (verbose) display status information while busy\n");
111 fprintf (file, " -V (version) display font config version and exit\n");
112 fprintf (file, " -h (help) display this help and exit\n");
117 static FcStrSet *processed_dirs;
120 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose)
132 * Now scan all of the directories into separate databases
133 * and write out the results
135 while ((dir = FcStrListNext (list)))
139 printf ("%s: ", dir);
146 printf ("skipping, no such directory\n");
150 if (FcStrSetMember (processed_dirs, dir))
153 printf ("skipping, looped directory detected\n");
157 if (stat ((char *) dir, &statb) == -1)
163 printf ("skipping, no such directory\n");
166 fprintf (stderr, "\"%s\": ", dir);
174 if (!S_ISDIR (statb.st_mode))
176 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
181 FcDirCacheUnlink (dir, config);
186 cache = FcDirCacheLoad (dir, config, NULL);
193 cache = FcDirCacheRead (dir, FcTrue, config);
196 fprintf (stderr, "%s: error scanning\n", dir);
205 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
206 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
211 printf ("caching, new cache contents: %d fonts, %d dirs\n",
212 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
214 if (!FcDirCacheValid (dir))
216 fprintf (stderr, "%s: failed to write cache\n", dir);
217 (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));
233 FcDirCacheUnload (cache);
235 sublist = FcStrListCreate (subdirs);
236 FcStrSetDestroy (subdirs);
239 fprintf (stderr, "%s: Can't create subdir list\n", dir);
243 FcStrSetAdd (processed_dirs, dir);
244 ret += scanDirs (sublist, config, force, really_force, verbose);
246 FcStrListDone (list);
251 cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
259 struct stat target_stat;
261 dir_base = FcStrPlus (dir, (FcChar8 *) "/");
264 fprintf (stderr, "%s: out of memory\n", dir);
267 if (access ((char *) dir, W_OK) != 0)
270 printf ("%s: not cleaning %s cache directory\n", dir,
271 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
272 FcStrFree (dir_base);
276 printf ("%s: cleaning cache directory\n", dir);
277 d = opendir ((char *) dir);
280 perror ((char *) dir);
281 FcStrFree (dir_base);
284 while ((ent = readdir (d)))
287 const FcChar8 *target_dir;
289 if (ent->d_name[0] == '.')
291 /* skip cache files for different architectures and */
292 /* files which are not cache files at all */
293 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
294 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
297 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
300 fprintf (stderr, "%s: allocation failure\n", dir);
305 cache = FcDirCacheLoadFile (file_name, NULL);
309 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
314 target_dir = FcCacheDir (cache);
315 if (stat ((char *) target_dir, &target_stat) < 0)
318 printf ("%s: %s: missing directory: %s \n",
319 dir, ent->d_name, target_dir);
325 if (unlink ((char *) file_name) < 0)
327 perror ((char *) file_name);
331 FcDirCacheUnload (cache);
332 FcStrFree (file_name);
336 FcStrFree (dir_base);
341 cleanCacheDirectories (FcConfig *config, FcBool verbose)
343 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
349 while ((cache_dir = FcStrListNext (cache_dirs)))
351 if (!cleanCacheDirectory (config, cache_dir, verbose))
357 FcStrListDone (cache_dirs);
362 main (int argc, char **argv)
366 FcBool verbose = FcFalse;
367 FcBool force = FcFalse;
368 FcBool really_force = FcFalse;
369 FcBool systemOnly = FcFalse;
373 #if HAVE_GETOPT_LONG || HAVE_GETOPT
377 while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
379 while ((c = getopt (argc, argv, "frsVvh")) != -1)
384 really_force = FcTrue;
393 fprintf (stderr, "fontconfig version %d.%d.%d\n",
394 FC_MAJOR, FC_MINOR, FC_REVISION);
411 FcConfigEnableHome (FcFalse);
412 config = FcInitLoadConfig ();
415 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
418 FcConfigSetCurrent (config);
422 dirs = FcStrSetCreate ();
425 fprintf (stderr, "%s: Can't create list of directories\n",
431 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
433 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
438 list = FcStrListCreate (dirs);
439 FcStrSetDestroy (dirs);
442 list = FcConfigGetConfigDirs (config);
444 if ((processed_dirs = FcStrSetCreate()) == NULL) {
445 fprintf(stderr, "Cannot malloc\n");
449 ret = scanDirs (list, config, force, really_force, verbose);
451 FcStrSetDestroy (processed_dirs);
453 cleanCacheDirectories (config, verbose);
456 * Now we need to sleep a second (or two, to be extra sure), to make
457 * sure that timestamps for changes after this run of fc-cache are later
458 * then any timestamps we wrote. We don't use gettimeofday() because
459 * sleep(3) can't be interrupted by a signal here -- this isn't in the
460 * library, and there aren't any signals flying around here.
462 FcConfigDestroy (config);
466 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");