]> git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
a0da50e5ec03141bf5b6f2fdcde67163d6ea7a47
[fontconfig.git] / fc-cache / fc-cache.c
1 /*
2 * fontconfig/fc-cache/fc-cache.c
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make 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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 "../fc-arch/fcarch.h"
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #else
30 #ifdef linux
31 #define HAVE_GETOPT_LONG 1
32 #endif
33 #define HAVE_GETOPT 1
34 #endif
35
36 #include <fontconfig/fontconfig.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <dirent.h>
45 #include <string.h>
46
47 #if defined (_WIN32)
48 #define STRICT
49 #include <windows.h>
50 #define sleep(x) Sleep((x) * 1000)
51 #undef STRICT
52 #endif
53
54 #ifndef O_BINARY
55 #define O_BINARY 0
56 #endif
57
58 #ifndef HAVE_GETOPT
59 #define HAVE_GETOPT 0
60 #endif
61 #ifndef HAVE_GETOPT_LONG
62 #define HAVE_GETOPT_LONG 0
63 #endif
64
65 #if HAVE_GETOPT_LONG
66 #undef _GNU_SOURCE
67 #define _GNU_SOURCE
68 #include <getopt.h>
69 const struct option longopts[] = {
70 {"force", 0, 0, 'f'},
71 {"really-force", 0, 0, 'r'},
72 {"system-only", 0, 0, 's'},
73 {"version", 0, 0, 'V'},
74 {"verbose", 0, 0, 'v'},
75 {"help", 0, 0, 'h'},
76 {NULL,0,0,0},
77 };
78 #else
79 #if HAVE_GETOPT
80 extern char *optarg;
81 extern int optind, opterr, optopt;
82 #endif
83 #endif
84
85 static void
86 usage (char *program, int error)
87 {
88 FILE *file = error ? stderr : stdout;
89 #if HAVE_GETOPT_LONG
90 fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
91 program);
92 #else
93 fprintf (file, "usage: %s [-frsvVh] [dirs]\n",
94 program);
95 #endif
96 fprintf (file, "Build font information caches in [dirs]\n"
97 "(all directories in font configuration by default).\n");
98 fprintf (file, "\n");
99 #if HAVE_GETOPT_LONG
100 fprintf (file, " -f, --force scan directories with apparently valid caches\n");
101 fprintf (file, " -r, --really-force erase all existing caches, then rescan\n");
102 fprintf (file, " -s, --system-only scan system-wide directories only\n");
103 fprintf (file, " -v, --verbose display status information while busy\n");
104 fprintf (file, " -V, --version display font config version and exit\n");
105 fprintf (file, " -h, --help display this help and exit\n");
106 #else
107 fprintf (file, " -f (force) scan directories with apparently valid caches\n");
108 fprintf (file, " -r, (really force) erase all existing caches, then rescan\n");
109 fprintf (file, " -s (system) scan system-wide directories only\n");
110 fprintf (file, " -v (verbose) display status information while busy\n");
111 fprintf (file, " -V (version) display font config version and exit\n");
112 fprintf (file, " -h (help) display this help and exit\n");
113 #endif
114 exit (error);
115 }
116
117 static FcStrSet *processed_dirs;
118
119 static int
120 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
121 {
122 int ret = 0;
123 const FcChar8 *dir;
124 FcStrSet *subdirs;
125 FcStrList *sublist;
126 FcCache *cache;
127 struct stat statb;
128 FcBool was_valid;
129 int i;
130
131 /*
132 * Now scan all of the directories into separate databases
133 * and write out the results
134 */
135 while ((dir = FcStrListNext (list)))
136 {
137 if (verbose)
138 {
139 printf ("%s: ", dir);
140 fflush (stdout);
141 }
142
143 if (!dir)
144 {
145 if (verbose)
146 printf ("skipping, no such directory\n");
147 continue;
148 }
149
150 if (FcStrSetMember (processed_dirs, dir))
151 {
152 if (verbose)
153 printf ("skipping, looped directory detected\n");
154 continue;
155 }
156
157 if (stat ((char *) dir, &statb) == -1)
158 {
159 switch (errno) {
160 case ENOENT:
161 case ENOTDIR:
162 if (verbose)
163 printf ("skipping, no such directory\n");
164 break;
165 default:
166 fprintf (stderr, "\"%s\": ", dir);
167 perror ("");
168 ret++;
169 break;
170 }
171 continue;
172 }
173
174 if (!S_ISDIR (statb.st_mode))
175 {
176 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
177 continue;
178 }
179
180 if (really_force)
181 FcDirCacheUnlink (dir, config);
182
183 cache = NULL;
184 was_valid = FcFalse;
185 if (!force) {
186 cache = FcDirCacheLoad (dir, config, NULL);
187 if (cache)
188 was_valid = FcTrue;
189 }
190
191 if (!cache)
192 {
193 (*changed)++;
194 cache = FcDirCacheRead (dir, FcTrue, config);
195 if (!cache)
196 {
197 fprintf (stderr, "%s: error scanning\n", dir);
198 ret++;
199 continue;
200 }
201 }
202
203 if (was_valid)
204 {
205 if (verbose)
206 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
207 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
208 }
209 else
210 {
211 if (verbose)
212 printf ("caching, new cache contents: %d fonts, %d dirs\n",
213 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
214
215 if (!FcDirCacheValid (dir))
216 {
217 fprintf (stderr, "%s: failed to write cache\n", dir);
218 (void) FcDirCacheUnlink (dir, config);
219 ret++;
220 }
221 }
222
223 subdirs = FcStrSetCreate ();
224 if (!subdirs)
225 {
226 fprintf (stderr, "%s: Can't create subdir set\n", dir);
227 ret++;
228 FcDirCacheUnload (cache);
229 continue;
230 }
231 for (i = 0; i < FcCacheNumSubdir (cache); i++)
232 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
233
234 FcDirCacheUnload (cache);
235
236 sublist = FcStrListCreate (subdirs);
237 FcStrSetDestroy (subdirs);
238 if (!sublist)
239 {
240 fprintf (stderr, "%s: Can't create subdir list\n", dir);
241 ret++;
242 continue;
243 }
244 FcStrSetAdd (processed_dirs, dir);
245 ret += scanDirs (sublist, config, force, really_force, verbose, changed);
246 }
247 FcStrListDone (list);
248 return ret;
249 }
250
251 static FcBool
252 cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
253 {
254 DIR *d;
255 struct dirent *ent;
256 FcChar8 *dir_base;
257 FcBool ret = FcTrue;
258 FcBool remove;
259 FcCache *cache;
260 struct stat target_stat;
261
262 dir_base = FcStrPlus (dir, (FcChar8 *) "/");
263 if (!dir_base)
264 {
265 fprintf (stderr, "%s: out of memory\n", dir);
266 return FcFalse;
267 }
268 if (access ((char *) dir, W_OK) != 0)
269 {
270 if (verbose)
271 printf ("%s: not cleaning %s cache directory\n", dir,
272 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
273 FcStrFree (dir_base);
274 return FcTrue;
275 }
276 if (verbose)
277 printf ("%s: cleaning cache directory\n", dir);
278 d = opendir ((char *) dir);
279 if (!d)
280 {
281 perror ((char *) dir);
282 FcStrFree (dir_base);
283 return FcFalse;
284 }
285 while ((ent = readdir (d)))
286 {
287 FcChar8 *file_name;
288 const FcChar8 *target_dir;
289
290 if (ent->d_name[0] == '.')
291 continue;
292 /* skip cache files for different architectures and */
293 /* files which are not cache files at all */
294 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
295 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
296 continue;
297
298 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
299 if (!file_name)
300 {
301 fprintf (stderr, "%s: allocation failure\n", dir);
302 ret = FcFalse;
303 break;
304 }
305 remove = FcFalse;
306 cache = FcDirCacheLoadFile (file_name, NULL);
307 if (!cache)
308 {
309 if (verbose)
310 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
311 remove = FcTrue;
312 }
313 else
314 {
315 target_dir = FcCacheDir (cache);
316 if (stat ((char *) target_dir, &target_stat) < 0)
317 {
318 if (verbose)
319 printf ("%s: %s: missing directory: %s \n",
320 dir, ent->d_name, target_dir);
321 remove = FcTrue;
322 }
323 }
324 if (remove)
325 {
326 if (unlink ((char *) file_name) < 0)
327 {
328 perror ((char *) file_name);
329 ret = FcFalse;
330 }
331 }
332 FcDirCacheUnload (cache);
333 FcStrFree (file_name);
334 }
335
336 closedir (d);
337 FcStrFree (dir_base);
338 return ret;
339 }
340
341 static FcBool
342 cleanCacheDirectories (FcConfig *config, FcBool verbose)
343 {
344 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
345 FcChar8 *cache_dir;
346 FcBool ret = FcTrue;
347
348 if (!cache_dirs)
349 return FcFalse;
350 while ((cache_dir = FcStrListNext (cache_dirs)))
351 {
352 if (!cleanCacheDirectory (config, cache_dir, verbose))
353 {
354 ret = FcFalse;
355 break;
356 }
357 }
358 FcStrListDone (cache_dirs);
359 return ret;
360 }
361
362 int
363 main (int argc, char **argv)
364 {
365 FcStrSet *dirs;
366 FcStrList *list;
367 FcBool verbose = FcFalse;
368 FcBool force = FcFalse;
369 FcBool really_force = FcFalse;
370 FcBool systemOnly = FcFalse;
371 FcConfig *config;
372 int i;
373 int changed;
374 int ret;
375 #if HAVE_GETOPT_LONG || HAVE_GETOPT
376 int c;
377
378 #if HAVE_GETOPT_LONG
379 while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
380 #else
381 while ((c = getopt (argc, argv, "frsVvh")) != -1)
382 #endif
383 {
384 switch (c) {
385 case 'r':
386 really_force = FcTrue;
387 /* fall through */
388 case 'f':
389 force = FcTrue;
390 break;
391 case 's':
392 systemOnly = FcTrue;
393 break;
394 case 'V':
395 fprintf (stderr, "fontconfig version %d.%d.%d\n",
396 FC_MAJOR, FC_MINOR, FC_REVISION);
397 exit (0);
398 case 'v':
399 verbose = FcTrue;
400 break;
401 case 'h':
402 usage (argv[0], 0);
403 default:
404 usage (argv[0], 1);
405 }
406 }
407 i = optind;
408 #else
409 i = 1;
410 #endif
411
412 if (systemOnly)
413 FcConfigEnableHome (FcFalse);
414 config = FcInitLoadConfig ();
415 if (!config)
416 {
417 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
418 return 1;
419 }
420 FcConfigSetCurrent (config);
421
422 if (argv[i])
423 {
424 dirs = FcStrSetCreate ();
425 if (!dirs)
426 {
427 fprintf (stderr, "%s: Can't create list of directories\n",
428 argv[0]);
429 return 1;
430 }
431 while (argv[i])
432 {
433 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
434 {
435 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
436 return 1;
437 }
438 i++;
439 }
440 list = FcStrListCreate (dirs);
441 FcStrSetDestroy (dirs);
442 }
443 else
444 list = FcConfigGetConfigDirs (config);
445
446 if ((processed_dirs = FcStrSetCreate()) == NULL) {
447 fprintf(stderr, "Cannot malloc\n");
448 return 1;
449 }
450
451 changed = 0;
452 ret = scanDirs (list, config, force, really_force, verbose, &changed);
453
454 FcStrSetDestroy (processed_dirs);
455
456 cleanCacheDirectories (config, verbose);
457
458 /*
459 * Now we need to sleep a second (or two, to be extra sure), to make
460 * sure that timestamps for changes after this run of fc-cache are later
461 * then any timestamps we wrote. We don't use gettimeofday() because
462 * sleep(3) can't be interrupted by a signal here -- this isn't in the
463 * library, and there aren't any signals flying around here.
464 */
465 FcConfigDestroy (config);
466 FcFini ();
467 if (changed)
468 sleep (2);
469 if (verbose)
470 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
471 return ret;
472 }