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 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.
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 "../src/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
[] = {
70 {"convert", 0, 0, 'c'},
72 {"really-force", 0, 0, 'r'},
74 {"system-only", 0, 0, 's'},
75 {"version", 0, 0, 'V'},
76 {"verbose", 0, 0, 'v'},
83 extern int optind
, opterr
, optopt
;
88 usage (char *program
, int error
)
90 FILE *file
= error
? stderr
: stdout
;
92 fprintf (file
, "usage: %s [-frRsvVh] [--force|--really-force] [--root <root>] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
95 fprintf (file
, "usage: %s [-frRsvVh] [dirs]\n",
98 fprintf (file
, "Build font information caches in [dirs]\n"
99 "(all directories in font configuration by default).\n");
100 fprintf (file
, "\n");
102 fprintf (file
, " -f, --force scan directories with apparently valid caches\n");
103 fprintf (file
, " -r, --really-force erase all existing caches, then rescan\n");
104 fprintf (file
, " -R, --root <root> change to <root> before loading files\n");
105 fprintf (file
, " -s, --system-only scan system-wide directories only\n");
106 fprintf (file
, " -v, --verbose display status information while busy\n");
107 fprintf (file
, " -V, --version display font config version and exit\n");
108 fprintf (file
, " -h, --help display this help and exit\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
, " -R <root> (root) change to <root> before loading files\n");
113 fprintf (file
, " -s (system) scan system-wide directories only\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");
121 static FcStrSet
*processed_dirs
;
124 scanDirs (FcStrList
*list
, FcConfig
*config
, FcBool force
, FcBool really_force
, FcBool verbose
, int *changed
)
136 * Now scan all of the directories into separate databases
137 * and write out the results
139 while ((dir
= FcStrListNext (list
)))
143 printf ("%s: ", dir
);
150 printf ("skipping, no such directory\n");
154 if (FcStrSetMember (processed_dirs
, dir
))
157 printf ("skipping, looped directory detected\n");
161 if (FcStat (config
, dir
, &statb
) == -1)
167 printf ("skipping, no such directory\n");
170 fprintf (stderr
, "\"%s\": ", dir
);
178 if (!S_ISDIR (statb
.st_mode
))
180 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
185 FcDirCacheUnlink (dir
, config
);
190 cache
= FcDirCacheLoad (dir
, config
, NULL
);
198 cache
= FcDirCacheRead (dir
, FcTrue
, config
);
201 fprintf (stderr
, "%s: error scanning\n", dir
);
210 printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
211 FcCacheNumFont (cache
), FcCacheNumSubdir (cache
));
216 printf ("caching, new cache contents: %d fonts, %d dirs\n",
217 FcCacheNumFont (cache
), FcCacheNumSubdir (cache
));
219 if (!FcDirCacheValid (dir
))
221 fprintf (stderr
, "%s: failed to write cache\n", dir
);
222 (void) FcDirCacheUnlink (dir
, config
);
227 subdirs
= FcStrSetCreate ();
230 fprintf (stderr
, "%s: Can't create subdir set\n", dir
);
232 FcDirCacheUnload (cache
);
235 for (i
= 0; i
< FcCacheNumSubdir (cache
); i
++)
236 FcStrSetAdd (subdirs
, FcCacheSubdir (cache
, i
));
238 FcDirCacheUnload (cache
);
240 sublist
= FcStrListCreate (subdirs
);
241 FcStrSetDestroy (subdirs
);
244 fprintf (stderr
, "%s: Can't create subdir list\n", dir
);
248 FcStrSetAdd (processed_dirs
, dir
);
249 ret
+= scanDirs (sublist
, config
, force
, really_force
, verbose
, changed
);
251 FcStrListDone (list
);
256 cleanCacheDirectory (FcConfig
*config
, FcChar8
*dir
, FcBool verbose
)
265 struct stat target_stat
;
267 fullDir
= FcConfigGetRootPlus (config
, dir
);
273 if (access ((char *) checkDir
, W_OK
) != 0)
276 printf ("%s: not cleaning %s cache directory\n", dir
,
277 access ((char *) dir
, F_OK
) == 0 ? "unwritable" : "non-existent");
281 printf ("%s: cleaning cache directory\n", dir
);
282 d
= opendir ((char *) checkDir
);
285 perror ((char *) dir
);
289 while ((ent
= readdir (d
)))
292 const FcChar8
*target_dir
;
294 if (ent
->d_name
[0] == '.')
296 /* skip cache files for different architectures and */
297 /* files which are not cache files at all */
298 if (strlen(ent
->d_name
) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX
) ||
299 strcmp(ent
->d_name
+ 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX
))
302 file_name
= FcStrPathPlus (dir
, (const FcChar8
*) ent
->d_name
, NULL
);
305 fprintf (stderr
, "%s: allocation failure\n", dir
);
310 cache
= FcDirCacheLoadFile2 (file_name
, config
, NULL
);
314 printf ("%s: invalid cache file: %s\n", dir
, ent
->d_name
);
319 target_dir
= FcCacheDir (cache
);
320 if (FcStat (config
, target_dir
, &target_stat
) < 0)
323 printf ("%s: %s: missing directory: %s \n",
324 dir
, ent
->d_name
, target_dir
);
330 FcChar8
*unlink_file
= FcConfigGetRootPlus (config
, file_name
);
332 unlink_file
= file_name
;
333 if (unlink ((char *) unlink_file
) < 0)
335 perror ((char *) unlink_file
);
338 if (unlink_file
!= file_name
)
339 FcStrFree (unlink_file
);
341 FcDirCacheUnload (cache
);
342 FcStrFree (file_name
);
353 cleanCacheDirectories (FcConfig
*config
, FcBool verbose
)
355 FcStrList
*cache_dirs
= FcConfigGetCacheDirs (config
);
361 while ((cache_dir
= FcStrListNext (cache_dirs
)))
363 if (!cleanCacheDirectory (config
, cache_dir
, verbose
))
369 FcStrListDone (cache_dirs
);
374 main (int argc
, char **argv
)
378 FcBool verbose
= FcFalse
;
379 FcBool force
= FcFalse
;
380 FcBool really_force
= FcFalse
;
381 FcBool systemOnly
= FcFalse
;
387 #if HAVE_GETOPT_LONG || HAVE_GETOPT
390 FcBool convert
= FcFalse
;
393 while ((c
= getopt_long (argc
, argv
, "cfrR:sVvh", longopts
, NULL
)) != -1)
395 while ((c
= getopt (argc
, argv
, "cfrR:sVvh")) != -1)
403 really_force
= FcTrue
;
415 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
416 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
434 const FcChar8
*src_cache
, *dst_cache
, *src_type
, *dst_type
;
436 src_cache
= (const FcChar8
*) argv
[i
++];
437 src_type
= (const FcChar8
*) argv
[i
++];
438 dst_cache
= (const FcChar8
*) argv
[i
++];
439 dst_type
= (const FcChar8
*) argv
[i
++];
441 FcDirCacheConvert (src_cache
, src_type
, dst_cache
, dst_type
);
447 FcConfigEnableHome (FcFalse
);
448 config
= FcInitLoadConfig ();
451 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
454 FcConfigSetCurrent (config
);
455 FcConfigSetRoot (config
, (const FcChar8
*) rootDir
);
459 dirs
= FcStrSetCreate ();
462 fprintf (stderr
, "%s: Can't create list of directories\n",
468 if (!FcStrSetAddFilename (dirs
, (FcChar8
*) argv
[i
]))
470 fprintf (stderr
, "%s: Can't add directory\n", argv
[0]);
475 list
= FcStrListCreate (dirs
);
476 FcStrSetDestroy (dirs
);
479 list
= FcConfigGetConfigDirs (config
);
481 if ((processed_dirs
= FcStrSetCreate()) == NULL
) {
482 fprintf(stderr
, "Cannot malloc\n");
487 ret
= scanDirs (list
, config
, force
, really_force
, verbose
, &changed
);
489 FcStrSetDestroy (processed_dirs
);
491 cleanCacheDirectories (config
, verbose
);
494 * Now we need to sleep a second (or two, to be extra sure), to make
495 * sure that timestamps for changes after this run of fc-cache are later
496 * then any timestamps we wrote. We don't use gettimeofday() because
497 * sleep(3) can't be interrupted by a signal here -- this isn't in the
498 * library, and there aren't any signals flying around here.
500 FcConfigDestroy (config
);
505 printf ("%s: %s\n", argv
[0], ret
? "failed" : "succeeded");