]>
git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
5e4ada40c0ad91eaa49457e42d681b722cfef7ed
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
) && FcDirCacheHasCurrentArch (dir
))
236 printf ("skipping, %d fonts, %d dirs\n",
237 set
->nfont
, nsubdirs(subdirs
));
242 printf ("caching, %d fonts, %d dirs\n",
243 set
->nfont
, nsubdirs (subdirs
));
245 /* This is the only reason we can't combine
246 * Valid w/HasCurrentArch... */
247 if (!FcDirCacheValid (dir
))
248 if (!FcDirCacheUnlink (dir
, config
))
251 if (!FcDirSave (set
, subdirs
, dir
))
254 fprintf (stderr
, "Caches are currently saved to \"%s\"\n", PKGCACHEDIR
);
255 fprintf (stderr
, "Can't save cache for \"%s\"\n", dir
);
259 FcFontSetDestroy (set
);
260 sublist
= FcStrListCreate (subdirs
);
261 FcStrSetDestroy (subdirs
);
264 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
268 FcStrSetAdd (processed_dirs
, dir
);
269 ret
+= scanDirs (sublist
, config
, program
, force
, really_force
, verbose
);
271 FcStrListDone (list
);
276 main (int argc
, char **argv
)
280 FcBool verbose
= FcFalse
;
281 FcBool force
= FcFalse
;
282 FcBool really_force
= FcFalse
;
283 FcBool systemOnly
= FcFalse
;
287 #if HAVE_GETOPT_LONG || HAVE_GETOPT
291 while ((c
= getopt_long (argc
, argv
, "frsVv?", longopts
, NULL
)) != -1)
293 while ((c
= getopt (argc
, argv
, "frsVv?")) != -1)
298 really_force
= FcTrue
;
307 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
308 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
323 FcConfigEnableHome (FcFalse
);
324 config
= FcInitLoadConfig ();
327 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
330 FcConfigSetCurrent (config
);
334 dirs
= FcStrSetCreate ();
337 fprintf (stderr
, "%s: Can't create list of directories\n",
343 if (!FcStrSetAdd (dirs
, (FcChar8
*) argv
[i
]))
345 fprintf (stderr
, "%s: Can't add directory\n", argv
[0]);
350 list
= FcStrListCreate (dirs
);
351 FcStrSetDestroy (dirs
);
354 list
= FcConfigGetConfigDirs (config
);
356 if ((processed_dirs
= FcStrSetCreate()) == NULL
) {
357 fprintf(stderr
, "Cannot malloc\n");
361 ret
= scanDirs (list
, config
, argv
[0], force
, really_force
, verbose
);
363 FcStrSetDestroy (processed_dirs
);
366 * Now we need to sleep a second (or two, to be extra sure), to make
367 * sure that timestamps for changes after this run of fc-cache are later
368 * then any timestamps we wrote. We don't use gettimeofday() because
369 * sleep(3) can't be interrupted by a signal here -- this isn't in the
370 * library, and there aren't any signals flying around here.
372 FcConfigDestroy (config
);
375 printf ("%s: %s\n", argv
[0], ret
? "failed" : "succeeded");