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