2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
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.
29 #define HAVE_GETOPT_LONG 1
34 #include <fontconfig/fontconfig.h>
38 #include <sys/types.h>
47 #define sleep(x) Sleep((x) * 1000)
58 #ifndef HAVE_GETOPT_LONG
59 #define HAVE_GETOPT_LONG 0
66 const struct option longopts[] = {
68 {"really-force", 0, 0, 'r'},
69 {"system-only", 0, 0, 's'},
70 {"version", 0, 0, 'V'},
71 {"verbose", 0, 0, 'v'},
78 extern int optind, opterr, optopt;
86 fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
89 fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
92 fprintf (stderr, "Build font information caches in [dirs]\n"
93 "(all directories in font configuration by default).\n");
94 fprintf (stderr, "\n");
96 fprintf (stderr, " -f, --force scan directories with apparently valid caches\n");
97 fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n");
98 fprintf (stderr, " -s, --system-only scan system-wide directories only\n");
99 fprintf (stderr, " -v, --verbose display status information while busy\n");
100 fprintf (stderr, " -V, --version display font config version and exit\n");
101 fprintf (stderr, " -?, --help display this help and exit\n");
103 fprintf (stderr, " -f (force) scan directories with apparently valid caches\n");
104 fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n");
105 fprintf (stderr, " -s (system) scan system-wide directories only\n");
106 fprintf (stderr, " -v (verbose) display status information while busy\n");
107 fprintf (stderr, " -V (version) display font config version and exit\n");
108 fprintf (stderr, " -? (help) display this help and exit\n");
113 static FcStrSet *processed_dirs;
116 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose)
128 * Now scan all of the directories into separate databases
129 * and write out the results
131 while ((dir = FcStrListNext (list)))
135 printf ("%s: ", dir);
142 printf ("skipping, no such directory\n");
146 if (FcStrSetMember (processed_dirs, dir))
149 printf ("skipping, looped directory detected\n");
153 if (access ((char *) dir, W_OK) < 0)
159 printf ("skipping, no such directory\n");
163 /* That's ok, caches go to /var anyway. */
164 /* Ideally we'd do an access on the hashed_name. */
165 /* But we hid that behind an abstraction barrier. */
168 fprintf (stderr, "\"%s\": ", dir);
175 if (stat ((char *) dir, &statb) == -1)
177 fprintf (stderr, "\"%s\": ", dir);
182 if (!S_ISDIR (statb.st_mode))
184 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
189 FcDirCacheUnlink (dir, config);
194 cache = FcDirCacheLoad (dir, config, NULL);
201 cache = FcDirCacheRead (dir, FcTrue, config);
204 fprintf (stderr, "%s: error scanning\n", dir);
213 printf ("skipping, %d fonts, %d dirs\n",
214 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
219 printf ("caching, %d fonts, %d dirs\n",
220 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
222 if (!FcDirCacheValid (dir))
224 fprintf (stderr, "%s: failed to write cache\n", dir);
225 (void) FcDirCacheUnlink (dir, config);
230 subdirs = FcStrSetCreate ();
233 fprintf (stderr, "%s: Can't create subdir set\n", dir);
235 FcDirCacheUnload (cache);
238 for (i = 0; i < FcCacheNumSubdir (cache); i++)
239 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
241 FcDirCacheUnload (cache);
243 sublist = FcStrListCreate (subdirs);
244 FcStrSetDestroy (subdirs);
247 fprintf (stderr, "%s: Can't create subdir list\n", dir);
251 FcStrSetAdd (processed_dirs, dir);
252 ret += scanDirs (sublist, config, force, really_force, verbose);
254 FcStrListDone (list);
259 cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
267 struct stat file_stat;
268 struct stat target_stat;
270 dir_base = FcStrPlus (dir, (FcChar8 *) "/");
273 fprintf (stderr, "%s: out of memory\n", dir);
276 if (access ((char *) dir, W_OK|X_OK) != 0)
279 printf ("%s: not cleaning unwritable cache directory\n", dir);
280 FcStrFree (dir_base);
284 printf ("%s: cleaning cache directory\n", dir);
285 d = opendir ((char *) dir);
288 perror ((char *) dir);
289 FcStrFree (dir_base);
292 while ((ent = readdir (d)))
295 const FcChar8 *target_dir;
297 if (ent->d_name[0] == '.')
299 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
302 fprintf (stderr, "%s: allocation failure\n", dir);
306 cache = FcDirCacheLoadFile (file_name, &file_stat);
309 fprintf (stderr, "%s: invalid cache file: %s\n", dir, ent->d_name);
310 FcStrFree (file_name);
314 target_dir = FcCacheDir (cache);
316 if (stat ((char *) target_dir, &target_stat) < 0)
319 printf ("%s: %s: missing directory: %s \n",
320 dir, ent->d_name, target_dir);
323 else if (target_stat.st_mtime > file_stat.st_mtime)
326 printf ("%s: %s: cache outdated: %s\n",
327 dir, ent->d_name, target_dir);
332 if (unlink ((char *) file_name) < 0)
334 perror ((char *) file_name);
338 FcDirCacheUnload (cache);
339 FcStrFree (file_name);
343 FcStrFree (dir_base);
348 cleanCacheDirectories (FcConfig *config, FcBool verbose)
350 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
356 while ((cache_dir = FcStrListNext (cache_dirs)))
358 if (!cleanCacheDirectory (config, cache_dir, verbose))
364 FcStrListDone (cache_dirs);
369 main (int argc, char **argv)
373 FcBool verbose = FcFalse;
374 FcBool force = FcFalse;
375 FcBool really_force = FcFalse;
376 FcBool systemOnly = FcFalse;
380 #if HAVE_GETOPT_LONG || HAVE_GETOPT
384 while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
386 while ((c = getopt (argc, argv, "frsVv?")) != -1)
391 really_force = FcTrue;
400 fprintf (stderr, "fontconfig version %d.%d.%d\n",
401 FC_MAJOR, FC_MINOR, FC_REVISION);
416 FcConfigEnableHome (FcFalse);
417 config = FcInitLoadConfig ();
420 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
423 FcConfigSetCurrent (config);
427 dirs = FcStrSetCreate ();
430 fprintf (stderr, "%s: Can't create list of directories\n",
436 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
438 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
443 list = FcStrListCreate (dirs);
444 FcStrSetDestroy (dirs);
447 list = FcConfigGetConfigDirs (config);
449 if ((processed_dirs = FcStrSetCreate()) == NULL) {
450 fprintf(stderr, "Cannot malloc\n");
454 ret = scanDirs (list, config, force, really_force, verbose);
456 FcStrSetDestroy (processed_dirs);
458 cleanCacheDirectories (config, verbose);
461 * Now we need to sleep a second (or two, to be extra sure), to make
462 * sure that timestamps for changes after this run of fc-cache are later
463 * then any timestamps we wrote. We don't use gettimeofday() because
464 * sleep(3) can't be interrupted by a signal here -- this isn't in the
465 * library, and there aren't any signals flying around here.
467 FcConfigDestroy (config);
470 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");