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.
25 #include <fontconfig/fontconfig.h>
29 #include <sys/types.h>
36 #define HAVE_GETOPT_LONG 1
44 #ifndef HAVE_GETOPT_LONG
45 #define HAVE_GETOPT_LONG 0
52 const struct option longopts[] = {
54 {"really-force", 0, 0, 'r'},
55 {"system-only", 0, 0, 's'},
56 {"version", 0, 0, 'V'},
57 {"verbose", 0, 0, 'v'},
64 extern int optind, opterr, optopt;
72 fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
75 fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
78 fprintf (stderr, "Build font information caches in [dirs]\n"
79 "(all directories in font configuration by default).\n");
80 fprintf (stderr, "\n");
82 fprintf (stderr, " -f, --force scan directories with apparently valid caches\n");
83 fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n");
84 fprintf (stderr, " -s, --system-only scan system-wide directories only\n");
85 fprintf (stderr, " -v, --verbose display status information while busy\n");
86 fprintf (stderr, " -V, --version display font config version and exit\n");
87 fprintf (stderr, " -?, --help display this help and exit\n");
89 fprintf (stderr, " -f (force) scan directories with apparently valid caches\n");
90 fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n");
91 fprintf (stderr, " -s (system) scan system-wide directories only\n");
92 fprintf (stderr, " -v (verbose) display status information while busy\n");
93 fprintf (stderr, " -V (version) display font config version and exit\n");
94 fprintf (stderr, " -? (help) display this help and exit\n");
99 static FcStrSet *processed_dirs;
102 nsubdirs (FcStrSet *set)
107 list = FcStrListCreate (set);
110 while (FcStrListNext (list))
112 FcStrListDone (list);
117 scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose)
121 const FcChar8 *dir_orig;
128 * Now scan all of the directories into separate databases
129 * and write out the results
131 while ((dir_orig = FcStrListNext (list)))
133 dir = FcConfigNormalizeFontDir (config, dir_orig);
137 printf ("%s: \"%s\": ", program, dir ? dir : dir_orig);
144 printf ("skipping, no such directory\n");
148 if (FcStrSetMember (processed_dirs, dir))
151 printf ("skipping, looped directory detected\n");
155 set = FcFontSetCreate ();
158 fprintf (stderr, "Can't create font set\n");
162 subdirs = FcStrSetCreate ();
165 fprintf (stderr, "Can't create directory set\n");
167 FcFontSetDestroy (set);
171 if (access ((char *) dir, W_OK) < 0)
177 printf ("skipping, no such directory\n");
178 FcFontSetDestroy (set);
179 FcStrSetDestroy (subdirs);
183 /* That's ok, caches go to /var anyway. */
184 /* Ideally we'd do an access on the hashed_name. */
185 /* But we hid that behind an abstraction barrier. */
188 fprintf (stderr, "\"%s\": ", dir);
192 FcFontSetDestroy (set);
193 FcStrSetDestroy (subdirs);
197 if (stat ((char *) dir, &statb) == -1)
199 fprintf (stderr, "\"%s\": ", dir);
201 FcFontSetDestroy (set);
202 FcStrSetDestroy (subdirs);
206 if (!S_ISDIR (statb.st_mode))
208 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
209 FcFontSetDestroy (set);
210 FcStrSetDestroy (subdirs);
215 FcDirCacheUnlink (dir, config);
217 if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
219 fprintf (stderr, "\"%s\": error scanning\n", dir);
220 FcFontSetDestroy (set);
221 FcStrSetDestroy (subdirs);
225 if (!force && FcDirCacheValid (dir) && FcDirCacheHasCurrentArch (dir))
228 printf ("skipping, %d fonts, %d dirs\n",
229 set->nfont, nsubdirs(subdirs));
234 printf ("caching, %d fonts, %d dirs\n",
235 set->nfont, nsubdirs (subdirs));
237 /* This is the only reason we can't combine
238 * Valid w/HasCurrentArch... */
239 if (!FcDirCacheValid (dir))
240 if (!FcDirCacheUnlink (dir, config))
243 if (!FcDirSave (set, subdirs, dir))
246 fprintf (stderr, "Caches are currently saved to \"%s\"\n", PKGCACHEDIR);
247 fprintf (stderr, "Can't save cache for \"%s\"\n", dir);
251 FcFontSetDestroy (set);
252 sublist = FcStrListCreate (subdirs);
253 FcStrSetDestroy (subdirs);
256 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
260 FcStrSetAdd (processed_dirs, dir);
261 ret += scanDirs (sublist, config, program, force, really_force, verbose);
263 FcStrListDone (list);
268 main (int argc, char **argv)
272 FcBool verbose = FcFalse;
273 FcBool force = FcFalse;
274 FcBool really_force = FcFalse;
275 FcBool systemOnly = FcFalse;
279 #if HAVE_GETOPT_LONG || HAVE_GETOPT
283 while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
285 while ((c = getopt (argc, argv, "frsVv?")) != -1)
290 really_force = FcTrue;
299 fprintf (stderr, "fontconfig version %d.%d.%d\n",
300 FC_MAJOR, FC_MINOR, FC_REVISION);
315 FcConfigEnableHome (FcFalse);
316 config = FcInitLoadConfig ();
319 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
322 FcConfigSetCurrent (config);
326 dirs = FcStrSetCreate ();
329 fprintf (stderr, "%s: Can't create list of directories\n",
335 if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
337 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
342 list = FcStrListCreate (dirs);
343 FcStrSetDestroy (dirs);
346 list = FcConfigGetConfigDirs (config);
348 if ((processed_dirs = FcStrSetCreate()) == NULL) {
349 fprintf(stderr, "Cannot malloc\n");
353 ret = scanDirs (list, config, argv[0], force, really_force, verbose);
355 FcStrSetDestroy (processed_dirs);
358 * Now we need to sleep a second (or two, to be extra sure), to make
359 * sure that timestamps for changes after this run of fc-cache are later
360 * then any timestamps we wrote. We don't use gettimeofday() because
361 * sleep(3) can't be interrupted by a signal here -- this isn't in the
362 * library, and there aren't any signals flying around here.
364 FcConfigDestroy (config);
367 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");