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