X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=fc-cache%2Ffc-cache.c;h=9df90ccea2f0814836097a0abbde58ccdc172e0e;hb=2d3387fd720f33f80847ae6cbb83d94c9a52fde3;hp=ed920fd53f6460f5521e5e2d24caef4d7cca13e4;hpb=0c35c0facb1f05a21f702636a291eb6ee3dea3a2;p=fontconfig.git diff --git a/fc-cache/fc-cache.c b/fc-cache/fc-cache.c index ed920fd..9df90cc 100644 --- a/fc-cache/fc-cache.c +++ b/fc-cache/fc-cache.c @@ -1,7 +1,7 @@ /* - * $XFree86: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.4 2002/05/21 17:06:21 keithp Exp $ + * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $ * - * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc. + * Copyright © 2002 Keith Packard * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -22,12 +22,6 @@ * PERFORMANCE OF THIS SOFTWARE. */ -#include -#include -#include -#include -#include -#include #ifdef HAVE_CONFIG_H #include #else @@ -37,6 +31,21 @@ #define HAVE_GETOPT 1 #endif +#include +#include +#include +#include +#include +#include +#include + +#if defined (_WIN32) +#define STRICT +#include +#define sleep(x) Sleep((x) * 1000) +#undef STRICT +#endif + #ifndef HAVE_GETOPT #define HAVE_GETOPT 0 #endif @@ -50,6 +59,8 @@ #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'}, {"help", 0, 0, '?'}, @@ -65,18 +76,36 @@ extern int optind, opterr, optopt; static void usage (char *program) { - fprintf (stderr, "usage: %s [-fvV?] [--force] [--verbose] [--version] [--help] [dirs]\n", +#if HAVE_GETOPT_LONG + fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", + program); +#else + fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n", program); +#endif fprintf (stderr, "Build font information caches in [dirs]\n" "(all directories in font configuration by default).\n"); fprintf (stderr, "\n"); - fprintf (stderr, " -v, --force scan directories with apparently valid caches\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"); + fprintf (stderr, " -? (help) display this help and exit\n"); +#endif exit (1); } +static FcStrSet *processed_dirs; + static int nsubdirs (FcStrSet *set) { @@ -93,14 +122,15 @@ nsubdirs (FcStrSet *set) } static int -scanDirs (FcStrList *list, 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 @@ -113,47 +143,95 @@ scanDirs (FcStrList *list, char *program, FcBool force, FcBool verbose) 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; } - if (stat ((char *) dir, &statb) == -1) + if (access ((char *) dir, W_OK) < 0) { - if (errno == ENOENT || errno == ENOTDIR) - { - fprintf (stderr, "\"%s\": no such directory, skipping\n", dir); - } - else - { + switch (errno) { + case ENOENT: + case ENOTDIR: + if (verbose) + printf ("skipping, no such directory\n"); + FcFontSetDestroy (set); + FcStrSetDestroy (subdirs); + continue; + case EACCES: + case EROFS: + /* 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; } + } + if (stat ((char *) dir, &statb) == -1) + { + fprintf (stderr, "\"%s\": ", dir); + perror (""); + FcFontSetDestroy (set); + FcStrSetDestroy (subdirs); + ret++; continue; } if (!S_ISDIR (statb.st_mode)) { fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); + FcFontSetDestroy (set); + FcStrSetDestroy (subdirs); continue; } - if (!FcDirScan (set, subdirs, 0, FcConfigGetBlanks (0), 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)) + if (!force && was_valid) { if (verbose) printf ("skipping, %d fonts, %d dirs\n", @@ -164,22 +242,25 @@ scanDirs (FcStrList *list, char *program, FcBool force, FcBool verbose) if (verbose) printf ("caching, %d fonts, %d dirs\n", set->nfont, nsubdirs (subdirs)); - if (!FcDirSave (set, subdirs, dir)) + + if (!FcDirCacheValid (dir, config)) { - fprintf (stderr, "Can't save cache in \"%s\"\n", dir); + fprintf (stderr, "%s: failed to write cache\n", dir); + (void) FcDirCacheUnlink (dir, config); ret++; } } FcFontSetDestroy (set); sublist = FcStrListCreate (subdirs); + 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, program, force, verbose); - FcStrSetDestroy (subdirs); + FcStrSetAdd (processed_dirs, dir); + ret += scanDirs (sublist, config, program, force, really_force, verbose); } FcStrListDone (list); return ret; @@ -192,6 +273,8 @@ main (int argc, char **argv) FcStrList *list; FcBool verbose = FcFalse; FcBool force = FcFalse; + FcBool really_force = FcFalse; + FcBool systemOnly = FcFalse; FcConfig *config; int i; int ret; @@ -199,15 +282,21 @@ main (int argc, char **argv) int c; #if HAVE_GETOPT_LONG - while ((c = getopt_long (argc, argv, "fVv?", longopts, NULL)) != -1) + while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1) #else - while ((c = getopt (argc, argv, "fVv?")) != -1) + while ((c = getopt (argc, argv, "frsVv?")) != -1) #endif { switch (c) { + case 'r': + really_force = FcTrue; + /* fall through */ case 'f': force = FcTrue; break; + case 's': + systemOnly = FcTrue; + break; case 'V': fprintf (stderr, "fontconfig version %d.%d.%d\n", FC_MAJOR, FC_MINOR, FC_REVISION); @@ -224,12 +313,16 @@ main (int argc, char **argv) i = 1; #endif + if (systemOnly) + FcConfigEnableHome (FcFalse); config = FcInitLoadConfig (); if (!config) { fprintf (stderr, "%s: Can't init font config library\n", argv[0]); return 1; } + FcConfigSetCurrent (config); + if (argv[i]) { dirs = FcStrSetCreate (); @@ -253,7 +346,25 @@ main (int argc, char **argv) } else list = FcConfigGetConfigDirs (config); - ret = scanDirs (list, 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); + + /* + * 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 + * then any timestamps we wrote. We don't use gettimeofday() because + * sleep(3) can't be interrupted by a signal here -- this isn't in the + * library, and there aren't any signals flying around here. + */ + FcConfigDestroy (config); + sleep (2); if (verbose) printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); return ret;