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