]> git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
Don't loop infinitely on recursive symlinks.
[fontconfig.git] / fc-cache / fc-cache.c
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 break;
176 case EACCES:
177 case EROFS:
178 if (verbose)
179 printf ("skipping, no write access\n");
180 break;
181 default:
182 fprintf (stderr, "\"%s\": ", dir);
183 perror ("");
184 ret++;
185 }
186 FcFontSetDestroy (set);
187 FcStrSetDestroy (subdirs);
188 continue;
189 }
190 if (stat ((char *) dir, &statb) == -1)
191 {
192 fprintf (stderr, "\"%s\": ", dir);
193 perror ("");
194 FcFontSetDestroy (set);
195 FcStrSetDestroy (subdirs);
196 ret++;
197 continue;
198 }
199 if (!S_ISDIR (statb.st_mode))
200 {
201 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
202 FcFontSetDestroy (set);
203 FcStrSetDestroy (subdirs);
204 continue;
205 }
206 if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
207 {
208 fprintf (stderr, "\"%s\": error scanning\n", dir);
209 FcFontSetDestroy (set);
210 FcStrSetDestroy (subdirs);
211 ret++;
212 continue;
213 }
214 if (!force && FcDirCacheValid (dir) && FcDirCacheHasCurrentArch (dir))
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));
225
226 if (!FcDirCacheValid (dir))
227 if (!FcDirCacheUnlink (dir, config))
228 ret++;
229
230 if (!FcDirSave (set, subdirs, dir))
231 {
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);
235 ret++;
236 }
237 }
238 FcFontSetDestroy (set);
239 sublist = FcStrListCreate (subdirs);
240 FcStrSetDestroy (subdirs);
241 if (!sublist)
242 {
243 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
244 ret++;
245 continue;
246 }
247 FcStrSetAdd (processed_dirs, dir);
248 ret += scanDirs (sublist, config, program, force, verbose);
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;
261 FcBool systemOnly = FcFalse;
262 FcConfig *config;
263 int i;
264 int ret;
265 #if HAVE_GETOPT_LONG || HAVE_GETOPT
266 int c;
267
268 #if HAVE_GETOPT_LONG
269 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
270 #else
271 while ((c = getopt (argc, argv, "fsVv?")) != -1)
272 #endif
273 {
274 switch (c) {
275 case 'f':
276 force = FcTrue;
277 break;
278 case 's':
279 systemOnly = FcTrue;
280 break;
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':
286 verbose = FcTrue;
287 break;
288 default:
289 usage (argv[0]);
290 }
291 }
292 i = optind;
293 #else
294 i = 1;
295 #endif
296
297 if (systemOnly)
298 FcConfigEnableHome (FcFalse);
299 config = FcInitLoadConfig ();
300 if (!config)
301 {
302 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
303 return 1;
304 }
305 FcConfigSetCurrent (config);
306
307 if (argv[i])
308 {
309 dirs = FcStrSetCreate ();
310 if (!dirs)
311 {
312 fprintf (stderr, "%s: Can't create list of directories\n",
313 argv[0]);
314 return 1;
315 }
316 while (argv[i])
317 {
318 if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
319 {
320 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
321 return 1;
322 }
323 i++;
324 }
325 list = FcStrListCreate (dirs);
326 FcStrSetDestroy (dirs);
327 }
328 else
329 list = FcConfigGetConfigDirs (config);
330
331 if ((processed_dirs = FcStrSetCreate()) == NULL) {
332 fprintf(stderr, "Cannot malloc\n");
333 return 1;
334 }
335
336 ret = scanDirs (list, config, argv[0], force, verbose);
337
338 FcStrSetDestroy (processed_dirs);
339
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 */
347 FcConfigDestroy (config);
348 sleep (2);
349 if (verbose)
350 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
351 return ret;
352 }