2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
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 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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
;
89 fprintf (stderr
, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
92 fprintf (stderr
, "usage: %s [-frsvV?] [dirs]\n",
95 fprintf (stderr
, "Build font information caches in [dirs]\n"
96 "(all directories in font configuration by default).\n");
97 fprintf (stderr
, "\n");
99 fprintf (stderr
, " -f, --force scan directories with apparently valid caches\n");
100 fprintf (stderr
, " -r, --really-force erase all existing caches, then rescan\n");
101 fprintf (stderr
, " -s, --system-only scan system-wide directories only\n");
102 fprintf (stderr
, " -v, --verbose display status information while busy\n");
103 fprintf (stderr
, " -V, --version display font config version and exit\n");
104 fprintf (stderr
, " -?, --help display this help and exit\n");
106 fprintf (stderr
, " -f (force) scan directories with apparently valid caches\n");
107 fprintf (stderr
, " -r, (really force) erase all existing caches, then rescan\n");
108 fprintf (stderr
, " -s (system) scan system-wide directories only\n");
109 fprintf (stderr
, " -v (verbose) display status information while busy\n");
110 fprintf (stderr
, " -V (version) display font config version and exit\n");
111 fprintf (stderr
, " -? (help) display this help and exit\n");
116 static FcStrSet
*processed_dirs
;
119 scanDirs (FcStrList
*list
, FcConfig
*config
, FcBool force
, FcBool really_force
, FcBool verbose
)
131 * Now scan all of the directories into separate databases
132 * and write out the results
134 while ((dir
= FcStrListNext (list
)))
138 printf ("%s: ", dir
);
145 printf ("skipping, no such directory\n");
149 if (FcStrSetMember (processed_dirs
, dir
))
152 printf ("skipping, looped directory detected\n");
156 if (access ((char *) dir
, W_OK
) < 0)
162 printf ("skipping, no such directory\n");
166 /* That's ok, caches go to /var anyway. */
167 /* Ideally we'd do an access on the hashed_name. */
168 /* But we hid that behind an abstraction barrier. */
171 fprintf (stderr
, "\"%s\": ", dir
);
178 if (stat ((char *) dir
, &statb
) == -1)
180 fprintf (stderr
, "\"%s\": ", dir
);
185 if (!S_ISDIR (statb
.st_mode
))
187 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
192 FcDirCacheUnlink (dir
, config
);
197 cache
= FcDirCacheLoad (dir
, config
, NULL
);
204 cache
= FcDirCacheRead (dir
, FcTrue
, config
);
207 fprintf (stderr
, "%s: error scanning\n", dir
);
216 printf ("skipping, %d fonts, %d dirs\n",
217 FcCacheNumFont (cache
), FcCacheNumSubdir (cache
));
222 printf ("caching, %d fonts, %d dirs\n",
223 FcCacheNumFont (cache
), FcCacheNumSubdir (cache
));
225 if (!FcDirCacheValid (dir
))
227 fprintf (stderr
, "%s: failed to write cache\n", dir
);
228 (void) FcDirCacheUnlink (dir
, config
);
233 subdirs
= FcStrSetCreate ();
236 fprintf (stderr
, "%s: Can't create subdir set\n", dir
);
238 FcDirCacheUnload (cache
);
241 for (i
= 0; i
< FcCacheNumSubdir (cache
); i
++)
242 FcStrSetAdd (subdirs
, FcCacheSubdir (cache
, i
));
244 FcDirCacheUnload (cache
);
246 sublist
= FcStrListCreate (subdirs
);
247 FcStrSetDestroy (subdirs
);
250 fprintf (stderr
, "%s: Can't create subdir list\n", dir
);
254 FcStrSetAdd (processed_dirs
, dir
);
255 ret
+= scanDirs (sublist
, config
, force
, really_force
, verbose
);
257 FcStrListDone (list
);
262 cleanCacheDirectory (FcConfig
*config
, FcChar8
*dir
, FcBool verbose
)
270 struct stat file_stat
;
271 struct stat target_stat
;
273 dir_base
= FcStrPlus (dir
, (FcChar8
*) "/");
276 fprintf (stderr
, "%s: out of memory\n", dir
);
279 if (access ((char *) dir
, W_OK
|X_OK
) != 0)
282 printf ("%s: not cleaning unwritable cache directory\n", dir
);
283 FcStrFree (dir_base
);
287 printf ("%s: cleaning cache directory\n", dir
);
288 d
= opendir ((char *) dir
);
291 perror ((char *) dir
);
292 FcStrFree (dir_base
);
295 while ((ent
= readdir (d
)))
298 const FcChar8
*target_dir
;
300 if (ent
->d_name
[0] == '.')
302 /* skip cache files for different architectures and */
303 /* files which are not cache files at all */
304 if (strlen(ent
->d_name
) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX
) ||
305 strcmp(ent
->d_name
+ 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX
))
308 file_name
= FcStrPlus (dir_base
, (FcChar8
*) ent
->d_name
);
311 fprintf (stderr
, "%s: allocation failure\n", dir
);
315 cache
= FcDirCacheLoadFile (file_name
, &file_stat
);
318 fprintf (stderr
, "%s: invalid cache file: %s\n", dir
, ent
->d_name
);
319 FcStrFree (file_name
);
323 target_dir
= FcCacheDir (cache
);
325 if (stat ((char *) target_dir
, &target_stat
) < 0)
328 printf ("%s: %s: missing directory: %s \n",
329 dir
, ent
->d_name
, target_dir
);
332 else if (target_stat
.st_mtime
> file_stat
.st_mtime
)
335 printf ("%s: %s: cache outdated: %s\n",
336 dir
, ent
->d_name
, target_dir
);
341 if (unlink ((char *) file_name
) < 0)
343 perror ((char *) file_name
);
347 FcDirCacheUnload (cache
);
348 FcStrFree (file_name
);
352 FcStrFree (dir_base
);
357 cleanCacheDirectories (FcConfig
*config
, FcBool verbose
)
359 FcStrList
*cache_dirs
= FcConfigGetCacheDirs (config
);
365 while ((cache_dir
= FcStrListNext (cache_dirs
)))
367 if (!cleanCacheDirectory (config
, cache_dir
, verbose
))
373 FcStrListDone (cache_dirs
);
378 main (int argc
, char **argv
)
382 FcBool verbose
= FcFalse
;
383 FcBool force
= FcFalse
;
384 FcBool really_force
= FcFalse
;
385 FcBool systemOnly
= FcFalse
;
389 #if HAVE_GETOPT_LONG || HAVE_GETOPT
393 while ((c
= getopt_long (argc
, argv
, "frsVv?", longopts
, NULL
)) != -1)
395 while ((c
= getopt (argc
, argv
, "frsVv?")) != -1)
400 really_force
= FcTrue
;
409 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
410 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
425 FcConfigEnableHome (FcFalse
);
426 config
= FcInitLoadConfig ();
429 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
432 FcConfigSetCurrent (config
);
436 dirs
= FcStrSetCreate ();
439 fprintf (stderr
, "%s: Can't create list of directories\n",
445 if (!FcStrSetAddFilename (dirs
, (FcChar8
*) argv
[i
]))
447 fprintf (stderr
, "%s: Can't add directory\n", argv
[0]);
452 list
= FcStrListCreate (dirs
);
453 FcStrSetDestroy (dirs
);
456 list
= FcConfigGetConfigDirs (config
);
458 if ((processed_dirs
= FcStrSetCreate()) == NULL
) {
459 fprintf(stderr
, "Cannot malloc\n");
463 ret
= scanDirs (list
, config
, force
, really_force
, verbose
);
465 FcStrSetDestroy (processed_dirs
);
467 cleanCacheDirectories (config
, verbose
);
470 * Now we need to sleep a second (or two, to be extra sure), to make
471 * sure that timestamps for changes after this run of fc-cache are later
472 * then any timestamps we wrote. We don't use gettimeofday() because
473 * sleep(3) can't be interrupted by a signal here -- this isn't in the
474 * library, and there aren't any signals flying around here.
476 FcConfigDestroy (config
);
479 printf ("%s: %s\n", argv
[0], ret
? "failed" : "succeeded");