+ fprintf (file, "\n");
+#if HAVE_GETOPT_LONG
+ fprintf (file, " -f, --force scan directories with apparently valid caches\n");
+ fprintf (file, " -r, --really-force erase all existing caches, then rescan\n");
+ fprintf (file, " -s, --system-only scan system-wide directories only\n");
+ fprintf (file, " -v, --verbose display status information while busy\n");
+ fprintf (file, " -V, --version display font config version and exit\n");
+ fprintf (file, " -h, --help display this help and exit\n");
+#else
+ fprintf (file, " -f (force) scan directories with apparently valid caches\n");
+ fprintf (file, " -r, (really force) erase all existing caches, then rescan\n");
+ fprintf (file, " -s (system) scan system-wide directories only\n");
+ fprintf (file, " -v (verbose) display status information while busy\n");
+ fprintf (file, " -V (version) display font config version and exit\n");
+ fprintf (file, " -h (help) display this help and exit\n");
+#endif
+ exit (error);
+}
+
+static FcStrSet *processed_dirs;
+
+static int
+scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose)
+{
+ int ret = 0;
+ const FcChar8 *dir;
+ FcStrSet *subdirs;
+ FcStrList *sublist;
+ FcCache *cache;
+ struct stat statb;
+ FcBool was_valid;
+ int i;
+
+ /*
+ * Now scan all of the directories into separate databases
+ * and write out the results
+ */
+ while ((dir = FcStrListNext (list)))
+ {
+ if (verbose)
+ {
+ printf ("%s: ", dir);
+ fflush (stdout);
+ }
+
+ if (!dir)
+ {
+ if (verbose)
+ printf ("skipping, no such directory\n");
+ continue;
+ }
+
+ if (FcStrSetMember (processed_dirs, dir))
+ {
+ if (verbose)
+ printf ("skipping, looped directory detected\n");
+ continue;
+ }
+
+ if (stat ((char *) dir, &statb) == -1)
+ {
+ switch (errno) {
+ case ENOENT:
+ case ENOTDIR:
+ if (verbose)
+ printf ("skipping, no such directory\n");
+ break;
+ default:
+ fprintf (stderr, "\"%s\": ", dir);
+ perror ("");
+ ret++;
+ break;
+ }
+ continue;
+ }
+
+ if (!S_ISDIR (statb.st_mode))
+ {
+ fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
+ continue;
+ }
+
+ if (really_force)
+ FcDirCacheUnlink (dir, config);
+
+ cache = NULL;
+ was_valid = FcFalse;
+ if (!force) {
+ cache = FcDirCacheLoad (dir, config, NULL);
+ if (cache)
+ was_valid = FcTrue;
+ }
+
+ if (!cache)
+ {
+ cache = FcDirCacheRead (dir, FcTrue, config);
+ if (!cache)
+ {
+ fprintf (stderr, "%s: error scanning\n", dir);
+ ret++;
+ continue;
+ }
+ }
+
+ if (was_valid)
+ {
+ if (verbose)
+ printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
+ FcCacheNumFont (cache), FcCacheNumSubdir (cache));
+ }
+ else
+ {
+ if (verbose)
+ printf ("caching, new cache contents: %d fonts, %d dirs\n",
+ FcCacheNumFont (cache), FcCacheNumSubdir (cache));
+
+ if (!FcDirCacheValid (dir))
+ {
+ fprintf (stderr, "%s: failed to write cache\n", dir);
+ (void) FcDirCacheUnlink (dir, config);
+ ret++;
+ }
+ }
+
+ subdirs = FcStrSetCreate ();
+ if (!subdirs)
+ {
+ fprintf (stderr, "%s: Can't create subdir set\n", dir);
+ ret++;
+ FcDirCacheUnload (cache);
+ continue;
+ }
+ for (i = 0; i < FcCacheNumSubdir (cache); i++)
+ FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
+
+ FcDirCacheUnload (cache);
+
+ sublist = FcStrListCreate (subdirs);
+ FcStrSetDestroy (subdirs);
+ if (!sublist)
+ {
+ fprintf (stderr, "%s: Can't create subdir list\n", dir);
+ ret++;
+ continue;
+ }
+ FcStrSetAdd (processed_dirs, dir);
+ ret += scanDirs (sublist, config, force, really_force, verbose);
+ }
+ FcStrListDone (list);
+ return ret;
+}
+
+static FcBool
+cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
+{
+ DIR *d;
+ struct dirent *ent;
+ FcChar8 *dir_base;
+ FcBool ret = FcTrue;
+ FcBool remove;
+ FcCache *cache;
+ struct stat target_stat;
+
+ dir_base = FcStrPlus (dir, (FcChar8 *) "/");
+ if (!dir_base)
+ {
+ fprintf (stderr, "%s: out of memory\n", dir);
+ return FcFalse;
+ }
+ if (access ((char *) dir, W_OK) != 0)
+ {
+ if (verbose)
+ printf ("%s: not cleaning %s cache directory\n", dir,
+ access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
+ FcStrFree (dir_base);
+ return FcTrue;
+ }
+ if (verbose)
+ printf ("%s: cleaning cache directory\n", dir);
+ d = opendir ((char *) dir);
+ if (!d)
+ {
+ perror ((char *) dir);
+ FcStrFree (dir_base);
+ return FcFalse;
+ }
+ while ((ent = readdir (d)))
+ {
+ FcChar8 *file_name;
+ const FcChar8 *target_dir;
+
+ if (ent->d_name[0] == '.')
+ continue;
+ /* skip cache files for different architectures and */
+ /* files which are not cache files at all */
+ if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
+ strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
+ continue;
+
+ file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
+ if (!file_name)
+ {
+ fprintf (stderr, "%s: allocation failure\n", dir);
+ ret = FcFalse;
+ break;
+ }
+ remove = FcFalse;
+ cache = FcDirCacheLoadFile (file_name, NULL);
+ if (!cache)
+ {
+ if (verbose)
+ printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
+ remove = FcTrue;
+ }
+ else
+ {
+ target_dir = FcCacheDir (cache);
+ if (stat ((char *) target_dir, &target_stat) < 0)
+ {
+ if (verbose)
+ printf ("%s: %s: missing directory: %s \n",
+ dir, ent->d_name, target_dir);
+ remove = FcTrue;
+ }
+ }
+ if (remove)
+ {
+ if (unlink ((char *) file_name) < 0)
+ {
+ perror ((char *) file_name);
+ ret = FcFalse;
+ }
+ }
+ FcDirCacheUnload (cache);
+ FcStrFree (file_name);
+ }
+
+ closedir (d);
+ FcStrFree (dir_base);
+ return ret;
+}
+
+static FcBool
+cleanCacheDirectories (FcConfig *config, FcBool verbose)
+{
+ FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
+ FcChar8 *cache_dir;
+ FcBool ret = FcTrue;
+
+ if (!cache_dirs)
+ return FcFalse;
+ while ((cache_dir = FcStrListNext (cache_dirs)))
+ {
+ if (!cleanCacheDirectory (config, cache_dir, verbose))
+ {
+ ret = FcFalse;
+ break;
+ }
+ }
+ FcStrListDone (cache_dirs);
+ return ret;