]> git.wh0rd.org Git - fontconfig.git/blob - fc-cache/fc-cache.c
Forcibly rescan a directory before writing a fresh local cache file for
[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 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
112 scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool verbose)
113 {
114     int         ret = 0;
115     FcChar8     *dir;
116     FcFontSet   *set;
117     FcStrSet    *subdirs;
118     FcStrList   *sublist;
119     struct stat statb;
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++;
144             FcFontSetDestroy (set);
145             continue;
146         }
147         
148         if (access ((char *) dir, W_OK) < 0)
149         {
150             switch (errno) {
151             case ENOENT:
152             case ENOTDIR:
153                 if (verbose)
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:
162                 fprintf (stderr, "\"%s\": ", dir);
163                 perror ("");
164                 ret++;
165             }
166             FcFontSetDestroy (set);
167             FcStrSetDestroy (subdirs);
168             continue;
169         }
170         if (stat ((char *) dir, &statb) == -1)
171         {
172             fprintf (stderr, "\"%s\": ", dir);
173             perror ("");
174             FcFontSetDestroy (set);
175             FcStrSetDestroy (subdirs);
176             ret++;
177             continue;
178         }
179         if (!S_ISDIR (statb.st_mode))
180         {
181             fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
182             FcFontSetDestroy (set);
183             FcStrSetDestroy (subdirs);
184             continue;
185         }
186         if (!FcDirScan (set, subdirs, 0, FcConfigGetBlanks (config), dir, force))
187         {
188             fprintf (stderr, "\"%s\": error scanning\n", dir);
189             FcFontSetDestroy (set);
190             FcStrSetDestroy (subdirs);
191             ret++;
192             continue;
193         }
194         if (!force && FcDirCacheValid (dir) && FcDirCacheHasCurrentArch (dir))
195         {
196             if (verbose)
197                 printf ("skipping, %d fonts, %d dirs\n",
198                         set->nfont, nsubdirs(subdirs));
199         }
200         else
201         {
202             if (verbose)
203                 printf ("caching, %d fonts, %d dirs\n", 
204                         set->nfont, nsubdirs (subdirs));
205
206             if (!FcDirCacheValid (dir))
207                 if (!FcDirCacheUnlink (dir))
208                     ret++;
209
210             if (!FcDirScan (set, subdirs, 0, FcConfigGetBlanks (config), dir, FcTrue))
211             {
212                 fprintf (stderr, "\"%s\": error scanning\n", dir);
213                 FcFontSetDestroy (set);
214                 FcStrSetDestroy (subdirs);
215                 ret++;
216                 continue;
217             }
218
219             if (!FcDirSave (set, subdirs, dir))
220             {
221                 fprintf (stderr, "Can't save cache in \"%s\"\n", dir);
222                 ret++;
223             }
224         }
225         FcFontSetDestroy (set);
226         sublist = FcStrListCreate (subdirs);
227         FcStrSetDestroy (subdirs);
228         if (!sublist)
229         {
230             fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
231             ret++;
232             continue;
233         }
234         ret += scanDirs (sublist, config, program, force, verbose);
235     }
236     FcStrListDone (list);
237     return ret;
238 }
239
240 int
241 main (int argc, char **argv)
242 {
243     FcStrSet    *dirs;
244     FcStrList   *list;
245     FcBool      verbose = FcFalse;
246     FcBool      force = FcFalse;
247     FcBool      systemOnly = FcFalse;
248     FcConfig    *config;
249     int         i;
250     int         ret;
251 #if HAVE_GETOPT_LONG || HAVE_GETOPT
252     int         c;
253
254 #if HAVE_GETOPT_LONG
255     while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
256 #else
257     while ((c = getopt (argc, argv, "fsVv?")) != -1)
258 #endif
259     {
260         switch (c) {
261         case 'f':
262             force = FcTrue;
263             break;
264         case 's':
265             systemOnly = FcTrue;
266             break;
267         case 'V':
268             fprintf (stderr, "fontconfig version %d.%d.%d\n", 
269                      FC_MAJOR, FC_MINOR, FC_REVISION);
270             exit (0);
271         case 'v':
272             verbose = FcTrue;
273             break;
274         default:
275             usage (argv[0]);
276         }
277     }
278     i = optind;
279 #else
280     i = 1;
281 #endif
282
283     if (systemOnly)
284         FcConfigEnableHome (FcFalse);
285     config = FcInitLoadConfig ();
286     if (!config)
287     {
288         fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
289         return 1;
290     }
291
292     if (argv[i])
293     {
294         dirs = FcStrSetCreate ();
295         if (!dirs)
296         {
297             fprintf (stderr, "%s: Can't create list of directories\n",
298                      argv[0]);
299             return 1;
300         }
301         while (argv[i])
302         {
303             if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
304             {
305                 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
306                 return 1;
307             }
308             i++;
309         }
310         list = FcStrListCreate (dirs);
311         FcStrSetDestroy (dirs);
312     }
313     else
314         list = FcConfigGetConfigDirs (config);
315     ret = scanDirs (list, config, argv[0], force, verbose);
316     /* 
317      * Now we need to sleep a second  (or two, to be extra sure), to make
318      * sure that timestamps for changes after this run of fc-cache are later
319      * then any timestamps we wrote.  We don't use gettimeofday() because
320      * sleep(3) can't be interrupted by a signal here -- this isn't in the
321      * library, and there aren't any signals flying around here.
322      */
323     FcConfigDestroy (config);
324     sleep (2);
325     if (verbose)
326         printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
327     return ret;
328 }