Imported Upstream version 2.13.0
[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 #ifdef ENABLE_NLS
53 #include <libintl.h>
54 #define _(x)            (dgettext(GETTEXT_PACKAGE, x))
55 #else
56 #define dgettext(d, s)  (s)
57 #define _(x)            (x)
58 #endif
59
60 #ifndef O_BINARY
61 #define O_BINARY 0
62 #endif
63
64 #ifndef HAVE_GETOPT
65 #define HAVE_GETOPT 0
66 #endif
67 #ifndef HAVE_GETOPT_LONG
68 #define HAVE_GETOPT_LONG 0
69 #endif
70
71 #if HAVE_GETOPT_LONG
72 #undef  _GNU_SOURCE
73 #define _GNU_SOURCE
74 #include <getopt.h>
75 const struct option longopts[] = {
76     {"error-on-no-fonts", 0, 0, 'E'},
77     {"force", 0, 0, 'f'},
78     {"really-force", 0, 0, 'r'},
79     {"sysroot", required_argument, 0, 'y'},
80     {"system-only", 0, 0, 's'},
81     {"version", 0, 0, 'V'},
82     {"verbose", 0, 0, 'v'},
83     {"help", 0, 0, 'h'},
84     {NULL,0,0,0},
85 };
86 #else
87 #if HAVE_GETOPT
88 extern char *optarg;
89 extern int optind, opterr, optopt;
90 #endif
91 #endif
92
93 static void
94 usage (char *program, int error)
95 {
96     FILE *file = error ? stderr : stdout;
97 #if HAVE_GETOPT_LONG
98     fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [--error-on-no-fonts] [--force|--really-force] [--sysroot=SYSROOT] [--system-only] [--verbose] [--version] [--help] [dirs]\n"),
99              program);
100 #else
101     fprintf (file, _("usage: %s [-EfrsvVh] [-y SYSROOT] [dirs]\n"),
102              program);
103 #endif
104     fprintf (file, _("Build font information caches in [dirs]\n"
105                      "(all directories in font configuration by default).\n"));
106     fprintf (file, "\n");
107 #if HAVE_GETOPT_LONG
108     fprintf (file, _("  -E, --error-on-no-fonts  raise an error if no fonts in a directory\n"));
109     fprintf (file, _("  -f, --force              scan directories with apparently valid caches\n"));
110     fprintf (file, _("  -r, --really-force       erase all existing caches, then rescan\n"));
111     fprintf (file, _("  -s, --system-only        scan system-wide directories only\n"));
112     fprintf (file, _("  -y, --sysroot=SYSROOT    prepend SYSROOT to all paths for scanning\n"));
113     fprintf (file, _("  -v, --verbose            display status information while busy\n"));
114     fprintf (file, _("  -V, --version            display font config version and exit\n"));
115     fprintf (file, _("  -h, --help               display this help and exit\n"));
116 #else
117     fprintf (file, _("  -E         (error-on-no-fonts)\n"));
118     fprintf (file, _("                       raise an error if no fonts in a directory\n"));
119     fprintf (file, _("  -f         (force)   scan directories with apparently valid caches\n"));
120     fprintf (file, _("  -r,   (really force) erase all existing caches, then rescan\n"));
121     fprintf (file, _("  -s         (system)  scan system-wide directories only\n"));
122     fprintf (file, _("  -y SYSROOT (sysroot) prepend SYSROOT to all paths for scanning\n"));
123     fprintf (file, _("  -v         (verbose) display status information while busy\n"));
124     fprintf (file, _("  -V         (version) display font config version and exit\n"));
125     fprintf (file, _("  -h         (help)    display this help and exit\n"));
126 #endif
127     exit (error);
128 }
129
130 static FcStrSet *processed_dirs;
131
132 static int
133 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, FcBool error_on_no_fonts, int *changed)
134 {
135     int             ret = 0;
136     const FcChar8   *dir;
137     FcStrSet        *subdirs;
138     FcStrList       *sublist;
139     FcCache         *cache;
140     struct stat     statb;
141     FcBool          was_valid, was_processed = FcFalse;
142     int             i;
143     const FcChar8   *sysroot = FcConfigGetSysRoot (config);
144
145     /*
146      * Now scan all of the directories into separate databases
147      * and write out the results
148      */
149     while ((dir = FcStrListNext (list)))
150     {
151         if (verbose)
152         {
153             if (sysroot)
154                 printf ("[%s]", sysroot);
155             printf ("%s: ", dir);
156             fflush (stdout);
157         }
158         
159         if (FcStrSetMember (processed_dirs, dir))
160         {
161             if (verbose)
162                 printf (_("skipping, looped directory detected\n"));
163             continue;
164         }
165
166         if (stat ((char *) dir, &statb) == -1)
167         {
168             switch (errno) {
169             case ENOENT:
170             case ENOTDIR:
171                 if (verbose)
172                     printf (_("skipping, no such directory\n"));
173                 break;
174             default:
175                 fprintf (stderr, "\"%s\": ", dir);
176                 perror ("");
177                 ret++;
178                 break;
179             }
180             continue;
181         }
182
183         if (!S_ISDIR (statb.st_mode))
184         {
185             fprintf (stderr, _("\"%s\": not a directory, skipping\n"), dir);
186             continue;
187         }
188         was_processed = FcTrue;
189
190         if (really_force)
191         {
192             FcDirCacheUnlink (dir, config);
193             FcDirCacheCreateUUID ((FcChar8 *) dir, FcTrue, config);
194         }
195
196         cache = NULL;
197         was_valid = FcFalse;
198         if (!force) {
199             cache = FcDirCacheLoad (dir, config, NULL);
200             if (cache)
201                 was_valid = FcTrue;
202         }
203         
204         if (!cache)
205         {
206             (*changed)++;
207             cache = FcDirCacheRead (dir, FcTrue, config);
208             if (!cache)
209             {
210                 fprintf (stderr, _("\"%s\": scanning error\n"), dir);
211                 ret++;
212                 continue;
213             }
214         }
215
216         if (was_valid)
217         {
218             if (verbose)
219                 printf (_("skipping, existing cache is valid: %d fonts, %d dirs\n"),
220                         FcCacheNumFont (cache), FcCacheNumSubdir (cache));
221         }
222         else
223         {
224             if (verbose)
225                 printf (_("caching, new cache contents: %d fonts, %d dirs\n"),
226                         FcCacheNumFont (cache), FcCacheNumSubdir (cache));
227
228             if (!FcDirCacheValid (dir))
229             {
230                 fprintf (stderr, _("%s: failed to write cache\n"), dir);
231                 (void) FcDirCacheUnlink (dir, config);
232                 ret++;
233             }
234         }
235
236         subdirs = FcStrSetCreate ();
237         if (!subdirs)
238         {
239             fprintf (stderr, _("%s: Can't create subdir set\n"), dir);
240             ret++;
241             FcDirCacheUnload (cache);
242             continue;
243         }
244         for (i = 0; i < FcCacheNumSubdir (cache); i++)
245             FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
246         
247         FcDirCacheUnload (cache);
248
249         sublist = FcStrListCreate (subdirs);
250         FcStrSetDestroy (subdirs);
251         if (!sublist)
252         {
253             fprintf (stderr, _("%s: Can't create subdir list\n"), dir);
254             ret++;
255             continue;
256         }
257         FcStrSetAdd (processed_dirs, dir);
258         ret += scanDirs (sublist, config, force, really_force, verbose, error_on_no_fonts, changed);
259         FcStrListDone (sublist);
260     }
261     if (error_on_no_fonts && !was_processed)
262         ret++;
263     return ret;
264 }
265
266 static FcBool
267 cleanCacheDirectories (FcConfig *config, FcBool verbose)
268 {
269     FcStrList   *cache_dirs = FcConfigGetCacheDirs (config);
270     FcChar8     *cache_dir;
271     FcBool      ret = FcTrue;
272
273     if (!cache_dirs)
274         return FcFalse;
275     while ((cache_dir = FcStrListNext (cache_dirs)))
276     {
277         if (!FcDirCacheClean (cache_dir, verbose))
278         {
279             ret = FcFalse;
280             break;
281         }
282     }
283     FcStrListDone (cache_dirs);
284     return ret;
285 }
286
287 int
288 main (int argc, char **argv)
289 {
290     FcStrSet    *dirs;
291     FcStrList   *list;
292     FcBool      verbose = FcFalse;
293     FcBool      force = FcFalse;
294     FcBool      really_force = FcFalse;
295     FcBool      systemOnly = FcFalse;
296     FcBool      error_on_no_fonts = FcFalse;
297     FcConfig    *config;
298     FcChar8     *sysroot = NULL;
299     int         i;
300     int         changed;
301     int         ret;
302 #if HAVE_GETOPT_LONG || HAVE_GETOPT
303     int         c;
304
305 #if HAVE_GETOPT_LONG
306     while ((c = getopt_long (argc, argv, "Efrsy:Vvh", longopts, NULL)) != -1)
307 #else
308     while ((c = getopt (argc, argv, "Efrsy:Vvh")) != -1)
309 #endif
310     {
311         switch (c) {
312         case 'E':
313             error_on_no_fonts = FcTrue;
314             break;
315         case 'r':
316             really_force = FcTrue;
317             /* fall through */
318         case 'f':
319             force = FcTrue;
320             break;
321         case 's':
322             systemOnly = FcTrue;
323             break;
324         case 'y':
325             sysroot = FcStrCopy ((const FcChar8 *)optarg);
326             break;
327         case 'V':
328             fprintf (stderr, "fontconfig version %d.%d.%d\n",
329                      FC_MAJOR, FC_MINOR, FC_REVISION);
330             exit (0);
331         case 'v':
332             verbose = FcTrue;
333             break;
334         case 'h':
335             usage (argv[0], 0);
336         default:
337             usage (argv[0], 1);
338         }
339     }
340     i = optind;
341 #else
342     i = 1;
343 #endif
344
345     if (systemOnly)
346         FcConfigEnableHome (FcFalse);
347     if (sysroot)
348     {
349         FcConfigSetSysRoot (NULL, sysroot);
350         FcStrFree (sysroot);
351         config = FcConfigGetCurrent();
352     }
353     else
354     {
355         config = FcInitLoadConfig ();
356     }
357     if (!config)
358     {
359         fprintf (stderr, _("%s: Can't initialize font config library\n"), argv[0]);
360         return 1;
361     }
362     FcConfigSetCurrent (config);
363
364     if (argv[i])
365     {
366         dirs = FcStrSetCreate ();
367         if (!dirs)
368         {
369             fprintf (stderr, _("%s: Can't create list of directories\n"),
370                      argv[0]);
371             return 1;
372         }
373         while (argv[i])
374         {
375             if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
376             {
377                 fprintf (stderr, _("%s: Can't add directory\n"), argv[0]);
378                 return 1;
379             }
380             i++;
381         }
382         list = FcStrListCreate (dirs);
383         FcStrSetDestroy (dirs);
384     }
385     else
386         list = FcConfigGetFontDirs (config);
387
388     if ((processed_dirs = FcStrSetCreate()) == NULL) {
389         fprintf(stderr, _("Out of Memory\n"));
390         return 1;
391     }
392
393     changed = 0;
394     ret = scanDirs (list, config, force, really_force, verbose, error_on_no_fonts, &changed);
395     FcStrListDone (list);
396
397     /*
398      * Try to create CACHEDIR.TAG anyway.
399      * This expects the fontconfig cache directory already exists.
400      * If it doesn't, it won't be simply created.
401      */
402     FcCacheCreateTagFile (config);
403
404     FcStrSetDestroy (processed_dirs);
405
406     cleanCacheDirectories (config, verbose);
407
408     FcConfigDestroy (config);
409     FcFini ();
410     /* 
411      * Now we need to sleep a second  (or two, to be extra sure), to make
412      * sure that timestamps for changes after this run of fc-cache are later
413      * then any timestamps we wrote.  We don't use gettimeofday() because
414      * sleep(3) can't be interrupted by a signal here -- this isn't in the
415      * library, and there aren't any signals flying around here.
416      */
417     /* the resolution of mtime on FAT is 2 seconds */
418     if (changed)
419         sleep (2);
420     if (verbose)
421         printf ("%s: %s\n", argv[0], ret ? _("failed") : _("succeeded"));
422     return ret;
423 }