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