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