+static FcBool
+cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
+{
+ DIR *d;
+ struct dirent *ent;
+ FcChar8 *fullDir;
+ FcChar8 *checkDir;
+ FcBool ret = FcTrue;
+ FcBool remove;
+ FcCache *cache;
+ struct stat target_stat;
+
+ fullDir = FcConfigGetRootPlus (config, dir);
+ if (fullDir)
+ checkDir = fullDir;
+ else
+ checkDir = dir;
+
+ if (access ((char *) checkDir, W_OK) != 0)
+ {
+ if (verbose)
+ printf ("%s: not cleaning %s cache directory\n", dir,
+ access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
+ goto done;
+ }
+ if (verbose)
+ printf ("%s: cleaning cache directory\n", dir);
+ d = opendir ((char *) checkDir);
+ if (!d)
+ {
+ perror ((char *) dir);
+ ret = FcFalse;
+ goto done;
+ }
+ 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 = FcStrPathPlus (dir, (const FcChar8 *) ent->d_name, NULL);
+ if (!file_name)
+ {
+ fprintf (stderr, "%s: allocation failure\n", dir);
+ ret = FcFalse;
+ break;
+ }
+ remove = FcFalse;
+ cache = FcDirCacheLoadFile2 (file_name, config, NULL);
+ if (!cache)
+ {
+ if (verbose)
+ printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
+ remove = FcTrue;
+ }
+ else
+ {
+ target_dir = FcCacheDir (cache);
+ if (FcStat (config, target_dir, &target_stat) < 0)
+ {
+ if (verbose)
+ printf ("%s: %s: missing directory: %s \n",
+ dir, ent->d_name, target_dir);
+ remove = FcTrue;
+ }
+ }
+ if (remove)
+ {
+ FcChar8 *unlink_file = FcConfigGetRootPlus (config, file_name);
+ if (!unlink_file)
+ unlink_file = file_name;
+ if (unlink ((char *) unlink_file) < 0)
+ {
+ perror ((char *) unlink_file);
+ ret = FcFalse;
+ }
+ if (unlink_file != file_name)
+ FcStrFree (unlink_file);
+ }
+ FcDirCacheUnload (cache);
+ FcStrFree (file_name);
+ }
+
+ closedir (d);
+ done:
+ if (fullDir)
+ FcStrFree (fullDir);
+ 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;
+}
+