Debian Codegraph @

fc-cache/fc-cache.c

raw
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 #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 <fontconfig/fontconfig.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #ifdef HAVE_DIRENT_H
46 # include <dirent.h>
47 #endif
48 #include <locale.h>
49 #include <string.h>
50
51 #if defined(_WIN32)
52 # define STRICT
53 # include <windows.h>
54 # define sleep(x) Sleep ((x) * 1000)
55 # undef STRICT
56 #endif
57
58 #ifdef ENABLE_NLS
59 # include <libintl.h>
60 # define _(x) (dgettext (GETTEXT_PACKAGE, x))
61 #else
62 # define dgettext(d, s) (s)
63 # define _(x) (x)
64 #endif
65
66 #ifndef O_BINARY
67 # define O_BINARY 0
68 #endif
69
70 #ifndef S_ISDIR
71 # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
72 #endif
73
74 #ifndef HAVE_GETOPT
75 # define HAVE_GETOPT 0
76 #endif
77 #ifndef HAVE_GETOPT_LONG
78 # define HAVE_GETOPT_LONG 0
79 #endif
80
81 #if HAVE_GETOPT_LONG
82 # undef _GNU_SOURCE
83 # define _GNU_SOURCE
84 # include <getopt.h>
85 const struct option longopts[] = {
86 { "error-on-no-fonts", 0, 0, 'E' },
87 { "force", 0, 0, 'f' },
88 { "really-force", 0, 0, 'r' },
89 { "sysroot", required_argument, 0, 'y' },
90 { "system-only", 0, 0, 's' },
91 { "version", 0, 0, 'V' },
92 { "verbose", 0, 0, 'v' },
93 { "help", 0, 0, 'h' },
94 { NULL, 0, 0, 0 },
95 };
96 #else
97 # if HAVE_GETOPT
98 extern char *optarg;
99 extern int optind, opterr, optopt;
100 # endif
101 #endif
102
103 static void
104 usage (char *program, int error)
105 {
106 FILE *file = error ? stderr : stdout;
107 #if HAVE_GETOPT_LONG
108 fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [--error-on-no-fonts] [--force|--really-force] [--sysroot=SYSROOT] [--system-only] [--verbose] [--version] [--help] [dirs]\n"),
109 program);
110 #else
111 fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [dirs]\n"),
112 program);
113 #endif
114 fprintf (file, _("Build font information caches in [dirs]\n"
115 "(all directories in font configuration by default).\n"));
116 fprintf (file, "\n");
117 #if HAVE_GETOPT_LONG
118 fprintf (file, _(" -E, --error-on-no-fonts raise an error if no fonts in a directory\n"));
119 fprintf (file, _(" -f, --force scan directories with apparently valid caches\n"));
120 fprintf (file, _(" -r, --really-force erase all existing caches, then rescan\n"));
121 fprintf (file, _(" -s, --system-only scan system-wide directories only\n"));
122 fprintf (file, _(" -y, --sysroot=SYSROOT prepend SYSROOT to all paths for scanning\n"));
123 fprintf (file, _(" -v, --verbose display status information while busy\n"));
124 fprintf (file, _(" -V, --version display font config version and exit\n"));
125 fprintf (file, _(" -h, --help display this help and exit\n"));
126 #else
127 fprintf (file, _(" -E (error-on-no-fonts)\n"));
128 fprintf (file, _(" raise an error if no fonts in a directory\n"));
129 fprintf (file, _(" -f (force) scan directories with apparently valid caches\n"));
130 fprintf (file, _(" -r, (really force) erase all existing caches, then rescan\n"));
131 fprintf (file, _(" -s (system) scan system-wide directories only\n"));
132 fprintf (file, _(" -y SYSROOT (sysroot) prepend SYSROOT to all paths for scanning\n"));
133 fprintf (file, _(" -v (verbose) display status information while busy\n"));
134 fprintf (file, _(" -V (version) display font config version and exit\n"));
135 fprintf (file, _(" -h (help) display this help and exit\n"));
136 #endif
137 exit (error);
138 }
139
140 static FcStrSet *processed_dirs;
141
142 static int
143 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, FcBool error_on_no_fonts, int *changed)
144 {
145 int ret = 0;
146 const FcChar8 *dir;
147 FcStrSet *subdirs;
148 FcStrList *sublist;
149 FcCache *cache;
150 struct stat statb;
151 FcBool was_valid, was_processed = FcFalse;
152 int i;
153 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
154
155 /*
156 * Now scan all of the directories into separate databases
157 * and write out the results
158 */
159 while ((dir = FcStrListNext (list))) {
160 if (verbose) {
161 if (sysroot)
162 printf ("[%s]", sysroot);
163 printf ("%s: ", dir);
164 fflush (stdout);
165 }
166
167 if (FcStrSetMember (processed_dirs, dir)) {
168 if (verbose)
169 printf (_("skipping, looped directory detected\n"));
170 continue;
171 }
172
173 FcChar8 *rooted_dir = NULL;
174 if (sysroot) {
175 rooted_dir = FcStrPlus (sysroot, dir);
176 } else {
177 rooted_dir = FcStrCopy (dir);
178 }
179
180 if (stat ((char *)rooted_dir, &statb) == -1) {
181 switch (errno) {
182 case ENOENT:
183 case ENOTDIR:
184 if (verbose)
185 printf (_("skipping, no such directory\n"));
186 break;
187 default:
188 fprintf (stderr, "\"%s\": ", dir);
189 perror ("");
190 ret++;
191 break;
192 }
193 FcStrFree (rooted_dir);
194 rooted_dir = NULL;
195 continue;
196 }
197
198 FcStrFree (rooted_dir);
199 rooted_dir = NULL;
200
201 if (!S_ISDIR (statb.st_mode)) {
202 fprintf (stderr, _("\"%s\": not a directory, skipping\n"), dir);
203 continue;
204 }
205 was_processed = FcTrue;
206
207 if (really_force) {
208 FcDirCacheUnlink (dir, config);
209 }
210
211 cache = NULL;
212 was_valid = FcFalse;
213 if (!force) {
214 cache = FcDirCacheLoad (dir, config, NULL);
215 if (cache)
216 was_valid = FcTrue;
217 }
218
219 if (!cache) {
220 (*changed)++;
221 cache = FcDirCacheRead (dir, FcTrue, config);
222 if (!cache) {
223 fprintf (stderr, _("\"%s\": scanning error\n"), dir);
224 ret++;
225 continue;
226 }
227 }
228
229 if (was_valid) {
230 if (verbose)
231 printf (_("skipping, existing cache is valid: %d fonts, %d dirs\n"),
232 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
233 } else {
234 if (verbose)
235 printf (_("caching, new cache contents: %d fonts, %d dirs\n"),
236 FcCacheNumFont (cache), FcCacheNumSubdir (cache));
237
238 if (!FcDirCacheValid (dir)) {
239 fprintf (stderr, _("%s: failed to write cache\n"), dir);
240 (void)FcDirCacheUnlink (dir, config);
241 ret++;
242 }
243 }
244
245 subdirs = FcStrSetCreate();
246 if (!subdirs) {
247 fprintf (stderr, _("%s: Can't create subdir set\n"), dir);
248 ret++;
249 FcDirCacheUnload (cache);
250 continue;
251 }
252 for (i = 0; i < FcCacheNumSubdir (cache); i++)
253 FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
254
255 FcDirCacheUnload (cache);
256
257 sublist = FcStrListCreate (subdirs);
258 FcStrSetDestroy (subdirs);
259 if (!sublist) {
260 fprintf (stderr, _("%s: Can't create subdir list\n"), dir);
261 ret++;
262 continue;
263 }
264 FcStrSetAdd (processed_dirs, dir);
265 ret += scanDirs (sublist, config, force, really_force, verbose, error_on_no_fonts, changed);
266 FcStrListDone (sublist);
267 }
268
269 if (error_on_no_fonts && !was_processed)
270 ret++;
271 return ret;
272 }
273
274 static FcBool
275 cleanCacheDirectories (FcConfig *config, FcBool verbose)
276 {
277 FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
278 FcChar8 *cache_dir;
279 FcBool ret = FcTrue;
280
281 if (!cache_dirs)
282 return FcFalse;
283 while ((cache_dir = FcStrListNext (cache_dirs))) {
284 if (!FcDirCacheClean (cache_dir, verbose)) {
285 ret = FcFalse;
286 break;
287 }
288 }
289 FcStrListDone (cache_dirs);
290 return ret;
291 }
292
293 int
294 main (int argc, char **argv)
295 {
296 FcStrSet *dirs;
297 FcStrList *list;
298 FcBool verbose = FcFalse;
299 FcBool force = FcFalse;
300 FcBool really_force = FcFalse;
301 FcBool systemOnly = FcFalse;
302 FcBool error_on_no_fonts = FcFalse;
303 FcConfig *config;
304 FcChar8 *sysroot = NULL;
305 int i;
306 int changed;
307 int ret;
308 #if HAVE_GETOPT_LONG || HAVE_GETOPT
309 int c;
310
311 setlocale (LC_ALL, "");
312 # if HAVE_GETOPT_LONG
313 while ((c = getopt_long (argc, argv, "Efrsy:Vvh", longopts, NULL)) != -1)
314 # else
315 while ((c = getopt (argc, argv, "Efrsy:Vvh")) != -1)
316 # endif
317 {
318 switch (c) {
319 case 'E':
320 error_on_no_fonts = FcTrue;
321 break;
322 case 'r':
323 really_force = FcTrue;
324 /* fall through */
325 case 'f':
326 force = FcTrue;
327 break;
328 case 's':
329 systemOnly = FcTrue;
330 break;
331 case 'y':
332 sysroot = FcStrCopy ((const FcChar8 *)optarg);
333 break;
334 case 'V':
335 fprintf (stderr, "fontconfig version %d.%d.%d\n",
336 FC_MAJOR, FC_MINOR, FC_REVISION);
337 exit (0);
338 case 'v':
339 verbose = FcTrue;
340 break;
341 case 'h':
342 usage (argv[0], 0);
343 default:
344 usage (argv[0], 1);
345 }
346 }
347 i = optind;
348 #else
349 i = 1;
350 #endif
351
352 if (systemOnly)
353 FcConfigEnableHome (FcFalse);
354 if (sysroot) {
355 FcConfigSetSysRoot (NULL, sysroot);
356 FcStrFree (sysroot);
357 config = FcConfigGetCurrent();
358 } else {
359 config = FcInitLoadConfig();
360 }
361 if (!config) {
362 fprintf (stderr, _("%s: Can't initialize font config library\n"), argv[0]);
363 return 1;
364 }
365 FcConfigSetCurrent (config);
366
367 if (argv[i]) {
368 dirs = FcStrSetCreate();
369 if (!dirs) {
370 fprintf (stderr, _("%s: Can't create list of directories\n"),
371 argv[0]);
372 return 1;
373 }
374 while (argv[i]) {
375 if (!FcStrSetAddFilename (dirs, (FcChar8 *)argv[i])) {
376 fprintf (stderr, _("%s: Can't add directory\n"), argv[0]);
377 return 1;
378 }
379 i++;
380 }
381 list = FcStrListCreate (dirs);
382 FcStrSetDestroy (dirs);
383 } else
384 list = FcConfigGetFontDirs (config);
385
386 if ((processed_dirs = FcStrSetCreate()) == NULL) {
387 fprintf (stderr, _("Out of Memory\n"));
388 return 1;
389 }
390
391 if (verbose) {
392 const FcChar8 *dir;
393
394 printf ("Font directories:\n");
395 while ((dir = FcStrListNext (list))) {
396 printf ("\t%s\n", dir);
397 }
398 FcStrListFirst (list);
399 }
400 changed = 0;
401 ret = scanDirs (list, config, force, really_force, verbose, error_on_no_fonts, &changed);
402 FcStrListDone (list);
403
404 /*
405 * Try to create CACHEDIR.TAG anyway.
406 * This expects the fontconfig cache directory already exists.
407 * If it doesn't, it won't be simply created.
408 */
409 FcCacheCreateTagFile (config);
410
411 FcStrSetDestroy (processed_dirs);
412
413 cleanCacheDirectories (config, verbose);
414
415 FcConfigDestroy (config);
416 FcFini();
417 /*
418 * Now we need to sleep a second (or two, to be extra sure), to make
419 * sure that timestamps for changes after this run of fc-cache are later
420 * then any timestamps we wrote. We don't use gettimeofday() because
421 * sleep(3) can't be interrupted by a signal here -- this isn't in the
422 * library, and there aren't any signals flying around here.
423 */
424 /* the resolution of mtime on FAT is 2 seconds */
425 if (changed)
426 sleep (2);
427 if (verbose)
428 printf ("%s: %s\n", argv[0], ret ? _("failed") : _("succeeded"));
429 return ret;
430 }