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