]> git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
fc-cache: convert
[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 "../src/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 {"convert", 0, 0, 'c'},
71 {"force", 0, 0, 'f'},
72 {"really-force", 0, 0, 'r'},
73 {"root", 1, 0, 'R'},
74 {"system-only", 0, 0, 's'},
75 {"version", 0, 0, 'V'},
76 {"verbose", 0, 0, 'v'},
77 {"help", 0, 0, 'h'},
78 {NULL,0,0,0},
79 };
80 #else
81 #if HAVE_GETOPT
82 extern char *optarg;
83 extern int optind, opterr, optopt;
84 #endif
85 #endif
86
87 static void
88 usage (char *program, int error)
89 {
90 FILE *file = error ? stderr : stdout;
91 #if HAVE_GETOPT_LONG
92 fprintf (file, "usage: %s [-frRsvVh] [--force|--really-force] [--root <root>] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
93 program);
94 #else
95 fprintf (file, "usage: %s [-frRsvVh] [dirs]\n",
96 program);
97 #endif
98 fprintf (file, "Build font information caches in [dirs]\n"
99 "(all directories in font configuration by default).\n");
100 fprintf (file, "\n");
101 #if HAVE_GETOPT_LONG
102 fprintf (file, " -f, --force scan directories with apparently valid caches\n");
103 fprintf (file, " -r, --really-force erase all existing caches, then rescan\n");
104 fprintf (file, " -R, --root <root> change to <root> before loading files\n");
105 fprintf (file, " -s, --system-only scan system-wide directories only\n");
106 fprintf (file, " -v, --verbose display status information while busy\n");
107 fprintf (file, " -V, --version display font config version and exit\n");
108 fprintf (file, " -h, --help display this help and exit\n");
109 #else
110 fprintf (file, " -f (force) scan directories with apparently valid caches\n");
111 fprintf (file, " -r, (really force) erase all existing caches, then rescan\n");
112 fprintf (file, " -R <root> (root) change to <root> before loading files\n");
113 fprintf (file, " -s (system) scan system-wide directories only\n");
114 fprintf (file, " -v (verbose) display status information while busy\n");
115 fprintf (file, " -V (version) display font config version and exit\n");
116 fprintf (file, " -h (help) display this help and exit\n");
117 #endif
118 exit (error);
119 }
120
121 static FcStrSet *processed_dirs;
122
123 static int
124 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
125 {
126 int ret = 0;
127 const FcChar8 *dir;
128 FcStrSet *subdirs;
129 FcStrList *sublist;
130 FcCache *cache;
131 struct stat statb;
132 FcBool was_valid;
133 int i;
134
135 /*
136 * Now scan all of the directories into separate databases
137 * and write out the results
138 */
139 while ((dir = FcStrListNext (list)))
140 {
141 if (verbose)
142 {
143 printf ("%s: ", dir);
144 fflush (stdout);
145 }
146
147 if (!dir)
148 {
149 if (verbose)
150 printf ("skipping, no such directory\n");
151 continue;
152 }
153
154 if (FcStrSetMember (processed_dirs, dir))
155 {
156 if (verbose)
157 printf ("skipping, looped directory detected\n");
158 continue;
159 }
160
161 if (FcStat (config, dir, &statb) == -1)
162 {
163 switch (errno) {
164 case ENOENT:
165 case ENOTDIR:
166 if (verbose)
167 printf ("skipping, no such directory\n");
168 break;
169 default:
170 fprintf (stderr, "\"%s\": ", dir);
171 perror ("");
172 ret++;
173 break;
174 }
175 continue;
176 }
177
178 if (!S_ISDIR (statb.st_mode))
179 {
180 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
181 continue;
182 }
183
184 if (really_force)
185 FcDirCacheUnlink (dir, config);
186
187 cache = NULL;
188 was_valid = FcFalse;
189 if (!force) {
190 cache = FcDirCacheLoad (dir, config, NULL);
191 if (cache)
192 was_valid = FcTrue;
193 }
194
195 if (!cache)
196 {
197 (*changed)++;
198 cache = FcDirCacheRead (dir, FcTrue, config);
199 if (!cache)
200 {
201 fprintf (stderr, "%s: error scanning\n", dir);
202 ret++;
203 continue;
204 }
205 }
206
207 if (was_valid)
208 {
209 if (verbose)
210 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
211 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
212 }
213 else
214 {
215 if (verbose)
216 printf ("caching, new cache contents: %d fonts, %d dirs\n",
217 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
218
219 if (!FcDirCacheValid (dir))
220 {
221 fprintf (stderr, "%s: failed to write cache\n", dir);
222 (void) FcDirCacheUnlink (dir, config);
223 ret++;
224 }
225 }
226
227 subdirs = FcStrSetCreate ();
228 if (!subdirs)
229 {
230 fprintf (stderr, "%s: Can't create subdir set\n", dir);
231 ret++;
232 FcDirCacheUnload (cache);
233 continue;
234 }
235 for (i = 0; i < FcCacheNumSubdir (cache); i++)
236 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
237
238 FcDirCacheUnload (cache);
239
240 sublist = FcStrListCreate (subdirs);
241 FcStrSetDestroy (subdirs);
242 if (!sublist)
243 {
244 fprintf (stderr, "%s: Can't create subdir list\n", dir);
245 ret++;
246 continue;
247 }
248 FcStrSetAdd (processed_dirs, dir);
249 ret += scanDirs (sublist, config, force, really_force, verbose, changed);
250 }
251 FcStrListDone (list);
252 return ret;
253 }
254
255 static FcBool
256 cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
257 {
258 DIR *d;
259 struct dirent *ent;
260 FcChar8 *fullDir;
261 FcChar8 *checkDir;
262 FcBool ret = FcTrue;
263 FcBool remove;
264 FcCache *cache;
265 struct stat target_stat;
266
267 fullDir = FcConfigGetRootPlus (config, dir);
268 if (fullDir)
269 checkDir = fullDir;
270 else
271 checkDir = dir;
272
273 if (access ((char *) checkDir, W_OK) != 0)
274 {
275 if (verbose)
276 printf ("%s: not cleaning %s cache directory\n", dir,
277 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
278 goto done;
279 }
280 if (verbose)
281 printf ("%s: cleaning cache directory\n", dir);
282 d = opendir ((char *) checkDir);
283 if (!d)
284 {
285 perror ((char *) dir);
286 ret = FcFalse;
287 goto done;
288 }
289 while ((ent = readdir (d)))
290 {
291 FcChar8 *file_name;
292 const FcChar8 *target_dir;
293
294 if (ent->d_name[0] == '.')
295 continue;
296 /* skip cache files for different architectures and */
297 /* files which are not cache files at all */
298 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
299 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
300 continue;
301
302 file_name = FcStrPathPlus (dir, (const FcChar8 *) ent->d_name, NULL);
303 if (!file_name)
304 {
305 fprintf (stderr, "%s: allocation failure\n", dir);
306 ret = FcFalse;
307 break;
308 }
309 remove = FcFalse;
310 cache = FcDirCacheLoadFile2 (file_name, config, NULL);
311 if (!cache)
312 {
313 if (verbose)
314 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
315 remove = FcTrue;
316 }
317 else
318 {
319 target_dir = FcCacheDir (cache);
320 if (FcStat (config, target_dir, &target_stat) < 0)
321 {
322 if (verbose)
323 printf ("%s: %s: missing directory: %s \n",
324 dir, ent->d_name, target_dir);
325 remove = FcTrue;
326 }
327 }
328 if (remove)
329 {
330 FcChar8 *unlink_file = FcConfigGetRootPlus (config, file_name);
331 if (!unlink_file)
332 unlink_file = file_name;
333 if (unlink ((char *) unlink_file) < 0)
334 {
335 perror ((char *) unlink_file);
336 ret = FcFalse;
337 }
338 if (unlink_file != file_name)
339 FcStrFree (unlink_file);
340 }
341 FcDirCacheUnload (cache);
342 FcStrFree (file_name);
343 }
344
345 closedir (d);
346 done:
347 if (fullDir)
348 FcStrFree (fullDir);
349 return ret;
350 }
351
352 static FcBool
353 cleanCacheDirectories (FcConfig *config, FcBool verbose)
354 {
355 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
356 FcChar8 *cache_dir;
357 FcBool ret = FcTrue;
358
359 if (!cache_dirs)
360 return FcFalse;
361 while ((cache_dir = FcStrListNext (cache_dirs)))
362 {
363 if (!cleanCacheDirectory (config, cache_dir, verbose))
364 {
365 ret = FcFalse;
366 break;
367 }
368 }
369 FcStrListDone (cache_dirs);
370 return ret;
371 }
372
373 int
374 main (int argc, char **argv)
375 {
376 FcStrSet *dirs;
377 FcStrList *list;
378 FcBool verbose = FcFalse;
379 FcBool force = FcFalse;
380 FcBool really_force = FcFalse;
381 FcBool systemOnly = FcFalse;
382 FcConfig *config;
383 const char *rootDir;
384 int i;
385 int changed;
386 int ret;
387 #if HAVE_GETOPT_LONG || HAVE_GETOPT
388 int c;
389
390 FcBool convert = FcFalse;
391
392 #if HAVE_GETOPT_LONG
393 while ((c = getopt_long (argc, argv, "cfrR:sVvh", longopts, NULL)) != -1)
394 #else
395 while ((c = getopt (argc, argv, "cfrR:sVvh")) != -1)
396 #endif
397 {
398 switch (c) {
399 case 'c':
400 convert = FcTrue;
401 break;
402 case 'r':
403 really_force = FcTrue;
404 /* fall through */
405 case 'f':
406 force = FcTrue;
407 break;
408 case 'R':
409 rootDir = optarg;
410 break;
411 case 's':
412 systemOnly = FcTrue;
413 break;
414 case 'V':
415 fprintf (stderr, "fontconfig version %d.%d.%d\n",
416 FC_MAJOR, FC_MINOR, FC_REVISION);
417 exit (0);
418 case 'v':
419 verbose = FcTrue;
420 break;
421 case 'h':
422 usage (argv[0], 0);
423 default:
424 usage (argv[0], 1);
425 }
426 }
427 i = optind;
428 #else
429 i = 1;
430 #endif
431
432 if (convert)
433 {
434 const FcChar8 *src_cache, *dst_cache, *src_type, *dst_type;
435
436 src_cache = (const FcChar8 *) argv[i++];
437 src_type = (const FcChar8 *) argv[i++];
438 dst_cache = (const FcChar8 *) argv[i++];
439 dst_type = (const FcChar8 *) argv[i++];
440
441 FcDirCacheConvert (src_cache, src_type, dst_cache, dst_type);
442
443 return 0;
444 }
445
446 if (systemOnly)
447 FcConfigEnableHome (FcFalse);
448 config = FcInitLoadConfig ();
449 if (!config)
450 {
451 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
452 return 1;
453 }
454 FcConfigSetCurrent (config);
455 FcConfigSetRoot (config, (const FcChar8 *) rootDir);
456
457 if (argv[i])
458 {
459 dirs = FcStrSetCreate ();
460 if (!dirs)
461 {
462 fprintf (stderr, "%s: Can't create list of directories\n",
463 argv[0]);
464 return 1;
465 }
466 while (argv[i])
467 {
468 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
469 {
470 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
471 return 1;
472 }
473 i++;
474 }
475 list = FcStrListCreate (dirs);
476 FcStrSetDestroy (dirs);
477 }
478 else
479 list = FcConfigGetConfigDirs (config);
480
481 if ((processed_dirs = FcStrSetCreate()) == NULL) {
482 fprintf(stderr, "Cannot malloc\n");
483 return 1;
484 }
485
486 changed = 0;
487 ret = scanDirs (list, config, force, really_force, verbose, &changed);
488
489 FcStrSetDestroy (processed_dirs);
490
491 cleanCacheDirectories (config, verbose);
492
493 /*
494 * Now we need to sleep a second (or two, to be extra sure), to make
495 * sure that timestamps for changes after this run of fc-cache are later
496 * then any timestamps we wrote. We don't use gettimeofday() because
497 * sleep(3) can't be interrupted by a signal here -- this isn't in the
498 * library, and there aren't any signals flying around here.
499 */
500 FcConfigDestroy (config);
501 FcFini ();
502 if (changed)
503 sleep (2);
504 if (verbose)
505 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
506 return ret;
507 }