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