2 * fontconfig/fc-cache/fc-cache.c
4 * Copyright © 2002 Keith Packard
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
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.
25 #include "../fc-arch/fcarch.h"
31 #define HAVE_GETOPT_LONG 1
36 #include <fontconfig/fontconfig.h>
40 #include <sys/types.h>
50 #define sleep(x) Sleep((x) * 1000)
61 #ifndef HAVE_GETOPT_LONG
62 #define HAVE_GETOPT_LONG 0
69 const struct option longopts
[] = {
71 {"really-force", 0, 0, 'r'},
72 {"system-only", 0, 0, 's'},
73 {"version", 0, 0, 'V'},
74 {"verbose", 0, 0, 'v'},
81 extern int optind
, opterr
, optopt
;
86 usage (char *program
, int error
)
88 FILE *file
= error
? stderr
: stdout
;
90 fprintf (file
, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
93 fprintf (file
, "usage: %s [-frsvVh] [dirs]\n",
96 fprintf (file
, "Build font information caches in [dirs]\n"
97 "(all directories in font configuration by default).\n");
100 fprintf (file
, " -f, --force scan directories with apparently valid caches\n");
101 fprintf (file
, " -r, --really-force erase all existing caches, then rescan\n");
102 fprintf (file
, " -s, --system-only scan system-wide directories only\n");
103 fprintf (file
, " -v, --verbose display status information while busy\n");
104 fprintf (file
, " -V, --version display font config version and exit\n");
105 fprintf (file
, " -h, --help display this help and exit\n");
107 fprintf (file
, " -f (force) scan directories with apparently valid caches\n");
108 fprintf (file
, " -r, (really force) erase all existing caches, then rescan\n");
109 fprintf (file
, " -s (system) scan system-wide directories only\n");
110 fprintf (file
, " -v (verbose) display status information while busy\n");
111 fprintf (file
, " -V (version) display font config version and exit\n");
112 fprintf (file
, " -h (help) display this help and exit\n");
117 static FcStrSet
*processed_dirs
;
120 scanDirs (FcStrList
*list
, FcConfig
*config
, FcBool force
, FcBool really_force
, FcBool verbose
, int *changed
)
132 * Now scan all of the directories into separate databases
133 * and write out the results
135 while ((dir
= FcStrListNext (list
)))
139 printf ("%s: ", dir
);
146 printf ("skipping, no such directory\n");
150 if (FcStrSetMember (processed_dirs
, dir
))
153 printf ("skipping, looped directory detected\n");
157 if (stat ((char *) dir
, &statb
) == -1)
163 printf ("skipping, no such directory\n");
166 fprintf (stderr
, "\"%s\": ", dir
);
174 if (!S_ISDIR (statb
.st_mode
))
176 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
181 FcDirCacheUnlink (dir
, config
);
186 cache
= FcDirCacheLoad (dir
, config
, NULL
);
194 cache
= FcDirCacheRead (dir
, FcTrue
, config
);
197 fprintf (stderr
, "%s: error scanning\n", dir
);
206 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
207 FcCacheNumFont (cache
), FcCacheNumSubdir (cache
));
212 printf ("caching, new cache contents: %d fonts, %d dirs\n",
213 FcCacheNumFont (cache
), FcCacheNumSubdir (cache
));
215 if (!FcDirCacheValid (dir
))
217 fprintf (stderr
, "%s: failed to write cache\n", dir
);
218 (void) FcDirCacheUnlink (dir
, config
);
223 subdirs
= FcStrSetCreate ();
226 fprintf (stderr
, "%s: Can't create subdir set\n", dir
);
228 FcDirCacheUnload (cache
);
231 for (i
= 0; i
< FcCacheNumSubdir (cache
); i
++)
232 FcStrSetAdd (subdirs
, FcCacheSubdir (cache
, i
));
234 FcDirCacheUnload (cache
);
236 sublist
= FcStrListCreate (subdirs
);
237 FcStrSetDestroy (subdirs
);
240 fprintf (stderr
, "%s: Can't create subdir list\n", dir
);
244 FcStrSetAdd (processed_dirs
, dir
);
245 ret
+= scanDirs (sublist
, config
, force
, really_force
, verbose
, changed
);
247 FcStrListDone (list
);
252 cleanCacheDirectory (FcConfig
*config
, FcChar8
*dir
, FcBool verbose
)
260 struct stat target_stat
;
262 dir_base
= FcStrPlus (dir
, (FcChar8
*) "/");
265 fprintf (stderr
, "%s: out of memory\n", dir
);
268 if (access ((char *) dir
, W_OK
) != 0)
271 printf ("%s: not cleaning %s cache directory\n", dir
,
272 access ((char *) dir
, F_OK
) == 0 ? "unwritable" : "non-existent");
273 FcStrFree (dir_base
);
277 printf ("%s: cleaning cache directory\n", dir
);
278 d
= opendir ((char *) dir
);
281 perror ((char *) dir
);
282 FcStrFree (dir_base
);
285 while ((ent
= readdir (d
)))
288 const FcChar8
*target_dir
;
290 if (ent
->d_name
[0] == '.')
292 /* skip cache files for different architectures and */
293 /* files which are not cache files at all */
294 if (strlen(ent
->d_name
) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX
) ||
295 strcmp(ent
->d_name
+ 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX
))
298 file_name
= FcStrPlus (dir_base
, (FcChar8
*) ent
->d_name
);
301 fprintf (stderr
, "%s: allocation failure\n", dir
);
306 cache
= FcDirCacheLoadFile (file_name
, NULL
);
310 printf ("%s: invalid cache file: %s\n", dir
, ent
->d_name
);
315 target_dir
= FcCacheDir (cache
);
316 if (stat ((char *) target_dir
, &target_stat
) < 0)
319 printf ("%s: %s: missing directory: %s \n",
320 dir
, ent
->d_name
, target_dir
);
326 if (unlink ((char *) file_name
) < 0)
328 perror ((char *) file_name
);
332 FcDirCacheUnload (cache
);
333 FcStrFree (file_name
);
337 FcStrFree (dir_base
);
342 cleanCacheDirectories (FcConfig
*config
, FcBool verbose
)
344 FcStrList
*cache_dirs
= FcConfigGetCacheDirs (config
);
350 while ((cache_dir
= FcStrListNext (cache_dirs
)))
352 if (!cleanCacheDirectory (config
, cache_dir
, verbose
))
358 FcStrListDone (cache_dirs
);
363 main (int argc
, char **argv
)
367 FcBool verbose
= FcFalse
;
368 FcBool force
= FcFalse
;
369 FcBool really_force
= FcFalse
;
370 FcBool systemOnly
= FcFalse
;
375 #if HAVE_GETOPT_LONG || HAVE_GETOPT
379 while ((c
= getopt_long (argc
, argv
, "frsVvh", longopts
, NULL
)) != -1)
381 while ((c
= getopt (argc
, argv
, "frsVvh")) != -1)
386 really_force
= FcTrue
;
395 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
396 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
413 FcConfigEnableHome (FcFalse
);
414 config
= FcInitLoadConfig ();
417 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
420 FcConfigSetCurrent (config
);
424 dirs
= FcStrSetCreate ();
427 fprintf (stderr
, "%s: Can't create list of directories\n",
433 if (!FcStrSetAddFilename (dirs
, (FcChar8
*) argv
[i
]))
435 fprintf (stderr
, "%s: Can't add directory\n", argv
[0]);
440 list
= FcStrListCreate (dirs
);
441 FcStrSetDestroy (dirs
);
444 list
= FcConfigGetConfigDirs (config
);
446 if ((processed_dirs
= FcStrSetCreate()) == NULL
) {
447 fprintf(stderr
, "Cannot malloc\n");
452 ret
= scanDirs (list
, config
, force
, really_force
, verbose
, &changed
);
454 FcStrSetDestroy (processed_dirs
);
456 cleanCacheDirectories (config
, verbose
);
459 * Now we need to sleep a second (or two, to be extra sure), to make
460 * sure that timestamps for changes after this run of fc-cache are later
461 * then any timestamps we wrote. We don't use gettimeofday() because
462 * sleep(3) can't be interrupted by a signal here -- this isn't in the
463 * library, and there aren't any signals flying around here.
465 FcConfigDestroy (config
);
470 printf ("%s: %s\n", argv
[0], ret
? "failed" : "succeeded");