Create CACHEDIR.TAG when fc-cache is run or only when the cache directory is created...
[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 (!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         if (stat ((char *) dir, &statb) == -1)
156         {
157             switch (errno) {
158             case ENOENT:
159             case ENOTDIR:
160                 if (verbose)
161                     printf ("skipping, no such directory\n");
162                 break;
163             default:
164                 fprintf (stderr, "\"%s\": ", dir);
165                 perror ("");
166                 ret++;
167                 break;
168             }
169             continue;
170         }
171
172         if (!S_ISDIR (statb.st_mode))
173         {
174             fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
175             continue;
176         }
177
178         if (really_force)
179             FcDirCacheUnlink (dir, config);
180
181         cache = NULL;
182         was_valid = FcFalse;
183         if (!force) {
184             cache = FcDirCacheLoad (dir, config, NULL);
185             if (cache)
186                 was_valid = FcTrue;
187         }
188         
189         if (!cache)
190         {
191             (*changed)++;
192             cache = FcDirCacheRead (dir, FcTrue, config);
193             if (!cache)
194             {
195                 fprintf (stderr, "%s: error scanning\n", dir);
196                 ret++;
197                 continue;
198             }
199         }
200
201         if (was_valid)
202         {
203             if (verbose)
204                 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
205                         FcCacheNumFont (cache), FcCacheNumSubdir (cache));
206         }
207         else
208         {
209             if (verbose)
210                 printf ("caching, new cache contents: %d fonts, %d dirs\n", 
211                         FcCacheNumFont (cache), FcCacheNumSubdir (cache));
212
213             if (!FcDirCacheValid (dir))
214             {
215                 fprintf (stderr, "%s: failed to write cache\n", dir);
216                 (void) FcDirCacheUnlink (dir, config);
217                 ret++;
218             }
219         }
220         
221         subdirs = FcStrSetCreate ();
222         if (!subdirs)
223         {
224             fprintf (stderr, "%s: Can't create subdir set\n", dir);
225             ret++;
226             FcDirCacheUnload (cache);
227             continue;
228         }
229         for (i = 0; i < FcCacheNumSubdir (cache); i++)
230             FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
231         
232         FcDirCacheUnload (cache);
233         
234         sublist = FcStrListCreate (subdirs);
235         FcStrSetDestroy (subdirs);
236         if (!sublist)
237         {
238             fprintf (stderr, "%s: Can't create subdir list\n", dir);
239             ret++;
240             continue;
241         }
242         FcStrSetAdd (processed_dirs, dir);
243         ret += scanDirs (sublist, config, force, really_force, verbose, changed);
244     }
245     FcStrListDone (list);
246     return ret;
247 }
248
249 static FcBool
250 cleanCacheDirectories (FcConfig *config, FcBool verbose)
251 {
252     FcStrList   *cache_dirs = FcConfigGetCacheDirs (config);
253     FcChar8     *cache_dir;
254     FcBool      ret = FcTrue;
255
256     if (!cache_dirs)
257         return FcFalse;
258     while ((cache_dir = FcStrListNext (cache_dirs)))
259     {
260         if (!FcDirCacheClean (cache_dir, verbose))
261         {
262             ret = FcFalse;
263             break;
264         }
265     }
266     FcStrListDone (cache_dirs);
267     return ret;
268 }
269
270 int
271 main (int argc, char **argv)
272 {
273     FcStrSet    *dirs;
274     FcStrList   *list;
275     FcBool      verbose = FcFalse;
276     FcBool      force = FcFalse;
277     FcBool      really_force = FcFalse;
278     FcBool      systemOnly = FcFalse;
279     FcConfig    *config;
280     int         i;
281     int         changed;
282     int         ret;
283 #if HAVE_GETOPT_LONG || HAVE_GETOPT
284     int         c;
285
286 #if HAVE_GETOPT_LONG
287     while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
288 #else
289     while ((c = getopt (argc, argv, "frsVvh")) != -1)
290 #endif
291     {
292         switch (c) {
293         case 'r':
294             really_force = FcTrue;
295             /* fall through */
296         case 'f':
297             force = FcTrue;
298             break;
299         case 's':
300             systemOnly = FcTrue;
301             break;
302         case 'V':
303             fprintf (stderr, "fontconfig version %d.%d.%d\n", 
304                      FC_MAJOR, FC_MINOR, FC_REVISION);
305             exit (0);
306         case 'v':
307             verbose = FcTrue;
308             break;
309         case 'h':
310             usage (argv[0], 0);
311         default:
312             usage (argv[0], 1);
313         }
314     }
315     i = optind;
316 #else
317     i = 1;
318 #endif
319
320     if (systemOnly)
321         FcConfigEnableHome (FcFalse);
322     config = FcInitLoadConfig ();
323     if (!config)
324     {
325         fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
326         return 1;
327     }
328     FcConfigSetCurrent (config);
329
330     if (argv[i])
331     {
332         dirs = FcStrSetCreate ();
333         if (!dirs)
334         {
335             fprintf (stderr, "%s: Can't create list of directories\n",
336                      argv[0]);
337             return 1;
338         }
339         while (argv[i])
340         {
341             if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
342             {
343                 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
344                 return 1;
345             }
346             i++;
347         }
348         list = FcStrListCreate (dirs);
349         FcStrSetDestroy (dirs);
350     }
351     else
352         list = FcConfigGetConfigDirs (config);
353
354     if ((processed_dirs = FcStrSetCreate()) == NULL) {
355         fprintf(stderr, "Cannot malloc\n");
356         return 1;
357     }
358         
359     changed = 0;
360     ret = scanDirs (list, config, force, really_force, verbose, &changed);
361
362     /*
363      * Try to create CACHEDIR.TAG anyway.
364      * This expects the fontconfig cache directory already exists.
365      * If it doesn't, it won't be simply created.
366      */
367     FcCacheCreateTagFile (config);
368
369     FcStrSetDestroy (processed_dirs);
370
371     cleanCacheDirectories (config, verbose);
372
373     /* 
374      * Now we need to sleep a second  (or two, to be extra sure), to make
375      * sure that timestamps for changes after this run of fc-cache are later
376      * then any timestamps we wrote.  We don't use gettimeofday() because
377      * sleep(3) can't be interrupted by a signal here -- this isn't in the
378      * library, and there aren't any signals flying around here.
379      */
380     FcConfigDestroy (config);
381     FcFini ();
382     if (changed)
383         sleep (2);
384     if (verbose)
385         printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
386     return ret;
387 }