]> git.wh0rd.org - fontconfig.git/blame - fc-cache/fc-cache.c
Eliminate NormalizeDir. Eliminate gratuitous stat/access calls per dir.
[fontconfig.git] / fc-cache / fc-cache.c
CommitLineData
24330d27 1/*
4bd4418a 2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
24330d27 3 *
46b51147 4 * Copyright © 2002 Keith Packard
24330d27
KP
5 *
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.
15 *
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.
23 */
24
24330d27
KP
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#else
c4bd0638
MALF
28#ifdef linux
29#define HAVE_GETOPT_LONG 1
30#endif
24330d27
KP
31#define HAVE_GETOPT 1
32#endif
33
f045376c
PL
34#include <fontconfig/fontconfig.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <errno.h>
41
d6217cc6
PL
42#if defined (_WIN32)
43#define STRICT
44#include <windows.h>
45#define sleep(x) Sleep((x) * 1000)
46#undef STRICT
47#endif
48
c4bd0638
MALF
49#ifndef HAVE_GETOPT
50#define HAVE_GETOPT 0
51#endif
52#ifndef HAVE_GETOPT_LONG
53#define HAVE_GETOPT_LONG 0
54#endif
55
24330d27 56#if HAVE_GETOPT_LONG
c4bd0638 57#undef _GNU_SOURCE
24330d27
KP
58#define _GNU_SOURCE
59#include <getopt.h>
60const struct option longopts[] = {
179c3995 61 {"force", 0, 0, 'f'},
d2c01029 62 {"really-force", 0, 0, 'r'},
ff3f1f98 63 {"system-only", 0, 0, 's'},
24330d27
KP
64 {"version", 0, 0, 'V'},
65 {"verbose", 0, 0, 'v'},
66 {"help", 0, 0, '?'},
67 {NULL,0,0,0},
68};
69#else
70#if HAVE_GETOPT
71extern char *optarg;
72extern int optind, opterr, optopt;
73#endif
74#endif
75
65018b4a
KP
76static void
77usage (char *program)
24330d27 78{
86b12431 79#if HAVE_GETOPT_LONG
d2c01029 80 fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
86b12431
KP
81 program);
82#else
d2c01029 83 fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
24330d27 84 program);
86b12431 85#endif
24330d27
KP
86 fprintf (stderr, "Build font information caches in [dirs]\n"
87 "(all directories in font configuration by default).\n");
88 fprintf (stderr, "\n");
86b12431 89#if HAVE_GETOPT_LONG
80a7d664 90 fprintf (stderr, " -f, --force scan directories with apparently valid caches\n");
d2c01029 91 fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n");
ff3f1f98 92 fprintf (stderr, " -s, --system-only scan system-wide directories only\n");
24330d27
KP
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");
86b12431
KP
96#else
97 fprintf (stderr, " -f (force) scan directories with apparently valid caches\n");
d2c01029 98 fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n");
86b12431
KP
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");
103#endif
24330d27
KP
104 exit (1);
105}
106
660acf8f
PL
107static FcStrSet *processed_dirs;
108
179c3995
KP
109static int
110nsubdirs (FcStrSet *set)
111{
112 FcStrList *list;
113 int n = 0;
114
115 list = FcStrListCreate (set);
116 if (!list)
117 return 0;
118 while (FcStrListNext (list))
119 n++;
120 FcStrListDone (list);
121 return n;
122}
123
124static int
d2c01029 125scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose)
24330d27
KP
126{
127 int ret = 0;
97293e07 128 const FcChar8 *dir;
24330d27 129 FcFontSet *set;
179c3995
KP
130 FcStrSet *subdirs;
131 FcStrList *sublist;
0c35c0fa 132 struct stat statb;
179c3995
KP
133
134 /*
135 * Now scan all of the directories into separate databases
136 * and write out the results
137 */
db50cbda 138 while ((dir = FcStrListNext (list)))
179c3995
KP
139 {
140 if (verbose)
141 {
db50cbda 142 printf ("%s: \"%s\": ", program, dir);
179c3995
KP
143 fflush (stdout);
144 }
275cf6cd
PL
145
146 if (!dir)
147 {
148 if (verbose)
149 printf ("skipping, no such directory\n");
150 continue;
151 }
152
660acf8f
PL
153 if (FcStrSetMember (processed_dirs, dir))
154 {
155 if (verbose)
156 printf ("skipping, looped directory detected\n");
157 continue;
158 }
275cf6cd 159
179c3995
KP
160 set = FcFontSetCreate ();
161 if (!set)
162 {
163 fprintf (stderr, "Can't create font set\n");
164 ret++;
165 continue;
166 }
167 subdirs = FcStrSetCreate ();
168 if (!subdirs)
169 {
170 fprintf (stderr, "Can't create directory set\n");
171 ret++;
5c1853cd 172 FcFontSetDestroy (set);
179c3995
KP
173 continue;
174 }
0c35c0fa 175
565a919e 176 if (access ((char *) dir, W_OK) < 0)
0c35c0fa 177 {
565a919e
KP
178 switch (errno) {
179 case ENOENT:
180 case ENOTDIR:
5d43e799 181 if (verbose)
565a919e 182 printf ("skipping, no such directory\n");
719f4b84
PL
183 FcFontSetDestroy (set);
184 FcStrSetDestroy (subdirs);
185 continue;
565a919e
KP
186 case EACCES:
187 case EROFS:
719f4b84
PL
188 /* That's ok, caches go to /var anyway. */
189 /* Ideally we'd do an access on the hashed_name. */
190 /* But we hid that behind an abstraction barrier. */
565a919e
KP
191 break;
192 default:
0c35c0fa
KP
193 fprintf (stderr, "\"%s\": ", dir);
194 perror ("");
195 ret++;
719f4b84
PL
196
197 FcFontSetDestroy (set);
198 FcStrSetDestroy (subdirs);
199 continue;
0c35c0fa 200 }
0c35c0fa 201 }
565a919e
KP
202 if (stat ((char *) dir, &statb) == -1)
203 {
204 fprintf (stderr, "\"%s\": ", dir);
205 perror ("");
5c1853cd
KP
206 FcFontSetDestroy (set);
207 FcStrSetDestroy (subdirs);
565a919e
KP
208 ret++;
209 continue;
210 }
0c35c0fa
KP
211 if (!S_ISDIR (statb.st_mode))
212 {
213 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
5c1853cd
KP
214 FcFontSetDestroy (set);
215 FcStrSetDestroy (subdirs);
0c35c0fa
KP
216 continue;
217 }
d2c01029
PL
218
219 if (really_force)
220 FcDirCacheUnlink (dir, config);
221
cd9bca69 222 if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
179c3995 223 {
0c35c0fa 224 fprintf (stderr, "\"%s\": error scanning\n", dir);
5c1853cd
KP
225 FcFontSetDestroy (set);
226 FcStrSetDestroy (subdirs);
179c3995
KP
227 ret++;
228 continue;
229 }
7410e40b
PL
230 if (!force && FcDirCacheValid (dir, config) &&
231 FcDirCacheHasCurrentArch (dir, config))
179c3995
KP
232 {
233 if (verbose)
234 printf ("skipping, %d fonts, %d dirs\n",
235 set->nfont, nsubdirs(subdirs));
236 }
237 else
238 {
239 if (verbose)
240 printf ("caching, %d fonts, %d dirs\n",
241 set->nfont, nsubdirs (subdirs));
4262e0b3 242
530e66b0
PL
243 /* This is the only reason we can't combine
244 * Valid w/HasCurrentArch... */
7410e40b 245 if (!FcDirCacheValid (dir, config))
8a0b0ed6 246 if (!FcDirCacheUnlink (dir, config))
55c8fa4f
PL
247 ret++;
248
2304e38f 249 if (!FcDirSave (set, subdirs, dir))
179c3995 250 {
83b67390 251 fprintf (stderr, "Can't save cache for \"%s\"\n", dir);
179c3995
KP
252 ret++;
253 }
254 }
255 FcFontSetDestroy (set);
256 sublist = FcStrListCreate (subdirs);
5c1853cd 257 FcStrSetDestroy (subdirs);
179c3995
KP
258 if (!sublist)
259 {
260 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
261 ret++;
262 continue;
263 }
660acf8f 264 FcStrSetAdd (processed_dirs, dir);
d2c01029 265 ret += scanDirs (sublist, config, program, force, really_force, verbose);
179c3995
KP
266 }
267 FcStrListDone (list);
268 return ret;
269}
270
271int
272main (int argc, char **argv)
273{
274 FcStrSet *dirs;
275 FcStrList *list;
276 FcBool verbose = FcFalse;
277 FcBool force = FcFalse;
d2c01029 278 FcBool really_force = FcFalse;
ff3f1f98 279 FcBool systemOnly = FcFalse;
179c3995 280 FcConfig *config;
24330d27 281 int i;
179c3995 282 int ret;
24330d27
KP
283#if HAVE_GETOPT_LONG || HAVE_GETOPT
284 int c;
285
286#if HAVE_GETOPT_LONG
d2c01029 287 while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
24330d27 288#else
d2c01029 289 while ((c = getopt (argc, argv, "frsVv?")) != -1)
24330d27
KP
290#endif
291 {
292 switch (c) {
d2c01029
PL
293 case 'r':
294 really_force = FcTrue;
295 /* fall through */
179c3995
KP
296 case 'f':
297 force = FcTrue;
298 break;
ff3f1f98
KP
299 case 's':
300 systemOnly = FcTrue;
301 break;
24330d27
KP
302 case 'V':
303 fprintf (stderr, "fontconfig version %d.%d.%d\n",
304 FC_MAJOR, FC_MINOR, FC_REVISION);
305 exit (0);
306 case 'v':
179c3995 307 verbose = FcTrue;
24330d27
KP
308 break;
309 default:
310 usage (argv[0]);
311 }
312 }
313 i = optind;
314#else
315 i = 1;
316#endif
317
ff3f1f98
KP
318 if (systemOnly)
319 FcConfigEnableHome (FcFalse);
5e678e94 320 config = FcInitLoadConfig ();
179c3995 321 if (!config)
24330d27 322 {
179c3995 323 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
24330d27
KP
324 return 1;
325 }
df3efc11 326 FcConfigSetCurrent (config);
212c9f43 327
24330d27 328 if (argv[i])
24330d27 329 {
179c3995
KP
330 dirs = FcStrSetCreate ();
331 if (!dirs)
24330d27 332 {
179c3995
KP
333 fprintf (stderr, "%s: Can't create list of directories\n",
334 argv[0]);
335 return 1;
24330d27 336 }
179c3995 337 while (argv[i])
24330d27 338 {
179c3995 339 if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
24330d27 340 {
179c3995
KP
341 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
342 return 1;
24330d27 343 }
179c3995 344 i++;
24330d27 345 }
179c3995
KP
346 list = FcStrListCreate (dirs);
347 FcStrSetDestroy (dirs);
24330d27 348 }
179c3995
KP
349 else
350 list = FcConfigGetConfigDirs (config);
660acf8f
PL
351
352 if ((processed_dirs = FcStrSetCreate()) == NULL) {
353 fprintf(stderr, "Cannot malloc\n");
354 return 1;
355 }
356
d2c01029 357 ret = scanDirs (list, config, argv[0], force, really_force, verbose);
660acf8f
PL
358
359 FcStrSetDestroy (processed_dirs);
360
54560b01
KP
361 /*
362 * Now we need to sleep a second (or two, to be extra sure), to make
363 * sure that timestamps for changes after this run of fc-cache are later
364 * then any timestamps we wrote. We don't use gettimeofday() because
365 * sleep(3) can't be interrupted by a signal here -- this isn't in the
366 * library, and there aren't any signals flying around here.
367 */
716ac8b8 368 FcConfigDestroy (config);
54560b01 369 sleep (2);
24330d27
KP
370 if (verbose)
371 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
372 return ret;
373}