fcarch: Check for architecture signature at compile time rather than configure time
[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 "../src/fcarch.h"
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <dirent.h>
44 #include <string.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 #ifndef O_BINARY
54 #define O_BINARY 0
55 #endif
56
57 #ifndef HAVE_GETOPT
58 #define HAVE_GETOPT 0
59 #endif
60 #ifndef HAVE_GETOPT_LONG
61 #define HAVE_GETOPT_LONG 0
62 #endif
63
64 #if HAVE_GETOPT_LONG
65 #undef  _GNU_SOURCE
66 #define _GNU_SOURCE
67 #include <getopt.h>
68 const struct option longopts[] = {
69     {"force", 0, 0, 'f'},
70     {"really-force", 0, 0, 'r'},
71     {"system-only", 0, 0, 's'},
72     {"version", 0, 0, 'V'},
73     {"verbose", 0, 0, 'v'},
74     {"help", 0, 0, 'h'},
75     {NULL,0,0,0},
76 };
77 #else
78 #if HAVE_GETOPT
79 extern char *optarg;
80 extern int optind, opterr, optopt;
81 #endif
82 #endif
83
84 static void
85 usage (char *program, int error)
86 {
87     FILE *file = error ? stderr : stdout;
88 #if HAVE_GETOPT_LONG
89     fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
90              program);
91 #else
92     fprintf (file, "usage: %s [-frsvVh] [dirs]\n",
93              program);
94 #endif
95     fprintf (file, "Build font information caches in [dirs]\n"
96              "(all directories in font configuration by default).\n");
97     fprintf (file, "\n");
98 #if HAVE_GETOPT_LONG
99     fprintf (file, "  -f, --force          scan directories with apparently valid caches\n");
100     fprintf (file, "  -r, --really-force   erase all existing caches, then rescan\n");
101     fprintf (file, "  -s, --system-only    scan system-wide directories only\n");
102     fprintf (file, "  -v, --verbose        display status information while busy\n");
103     fprintf (file, "  -V, --version        display font config version and exit\n");
104     fprintf (file, "  -h, --help           display this help and exit\n");
105 #else
106     fprintf (file, "  -f         (force)   scan directories with apparently valid caches\n");
107     fprintf (file, "  -r,   (really force) erase all existing caches, then rescan\n");
108     fprintf (file, "  -s         (system)  scan system-wide directories only\n");
109     fprintf (file, "  -v         (verbose) display status information while busy\n");
110     fprintf (file, "  -V         (version) display font config version and exit\n");
111     fprintf (file, "  -h         (help)    display this help and exit\n");
112 #endif
113     exit (error);
114 }
115
116 static FcStrSet *processed_dirs;
117
118 static int
119 scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed)
120 {
121     int             ret = 0;
122     const FcChar8   *dir;
123     FcStrSet        *subdirs;
124     FcStrList       *sublist;
125     FcCache         *cache;
126     struct stat     statb;
127     FcBool          was_valid;
128     int             i;
129     
130     /*
131      * Now scan all of the directories into separate databases
132      * and write out the results
133      */
134     while ((dir = FcStrListNext (list)))
135     {
136         if (verbose)
137         {
138             printf ("%s: ", dir);
139             fflush (stdout);
140         }
141         
142         if (!dir)
143         {
144             if (verbose)
145                 printf ("skipping, no such directory\n");
146             continue;
147         }
148         
149         if (FcStrSetMember (processed_dirs, dir))
150         {
151             if (verbose)
152                 printf ("skipping, looped directory detected\n");
153             continue;
154         }
155
156         if (stat ((char *) dir, &statb) == -1)
157         {
158             switch (errno) {
159             case ENOENT:
160             case ENOTDIR:
161                 if (verbose)
162                     printf ("skipping, no such directory\n");
163                 break;
164             default:
165                 fprintf (stderr, "\"%s\": ", dir);
166                 perror ("");
167                 ret++;
168                 break;
169             }
170             continue;
171         }
172
173         if (!S_ISDIR (statb.st_mode))
174         {
175             fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
176             continue;
177         }
178
179         if (really_force)
180             FcDirCacheUnlink (dir, config);
181
182         cache = NULL;
183         was_valid = FcFalse;
184         if (!force) {
185             cache = FcDirCacheLoad (dir, config, NULL);
186             if (cache)
187                 was_valid = FcTrue;
188         }
189         
190         if (!cache)
191         {
192             (*changed)++;
193             cache = FcDirCacheRead (dir, FcTrue, config);
194             if (!cache)
195             {
196                 fprintf (stderr, "%s: error scanning\n", dir);
197                 ret++;
198                 continue;
199             }
200         }
201
202         if (was_valid)
203         {
204             if (verbose)
205                 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
206                         FcCacheNumFont (cache), FcCacheNumSubdir (cache));
207         }
208         else
209         {
210             if (verbose)
211                 printf ("caching, new cache contents: %d fonts, %d dirs\n", 
212                         FcCacheNumFont (cache), FcCacheNumSubdir (cache));
213
214             if (!FcDirCacheValid (dir))
215             {
216                 fprintf (stderr, "%s: failed to write cache\n", dir);
217                 (void) FcDirCacheUnlink (dir, config);
218                 ret++;
219             }
220         }
221         
222         subdirs = FcStrSetCreate ();
223         if (!subdirs)
224         {
225             fprintf (stderr, "%s: Can't create subdir set\n", dir);
226             ret++;
227             FcDirCacheUnload (cache);
228             continue;
229         }
230         for (i = 0; i < FcCacheNumSubdir (cache); i++)
231             FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
232         
233         FcDirCacheUnload (cache);
234         
235         sublist = FcStrListCreate (subdirs);
236         FcStrSetDestroy (subdirs);
237         if (!sublist)
238         {
239             fprintf (stderr, "%s: Can't create subdir list\n", dir);
240             ret++;
241             continue;
242         }
243         FcStrSetAdd (processed_dirs, dir);
244         ret += scanDirs (sublist, config, force, really_force, verbose, changed);
245     }
246     FcStrListDone (list);
247     return ret;
248 }
249
250 static FcBool
251 cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
252 {
253     DIR         *d;
254     struct dirent *ent;
255     FcChar8     *dir_base;
256     FcBool      ret = FcTrue;
257     FcBool      remove;
258     FcCache     *cache;
259     struct stat target_stat;
260
261     dir_base = FcStrPlus (dir, (FcChar8 *) "/");
262     if (!dir_base)
263     {
264         fprintf (stderr, "%s: out of memory\n", dir);
265         return FcFalse;
266     }
267     if (access ((char *) dir, W_OK) != 0)
268     {
269         if (verbose)
270             printf ("%s: not cleaning %s cache directory\n", dir,
271                     access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
272         FcStrFree (dir_base);
273         return FcTrue;
274     }
275     if (verbose)
276         printf ("%s: cleaning cache directory\n", dir);
277     d = opendir ((char *) dir);
278     if (!d)
279     {
280         perror ((char *) dir);
281         FcStrFree (dir_base);
282         return FcFalse;
283     }
284     while ((ent = readdir (d)))
285     {
286         FcChar8 *file_name;
287         const FcChar8   *target_dir;
288
289         if (ent->d_name[0] == '.')
290             continue;
291         /* skip cache files for different architectures and */
292         /* files which are not cache files at all */
293         if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
294             strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
295             continue;
296         
297         file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
298         if (!file_name)
299         {
300             fprintf (stderr, "%s: allocation failure\n", dir);
301             ret = FcFalse;
302             break;
303         }
304         remove = FcFalse;
305         cache = FcDirCacheLoadFile (file_name, NULL);
306         if (!cache)
307         {
308             if (verbose)
309                 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
310             remove = FcTrue;
311         }
312         else
313         {
314             target_dir = FcCacheDir (cache);
315             if (stat ((char *) target_dir, &target_stat) < 0)
316             {
317                 if (verbose)
318                     printf ("%s: %s: missing directory: %s \n",
319                             dir, ent->d_name, target_dir);
320                 remove = FcTrue;
321             }
322         }
323         if (remove)
324         {
325             if (unlink ((char *) file_name) < 0)
326             {
327                 perror ((char *) file_name);
328                 ret = FcFalse;
329             }
330         }
331         FcDirCacheUnload (cache);
332         FcStrFree (file_name);
333     }
334     
335     closedir (d);
336     FcStrFree (dir_base);
337     return ret;
338 }
339
340 static FcBool
341 cleanCacheDirectories (FcConfig *config, FcBool verbose)
342 {
343     FcStrList   *cache_dirs = FcConfigGetCacheDirs (config);
344     FcChar8     *cache_dir;
345     FcBool      ret = FcTrue;
346
347     if (!cache_dirs)
348         return FcFalse;
349     while ((cache_dir = FcStrListNext (cache_dirs)))
350     {
351         if (!cleanCacheDirectory (config, cache_dir, verbose))
352         {
353             ret = FcFalse;
354             break;
355         }
356     }
357     FcStrListDone (cache_dirs);
358     return ret;
359 }
360
361 int
362 main (int argc, char **argv)
363 {
364     FcStrSet    *dirs;
365     FcStrList   *list;
366     FcBool      verbose = FcFalse;
367     FcBool      force = FcFalse;
368     FcBool      really_force = FcFalse;
369     FcBool      systemOnly = FcFalse;
370     FcConfig    *config;
371     int         i;
372     int         changed;
373     int         ret;
374 #if HAVE_GETOPT_LONG || HAVE_GETOPT
375     int         c;
376
377 #if HAVE_GETOPT_LONG
378     while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
379 #else
380     while ((c = getopt (argc, argv, "frsVvh")) != -1)
381 #endif
382     {
383         switch (c) {
384         case 'r':
385             really_force = FcTrue;
386             /* fall through */
387         case 'f':
388             force = FcTrue;
389             break;
390         case 's':
391             systemOnly = FcTrue;
392             break;
393         case 'V':
394             fprintf (stderr, "fontconfig version %d.%d.%d\n", 
395                      FC_MAJOR, FC_MINOR, FC_REVISION);
396             exit (0);
397         case 'v':
398             verbose = FcTrue;
399             break;
400         case 'h':
401             usage (argv[0], 0);
402         default:
403             usage (argv[0], 1);
404         }
405     }
406     i = optind;
407 #else
408     i = 1;
409 #endif
410
411     if (systemOnly)
412         FcConfigEnableHome (FcFalse);
413     config = FcInitLoadConfig ();
414     if (!config)
415     {
416         fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
417         return 1;
418     }
419     FcConfigSetCurrent (config);
420
421     if (argv[i])
422     {
423         dirs = FcStrSetCreate ();
424         if (!dirs)
425         {
426             fprintf (stderr, "%s: Can't create list of directories\n",
427                      argv[0]);
428             return 1;
429         }
430         while (argv[i])
431         {
432             if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
433             {
434                 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
435                 return 1;
436             }
437             i++;
438         }
439         list = FcStrListCreate (dirs);
440         FcStrSetDestroy (dirs);
441     }
442     else
443         list = FcConfigGetConfigDirs (config);
444
445     if ((processed_dirs = FcStrSetCreate()) == NULL) {
446         fprintf(stderr, "Cannot malloc\n");
447         return 1;
448     }
449         
450     changed = 0;
451     ret = scanDirs (list, config, force, really_force, verbose, &changed);
452
453     FcStrSetDestroy (processed_dirs);
454
455     cleanCacheDirectories (config, verbose);
456
457     /* 
458      * Now we need to sleep a second  (or two, to be extra sure), to make
459      * sure that timestamps for changes after this run of fc-cache are later
460      * then any timestamps we wrote.  We don't use gettimeofday() because
461      * sleep(3) can't be interrupted by a signal here -- this isn't in the
462      * library, and there aren't any signals flying around here.
463      */
464     FcConfigDestroy (config);
465     FcFini ();
466     if (changed)
467         sleep (2);
468     if (verbose)
469         printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
470     return ret;
471 }