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>
35 #include "../src/fcarch.h"
39 #include <sys/types.h>
49 #define sleep(x) Sleep((x) * 1000)
60 #ifndef HAVE_GETOPT_LONG
61 #define HAVE_GETOPT_LONG 0
68 const struct option longopts[] = {
70 {"really-force", 0, 0, 'r'},
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] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
92 fprintf (file, "usage: %s [-frsvVh] [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, " -v, --verbose display status information while busy\n");
103 fprintf (file, " -V, --version display font config version and exit\n");
104 fprintf (file, " -h, --help display this help and exit\n");
106 fprintf (file, " -f (force) scan directories with apparently valid caches\n");
107 fprintf (file, " -r, (really force) erase all existing caches, then rescan\n");
108 fprintf (file, " -s (system) scan system-wide directories only\n");
109 fprintf (file, " -v (verbose) display status information while busy\n");
110 fprintf (file, " -V (version) display font config version and exit\n");
111 fprintf (file, " -h (help) display this help and exit\n");
116 static FcStrSet *processed_dirs;
119 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
131 * Now scan all of the directories into separate databases
132 * and write out the results
134 while ((dir = FcStrListNext (list)))
138 printf ("%s: ", dir);
145 printf ("skipping, no such directory\n");
149 if (FcStrSetMember (processed_dirs, dir))
152 printf ("skipping, looped directory detected\n");
156 if (stat ((char *) dir, &statb) == -1)
162 printf ("skipping, no such directory\n");
165 fprintf (stderr, "\"%s\": ", dir);
173 if (!S_ISDIR (statb.st_mode))
175 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
180 FcDirCacheUnlink (dir, config);
185 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, changed);
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;
374 #if HAVE_GETOPT_LONG || HAVE_GETOPT
378 while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
380 while ((c = getopt (argc, argv, "frsVvh")) != -1)
385 really_force = FcTrue;
394 fprintf (stderr, "fontconfig version %d.%d.%d\n",
395 FC_MAJOR, FC_MINOR, FC_REVISION);
412 FcConfigEnableHome (FcFalse);
413 config = FcInitLoadConfig ();
416 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
419 FcConfigSetCurrent (config);
423 dirs = FcStrSetCreate ();
426 fprintf (stderr, "%s: Can't create list of directories\n",
432 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
434 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
439 list = FcStrListCreate (dirs);
440 FcStrSetDestroy (dirs);
443 list = FcConfigGetConfigDirs (config);
445 if ((processed_dirs = FcStrSetCreate()) == NULL) {
446 fprintf(stderr, "Cannot malloc\n");
451 ret = scanDirs (list, config, force, really_force, verbose, &changed);
453 FcStrSetDestroy (processed_dirs);
455 cleanCacheDirectories (config, verbose);
458 * Now we need to sleep a second (or two, to be extra sure), to make
459 * sure that timestamps for changes after this run of fc-cache are later
460 * then any timestamps we wrote. We don't use gettimeofday() because
461 * sleep(3) can't be interrupted by a signal here -- this isn't in the
462 * library, and there aren't any signals flying around here.
464 FcConfigDestroy (config);
469 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");