]> git.wh0rd.org Git - fontconfig.git/blob - fc-cache/fc-cache.c
8519388cf37576021876ff8c321cc69a826cc9cc
[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 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #else
28 #ifdef linux
29 #define HAVE_GETOPT_LONG 1
30 #endif
31 #define HAVE_GETOPT 1
32 #endif
33
34 #include <fontconfig/fontconfig.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <errno.h>
41
42 #if defined (_WIN32)
43 #define STRICT
44 #include <windows.h>
45 #define sleep(x) Sleep((x) * 1000)
46 #undef STRICT
47 #endif
48
49 #ifndef HAVE_GETOPT
50 #define HAVE_GETOPT 0
51 #endif
52 #ifndef HAVE_GETOPT_LONG
53 #define HAVE_GETOPT_LONG 0
54 #endif
55
56 #if HAVE_GETOPT_LONG
57 #undef  _GNU_SOURCE
58 #define _GNU_SOURCE
59 #include <getopt.h>
60 const struct option longopts[] = {
61     {"force", 0, 0, 'f'},
62     {"really-force", 0, 0, 'r'},
63     {"system-only", 0, 0, 's'},
64     {"version", 0, 0, 'V'},
65     {"verbose", 0, 0, 'v'},
66     {"help", 0, 0, '?'},
67     {NULL,0,0,0},
68 };
69 #else
70 #if HAVE_GETOPT
71 extern char *optarg;
72 extern int optind, opterr, optopt;
73 #endif
74 #endif
75
76 static void
77 usage (char *program)
78 {
79 #if HAVE_GETOPT_LONG
80     fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
81              program);
82 #else
83     fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
84              program);
85 #endif
86     fprintf (stderr, "Build font information caches in [dirs]\n"
87              "(all directories in font configuration by default).\n");
88     fprintf (stderr, "\n");
89 #if HAVE_GETOPT_LONG
90     fprintf (stderr, "  -f, --force          scan directories with apparently valid caches\n");
91     fprintf (stderr, "  -r, --really-force   erase all existing caches, then rescan\n");
92     fprintf (stderr, "  -s, --system-only    scan system-wide directories only\n");
93     fprintf (stderr, "  -v, --verbose        display status information while busy\n");
94     fprintf (stderr, "  -V, --version        display font config version and exit\n");
95     fprintf (stderr, "  -?, --help           display this help and exit\n");
96 #else
97     fprintf (stderr, "  -f         (force)   scan directories with apparently valid caches\n");
98     fprintf (stderr, "  -r,   (really force) erase all existing caches, then rescan\n");
99     fprintf (stderr, "  -s         (system)  scan system-wide directories only\n");
100     fprintf (stderr, "  -v         (verbose) display status information while busy\n");
101     fprintf (stderr, "  -V         (version) display font config version and exit\n");
102     fprintf (stderr, "  -?         (help)    display this help and exit\n");
103 #endif
104     exit (1);
105 }
106
107 static FcStrSet *processed_dirs;
108
109 static int
110 nsubdirs (FcStrSet *set)
111 {
112     FcStrList   *list;
113     int         n = 0;
114
115     list = FcStrListCreate (set);
116     if (!list)
117         return 0;
118     while (FcStrListNext (list))
119         n++;
120     FcStrListDone (list);
121     return n;
122 }
123
124 static int
125 scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose)
126 {
127     int         ret = 0;
128     const FcChar8 *dir;
129     const FcChar8 *dir_orig;
130     FcFontSet   *set;
131     FcStrSet    *subdirs;
132     FcStrList   *sublist;
133     struct stat statb;
134     
135     /*
136      * Now scan all of the directories into separate databases
137      * and write out the results
138      */
139     while ((dir_orig = FcStrListNext (list)))
140     {
141         dir = FcConfigNormalizeFontDir (config, dir_orig);
142         
143         if (verbose)
144         {
145             printf ("%s: \"%s\": ", program, dir ? dir : dir_orig);
146             fflush (stdout);
147         }
148         
149         if (!dir)
150         {
151             if (verbose)
152                 printf ("skipping, no such directory\n");
153             continue;
154         }
155         
156         if (FcStrSetMember (processed_dirs, dir))
157         {
158             if (verbose)
159                 printf ("skipping, looped directory detected\n");
160             continue;
161         }
162
163         set = FcFontSetCreate ();
164         if (!set)
165         {
166             fprintf (stderr, "Can't create font set\n");
167             ret++;
168             continue;
169         }
170         subdirs = FcStrSetCreate ();
171         if (!subdirs)
172         {
173             fprintf (stderr, "Can't create directory set\n");
174             ret++;
175             FcFontSetDestroy (set);
176             continue;
177         }
178         
179         if (access ((char *) dir, W_OK) < 0)
180         {
181             switch (errno) {
182             case ENOENT:
183             case ENOTDIR:
184                 if (verbose)
185                     printf ("skipping, no such directory\n");
186                 FcFontSetDestroy (set);
187                 FcStrSetDestroy (subdirs);
188                 continue;
189             case EACCES:
190             case EROFS:
191                 /* That's ok, caches go to /var anyway. */
192                 /* Ideally we'd do an access on the hashed_name. */
193                 /* But we hid that behind an abstraction barrier. */
194                 break;
195             default:
196                 fprintf (stderr, "\"%s\": ", dir);
197                 perror ("");
198                 ret++;
199
200                 FcFontSetDestroy (set);
201                 FcStrSetDestroy (subdirs);
202                 continue;
203             }
204         }
205         if (stat ((char *) dir, &statb) == -1)
206         {
207             fprintf (stderr, "\"%s\": ", dir);
208             perror ("");
209             FcFontSetDestroy (set);
210             FcStrSetDestroy (subdirs);
211             ret++;
212             continue;
213         }
214         if (!S_ISDIR (statb.st_mode))
215         {
216             fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
217             FcFontSetDestroy (set);
218             FcStrSetDestroy (subdirs);
219             continue;
220         }
221
222         if (really_force)
223             FcDirCacheUnlink (dir, config);
224
225         if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
226         {
227             fprintf (stderr, "\"%s\": error scanning\n", dir);
228             FcFontSetDestroy (set);
229             FcStrSetDestroy (subdirs);
230             ret++;
231             continue;
232         }
233         if (!force && FcDirCacheValid (dir, config) && 
234             FcDirCacheHasCurrentArch (dir, config))
235         {
236             if (verbose)
237                 printf ("skipping, %d fonts, %d dirs\n",
238                         set->nfont, nsubdirs(subdirs));
239         }
240         else
241         {
242             if (verbose)
243                 printf ("caching, %d fonts, %d dirs\n", 
244                         set->nfont, nsubdirs (subdirs));
245
246             /* This is the only reason we can't combine 
247              * Valid w/HasCurrentArch... */
248             if (!FcDirCacheValid (dir, config))
249                 if (!FcDirCacheUnlink (dir, config))
250                     ret++;
251
252             if (!FcDirSave (set, subdirs, dir))
253             {
254                 fprintf (stderr, "Can't save cache for \"%s\"\n", dir);
255                 ret++;
256             }
257         }
258         FcFontSetDestroy (set);
259         sublist = FcStrListCreate (subdirs);
260         FcStrSetDestroy (subdirs);
261         if (!sublist)
262         {
263             fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
264             ret++;
265             continue;
266         }
267         FcStrSetAdd (processed_dirs, dir);
268         ret += scanDirs (sublist, config, program, force, really_force, verbose);
269     }
270     FcStrListDone (list);
271     return ret;
272 }
273
274 int
275 main (int argc, char **argv)
276 {
277     FcStrSet    *dirs;
278     FcStrList   *list;
279     FcBool      verbose = FcFalse;
280     FcBool      force = FcFalse;
281     FcBool      really_force = FcFalse;
282     FcBool      systemOnly = FcFalse;
283     FcConfig    *config;
284     int         i;
285     int         ret;
286 #if HAVE_GETOPT_LONG || HAVE_GETOPT
287     int         c;
288
289 #if HAVE_GETOPT_LONG
290     while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
291 #else
292     while ((c = getopt (argc, argv, "frsVv?")) != -1)
293 #endif
294     {
295         switch (c) {
296         case 'r':
297             really_force = FcTrue;
298             /* fall through */
299         case 'f':
300             force = FcTrue;
301             break;
302         case 's':
303             systemOnly = FcTrue;
304             break;
305         case 'V':
306             fprintf (stderr, "fontconfig version %d.%d.%d\n", 
307                      FC_MAJOR, FC_MINOR, FC_REVISION);
308             exit (0);
309         case 'v':
310             verbose = FcTrue;
311             break;
312         default:
313             usage (argv[0]);
314         }
315     }
316     i = optind;
317 #else
318     i = 1;
319 #endif
320
321     if (systemOnly)
322         FcConfigEnableHome (FcFalse);
323     config = FcInitLoadConfig ();
324     if (!config)
325     {
326         fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
327         return 1;
328     }
329     FcConfigSetCurrent (config);
330
331     if (argv[i])
332     {
333         dirs = FcStrSetCreate ();
334         if (!dirs)
335         {
336             fprintf (stderr, "%s: Can't create list of directories\n",
337                      argv[0]);
338             return 1;
339         }
340         while (argv[i])
341         {
342             if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
343             {
344                 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
345                 return 1;
346             }
347             i++;
348         }
349         list = FcStrListCreate (dirs);
350         FcStrSetDestroy (dirs);
351     }
352     else
353         list = FcConfigGetConfigDirs (config);
354
355     if ((processed_dirs = FcStrSetCreate()) == NULL) {
356         fprintf(stderr, "Cannot malloc\n");
357         return 1;
358     }
359         
360     ret = scanDirs (list, config, argv[0], force, really_force, verbose);
361
362     FcStrSetDestroy (processed_dirs);
363
364     /* 
365      * Now we need to sleep a second  (or two, to be extra sure), to make
366      * sure that timestamps for changes after this run of fc-cache are later
367      * then any timestamps we wrote.  We don't use gettimeofday() because
368      * sleep(3) can't be interrupted by a signal here -- this isn't in the
369      * library, and there aren't any signals flying around here.
370      */
371     FcConfigDestroy (config);
372     sleep (2);
373     if (verbose)
374         printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
375     return ret;
376 }