]> git.wh0rd.org - fontconfig.git/blame - fc-cache/fc-cache.c
Make fc-arch stuff cross-compiling-safe
[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'},
ff3f1f98 72 {"system-only", 0, 0, 's'},
24330d27
KP
73 {"version", 0, 0, 'V'},
74 {"verbose", 0, 0, 'v'},
1439c8f2 75 {"help", 0, 0, 'h'},
24330d27
KP
76 {NULL,0,0,0},
77};
78#else
79#if HAVE_GETOPT
80extern char *optarg;
81extern int optind, opterr, optopt;
82#endif
83#endif
84
65018b4a 85static void
1439c8f2 86usage (char *program, int error)
24330d27 87{
1439c8f2 88 FILE *file = error ? stderr : stdout;
86b12431 89#if HAVE_GETOPT_LONG
1439c8f2 90 fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
86b12431
KP
91 program);
92#else
1439c8f2 93 fprintf (file, "usage: %s [-frsvVh] [dirs]\n",
24330d27 94 program);
86b12431 95#endif
1439c8f2 96 fprintf (file, "Build font information caches in [dirs]\n"
24330d27 97 "(all directories in font configuration by default).\n");
1439c8f2 98 fprintf (file, "\n");
86b12431 99#if HAVE_GETOPT_LONG
1439c8f2
BE
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");
86b12431 106#else
1439c8f2
BE
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");
86b12431 113#endif
1439c8f2 114 exit (error);
24330d27
KP
115}
116
660acf8f
PL
117static FcStrSet *processed_dirs;
118
179c3995 119static int
111e5b6d 120scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
24330d27 121{
4984242e
KP
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;
179c3995
KP
130
131 /*
132 * Now scan all of the directories into separate databases
133 * and write out the results
134 */
db50cbda 135 while ((dir = FcStrListNext (list)))
179c3995
KP
136 {
137 if (verbose)
138 {
bc5e487f 139 printf ("%s: ", dir);
179c3995
KP
140 fflush (stdout);
141 }
275cf6cd
PL
142
143 if (!dir)
144 {
145 if (verbose)
146 printf ("skipping, no such directory\n");
147 continue;
148 }
149
660acf8f
PL
150 if (FcStrSetMember (processed_dirs, dir))
151 {
152 if (verbose)
153 printf ("skipping, looped directory detected\n");
154 continue;
155 }
275cf6cd 156
e12f718f 157 if (stat ((char *) dir, &statb) == -1)
0c35c0fa 158 {
565a919e
KP
159 switch (errno) {
160 case ENOENT:
161 case ENOTDIR:
5d43e799 162 if (verbose)
565a919e 163 printf ("skipping, no such directory\n");
565a919e
KP
164 break;
165 default:
0c35c0fa
KP
166 fprintf (stderr, "\"%s\": ", dir);
167 perror ("");
168 ret++;
e12f718f 169 break;
0c35c0fa 170 }
565a919e
KP
171 continue;
172 }
e12f718f 173
0c35c0fa
KP
174 if (!S_ISDIR (statb.st_mode))
175 {
176 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
177 continue;
178 }
d2c01029
PL
179
180 if (really_force)
181 FcDirCacheUnlink (dir, config);
182
bc5e487f
KP
183 cache = NULL;
184 was_valid = FcFalse;
185 if (!force) {
186 cache = FcDirCacheLoad (dir, config, NULL);
187 if (cache)
188 was_valid = FcTrue;
189 }
2d3387fd 190
bc5e487f 191 if (!cache)
179c3995 192 {
111e5b6d 193 (*changed)++;
bc5e487f
KP
194 cache = FcDirCacheRead (dir, FcTrue, config);
195 if (!cache)
196 {
197 fprintf (stderr, "%s: error scanning\n", dir);
198 ret++;
199 continue;
200 }
179c3995 201 }
bc5e487f 202
bc5e487f 203 if (was_valid)
179c3995
KP
204 {
205 if (verbose)
50124d1e 206 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
4984242e 207 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
179c3995
KP
208 }
209 else
210 {
211 if (verbose)
50124d1e 212 printf ("caching, new cache contents: %d fonts, %d dirs\n",
4984242e 213 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
4262e0b3 214
f57783d2 215 if (!FcDirCacheValid (dir))
179c3995 216 {
2d3387fd
KP
217 fprintf (stderr, "%s: failed to write cache\n", dir);
218 (void) FcDirCacheUnlink (dir, config);
179c3995
KP
219 ret++;
220 }
221 }
bc5e487f
KP
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 }
4984242e 231 for (i = 0; i < FcCacheNumSubdir (cache); i++)
bc5e487f
KP
232 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
233
234 FcDirCacheUnload (cache);
235
179c3995 236 sublist = FcStrListCreate (subdirs);
5c1853cd 237 FcStrSetDestroy (subdirs);
179c3995
KP
238 if (!sublist)
239 {
2d3387fd 240 fprintf (stderr, "%s: Can't create subdir list\n", dir);
179c3995
KP
241 ret++;
242 continue;
243 }
660acf8f 244 FcStrSetAdd (processed_dirs, dir);
111e5b6d 245 ret += scanDirs (sublist, config, force, really_force, verbose, changed);
179c3995
KP
246 }
247 FcStrListDone (list);
248 return ret;
249}
250
d8ab9e6c
KP
251static FcBool
252cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
253{
254 DIR *d;
255 struct dirent *ent;
b190ad9d 256 FcChar8 *dir_base;
d8ab9e6c
KP
257 FcBool ret = FcTrue;
258 FcBool remove;
259 FcCache *cache;
d8ab9e6c
KP
260 struct stat target_stat;
261
b190ad9d 262 dir_base = FcStrPlus (dir, (FcChar8 *) "/");
1741499e
KP
263 if (!dir_base)
264 {
265 fprintf (stderr, "%s: out of memory\n", dir);
266 return FcFalse;
267 }
23848903 268 if (access ((char *) dir, W_OK) != 0)
d8ab9e6c
KP
269 {
270 if (verbose)
2b0d3d8a
KP
271 printf ("%s: not cleaning %s cache directory\n", dir,
272 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
1741499e 273 FcStrFree (dir_base);
d8ab9e6c
KP
274 return FcTrue;
275 }
bc5e487f
KP
276 if (verbose)
277 printf ("%s: cleaning cache directory\n", dir);
b190ad9d 278 d = opendir ((char *) dir);
d8ab9e6c
KP
279 if (!d)
280 {
b190ad9d 281 perror ((char *) dir);
1741499e 282 FcStrFree (dir_base);
d8ab9e6c
KP
283 return FcFalse;
284 }
285 while ((ent = readdir (d)))
286 {
287 FcChar8 *file_name;
4984242e 288 const FcChar8 *target_dir;
d8ab9e6c
KP
289
290 if (ent->d_name[0] == '.')
291 continue;
0334e5a2
MF
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
b190ad9d 298 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
d8ab9e6c
KP
299 if (!file_name)
300 {
301 fprintf (stderr, "%s: allocation failure\n", dir);
302 ret = FcFalse;
303 break;
304 }
2a3e3c44 305 remove = FcFalse;
9b84ecff 306 cache = FcDirCacheLoadFile (file_name, NULL);
d8ab9e6c 307 if (!cache)
d8ab9e6c
KP
308 {
309 if (verbose)
2a3e3c44 310 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
d8ab9e6c
KP
311 remove = FcTrue;
312 }
2a3e3c44 313 else
d8ab9e6c 314 {
2a3e3c44
KP
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);
2a3e3c44
KP
321 remove = FcTrue;
322 }
d8ab9e6c
KP
323 }
324 if (remove)
325 {
b190ad9d 326 if (unlink ((char *) file_name) < 0)
d8ab9e6c 327 {
b190ad9d 328 perror ((char *) file_name);
d8ab9e6c
KP
329 ret = FcFalse;
330 }
331 }
1741499e 332 FcDirCacheUnload (cache);
d8ab9e6c
KP
333 FcStrFree (file_name);
334 }
335
336 closedir (d);
1741499e 337 FcStrFree (dir_base);
d8ab9e6c
KP
338 return ret;
339}
340
341static FcBool
342cleanCacheDirectories (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
179c3995
KP
362int
363main (int argc, char **argv)
364{
365 FcStrSet *dirs;
366 FcStrList *list;
367 FcBool verbose = FcFalse;
368 FcBool force = FcFalse;
d2c01029 369 FcBool really_force = FcFalse;
ff3f1f98 370 FcBool systemOnly = FcFalse;
179c3995 371 FcConfig *config;
24330d27 372 int i;
111e5b6d 373 int changed;
179c3995 374 int ret;
24330d27
KP
375#if HAVE_GETOPT_LONG || HAVE_GETOPT
376 int c;
377
378#if HAVE_GETOPT_LONG
1439c8f2 379 while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
24330d27 380#else
1439c8f2 381 while ((c = getopt (argc, argv, "frsVvh")) != -1)
24330d27
KP
382#endif
383 {
384 switch (c) {
d2c01029
PL
385 case 'r':
386 really_force = FcTrue;
387 /* fall through */
179c3995
KP
388 case 'f':
389 force = FcTrue;
390 break;
ff3f1f98
KP
391 case 's':
392 systemOnly = FcTrue;
393 break;
24330d27
KP
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':
179c3995 399 verbose = FcTrue;
24330d27 400 break;
1439c8f2
BE
401 case 'h':
402 usage (argv[0], 0);
24330d27 403 default:
1439c8f2 404 usage (argv[0], 1);
24330d27
KP
405 }
406 }
407 i = optind;
408#else
409 i = 1;
410#endif
411
ff3f1f98
KP
412 if (systemOnly)
413 FcConfigEnableHome (FcFalse);
5e678e94 414 config = FcInitLoadConfig ();
179c3995 415 if (!config)
24330d27 416 {
179c3995 417 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
24330d27
KP
418 return 1;
419 }
df3efc11 420 FcConfigSetCurrent (config);
212c9f43 421
24330d27 422 if (argv[i])
24330d27 423 {
179c3995
KP
424 dirs = FcStrSetCreate ();
425 if (!dirs)
24330d27 426 {
179c3995
KP
427 fprintf (stderr, "%s: Can't create list of directories\n",
428 argv[0]);
429 return 1;
24330d27 430 }
179c3995 431 while (argv[i])
24330d27 432 {
9b511b29 433 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
24330d27 434 {
179c3995
KP
435 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
436 return 1;
24330d27 437 }
179c3995 438 i++;
24330d27 439 }
179c3995
KP
440 list = FcStrListCreate (dirs);
441 FcStrSetDestroy (dirs);
24330d27 442 }
179c3995
KP
443 else
444 list = FcConfigGetConfigDirs (config);
660acf8f
PL
445
446 if ((processed_dirs = FcStrSetCreate()) == NULL) {
447 fprintf(stderr, "Cannot malloc\n");
448 return 1;
449 }
450
111e5b6d
BE
451 changed = 0;
452 ret = scanDirs (list, config, force, really_force, verbose, &changed);
660acf8f
PL
453
454 FcStrSetDestroy (processed_dirs);
455
d8ab9e6c
KP
456 cleanCacheDirectories (config, verbose);
457
54560b01
KP
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 */
716ac8b8 465 FcConfigDestroy (config);
c6c9400d 466 FcFini ();
111e5b6d
BE
467 if (changed)
468 sleep (2);
24330d27
KP
469 if (verbose)
470 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
471 return ret;
472}