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
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 nsubdirs (FcStrSet *set)
121 list = FcStrListCreate (set);
124 while (FcStrListNext (list))
126 FcStrListDone (list);
131 scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose)
142 * Now scan all of the directories into separate databases
143 * and write out the results
145 while ((dir = FcStrListNext (list)))
149 printf ("%s: \"%s\": ", program, dir);
156 printf ("skipping, no such directory\n");
160 if (FcStrSetMember (processed_dirs, dir))
163 printf ("skipping, looped directory detected\n");
167 set = FcFontSetCreate ();
170 fprintf (stderr, "%s: Can't create font set\n", dir);
174 subdirs = FcStrSetCreate ();
177 fprintf (stderr, "%s: Can't create directory set\n", dir);
179 FcFontSetDestroy (set);
183 if (access ((char *) dir, W_OK) < 0)
189 printf ("skipping, no such directory\n");
190 FcFontSetDestroy (set);
191 FcStrSetDestroy (subdirs);
195 /* That's ok, caches go to /var anyway. */
196 /* Ideally we'd do an access on the hashed_name. */
197 /* But we hid that behind an abstraction barrier. */
200 fprintf (stderr, "\"%s\": ", dir);
204 FcFontSetDestroy (set);
205 FcStrSetDestroy (subdirs);
209 if (stat ((char *) dir, &statb) == -1)
211 fprintf (stderr, "\"%s\": ", dir);
213 FcFontSetDestroy (set);
214 FcStrSetDestroy (subdirs);
218 if (!S_ISDIR (statb.st_mode))
220 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
221 FcFontSetDestroy (set);
222 FcStrSetDestroy (subdirs);
227 FcDirCacheUnlink (dir, config);
230 was_valid = FcDirCacheValid (dir);
232 if (!FcDirScanConfig (set, subdirs, FcConfigGetBlanks (config), dir, force, config))
234 fprintf (stderr, "%s: error scanning\n", dir);
235 FcFontSetDestroy (set);
236 FcStrSetDestroy (subdirs);
240 if (!force && was_valid)
243 printf ("skipping, %d fonts, %d dirs\n",
244 set->nfont, nsubdirs(subdirs));
249 printf ("caching, %d fonts, %d dirs\n",
250 set->nfont, nsubdirs (subdirs));
252 if (!FcDirCacheValid (dir))
254 fprintf (stderr, "%s: failed to write cache\n", dir);
255 (void) FcDirCacheUnlink (dir, config);
259 FcFontSetDestroy (set);
260 sublist = FcStrListCreate (subdirs);
261 FcStrSetDestroy (subdirs);
264 fprintf (stderr, "%s: Can't create subdir list\n", dir);
268 FcStrSetAdd (processed_dirs, dir);
269 ret += scanDirs (sublist, config, program, force, really_force, verbose);
271 FcStrListDone (list);
276 FcCacheFileMap (const FcChar8 *file, struct stat *file_stat)
281 fd = open (file, O_RDONLY | O_BINARY);
284 if (fstat (fd, file_stat) < 0) {
288 if (FcDirCacheLoad (fd, file_stat->st_size, &cache)) {
297 cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
305 struct stat file_stat;
306 struct stat target_stat;
308 dir_base = FcStrPlus (dir, "/");
309 if (access ((char *) dir, W_OK|X_OK) != 0)
312 printf ("%s: skipping unwritable cache directory\n", dir);
321 while ((ent = readdir (d)))
326 if (ent->d_name[0] == '.')
328 file_name = FcStrPlus (dir_base, ent->d_name);
331 fprintf (stderr, "%s: allocation failure\n", dir);
335 cache = FcCacheFileMap (file_name, &file_stat);
338 fprintf (stderr, "%s: invalid cache file: %s\n", dir, ent->d_name);
339 FcStrFree (file_name);
343 target_dir = FcCacheDir (cache);
345 if (stat (target_dir, &target_stat) < 0)
348 printf ("%s: %s: missing directory: %s \n",
349 dir, ent->d_name, target_dir);
352 else if (target_stat.st_mtime > file_stat.st_mtime)
355 printf ("%s: %s: cache outdated: %s\n",
356 dir, ent->d_name, target_dir);
361 if (unlink (file_name) < 0)
367 FcStrFree (file_name);
375 cleanCacheDirectories (FcConfig *config, FcBool verbose)
377 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
383 while ((cache_dir = FcStrListNext (cache_dirs)))
385 if (!cleanCacheDirectory (config, cache_dir, verbose))
391 FcStrListDone (cache_dirs);
396 main (int argc, char **argv)
400 FcBool verbose = FcFalse;
401 FcBool force = FcFalse;
402 FcBool really_force = FcFalse;
403 FcBool systemOnly = FcFalse;
407 #if HAVE_GETOPT_LONG || HAVE_GETOPT
411 while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
413 while ((c = getopt (argc, argv, "frsVv?")) != -1)
418 really_force = FcTrue;
427 fprintf (stderr, "fontconfig version %d.%d.%d\n",
428 FC_MAJOR, FC_MINOR, FC_REVISION);
443 FcConfigEnableHome (FcFalse);
444 config = FcInitLoadConfig ();
447 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
450 FcConfigSetCurrent (config);
454 dirs = FcStrSetCreate ();
457 fprintf (stderr, "%s: Can't create list of directories\n",
463 if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
465 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
470 list = FcStrListCreate (dirs);
471 FcStrSetDestroy (dirs);
474 list = FcConfigGetConfigDirs (config);
476 if ((processed_dirs = FcStrSetCreate()) == NULL) {
477 fprintf(stderr, "Cannot malloc\n");
481 ret = scanDirs (list, config, argv[0], force, really_force, verbose);
483 FcStrSetDestroy (processed_dirs);
485 cleanCacheDirectories (config, verbose);
488 * Now we need to sleep a second (or two, to be extra sure), to make
489 * sure that timestamps for changes after this run of fc-cache are later
490 * then any timestamps we wrote. We don't use gettimeofday() because
491 * sleep(3) can't be interrupted by a signal here -- this isn't in the
492 * library, and there aren't any signals flying around here.
494 FcConfigDestroy (config);
497 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");