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