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