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