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>
45 #define sleep(x) Sleep((x) * 1000)
52 #ifndef HAVE_GETOPT_LONG
53 #define HAVE_GETOPT_LONG 0
60 const struct option longopts[] = {
62 {"really-force", 0, 0, 'r'},
63 {"system-only", 0, 0, 's'},
64 {"version", 0, 0, 'V'},
65 {"verbose", 0, 0, 'v'},
72 extern int optind, opterr, optopt;
80 fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
83 fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
86 fprintf (stderr, "Build font information caches in [dirs]\n"
87 "(all directories in font configuration by default).\n");
88 fprintf (stderr, "\n");
90 fprintf (stderr, " -f, --force scan directories with apparently valid caches\n");
91 fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n");
92 fprintf (stderr, " -s, --system-only scan system-wide directories only\n");
93 fprintf (stderr, " -v, --verbose display status information while busy\n");
94 fprintf (stderr, " -V, --version display font config version and exit\n");
95 fprintf (stderr, " -?, --help display this help and exit\n");
97 fprintf (stderr, " -f (force) scan directories with apparently valid caches\n");
98 fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n");
99 fprintf (stderr, " -s (system) scan system-wide directories only\n");
100 fprintf (stderr, " -v (verbose) display status information while busy\n");
101 fprintf (stderr, " -V (version) display font config version and exit\n");
102 fprintf (stderr, " -? (help) display this help and exit\n");
107 static FcStrSet *processed_dirs;
110 nsubdirs (FcStrSet *set)
115 list = FcStrListCreate (set);
118 while (FcStrListNext (list))
120 FcStrListDone (list);
125 scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose)
129 const FcChar8 *dir_orig;
136 * Now scan all of the directories into separate databases
137 * and write out the results
139 while ((dir_orig = FcStrListNext (list)))
141 dir = FcConfigNormalizeFontDir (config, dir_orig);
145 printf ("%s: \"%s\": ", program, dir ? dir : dir_orig);
152 printf ("skipping, no such directory\n");
156 if (FcStrSetMember (processed_dirs, dir))
159 printf ("skipping, looped directory detected\n");
163 set = FcFontSetCreate ();
166 fprintf (stderr, "Can't create font set\n");
170 subdirs = FcStrSetCreate ();
173 fprintf (stderr, "Can't create directory set\n");
175 FcFontSetDestroy (set);
179 if (access ((char *) dir, W_OK) < 0)
185 printf ("skipping, no such directory\n");
186 FcFontSetDestroy (set);
187 FcStrSetDestroy (subdirs);
191 /* That's ok, caches go to /var anyway. */
192 /* Ideally we'd do an access on the hashed_name. */
193 /* But we hid that behind an abstraction barrier. */
196 fprintf (stderr, "\"%s\": ", dir);
200 FcFontSetDestroy (set);
201 FcStrSetDestroy (subdirs);
205 if (stat ((char *) dir, &statb) == -1)
207 fprintf (stderr, "\"%s\": ", dir);
209 FcFontSetDestroy (set);
210 FcStrSetDestroy (subdirs);
214 if (!S_ISDIR (statb.st_mode))
216 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
217 FcFontSetDestroy (set);
218 FcStrSetDestroy (subdirs);
223 FcDirCacheUnlink (dir, config);
225 if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
227 fprintf (stderr, "\"%s\": error scanning\n", dir);
228 FcFontSetDestroy (set);
229 FcStrSetDestroy (subdirs);
233 if (!force && FcDirCacheValid (dir, config) &&
234 FcDirCacheHasCurrentArch (dir, config))
237 printf ("skipping, %d fonts, %d dirs\n",
238 set->nfont, nsubdirs(subdirs));
243 printf ("caching, %d fonts, %d dirs\n",
244 set->nfont, nsubdirs (subdirs));
246 /* This is the only reason we can't combine
247 * Valid w/HasCurrentArch... */
248 if (!FcDirCacheValid (dir, config))
249 if (!FcDirCacheUnlink (dir, config))
252 if (!FcDirSave (set, subdirs, dir))
254 fprintf (stderr, "Can't save cache for \"%s\"\n", dir);
258 FcFontSetDestroy (set);
259 sublist = FcStrListCreate (subdirs);
260 FcStrSetDestroy (subdirs);
263 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
267 FcStrSetAdd (processed_dirs, dir);
268 ret += scanDirs (sublist, config, program, force, really_force, verbose);
270 FcStrListDone (list);
275 main (int argc, char **argv)
279 FcBool verbose = FcFalse;
280 FcBool force = FcFalse;
281 FcBool really_force = FcFalse;
282 FcBool systemOnly = FcFalse;
286 #if HAVE_GETOPT_LONG || HAVE_GETOPT
290 while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
292 while ((c = getopt (argc, argv, "frsVv?")) != -1)
297 really_force = FcTrue;
306 fprintf (stderr, "fontconfig version %d.%d.%d\n",
307 FC_MAJOR, FC_MINOR, FC_REVISION);
322 FcConfigEnableHome (FcFalse);
323 config = FcInitLoadConfig ();
326 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
329 FcConfigSetCurrent (config);
333 dirs = FcStrSetCreate ();
336 fprintf (stderr, "%s: Can't create list of directories\n",
342 if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
344 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
349 list = FcStrListCreate (dirs);
350 FcStrSetDestroy (dirs);
353 list = FcConfigGetConfigDirs (config);
355 if ((processed_dirs = FcStrSetCreate()) == NULL) {
356 fprintf(stderr, "Cannot malloc\n");
360 ret = scanDirs (list, config, argv[0], force, really_force, verbose);
362 FcStrSetDestroy (processed_dirs);
365 * Now we need to sleep a second (or two, to be extra sure), to make
366 * sure that timestamps for changes after this run of fc-cache are later
367 * then any timestamps we wrote. We don't use gettimeofday() because
368 * sleep(3) can't be interrupted by a signal here -- this isn't in the
369 * library, and there aren't any signals flying around here.
371 FcConfigDestroy (config);
374 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");