2206096048193681af17207d18b0a93a7e1452ff
[platform/upstream/fontconfig.git] / fc-cache / fc-cache.c
1 /*
2  * fontconfig/fc-cache/fc-cache.c
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 the author(s) not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  The authors make 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  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE AUTHOR(S) 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 #include <fcntl.h>
42 #include <dirent.h>
43 #include <string.h>
44
45 #if defined (_WIN32)
46 #define STRICT
47 #include <windows.h>
48 #define sleep(x) Sleep((x) * 1000)
49 #undef STRICT
50 #endif
51
52 #ifndef O_BINARY
53 #define O_BINARY 0
54 #endif
55
56 #ifndef HAVE_GETOPT
57 #define HAVE_GETOPT 0
58 #endif
59 #ifndef HAVE_GETOPT_LONG
60 #define HAVE_GETOPT_LONG 0
61 #endif
62
63 #if HAVE_GETOPT_LONG
64 #undef  _GNU_SOURCE
65 #define _GNU_SOURCE
66 #include <getopt.h>
67 const struct option longopts[] = {
68     {"force", 0, 0, 'f'},
69     {"really-force", 0, 0, 'r'},
70     {"system-only", 0, 0, 's'},
71     {"version", 0, 0, 'V'},
72     {"verbose", 0, 0, 'v'},
73     {"help", 0, 0, 'h'},
74     {NULL,0,0,0},
75 };
76 #else
77 #if HAVE_GETOPT
78 extern char *optarg;
79 extern int optind, opterr, optopt;
80 #endif
81 #endif
82
83 static void
84 usage (char *program, int error)
85 {
86     FILE *file = error ? stderr : stdout;
87 #if HAVE_GETOPT_LONG
88     fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
89              program);
90 #else
91     fprintf (file, "usage: %s [-frsvVh] [dirs]\n",
92              program);
93 #endif
94     fprintf (file, "Build font information caches in [dirs]\n"
95              "(all directories in font configuration by default).\n");
96     fprintf (file, "\n");
97 #if HAVE_GETOPT_LONG
98     fprintf (file, "  -f, --force          scan directories with apparently valid caches\n");
99     fprintf (file, "  -r, --really-force   erase all existing caches, then rescan\n");
100     fprintf (file, "  -s, --system-only    scan system-wide directories only\n");
101     fprintf (file, "  -v, --verbose        display status information while busy\n");
102     fprintf (file, "  -V, --version        display font config version and exit\n");
103     fprintf (file, "  -h, --help           display this help and exit\n");
104 #else
105     fprintf (file, "  -f         (force)   scan directories with apparently valid caches\n");
106     fprintf (file, "  -r,   (really force) erase all existing caches, then rescan\n");
107     fprintf (file, "  -s         (system)  scan system-wide directories only\n");
108     fprintf (file, "  -v         (verbose) display status information while busy\n");
109     fprintf (file, "  -V         (version) display font config version and exit\n");
110     fprintf (file, "  -h         (help)    display this help and exit\n");
111 #endif
112     exit (error);
113 }
114
115 static FcStrSet *processed_dirs;
116
117 static int
118 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
119 {
120     int             ret = 0;
121     const FcChar8   *dir;
122     FcStrSet        *subdirs;
123     FcStrList       *sublist;
124     FcCache         *cache;
125     struct stat     statb;
126     FcBool          was_valid;
127     int             i;
128     
129     /*
130      * Now scan all of the directories into separate databases
131      * and write out the results
132      */
133     while ((dir = FcStrListNext (list)))
134     {
135         if (verbose)
136         {
137             printf ("%s: ", dir);
138             fflush (stdout);
139         }
140         
141         if (FcStrSetMember (processed_dirs, dir))
142         {
143             if (verbose)
144                 printf ("skipping, looped directory detected\n");
145             continue;
146         }
147
148         if (stat ((char *) dir, &statb) == -1)
149         {
150             switch (errno) {
151             case ENOENT:
152             case ENOTDIR:
153                 if (verbose)
154                     printf ("skipping, no such directory\n");
155                 break;
156             default:
157                 fprintf (stderr, "\"%s\": ", dir);
158                 perror ("");
159                 ret++;
160                 break;
161             }
162             continue;
163         }
164
165         if (!S_ISDIR (statb.st_mode))
166         {
167             fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
168             continue;
169         }
170
171         if (really_force)
172             FcDirCacheUnlink (dir, config);
173
174         cache = NULL;
175         was_valid = FcFalse;
176         if (!force) {
177             cache = FcDirCacheLoad (dir, config, NULL);
178             if (cache)
179                 was_valid = FcTrue;
180         }
181         
182         if (!cache)
183         {
184             (*changed)++;
185             cache = FcDirCacheRead (dir, FcTrue, config);
186             if (!cache)
187             {
188                 fprintf (stderr, "%s: error scanning\n", dir);
189                 ret++;
190                 continue;
191             }
192         }
193
194         if (was_valid)
195         {
196             if (verbose)
197                 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
198                         FcCacheNumFont (cache), FcCacheNumSubdir (cache));
199         }
200         else
201         {
202             if (verbose)
203                 printf ("caching, new cache contents: %d fonts, %d dirs\n", 
204                         FcCacheNumFont (cache), FcCacheNumSubdir (cache));
205
206             if (!FcDirCacheValid (dir))
207             {
208                 fprintf (stderr, "%s: failed to write cache\n", dir);
209                 (void) FcDirCacheUnlink (dir, config);
210                 ret++;
211             }
212         }
213         
214         subdirs = FcStrSetCreate ();
215         if (!subdirs)
216         {
217             fprintf (stderr, "%s: Can't create subdir set\n", dir);
218             ret++;
219             FcDirCacheUnload (cache);
220             continue;
221         }
222         for (i = 0; i < FcCacheNumSubdir (cache); i++)
223             FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
224         
225         FcDirCacheUnload (cache);
226         
227         sublist = FcStrListCreate (subdirs);
228         FcStrSetDestroy (subdirs);
229         if (!sublist)
230         {
231             fprintf (stderr, "%s: Can't create subdir list\n", dir);
232             ret++;
233             continue;
234         }
235         FcStrSetAdd (processed_dirs, dir);
236         ret += scanDirs (sublist, config, force, really_force, verbose, changed);
237     }
238     FcStrListDone (list);
239     return ret;
240 }
241
242 static FcBool
243 cleanCacheDirectories (FcConfig *config, FcBool verbose)
244 {
245     FcStrList   *cache_dirs = FcConfigGetCacheDirs (config);
246     FcChar8     *cache_dir;
247     FcBool      ret = FcTrue;
248
249     if (!cache_dirs)
250         return FcFalse;
251     while ((cache_dir = FcStrListNext (cache_dirs)))
252     {
253         if (!FcDirCacheClean (cache_dir, verbose))
254         {
255             ret = FcFalse;
256             break;
257         }
258     }
259     FcStrListDone (cache_dirs);
260     return ret;
261 }
262
263 int
264 main (int argc, char **argv)
265 {
266     FcStrSet    *dirs;
267     FcStrList   *list;
268     FcBool      verbose = FcFalse;
269     FcBool      force = FcFalse;
270     FcBool      really_force = FcFalse;
271     FcBool      systemOnly = FcFalse;
272     FcConfig    *config;
273     int         i;
274     int         changed;
275     int         ret;
276 #if HAVE_GETOPT_LONG || HAVE_GETOPT
277     int         c;
278
279 #if HAVE_GETOPT_LONG
280     while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
281 #else
282     while ((c = getopt (argc, argv, "frsVvh")) != -1)
283 #endif
284     {
285         switch (c) {
286         case 'r':
287             really_force = FcTrue;
288             /* fall through */
289         case 'f':
290             force = FcTrue;
291             break;
292         case 's':
293             systemOnly = FcTrue;
294             break;
295         case 'V':
296             fprintf (stderr, "fontconfig version %d.%d.%d\n", 
297                      FC_MAJOR, FC_MINOR, FC_REVISION);
298             exit (0);
299         case 'v':
300             verbose = FcTrue;
301             break;
302         case 'h':
303             usage (argv[0], 0);
304         default:
305             usage (argv[0], 1);
306         }
307     }
308     i = optind;
309 #else
310     i = 1;
311 #endif
312
313     if (systemOnly)
314         FcConfigEnableHome (FcFalse);
315     config = FcInitLoadConfig ();
316     if (!config)
317     {
318         fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
319         return 1;
320     }
321     FcConfigSetCurrent (config);
322
323     if (argv[i])
324     {
325         dirs = FcStrSetCreate ();
326         if (!dirs)
327         {
328             fprintf (stderr, "%s: Can't create list of directories\n",
329                      argv[0]);
330             return 1;
331         }
332         while (argv[i])
333         {
334             if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
335             {
336                 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
337                 return 1;
338             }
339             i++;
340         }
341         list = FcStrListCreate (dirs);
342         FcStrSetDestroy (dirs);
343     }
344     else
345         list = FcConfigGetConfigDirs (config);
346
347     if ((processed_dirs = FcStrSetCreate()) == NULL) {
348         fprintf(stderr, "Cannot malloc\n");
349         return 1;
350     }
351         
352     changed = 0;
353     ret = scanDirs (list, config, force, really_force, verbose, &changed);
354
355     /*
356      * Try to create CACHEDIR.TAG anyway.
357      * This expects the fontconfig cache directory already exists.
358      * If it doesn't, it won't be simply created.
359      */
360     FcCacheCreateTagFile (config);
361
362     FcStrSetDestroy (processed_dirs);
363
364     cleanCacheDirectories (config, verbose);
365
366     /* 
367      * Now we need to sleep a second  (or two, to be extra sure), to make
368      * sure that timestamps for changes after this run of fc-cache are later
369      * then any timestamps we wrote.  We don't use gettimeofday() because
370      * sleep(3) can't be interrupted by a signal here -- this isn't in the
371      * library, and there aren't any signals flying around here.
372      */
373     FcConfigDestroy (config);
374     FcFini ();
375     if (changed)
376         sleep (2);
377     if (verbose)
378         printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
379     return ret;
380 }