]> git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
Patrick Lam <plam@mit.edu>
[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 #include <fontconfig/fontconfig.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #else
35 #ifdef linux
36 #define HAVE_GETOPT_LONG 1
37 #endif
38 #define HAVE_GETOPT 1
39 #endif
40
41 #if defined (_WIN32)
42 #define STRICT
43 #include <windows.h>
44 #define sleep(x) Sleep((x) * 1000)
45 #undef STRICT
46 #endif
47
48 #ifndef HAVE_GETOPT
49 #define HAVE_GETOPT 0
50 #endif
51 #ifndef HAVE_GETOPT_LONG
52 #define HAVE_GETOPT_LONG 0
53 #endif
54
55 #if HAVE_GETOPT_LONG
56 #undef _GNU_SOURCE
57 #define _GNU_SOURCE
58 #include <getopt.h>
59 const struct option longopts[] = {
60 {"force", 0, 0, 'f'},
61 {"really-force", 0, 0, 'r'},
62 {"system-only", 0, 0, 's'},
63 {"version", 0, 0, 'V'},
64 {"verbose", 0, 0, 'v'},
65 {"help", 0, 0, '?'},
66 {NULL,0,0,0},
67 };
68 #else
69 #if HAVE_GETOPT
70 extern char *optarg;
71 extern int optind, opterr, optopt;
72 #endif
73 #endif
74
75 static void
76 usage (char *program)
77 {
78 #if HAVE_GETOPT_LONG
79 fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
80 program);
81 #else
82 fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
83 program);
84 #endif
85 fprintf (stderr, "Build font information caches in [dirs]\n"
86 "(all directories in font configuration by default).\n");
87 fprintf (stderr, "\n");
88 #if HAVE_GETOPT_LONG
89 fprintf (stderr, " -f, --force scan directories with apparently valid caches\n");
90 fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n");
91 fprintf (stderr, " -s, --system-only scan system-wide directories only\n");
92 fprintf (stderr, " -v, --verbose display status information while busy\n");
93 fprintf (stderr, " -V, --version display font config version and exit\n");
94 fprintf (stderr, " -?, --help display this help and exit\n");
95 #else
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) 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 #endif
103 exit (1);
104 }
105
106 static FcStrSet *processed_dirs;
107
108 static int
109 nsubdirs (FcStrSet *set)
110 {
111 FcStrList *list;
112 int n = 0;
113
114 list = FcStrListCreate (set);
115 if (!list)
116 return 0;
117 while (FcStrListNext (list))
118 n++;
119 FcStrListDone (list);
120 return n;
121 }
122
123 static int
124 scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose)
125 {
126 int ret = 0;
127 const FcChar8 *dir;
128 const FcChar8 *dir_orig;
129 FcFontSet *set;
130 FcStrSet *subdirs;
131 FcStrList *sublist;
132 struct stat statb;
133
134 /*
135 * Now scan all of the directories into separate databases
136 * and write out the results
137 */
138 while ((dir_orig = FcStrListNext (list)))
139 {
140 dir = FcConfigNormalizeFontDir (config, dir_orig);
141
142 if (verbose)
143 {
144 printf ("%s: \"%s\": ", program, dir ? dir : dir_orig);
145 fflush (stdout);
146 }
147
148 if (!dir)
149 {
150 if (verbose)
151 printf ("skipping, no such directory\n");
152 continue;
153 }
154
155 if (FcStrSetMember (processed_dirs, dir))
156 {
157 if (verbose)
158 printf ("skipping, looped directory detected\n");
159 continue;
160 }
161
162 set = FcFontSetCreate ();
163 if (!set)
164 {
165 fprintf (stderr, "Can't create font set\n");
166 ret++;
167 continue;
168 }
169 subdirs = FcStrSetCreate ();
170 if (!subdirs)
171 {
172 fprintf (stderr, "Can't create directory set\n");
173 ret++;
174 FcFontSetDestroy (set);
175 continue;
176 }
177
178 if (access ((char *) dir, W_OK) < 0)
179 {
180 switch (errno) {
181 case ENOENT:
182 case ENOTDIR:
183 if (verbose)
184 printf ("skipping, no such directory\n");
185 FcFontSetDestroy (set);
186 FcStrSetDestroy (subdirs);
187 continue;
188 case EACCES:
189 case EROFS:
190 /* That's ok, caches go to /var anyway. */
191 /* Ideally we'd do an access on the hashed_name. */
192 /* But we hid that behind an abstraction barrier. */
193 break;
194 default:
195 fprintf (stderr, "\"%s\": ", dir);
196 perror ("");
197 ret++;
198
199 FcFontSetDestroy (set);
200 FcStrSetDestroy (subdirs);
201 continue;
202 }
203 }
204 if (stat ((char *) dir, &statb) == -1)
205 {
206 fprintf (stderr, "\"%s\": ", dir);
207 perror ("");
208 FcFontSetDestroy (set);
209 FcStrSetDestroy (subdirs);
210 ret++;
211 continue;
212 }
213 if (!S_ISDIR (statb.st_mode))
214 {
215 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
216 FcFontSetDestroy (set);
217 FcStrSetDestroy (subdirs);
218 continue;
219 }
220
221 if (really_force)
222 FcDirCacheUnlink (dir, config);
223
224 if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
225 {
226 fprintf (stderr, "\"%s\": error scanning\n", dir);
227 FcFontSetDestroy (set);
228 FcStrSetDestroy (subdirs);
229 ret++;
230 continue;
231 }
232 if (!force && FcDirCacheValid (dir) && FcDirCacheHasCurrentArch (dir))
233 {
234 if (verbose)
235 printf ("skipping, %d fonts, %d dirs\n",
236 set->nfont, nsubdirs(subdirs));
237 }
238 else
239 {
240 if (verbose)
241 printf ("caching, %d fonts, %d dirs\n",
242 set->nfont, nsubdirs (subdirs));
243
244 /* This is the only reason we can't combine
245 * Valid w/HasCurrentArch... */
246 if (!FcDirCacheValid (dir))
247 if (!FcDirCacheUnlink (dir, config))
248 ret++;
249
250 if (!FcDirSave (set, subdirs, dir))
251 {
252 if (!ret)
253 fprintf (stderr, "Caches are currently saved to \"%s\"\n", PKGCACHEDIR);
254 fprintf (stderr, "Can't save cache for \"%s\"\n", dir);
255 ret++;
256 }
257 }
258 FcFontSetDestroy (set);
259 sublist = FcStrListCreate (subdirs);
260 FcStrSetDestroy (subdirs);
261 if (!sublist)
262 {
263 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
264 ret++;
265 continue;
266 }
267 FcStrSetAdd (processed_dirs, dir);
268 ret += scanDirs (sublist, config, program, force, really_force, verbose);
269 }
270 FcStrListDone (list);
271 return ret;
272 }
273
274 int
275 main (int argc, char **argv)
276 {
277 FcStrSet *dirs;
278 FcStrList *list;
279 FcBool verbose = FcFalse;
280 FcBool force = FcFalse;
281 FcBool really_force = FcFalse;
282 FcBool systemOnly = FcFalse;
283 FcConfig *config;
284 int i;
285 int ret;
286 #if HAVE_GETOPT_LONG || HAVE_GETOPT
287 int c;
288
289 #if HAVE_GETOPT_LONG
290 while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
291 #else
292 while ((c = getopt (argc, argv, "frsVv?")) != -1)
293 #endif
294 {
295 switch (c) {
296 case 'r':
297 really_force = FcTrue;
298 /* fall through */
299 case 'f':
300 force = FcTrue;
301 break;
302 case 's':
303 systemOnly = FcTrue;
304 break;
305 case 'V':
306 fprintf (stderr, "fontconfig version %d.%d.%d\n",
307 FC_MAJOR, FC_MINOR, FC_REVISION);
308 exit (0);
309 case 'v':
310 verbose = FcTrue;
311 break;
312 default:
313 usage (argv[0]);
314 }
315 }
316 i = optind;
317 #else
318 i = 1;
319 #endif
320
321 if (systemOnly)
322 FcConfigEnableHome (FcFalse);
323 config = FcInitLoadConfig ();
324 if (!config)
325 {
326 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
327 return 1;
328 }
329 FcConfigSetCurrent (config);
330
331 if (argv[i])
332 {
333 dirs = FcStrSetCreate ();
334 if (!dirs)
335 {
336 fprintf (stderr, "%s: Can't create list of directories\n",
337 argv[0]);
338 return 1;
339 }
340 while (argv[i])
341 {
342 if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
343 {
344 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
345 return 1;
346 }
347 i++;
348 }
349 list = FcStrListCreate (dirs);
350 FcStrSetDestroy (dirs);
351 }
352 else
353 list = FcConfigGetConfigDirs (config);
354
355 if ((processed_dirs = FcStrSetCreate()) == NULL) {
356 fprintf(stderr, "Cannot malloc\n");
357 return 1;
358 }
359
360 ret = scanDirs (list, config, argv[0], force, really_force, verbose);
361
362 FcStrSetDestroy (processed_dirs);
363
364 /*
365 * Now we need to sleep a second (or two, to be extra sure), to make
366 * sure that timestamps for changes after this run of fc-cache are later
367 * then any timestamps we wrote. We don't use gettimeofday() because
368 * sleep(3) can't be interrupted by a signal here -- this isn't in the
369 * library, and there aren't any signals flying around here.
370 */
371 FcConfigDestroy (config);
372 sleep (2);
373 if (verbose)
374 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
375 return ret;
376 }