]> git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
FcStrPathPlus: new helper function
[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 {"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 FcBool ret = FcTrue;
257 FcBool remove;
258 FcCache *cache;
259 struct stat target_stat;
260
261 if (access ((char *) dir, W_OK) != 0)
262 {
263 if (verbose)
264 printf ("%s: not cleaning %s cache directory\n", dir,
265 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
266 return FcTrue;
267 }
268 if (verbose)
269 printf ("%s: cleaning cache directory\n", dir);
270 d = opendir ((char *) dir);
271 if (!d)
272 {
273 perror ((char *) dir);
274 return FcFalse;
275 }
276 while ((ent = readdir (d)))
277 {
278 FcChar8 *file_name;
279 const FcChar8 *target_dir;
280
281 if (ent->d_name[0] == '.')
282 continue;
283 /* skip cache files for different architectures and */
284 /* files which are not cache files at all */
285 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
286 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
287 continue;
288
289 file_name = FcStrPathPlus (dir, (const FcChar8 *) ent->d_name, NULL);
290 if (!file_name)
291 {
292 fprintf (stderr, "%s: allocation failure\n", dir);
293 ret = FcFalse;
294 break;
295 }
296 remove = FcFalse;
297 cache = FcDirCacheLoadFile (file_name, NULL);
298 if (!cache)
299 {
300 if (verbose)
301 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
302 remove = FcTrue;
303 }
304 else
305 {
306 target_dir = FcCacheDir (cache);
307 if (stat ((char *) target_dir, &target_stat) < 0)
308 {
309 if (verbose)
310 printf ("%s: %s: missing directory: %s \n",
311 dir, ent->d_name, target_dir);
312 remove = FcTrue;
313 }
314 }
315 if (remove)
316 {
317 if (unlink ((char *) file_name) < 0)
318 {
319 perror ((char *) file_name);
320 ret = FcFalse;
321 }
322 }
323 FcDirCacheUnload (cache);
324 FcStrFree (file_name);
325 }
326
327 closedir (d);
328 return ret;
329 }
330
331 static FcBool
332 cleanCacheDirectories (FcConfig *config, FcBool verbose)
333 {
334 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
335 FcChar8 *cache_dir;
336 FcBool ret = FcTrue;
337
338 if (!cache_dirs)
339 return FcFalse;
340 while ((cache_dir = FcStrListNext (cache_dirs)))
341 {
342 if (!cleanCacheDirectory (config, cache_dir, verbose))
343 {
344 ret = FcFalse;
345 break;
346 }
347 }
348 FcStrListDone (cache_dirs);
349 return ret;
350 }
351
352 int
353 main (int argc, char **argv)
354 {
355 FcStrSet *dirs;
356 FcStrList *list;
357 FcBool verbose = FcFalse;
358 FcBool force = FcFalse;
359 FcBool really_force = FcFalse;
360 FcBool systemOnly = FcFalse;
361 FcConfig *config;
362 int i;
363 int changed;
364 int ret;
365 #if HAVE_GETOPT_LONG || HAVE_GETOPT
366 int c;
367
368 #if HAVE_GETOPT_LONG
369 while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
370 #else
371 while ((c = getopt (argc, argv, "frsVvh")) != -1)
372 #endif
373 {
374 switch (c) {
375 case 'r':
376 really_force = FcTrue;
377 /* fall through */
378 case 'f':
379 force = FcTrue;
380 break;
381 case 's':
382 systemOnly = FcTrue;
383 break;
384 case 'V':
385 fprintf (stderr, "fontconfig version %d.%d.%d\n",
386 FC_MAJOR, FC_MINOR, FC_REVISION);
387 exit (0);
388 case 'v':
389 verbose = FcTrue;
390 break;
391 case 'h':
392 usage (argv[0], 0);
393 default:
394 usage (argv[0], 1);
395 }
396 }
397 i = optind;
398 #else
399 i = 1;
400 #endif
401
402 if (systemOnly)
403 FcConfigEnableHome (FcFalse);
404 config = FcInitLoadConfig ();
405 if (!config)
406 {
407 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
408 return 1;
409 }
410 FcConfigSetCurrent (config);
411
412 if (argv[i])
413 {
414 dirs = FcStrSetCreate ();
415 if (!dirs)
416 {
417 fprintf (stderr, "%s: Can't create list of directories\n",
418 argv[0]);
419 return 1;
420 }
421 while (argv[i])
422 {
423 if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
424 {
425 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
426 return 1;
427 }
428 i++;
429 }
430 list = FcStrListCreate (dirs);
431 FcStrSetDestroy (dirs);
432 }
433 else
434 list = FcConfigGetConfigDirs (config);
435
436 if ((processed_dirs = FcStrSetCreate()) == NULL) {
437 fprintf(stderr, "Cannot malloc\n");
438 return 1;
439 }
440
441 changed = 0;
442 ret = scanDirs (list, config, force, really_force, verbose, &changed);
443
444 FcStrSetDestroy (processed_dirs);
445
446 cleanCacheDirectories (config, verbose);
447
448 /*
449 * Now we need to sleep a second (or two, to be extra sure), to make
450 * sure that timestamps for changes after this run of fc-cache are later
451 * then any timestamps we wrote. We don't use gettimeofday() because
452 * sleep(3) can't be interrupted by a signal here -- this isn't in the
453 * library, and there aren't any signals flying around here.
454 */
455 FcConfigDestroy (config);
456 FcFini ();
457 if (changed)
458 sleep (2);
459 if (verbose)
460 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
461 return ret;
462 }