]>
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'}, |
ff3f1f98 | 54 | {"system-only", 0, 0, 's'}, |
24330d27 KP |
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 | ||
65018b4a KP |
67 | static void |
68 | usage (char *program) | |
24330d27 | 69 | { |
86b12431 KP |
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", | |
24330d27 | 75 | program); |
86b12431 | 76 | #endif |
24330d27 KP |
77 | fprintf (stderr, "Build font information caches in [dirs]\n" |
78 | "(all directories in font configuration by default).\n"); | |
79 | fprintf (stderr, "\n"); | |
86b12431 | 80 | #if HAVE_GETOPT_LONG |
80a7d664 | 81 | fprintf (stderr, " -f, --force scan directories with apparently valid caches\n"); |
ff3f1f98 | 82 | fprintf (stderr, " -s, --system-only scan system-wide directories only\n"); |
24330d27 KP |
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"); | |
86b12431 KP |
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 | |
24330d27 KP |
93 | exit (1); |
94 | } | |
95 | ||
660acf8f PL |
96 | static FcStrSet *processed_dirs; |
97 | ||
179c3995 KP |
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 | |
ad07dcf4 | 114 | scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool verbose) |
24330d27 KP |
115 | { |
116 | int ret = 0; | |
97293e07 | 117 | const FcChar8 *dir; |
275cf6cd | 118 | const FcChar8 *dir_orig; |
24330d27 | 119 | FcFontSet *set; |
179c3995 KP |
120 | FcStrSet *subdirs; |
121 | FcStrList *sublist; | |
0c35c0fa | 122 | struct stat statb; |
179c3995 KP |
123 | |
124 | /* | |
125 | * Now scan all of the directories into separate databases | |
126 | * and write out the results | |
127 | */ | |
275cf6cd | 128 | while ((dir_orig = FcStrListNext (list))) |
179c3995 | 129 | { |
275cf6cd PL |
130 | dir = FcConfigNormalizeFontDir (config, dir_orig); |
131 | ||
179c3995 KP |
132 | if (verbose) |
133 | { | |
275cf6cd | 134 | printf ("%s: \"%s\": ", program, dir ? dir : dir_orig); |
179c3995 KP |
135 | fflush (stdout); |
136 | } | |
275cf6cd PL |
137 | |
138 | if (!dir) | |
139 | { | |
140 | if (verbose) | |
141 | printf ("skipping, no such directory\n"); | |
142 | continue; | |
143 | } | |
144 | ||
660acf8f PL |
145 | if (FcStrSetMember (processed_dirs, dir)) |
146 | { | |
147 | if (verbose) | |
148 | printf ("skipping, looped directory detected\n"); | |
149 | continue; | |
150 | } | |
275cf6cd | 151 | |
179c3995 KP |
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++; | |
5c1853cd | 164 | FcFontSetDestroy (set); |
179c3995 KP |
165 | continue; |
166 | } | |
0c35c0fa | 167 | |
565a919e | 168 | if (access ((char *) dir, W_OK) < 0) |
0c35c0fa | 169 | { |
565a919e KP |
170 | switch (errno) { |
171 | case ENOENT: | |
172 | case ENOTDIR: | |
5d43e799 | 173 | if (verbose) |
565a919e KP |
174 | printf ("skipping, no such directory\n"); |
175 | break; | |
176 | case EACCES: | |
177 | case EROFS: | |
178 | if (verbose) | |
179 | printf ("skipping, no write access\n"); | |
180 | break; | |
181 | default: | |
0c35c0fa KP |
182 | fprintf (stderr, "\"%s\": ", dir); |
183 | perror (""); | |
184 | ret++; | |
185 | } | |
5c1853cd KP |
186 | FcFontSetDestroy (set); |
187 | FcStrSetDestroy (subdirs); | |
0c35c0fa KP |
188 | continue; |
189 | } | |
565a919e KP |
190 | if (stat ((char *) dir, &statb) == -1) |
191 | { | |
192 | fprintf (stderr, "\"%s\": ", dir); | |
193 | perror (""); | |
5c1853cd KP |
194 | FcFontSetDestroy (set); |
195 | FcStrSetDestroy (subdirs); | |
565a919e KP |
196 | ret++; |
197 | continue; | |
198 | } | |
0c35c0fa KP |
199 | if (!S_ISDIR (statb.st_mode)) |
200 | { | |
201 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
5c1853cd KP |
202 | FcFontSetDestroy (set); |
203 | FcStrSetDestroy (subdirs); | |
0c35c0fa KP |
204 | continue; |
205 | } | |
cd9bca69 | 206 | if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config)) |
179c3995 | 207 | { |
0c35c0fa | 208 | fprintf (stderr, "\"%s\": error scanning\n", dir); |
5c1853cd KP |
209 | FcFontSetDestroy (set); |
210 | FcStrSetDestroy (subdirs); | |
179c3995 KP |
211 | ret++; |
212 | continue; | |
213 | } | |
55c8fa4f | 214 | if (!force && FcDirCacheValid (dir) && FcDirCacheHasCurrentArch (dir)) |
179c3995 KP |
215 | { |
216 | if (verbose) | |
217 | printf ("skipping, %d fonts, %d dirs\n", | |
218 | set->nfont, nsubdirs(subdirs)); | |
219 | } | |
220 | else | |
221 | { | |
222 | if (verbose) | |
223 | printf ("caching, %d fonts, %d dirs\n", | |
224 | set->nfont, nsubdirs (subdirs)); | |
4262e0b3 | 225 | |
55c8fa4f | 226 | if (!FcDirCacheValid (dir)) |
8a0b0ed6 | 227 | if (!FcDirCacheUnlink (dir, config)) |
55c8fa4f PL |
228 | ret++; |
229 | ||
2304e38f | 230 | if (!FcDirSave (set, subdirs, dir)) |
179c3995 | 231 | { |
83b67390 PL |
232 | if (!ret) |
233 | fprintf (stderr, "Caches are currently saved to \"%s\"\n", PKGCACHEDIR); | |
234 | fprintf (stderr, "Can't save cache for \"%s\"\n", dir); | |
179c3995 KP |
235 | ret++; |
236 | } | |
237 | } | |
238 | FcFontSetDestroy (set); | |
239 | sublist = FcStrListCreate (subdirs); | |
5c1853cd | 240 | FcStrSetDestroy (subdirs); |
179c3995 KP |
241 | if (!sublist) |
242 | { | |
243 | fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); | |
244 | ret++; | |
245 | continue; | |
246 | } | |
660acf8f | 247 | FcStrSetAdd (processed_dirs, dir); |
ad07dcf4 | 248 | ret += scanDirs (sublist, config, program, force, verbose); |
179c3995 KP |
249 | } |
250 | FcStrListDone (list); | |
251 | return ret; | |
252 | } | |
253 | ||
254 | int | |
255 | main (int argc, char **argv) | |
256 | { | |
257 | FcStrSet *dirs; | |
258 | FcStrList *list; | |
259 | FcBool verbose = FcFalse; | |
260 | FcBool force = FcFalse; | |
ff3f1f98 | 261 | FcBool systemOnly = FcFalse; |
179c3995 | 262 | FcConfig *config; |
24330d27 | 263 | int i; |
179c3995 | 264 | int ret; |
24330d27 KP |
265 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
266 | int c; | |
267 | ||
268 | #if HAVE_GETOPT_LONG | |
86b12431 | 269 | while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1) |
24330d27 | 270 | #else |
86b12431 | 271 | while ((c = getopt (argc, argv, "fsVv?")) != -1) |
24330d27 KP |
272 | #endif |
273 | { | |
274 | switch (c) { | |
179c3995 KP |
275 | case 'f': |
276 | force = FcTrue; | |
277 | break; | |
ff3f1f98 KP |
278 | case 's': |
279 | systemOnly = FcTrue; | |
280 | break; | |
24330d27 KP |
281 | case 'V': |
282 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
283 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
284 | exit (0); | |
285 | case 'v': | |
179c3995 | 286 | verbose = FcTrue; |
24330d27 KP |
287 | break; |
288 | default: | |
289 | usage (argv[0]); | |
290 | } | |
291 | } | |
292 | i = optind; | |
293 | #else | |
294 | i = 1; | |
295 | #endif | |
296 | ||
ff3f1f98 KP |
297 | if (systemOnly) |
298 | FcConfigEnableHome (FcFalse); | |
5e678e94 | 299 | config = FcInitLoadConfig (); |
179c3995 | 300 | if (!config) |
24330d27 | 301 | { |
179c3995 | 302 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); |
24330d27 KP |
303 | return 1; |
304 | } | |
df3efc11 | 305 | FcConfigSetCurrent (config); |
212c9f43 | 306 | |
24330d27 | 307 | if (argv[i]) |
24330d27 | 308 | { |
179c3995 KP |
309 | dirs = FcStrSetCreate (); |
310 | if (!dirs) | |
24330d27 | 311 | { |
179c3995 KP |
312 | fprintf (stderr, "%s: Can't create list of directories\n", |
313 | argv[0]); | |
314 | return 1; | |
24330d27 | 315 | } |
179c3995 | 316 | while (argv[i]) |
24330d27 | 317 | { |
179c3995 | 318 | if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i])) |
24330d27 | 319 | { |
179c3995 KP |
320 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); |
321 | return 1; | |
24330d27 | 322 | } |
179c3995 | 323 | i++; |
24330d27 | 324 | } |
179c3995 KP |
325 | list = FcStrListCreate (dirs); |
326 | FcStrSetDestroy (dirs); | |
24330d27 | 327 | } |
179c3995 KP |
328 | else |
329 | list = FcConfigGetConfigDirs (config); | |
660acf8f PL |
330 | |
331 | if ((processed_dirs = FcStrSetCreate()) == NULL) { | |
332 | fprintf(stderr, "Cannot malloc\n"); | |
333 | return 1; | |
334 | } | |
335 | ||
ad07dcf4 | 336 | ret = scanDirs (list, config, argv[0], force, verbose); |
660acf8f PL |
337 | |
338 | FcStrSetDestroy (processed_dirs); | |
339 | ||
54560b01 KP |
340 | /* |
341 | * Now we need to sleep a second (or two, to be extra sure), to make | |
342 | * sure that timestamps for changes after this run of fc-cache are later | |
343 | * then any timestamps we wrote. We don't use gettimeofday() because | |
344 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
345 | * library, and there aren't any signals flying around here. | |
346 | */ | |
716ac8b8 | 347 | FcConfigDestroy (config); |
54560b01 | 348 | sleep (2); |
24330d27 KP |
349 | if (verbose) |
350 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
351 | return ret; | |
352 | } |