]> git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
Eliminate global cache. Eliminate multi-arch cache code.
[fontconfig.git] / fc-cache / fc-cache.c
1 /*
2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
3 *
4 * Copyright © 2002 Keith Packard
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 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #else
28 #ifdef linux
29 #define HAVE_GETOPT_LONG 1
30 #endif
31 #define HAVE_GETOPT 1
32 #endif
33
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
42 #if defined (_WIN32)
43 #define STRICT
44 #include <windows.h>
45 #define sleep(x) Sleep((x) * 1000)
46 #undef STRICT
47 #endif
48
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
56 #if HAVE_GETOPT_LONG
57 #undef _GNU_SOURCE
58 #define _GNU_SOURCE
59 #include <getopt.h>
60 const struct option longopts[] = {
61 {"force", 0, 0, 'f'},
62 {"really-force", 0, 0, 'r'},
63 {"system-only", 0, 0, 's'},
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
71 extern char *optarg;
72 extern int optind, opterr, optopt;
73 #endif
74 #endif
75
76 static void
77 usage (char *program)
78 {
79 #if HAVE_GETOPT_LONG
80 fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
81 program);
82 #else
83 fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
84 program);
85 #endif
86 fprintf (stderr, "Build font information caches in [dirs]\n"
87 "(all directories in font configuration by default).\n");
88 fprintf (stderr, "\n");
89 #if HAVE_GETOPT_LONG
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");
96 #else
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");
103 #endif
104 exit (1);
105 }
106
107 static FcStrSet *processed_dirs;
108
109 static int
110 nsubdirs (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
124 static int
125 scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose)
126 {
127 int ret = 0;
128 const FcChar8 *dir;
129 FcFontSet *set;
130 FcStrSet *subdirs;
131 FcStrList *sublist;
132 struct stat statb;
133
134 /*
135 * Now scan all of the directories into separate databases
136 * and write out the results
137 */
138 while ((dir = FcStrListNext (list)))
139 {
140 if (verbose)
141 {
142 printf ("%s: \"%s\": ", program, dir);
143 fflush (stdout);
144 }
145
146 if (!dir)
147 {
148 if (verbose)
149 printf ("skipping, no such directory\n");
150 continue;
151 }
152
153 if (FcStrSetMember (processed_dirs, dir))
154 {
155 if (verbose)
156 printf ("skipping, looped directory detected\n");
157 continue;
158 }
159
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++;
172 FcFontSetDestroy (set);
173 continue;
174 }
175
176 if (access ((char *) dir, W_OK) < 0)
177 {
178 switch (errno) {
179 case ENOENT:
180 case ENOTDIR:
181 if (verbose)
182 printf ("skipping, no such directory\n");
183 FcFontSetDestroy (set);
184 FcStrSetDestroy (subdirs);
185 continue;
186 case EACCES:
187 case EROFS:
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. */
191 break;
192 default:
193 fprintf (stderr, "\"%s\": ", dir);
194 perror ("");
195 ret++;
196
197 FcFontSetDestroy (set);
198 FcStrSetDestroy (subdirs);
199 continue;
200 }
201 }
202 if (stat ((char *) dir, &statb) == -1)
203 {
204 fprintf (stderr, "\"%s\": ", dir);
205 perror ("");
206 FcFontSetDestroy (set);
207 FcStrSetDestroy (subdirs);
208 ret++;
209 continue;
210 }
211 if (!S_ISDIR (statb.st_mode))
212 {
213 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
214 FcFontSetDestroy (set);
215 FcStrSetDestroy (subdirs);
216 continue;
217 }
218
219 if (really_force)
220 FcDirCacheUnlink (dir, config);
221
222 if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
223 {
224 fprintf (stderr, "\"%s\": error scanning\n", dir);
225 FcFontSetDestroy (set);
226 FcStrSetDestroy (subdirs);
227 ret++;
228 continue;
229 }
230 if (!force && FcDirCacheValid (dir, config))
231 {
232 if (verbose)
233 printf ("skipping, %d fonts, %d dirs\n",
234 set->nfont, nsubdirs(subdirs));
235 }
236 else
237 {
238 if (verbose)
239 printf ("caching, %d fonts, %d dirs\n",
240 set->nfont, nsubdirs (subdirs));
241
242 /* This is the only reason we can't combine
243 * Valid w/HasCurrentArch... */
244 if (!FcDirCacheValid (dir, config))
245 if (!FcDirCacheUnlink (dir, config))
246 ret++;
247
248 if (!FcDirSave (set, subdirs, dir))
249 {
250 fprintf (stderr, "Can't save cache for \"%s\"\n", dir);
251 ret++;
252 }
253 }
254 FcFontSetDestroy (set);
255 sublist = FcStrListCreate (subdirs);
256 FcStrSetDestroy (subdirs);
257 if (!sublist)
258 {
259 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
260 ret++;
261 continue;
262 }
263 FcStrSetAdd (processed_dirs, dir);
264 ret += scanDirs (sublist, config, program, force, really_force, verbose);
265 }
266 FcStrListDone (list);
267 return ret;
268 }
269
270 int
271 main (int argc, char **argv)
272 {
273 FcStrSet *dirs;
274 FcStrList *list;
275 FcBool verbose = FcFalse;
276 FcBool force = FcFalse;
277 FcBool really_force = FcFalse;
278 FcBool systemOnly = FcFalse;
279 FcConfig *config;
280 int i;
281 int ret;
282 #if HAVE_GETOPT_LONG || HAVE_GETOPT
283 int c;
284
285 #if HAVE_GETOPT_LONG
286 while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
287 #else
288 while ((c = getopt (argc, argv, "frsVv?")) != -1)
289 #endif
290 {
291 switch (c) {
292 case 'r':
293 really_force = FcTrue;
294 /* fall through */
295 case 'f':
296 force = FcTrue;
297 break;
298 case 's':
299 systemOnly = FcTrue;
300 break;
301 case 'V':
302 fprintf (stderr, "fontconfig version %d.%d.%d\n",
303 FC_MAJOR, FC_MINOR, FC_REVISION);
304 exit (0);
305 case 'v':
306 verbose = FcTrue;
307 break;
308 default:
309 usage (argv[0]);
310 }
311 }
312 i = optind;
313 #else
314 i = 1;
315 #endif
316
317 if (systemOnly)
318 FcConfigEnableHome (FcFalse);
319 config = FcInitLoadConfig ();
320 if (!config)
321 {
322 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
323 return 1;
324 }
325 FcConfigSetCurrent (config);
326
327 if (argv[i])
328 {
329 dirs = FcStrSetCreate ();
330 if (!dirs)
331 {
332 fprintf (stderr, "%s: Can't create list of directories\n",
333 argv[0]);
334 return 1;
335 }
336 while (argv[i])
337 {
338 if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
339 {
340 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
341 return 1;
342 }
343 i++;
344 }
345 list = FcStrListCreate (dirs);
346 FcStrSetDestroy (dirs);
347 }
348 else
349 list = FcConfigGetConfigDirs (config);
350
351 if ((processed_dirs = FcStrSetCreate()) == NULL) {
352 fprintf(stderr, "Cannot malloc\n");
353 return 1;
354 }
355
356 ret = scanDirs (list, config, argv[0], force, really_force, verbose);
357
358 FcStrSetDestroy (processed_dirs);
359
360 /*
361 * Now we need to sleep a second (or two, to be extra sure), to make
362 * sure that timestamps for changes after this run of fc-cache are later
363 * then any timestamps we wrote. We don't use gettimeofday() because
364 * sleep(3) can't be interrupted by a signal here -- this isn't in the
365 * library, and there aren't any signals flying around here.
366 */
367 FcConfigDestroy (config);
368 sleep (2);
369 if (verbose)
370 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
371 return ret;
372 }