]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
4bd4418a | 2 | * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2002 Keith Packard |
24330d27 KP |
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> | |
d1bec8c6 | 27 | #include <stdlib.h> |
24330d27 | 28 | #include <unistd.h> |
0c35c0fa KP |
29 | #include <sys/types.h> |
30 | #include <sys/stat.h> | |
31 | #include <errno.h> | |
24330d27 KP |
32 | #ifdef HAVE_CONFIG_H |
33 | #include <config.h> | |
34 | #else | |
c4bd0638 MALF |
35 | #ifdef linux |
36 | #define HAVE_GETOPT_LONG 1 | |
37 | #endif | |
24330d27 KP |
38 | #define HAVE_GETOPT 1 |
39 | #endif | |
40 | ||
c4bd0638 MALF |
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 | ||
24330d27 | 48 | #if HAVE_GETOPT_LONG |
c4bd0638 | 49 | #undef _GNU_SOURCE |
24330d27 KP |
50 | #define _GNU_SOURCE |
51 | #include <getopt.h> | |
52 | const struct option longopts[] = { | |
179c3995 | 53 | {"force", 0, 0, 'f'}, |
d2c01029 | 54 | {"really-force", 0, 0, 'r'}, |
ff3f1f98 | 55 | {"system-only", 0, 0, 's'}, |
24330d27 KP |
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 | ||
65018b4a KP |
68 | static void |
69 | usage (char *program) | |
24330d27 | 70 | { |
86b12431 | 71 | #if HAVE_GETOPT_LONG |
d2c01029 | 72 | fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", |
86b12431 KP |
73 | program); |
74 | #else | |
d2c01029 | 75 | fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n", |
24330d27 | 76 | program); |
86b12431 | 77 | #endif |
24330d27 KP |
78 | fprintf (stderr, "Build font information caches in [dirs]\n" |
79 | "(all directories in font configuration by default).\n"); | |
80 | fprintf (stderr, "\n"); | |
86b12431 | 81 | #if HAVE_GETOPT_LONG |
80a7d664 | 82 | fprintf (stderr, " -f, --force scan directories with apparently valid caches\n"); |
d2c01029 | 83 | fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n"); |
ff3f1f98 | 84 | fprintf (stderr, " -s, --system-only scan system-wide directories only\n"); |
24330d27 KP |
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"); | |
86b12431 KP |
88 | #else |
89 | fprintf (stderr, " -f (force) scan directories with apparently valid caches\n"); | |
d2c01029 | 90 | fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n"); |
86b12431 KP |
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 | |
24330d27 KP |
96 | exit (1); |
97 | } | |
98 | ||
660acf8f PL |
99 | static FcStrSet *processed_dirs; |
100 | ||
179c3995 KP |
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 | |
d2c01029 | 117 | scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose) |
24330d27 KP |
118 | { |
119 | int ret = 0; | |
97293e07 | 120 | const FcChar8 *dir; |
275cf6cd | 121 | const FcChar8 *dir_orig; |
24330d27 | 122 | FcFontSet *set; |
179c3995 KP |
123 | FcStrSet *subdirs; |
124 | FcStrList *sublist; | |
0c35c0fa | 125 | struct stat statb; |
179c3995 KP |
126 | |
127 | /* | |
128 | * Now scan all of the directories into separate databases | |
129 | * and write out the results | |
130 | */ | |
275cf6cd | 131 | while ((dir_orig = FcStrListNext (list))) |
179c3995 | 132 | { |
275cf6cd PL |
133 | dir = FcConfigNormalizeFontDir (config, dir_orig); |
134 | ||
179c3995 KP |
135 | if (verbose) |
136 | { | |
275cf6cd | 137 | printf ("%s: \"%s\": ", program, dir ? dir : dir_orig); |
179c3995 KP |
138 | fflush (stdout); |
139 | } | |
275cf6cd PL |
140 | |
141 | if (!dir) | |
142 | { | |
143 | if (verbose) | |
144 | printf ("skipping, no such directory\n"); | |
145 | continue; | |
146 | } | |
147 | ||
660acf8f PL |
148 | if (FcStrSetMember (processed_dirs, dir)) |
149 | { | |
150 | if (verbose) | |
151 | printf ("skipping, looped directory detected\n"); | |
152 | continue; | |
153 | } | |
275cf6cd | 154 | |
179c3995 KP |
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++; | |
5c1853cd | 167 | FcFontSetDestroy (set); |
179c3995 KP |
168 | continue; |
169 | } | |
0c35c0fa | 170 | |
565a919e | 171 | if (access ((char *) dir, W_OK) < 0) |
0c35c0fa | 172 | { |
565a919e KP |
173 | switch (errno) { |
174 | case ENOENT: | |
175 | case ENOTDIR: | |
5d43e799 | 176 | if (verbose) |
565a919e | 177 | printf ("skipping, no such directory\n"); |
719f4b84 PL |
178 | FcFontSetDestroy (set); |
179 | FcStrSetDestroy (subdirs); | |
180 | continue; | |
565a919e KP |
181 | case EACCES: |
182 | case EROFS: | |
719f4b84 PL |
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. */ | |
565a919e KP |
186 | break; |
187 | default: | |
0c35c0fa KP |
188 | fprintf (stderr, "\"%s\": ", dir); |
189 | perror (""); | |
190 | ret++; | |
719f4b84 PL |
191 | |
192 | FcFontSetDestroy (set); | |
193 | FcStrSetDestroy (subdirs); | |
194 | continue; | |
0c35c0fa | 195 | } |
0c35c0fa | 196 | } |
565a919e KP |
197 | if (stat ((char *) dir, &statb) == -1) |
198 | { | |
199 | fprintf (stderr, "\"%s\": ", dir); | |
200 | perror (""); | |
5c1853cd KP |
201 | FcFontSetDestroy (set); |
202 | FcStrSetDestroy (subdirs); | |
565a919e KP |
203 | ret++; |
204 | continue; | |
205 | } | |
0c35c0fa KP |
206 | if (!S_ISDIR (statb.st_mode)) |
207 | { | |
208 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
5c1853cd KP |
209 | FcFontSetDestroy (set); |
210 | FcStrSetDestroy (subdirs); | |
0c35c0fa KP |
211 | continue; |
212 | } | |
d2c01029 PL |
213 | |
214 | if (really_force) | |
215 | FcDirCacheUnlink (dir, config); | |
216 | ||
cd9bca69 | 217 | if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config)) |
179c3995 | 218 | { |
0c35c0fa | 219 | fprintf (stderr, "\"%s\": error scanning\n", dir); |
5c1853cd KP |
220 | FcFontSetDestroy (set); |
221 | FcStrSetDestroy (subdirs); | |
179c3995 KP |
222 | ret++; |
223 | continue; | |
224 | } | |
55c8fa4f | 225 | if (!force && FcDirCacheValid (dir) && FcDirCacheHasCurrentArch (dir)) |
179c3995 KP |
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)); | |
4262e0b3 | 236 | |
530e66b0 PL |
237 | /* This is the only reason we can't combine |
238 | * Valid w/HasCurrentArch... */ | |
55c8fa4f | 239 | if (!FcDirCacheValid (dir)) |
8a0b0ed6 | 240 | if (!FcDirCacheUnlink (dir, config)) |
55c8fa4f PL |
241 | ret++; |
242 | ||
2304e38f | 243 | if (!FcDirSave (set, subdirs, dir)) |
179c3995 | 244 | { |
83b67390 PL |
245 | if (!ret) |
246 | fprintf (stderr, "Caches are currently saved to \"%s\"\n", PKGCACHEDIR); | |
247 | fprintf (stderr, "Can't save cache for \"%s\"\n", dir); | |
179c3995 KP |
248 | ret++; |
249 | } | |
250 | } | |
251 | FcFontSetDestroy (set); | |
252 | sublist = FcStrListCreate (subdirs); | |
5c1853cd | 253 | FcStrSetDestroy (subdirs); |
179c3995 KP |
254 | if (!sublist) |
255 | { | |
256 | fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); | |
257 | ret++; | |
258 | continue; | |
259 | } | |
660acf8f | 260 | FcStrSetAdd (processed_dirs, dir); |
d2c01029 | 261 | ret += scanDirs (sublist, config, program, force, really_force, verbose); |
179c3995 KP |
262 | } |
263 | FcStrListDone (list); | |
264 | return ret; | |
265 | } | |
266 | ||
267 | int | |
268 | main (int argc, char **argv) | |
269 | { | |
270 | FcStrSet *dirs; | |
271 | FcStrList *list; | |
272 | FcBool verbose = FcFalse; | |
273 | FcBool force = FcFalse; | |
d2c01029 | 274 | FcBool really_force = FcFalse; |
ff3f1f98 | 275 | FcBool systemOnly = FcFalse; |
179c3995 | 276 | FcConfig *config; |
24330d27 | 277 | int i; |
179c3995 | 278 | int ret; |
24330d27 KP |
279 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
280 | int c; | |
281 | ||
282 | #if HAVE_GETOPT_LONG | |
d2c01029 | 283 | while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) |
24330d27 | 284 | #else |
d2c01029 | 285 | while ((c = getopt (argc, argv, "frsVv?")) != -1) |
24330d27 KP |
286 | #endif |
287 | { | |
288 | switch (c) { | |
d2c01029 PL |
289 | case 'r': |
290 | really_force = FcTrue; | |
291 | /* fall through */ | |
179c3995 KP |
292 | case 'f': |
293 | force = FcTrue; | |
294 | break; | |
ff3f1f98 KP |
295 | case 's': |
296 | systemOnly = FcTrue; | |
297 | break; | |
24330d27 KP |
298 | case 'V': |
299 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
300 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
301 | exit (0); | |
302 | case 'v': | |
179c3995 | 303 | verbose = FcTrue; |
24330d27 KP |
304 | break; |
305 | default: | |
306 | usage (argv[0]); | |
307 | } | |
308 | } | |
309 | i = optind; | |
310 | #else | |
311 | i = 1; | |
312 | #endif | |
313 | ||
ff3f1f98 KP |
314 | if (systemOnly) |
315 | FcConfigEnableHome (FcFalse); | |
5e678e94 | 316 | config = FcInitLoadConfig (); |
179c3995 | 317 | if (!config) |
24330d27 | 318 | { |
179c3995 | 319 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); |
24330d27 KP |
320 | return 1; |
321 | } | |
df3efc11 | 322 | FcConfigSetCurrent (config); |
212c9f43 | 323 | |
24330d27 | 324 | if (argv[i]) |
24330d27 | 325 | { |
179c3995 KP |
326 | dirs = FcStrSetCreate (); |
327 | if (!dirs) | |
24330d27 | 328 | { |
179c3995 KP |
329 | fprintf (stderr, "%s: Can't create list of directories\n", |
330 | argv[0]); | |
331 | return 1; | |
24330d27 | 332 | } |
179c3995 | 333 | while (argv[i]) |
24330d27 | 334 | { |
179c3995 | 335 | if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i])) |
24330d27 | 336 | { |
179c3995 KP |
337 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); |
338 | return 1; | |
24330d27 | 339 | } |
179c3995 | 340 | i++; |
24330d27 | 341 | } |
179c3995 KP |
342 | list = FcStrListCreate (dirs); |
343 | FcStrSetDestroy (dirs); | |
24330d27 | 344 | } |
179c3995 KP |
345 | else |
346 | list = FcConfigGetConfigDirs (config); | |
660acf8f PL |
347 | |
348 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
349 | fprintf(stderr, "Cannot malloc\n"); | |
350 | return 1; | |
351 | } | |
352 | ||
d2c01029 | 353 | ret = scanDirs (list, config, argv[0], force, really_force, verbose); |
660acf8f PL |
354 | |
355 | FcStrSetDestroy (processed_dirs); | |
356 | ||
54560b01 KP |
357 | /* |
358 | * Now we need to sleep a second (or two, to be extra sure), to make | |
359 | * sure that timestamps for changes after this run of fc-cache are later | |
360 | * then any timestamps we wrote. We don't use gettimeofday() because | |
361 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
362 | * library, and there aren't any signals flying around here. | |
363 | */ | |
716ac8b8 | 364 | FcConfigDestroy (config); |
54560b01 | 365 | sleep (2); |
24330d27 KP |
366 | if (verbose) |
367 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
368 | return ret; | |
369 | } |