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