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