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