X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=fc-cache%2Ffc-cache.c;h=cf4ea6c36ab9a3b1139a40417475d910e26c4fbb;hb=d8ab9e6c42cb3513a6623df0c2866e1ebbd96485;hp=7acaafe4a9acdc727836300c573c153d4e6ee8b7;hpb=83b6739035fc17d97b8ce01d6a9b9ef6e78d694c;p=fontconfig.git diff --git a/fc-cache/fc-cache.c b/fc-cache/fc-cache.c index 7acaafe..cf4ea6c 100644 --- a/fc-cache/fc-cache.c +++ b/fc-cache/fc-cache.c @@ -22,13 +22,6 @@ * PERFORMANCE OF THIS SOFTWARE. */ -#include -#include -#include -#include -#include -#include -#include #ifdef HAVE_CONFIG_H #include #else @@ -38,6 +31,27 @@ #define HAVE_GETOPT 1 #endif +#include "fcint.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined (_WIN32) +#define STRICT +#include +#define sleep(x) Sleep((x) * 1000) +#undef STRICT +#endif + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + #ifndef HAVE_GETOPT #define HAVE_GETOPT 0 #endif @@ -51,6 +65,7 @@ #include const struct option longopts[] = { {"force", 0, 0, 'f'}, + {"really-force", 0, 0, 'r'}, {"system-only", 0, 0, 's'}, {"version", 0, 0, 'V'}, {"verbose", 0, 0, 'v'}, @@ -68,10 +83,10 @@ static void usage (char *program) { #if HAVE_GETOPT_LONG - fprintf (stderr, "usage: %s [-fsvV?] [--force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", + fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", program); #else - fprintf (stderr, "usage: %s [-fsvV?] [dirs]\n", + fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n", program); #endif fprintf (stderr, "Build font information caches in [dirs]\n" @@ -79,12 +94,14 @@ usage (char *program) fprintf (stderr, "\n"); #if HAVE_GETOPT_LONG fprintf (stderr, " -f, --force scan directories with apparently valid caches\n"); + fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n"); fprintf (stderr, " -s, --system-only scan system-wide directories only\n"); fprintf (stderr, " -v, --verbose display status information while busy\n"); fprintf (stderr, " -V, --version display font config version and exit\n"); fprintf (stderr, " -?, --help display this help and exit\n"); #else fprintf (stderr, " -f (force) scan directories with apparently valid caches\n"); + fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n"); fprintf (stderr, " -s (system) scan system-wide directories only\n"); fprintf (stderr, " -v (verbose) display status information while busy\n"); fprintf (stderr, " -V (version) display font config version and exit\n"); @@ -93,6 +110,8 @@ usage (char *program) exit (1); } +static FcStrSet *processed_dirs; + static int nsubdirs (FcStrSet *set) { @@ -109,14 +128,15 @@ nsubdirs (FcStrSet *set) } static int -scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool verbose) +scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool really_force, FcBool verbose) { int ret = 0; - FcChar8 *dir; + const FcChar8 *dir; FcFontSet *set; FcStrSet *subdirs; FcStrList *sublist; struct stat statb; + FcBool was_valid; /* * Now scan all of the directories into separate databases @@ -129,17 +149,32 @@ scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool printf ("%s: \"%s\": ", program, 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; + } + set = FcFontSetCreate (); if (!set) { - fprintf (stderr, "Can't create font set\n"); + fprintf (stderr, "%s: Can't create font set\n", dir); ret++; continue; } subdirs = FcStrSetCreate (); if (!subdirs) { - fprintf (stderr, "Can't create directory set\n"); + fprintf (stderr, "%s: Can't create directory set\n", dir); ret++; FcFontSetDestroy (set); continue; @@ -152,20 +187,24 @@ scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool case ENOTDIR: if (verbose) printf ("skipping, no such directory\n"); - break; + FcFontSetDestroy (set); + FcStrSetDestroy (subdirs); + continue; case EACCES: case EROFS: - if (verbose) - printf ("skipping, no write access\n"); + /* That's ok, caches go to /var anyway. */ + /* Ideally we'd do an access on the hashed_name. */ + /* But we hid that behind an abstraction barrier. */ break; default: fprintf (stderr, "\"%s\": ", dir); perror (""); ret++; + + FcFontSetDestroy (set); + FcStrSetDestroy (subdirs); + continue; } - FcFontSetDestroy (set); - FcStrSetDestroy (subdirs); - continue; } if (stat ((char *) dir, &statb) == -1) { @@ -183,15 +222,22 @@ scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool FcStrSetDestroy (subdirs); continue; } - if (!FcDirScan (set, subdirs, 0, FcConfigGetBlanks (config), dir, force)) + + if (really_force) + FcDirCacheUnlink (dir, config); + + if (!force) + was_valid = FcDirCacheValid (dir, config); + + if (!FcDirScanConfig (set, subdirs, FcConfigGetBlanks (config), dir, force, config)) { - fprintf (stderr, "\"%s\": error scanning\n", dir); + fprintf (stderr, "%s: error scanning\n", dir); FcFontSetDestroy (set); FcStrSetDestroy (subdirs); ret++; continue; } - if (!force && FcDirCacheValid (dir) && FcDirCacheHasCurrentArch (dir)) + if (!force && was_valid) { if (verbose) printf ("skipping, %d fonts, %d dirs\n", @@ -203,15 +249,10 @@ scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool printf ("caching, %d fonts, %d dirs\n", set->nfont, nsubdirs (subdirs)); - if (!FcDirCacheValid (dir)) - if (!FcDirCacheUnlink (dir)) - ret++; - - if (!FcDirSave (set, subdirs, dir)) + if (!FcDirCacheValid (dir, config)) { - if (!ret) - fprintf (stderr, "Caches are currently saved to \"%s\"\n", PKGCACHEDIR); - fprintf (stderr, "Can't save cache for \"%s\"\n", dir); + fprintf (stderr, "%s: failed to write cache\n", dir); + (void) FcDirCacheUnlink (dir, config); ret++; } } @@ -220,16 +261,137 @@ scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool FcStrSetDestroy (subdirs); if (!sublist) { - fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); + fprintf (stderr, "%s: Can't create subdir list\n", dir); ret++; continue; } - ret += scanDirs (sublist, config, program, force, verbose); + FcStrSetAdd (processed_dirs, dir); + ret += scanDirs (sublist, config, program, force, really_force, verbose); } FcStrListDone (list); return ret; } +FcCache * +FcCacheFileMap (const FcChar8 *file, struct stat *file_stat) +{ + FcCache *cache; + int fd; + + fd = open (file, O_RDONLY | O_BINARY); + if (fd < 0) + return NULL; + if (fstat (fd, file_stat) < 0) { + close (fd); + return NULL; + } + if (FcDirCacheLoad (fd, file_stat->st_size, &cache)) { + close (fd); + return cache; + } + close (fd); + return NULL; +} + +static FcBool +cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) +{ + DIR *d; + struct dirent *ent; + char *dir_base; + FcBool ret = FcTrue; + FcBool remove; + FcCache *cache; + struct stat file_stat; + struct stat target_stat; + + dir_base = FcStrPlus (dir, "/"); + if (access ((char *) dir, W_OK|X_OK) != 0) + { + if (verbose) + printf ("%s: skipping unwritable cache directory\n", dir); + return FcTrue; + } + d = opendir (dir); + if (!d) + { + perror (dir); + return FcFalse; + } + while ((ent = readdir (d))) + { + FcChar8 *file_name; + FcChar8 *target_dir; + + if (ent->d_name[0] == '.') + continue; + file_name = FcStrPlus (dir_base, ent->d_name); + if (!file_name) + { + fprintf (stderr, "%s: allocation failure\n", dir); + ret = FcFalse; + break; + } + cache = FcCacheFileMap (file_name, &file_stat); + if (!cache) + { + fprintf (stderr, "%s: invalid cache file: %s\n", dir, ent->d_name); + FcStrFree (file_name); + ret = FcFalse; + continue; + } + target_dir = FcCacheDir (cache); + remove = FcFalse; + if (stat (target_dir, &target_stat) < 0) + { + if (verbose) + printf ("%s: %s: missing directory: %s \n", + dir, ent->d_name, target_dir); + remove = FcTrue; + } + else if (target_stat.st_mtime > file_stat.st_mtime) + { + if (verbose) + printf ("%s: %s: cache outdated: %s\n", + dir, ent->d_name, target_dir); + remove = FcTrue; + } + if (remove) + { + if (unlink (file_name) < 0) + { + perror (file_name); + ret = FcFalse; + } + } + FcStrFree (file_name); + } + + closedir (d); + 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; +} + int main (int argc, char **argv) { @@ -237,6 +399,7 @@ main (int argc, char **argv) FcStrList *list; FcBool verbose = FcFalse; FcBool force = FcFalse; + FcBool really_force = FcFalse; FcBool systemOnly = FcFalse; FcConfig *config; int i; @@ -245,12 +408,15 @@ main (int argc, char **argv) int c; #if HAVE_GETOPT_LONG - while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1) + while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) #else - while ((c = getopt (argc, argv, "fsVv?")) != -1) + while ((c = getopt (argc, argv, "frsVv?")) != -1) #endif { switch (c) { + case 'r': + really_force = FcTrue; + /* fall through */ case 'f': force = FcTrue; break; @@ -281,6 +447,7 @@ main (int argc, char **argv) fprintf (stderr, "%s: Can't init font config library\n", argv[0]); return 1; } + FcConfigSetCurrent (config); if (argv[i]) { @@ -305,7 +472,18 @@ main (int argc, char **argv) } else list = FcConfigGetConfigDirs (config); - ret = scanDirs (list, config, argv[0], force, verbose); + + if ((processed_dirs = FcStrSetCreate()) == NULL) { + fprintf(stderr, "Cannot malloc\n"); + return 1; + } + + ret = scanDirs (list, config, argv[0], force, really_force, verbose); + + FcStrSetDestroy (processed_dirs); + + cleanCacheDirectories (config, verbose); + /* * Now we need to sleep a second (or two, to be extra sure), to make * sure that timestamps for changes after this run of fc-cache are later