Whether one needs to generate the font cache offline and then deploy the image to a target or do some testing in a separate rootfs, the sysroot option will facilitate that. Suppose you've got a rootfs in the following directory: /path/to/test/rootfs. In order to contain the fc-cache generation to that particular directory, the following command can be used: fc-cache --sysroot=/path/to/test/rootfs That will make fc-cache to prepend the sysroot directory to all paths during scanning. For example, instead of searching /etc/fonts/ directory for configuration files, it will look in /path/to/test/rootfs/etc/fonts. The paths found in fonts.conf will also be prepended with the sysroot. However, the generated cache files will not contain any references to sysroot. This way, one can generate the font cache offline and then deploy the image to target. Or, simply, use it for various tests without polluting the system/user cache files. In order to inspect the cache generated using the sysroot option, one has to use fc-cat like below (for example): fc-cat --sysroot=/path/to/test/rootfs /path/to/test/rootfs/var/cache/fontconfig/* Signed-off-by: Laurentiu Palcu <laurentiu.palcu@xxxxxxxxx> --- fc-cache/fc-cache.c | 57 ++++++++++++++++++----- fc-cat/fc-cat.c | 54 +++++++++++++++++---- fontconfig/fontconfig.h | 6 +++ src/fccache.c | 119 ++++++++++++++++++++++++++++++++++++++++------- src/fccfg.c | 32 +++++++++++++ src/fcfreetype.c | 4 ++ src/fcstr.c | 18 ++++++- 7 files changed, 251 insertions(+), 39 deletions(-) diff --git a/fc-cache/fc-cache.c b/fc-cache/fc-cache.c index 2206096..4ef9a32 100644 --- a/fc-cache/fc-cache.c +++ b/fc-cache/fc-cache.c @@ -68,6 +68,7 @@ const struct option longopts[] = { {"force", 0, 0, 'f'}, {"really-force", 0, 0, 'r'}, {"system-only", 0, 0, 's'}, + {"sysroot", 1, 0, 'y'}, {"version", 0, 0, 'V'}, {"verbose", 0, 0, 'v'}, {"help", 0, 0, 'h'}, @@ -85,26 +86,28 @@ usage (char *program, int error) { FILE *file = error ? stderr : stdout; #if HAVE_GETOPT_LONG - fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", + fprintf (file, "usage: %s [-frsvVh] [-y SYSROOT] [--force|--really-force] [--system-only] [--sysroot=SYSROOT] [--verbose] [--version] [--help] [dirs]\n", program); #else - fprintf (file, "usage: %s [-frsvVh] [dirs]\n", + fprintf (file, "usage: %s [-frsvVh] [-y SYSROOT] [dirs]\n", program); #endif fprintf (file, "Build font information caches in [dirs]\n" "(all directories in font configuration by default).\n"); 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"); + 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, " -y, --sysroot=SYSROOT for scanning, prefix all paths with SYSROOT. The cache file will not contain the SYSROOT!\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, " -y SYSROOT for scanning, prefix all paths with SYSROOT. The cache file will not contain the SYSROOT!\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"); @@ -220,7 +223,18 @@ scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, continue; } for (i = 0; i < FcCacheNumSubdir (cache); i++) - FcStrSetAdd (subdirs, FcCacheSubdir (cache, i)); + { + const FcChar8 *subdir = FcCacheSubdir (cache, i); + if (!subdir) + { + fprintf (stderr, "malloc failure\n"); + return ++ret; + } + + FcStrSetAdd (subdirs, subdir); + if (FcConfigGetSysRoot ()) + FcStrFree ((FcChar8 *) subdir); + } FcDirCacheUnload (cache); @@ -270,6 +284,7 @@ main (int argc, char **argv) FcBool really_force = FcFalse; FcBool systemOnly = FcFalse; FcConfig *config; + FcChar8 *sysroot = NULL; int i; int changed; int ret; @@ -277,9 +292,9 @@ main (int argc, char **argv) int c; #if HAVE_GETOPT_LONG - while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1) + while ((c = getopt_long (argc, argv, "frsy:Vvh", longopts, NULL)) != -1) #else - while ((c = getopt (argc, argv, "frsVvh")) != -1) + while ((c = getopt (argc, argv, "frsy:Vvh")) != -1) #endif { switch (c) { @@ -292,6 +307,9 @@ main (int argc, char **argv) case 's': systemOnly = FcTrue; break; + case 'y': + sysroot = FcStrCopy ((const FcChar8*) optarg); + break; case 'V': fprintf (stderr, "fontconfig version %d.%d.%d\n", FC_MAJOR, FC_MINOR, FC_REVISION); @@ -312,6 +330,21 @@ main (int argc, char **argv) if (systemOnly) FcConfigEnableHome (FcFalse); + + if (sysroot) + { + FcChar8 *canon_sysroot; + canon_sysroot = FcConfigSetSysRoot(sysroot); + FcStrFree (sysroot); + if (!canon_sysroot) + { + fprintf (stderr, "Cannot set the sysroot. Out of memory!\n"); + return 1; + } + + sysroot = canon_sysroot; + } + config = FcInitLoadConfig (); if (!config) { @@ -371,6 +404,8 @@ main (int argc, char **argv) * library, and there aren't any signals flying around here. */ FcConfigDestroy (config); + if (sysroot) + FcStrFree (sysroot); FcFini (); if (changed) sleep (2); diff --git a/fc-cat/fc-cat.c b/fc-cat/fc-cat.c index 9a2abb3..763f1bc 100644 --- a/fc-cat/fc-cat.c +++ b/fc-cat/fc-cat.c @@ -57,6 +57,7 @@ const struct option longopts[] = { {"verbose", 0, 0, 'v'}, {"recurse", 0, 0, 'r'}, {"help", 0, 0, 'h'}, + {"sysroot", 1, 0, 'y'}, {NULL,0,0,0}, }; #else @@ -148,11 +149,11 @@ usage (char *program, int error) { FILE *file = error ? stderr : stdout; #if HAVE_GETOPT_LONG - fprintf (file, "usage: %s [-rv] [--recurse] [--verbose] [*-%s" FC_CACHE_SUFFIX "|directory]...\n", + fprintf (file, "usage: %s [-rv] [--recurse] [--verbose] [--sysroot=SYSROOT] [*-%s" FC_CACHE_SUFFIX "|directory]...\n", program, FC_ARCHITECTURE); fprintf (file, " %s [-Vh] [--version] [--help]\n", program); #else - fprintf (file, "usage: %s [-rvVh] [*-%s" FC_CACHE_SUFFIX "|directory]...\n", + fprintf (file, "usage: %s [-rvVh] [-y SYSROOT] [*-%s" FC_CACHE_SUFFIX "|directory]...\n", program, FC_ARCHITECTURE); #endif fprintf (file, "Reads font information cache from:\n"); @@ -160,15 +161,17 @@ usage (char *program, int error) fprintf (file, " 2) related to a particular font directory\n"); fprintf (file, "\n"); #if HAVE_GETOPT_LONG - fprintf (file, " -r, --recurse recurse into subdirectories\n"); - fprintf (file, " -v, --verbose be verbose\n"); - fprintf (file, " -V, --version display font config version and exit\n"); - fprintf (file, " -h, --help display this help and exit\n"); + fprintf (file, " -r, --recurse recurse into subdirectories\n"); + fprintf (file, " -v, --verbose be verbose\n"); + fprintf (file, " -V, --version display font config version and exit\n"); + fprintf (file, " -h, --help display this help and exit\n"); + fprintf (file, " -y, --sysroot=SYSROOT needed if the cache was generated using --sysroot\n"); #else fprintf (file, " -r (recurse) recurse into subdirectories\n"); fprintf (file, " -v (verbose) be verbose\n"); fprintf (file, " -V (version) display font config version and exit\n"); fprintf (file, " -h (help) display this help and exit\n"); + fprintf (file, " -y SYSROOT needed if the cache was generated using --sysroot\n"); #endif exit (error); } @@ -260,13 +263,14 @@ main (int argc, char **argv) int verbose = 0; int recurse = 0; FcBool first = FcTrue; + FcChar8 *sysroot = NULL; #if HAVE_GETOPT_LONG || HAVE_GETOPT int c; #if HAVE_GETOPT_LONG - while ((c = getopt_long (argc, argv, "Vvrh", longopts, NULL)) != -1) + while ((c = getopt_long (argc, argv, "Vvrhy:", longopts, NULL)) != -1) #else - while ((c = getopt (argc, argv, "Vvrh")) != -1) + while ((c = getopt (argc, argv, "Vvrhy:")) != -1) #endif { switch (c) { @@ -282,6 +286,9 @@ main (int argc, char **argv) break; case 'h': usage (argv[0], 0); + case 'y': + sysroot = FcStrCopy ((const FcChar8*) optarg); + break; default: usage (argv[0], 1); } @@ -291,6 +298,20 @@ main (int argc, char **argv) i = 1; #endif + if (sysroot) + { + FcChar8 *canon_sysroot; + canon_sysroot = FcConfigSetSysRoot(sysroot); + FcStrFree (sysroot); + if (!canon_sysroot) + { + fprintf (stderr, "%s: malloc failure\n", argv[0]); + return 1; + } + + sysroot = canon_sysroot; + } + config = FcInitLoadConfig (); if (!config) { @@ -362,9 +383,19 @@ main (int argc, char **argv) fs = FcCacheCopySet (cache); for (j = 0; j < FcCacheNumSubdir (cache); j++) { - FcStrSetAdd (dirs, FcCacheSubdir (cache, j)); + const FcChar8 *subdir = FcCacheSubdir (cache, j); + if (!subdir) + { + fprintf (stderr, "%s: malloc failure\n", argv[0]); + return 1; + } + + FcStrSetAdd (dirs, subdir); if (recurse) - FcStrSetAdd (args, FcCacheSubdir (cache, j)); + FcStrSetAdd (args, subdir); + + if (sysroot) + FcStrFree ((FcChar8 *) subdir); } if (verbose) @@ -385,6 +416,9 @@ main (int argc, char **argv) FcStrFree (cache_file); } + if (sysroot) + FcStrFree (sysroot); + FcFini (); return 0; } diff --git a/fontconfig/fontconfig.h b/fontconfig/fontconfig.h index dc2532f..bfd884d 100644 --- a/fontconfig/fontconfig.h +++ b/fontconfig/fontconfig.h @@ -419,6 +419,12 @@ FcConfigSubstitute (FcConfig *config, FcPattern *p, FcMatchKind kind); +FcPublic FcChar8 * +FcConfigSetSysRoot (const FcChar8 *sysroot); + +FcPublic FcChar8 * +FcConfigGetSysRoot (void); + /* fccharset.c */ FcPublic FcCharSet* FcCharSetCreate (void); diff --git a/src/fccache.c b/src/fccache.c index 2c63125..45ee1af 100644 --- a/src/fccache.c +++ b/src/fccache.c @@ -55,6 +55,13 @@ static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]); #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX)) +/* + * Hokey little macro trick to permit the definitions of C functions + * with the same name as CPP macros + */ +#define args1(x) (x) +#define args2(x,y) (x,y) + static FcBool FcCacheIsMmapSafe (int fd) { @@ -98,6 +105,14 @@ FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN]) FcChar8 *hex_hash; int cnt; struct MD5Context ctx; + FcChar8 *sysroot = FcConfigGetSysRoot(); + + /* + * remove sysroot when generating the hex hash + */ + if (sysroot && !strncmp ((const char*) sysroot, (const char*) dir, + strlen ((const char*) sysroot))) + dir += strlen((const char*) sysroot); MD5Init (&ctx); MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir)); @@ -524,16 +539,27 @@ static FcBool FcCacheTimeValid (FcCache *cache, struct stat *dir_stat) { struct stat dir_static; + const FcChar8 *dir = FcCacheDir args1(cache); + FcChar8 *sysroot = FcConfigGetSysRoot (); + + if (!dir) + return FcFalse; if (!dir_stat) { - if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0) + if (FcStatChecksum (dir, &dir_static) < 0) + { + if (sysroot) + FcStrFree ((FcChar8 *) dir); return FcFalse; + } dir_stat = &dir_static; } if (FcDebug () & FC_DBG_CACHE) printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n", - FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime); + dir, cache->checksum, (int) dir_stat->st_mtime); + if (sysroot) + FcStrFree ((FcChar8 *) dir); return cache->checksum == (int) dir_stat->st_mtime; } @@ -735,9 +761,27 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt FcChar8 *dir_serialize; intptr_t *dirs_serialize; FcFontSet *set_serialize; + FcChar8 *sysroot = FcConfigGetSysRoot (); + FcStrSet *dirs_without_sysroot; if (!serialize) return NULL; + + if (sysroot) + { + dir += strlen ((const char*) sysroot); + + dirs_without_sysroot = FcStrSetCreate (); + if (!dirs_without_sysroot) + return NULL; + + for (i = 0; i < dirs->num; i++) + FcStrSetAdd (dirs_without_sysroot, + dirs->strs[i] + strlen ((const char*) sysroot)); + + dirs = dirs_without_sysroot; + } + /* * Space for cache structure */ @@ -811,11 +855,17 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt FcCacheInsert (cache, NULL); + if (sysroot) + FcStrSetDestroy(dirs_without_sysroot); + return cache; bail2: free (cache); bail1: + if (sysroot) + FcStrSetDestroy(dirs_without_sysroot); + FcSerializeDestroy (serialize); return NULL; } @@ -852,7 +902,7 @@ FcMakeDirectory (const FcChar8 *dir) FcBool FcDirCacheWrite (FcCache *cache, FcConfig *config) { - FcChar8 *dir = FcCacheDir (cache); + const FcChar8 *dir = FcCacheDir args1(cache); FcChar8 cache_base[CACHEBASE_LEN]; FcChar8 *cache_hashed; int fd; @@ -864,6 +914,10 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) struct stat cache_stat; unsigned int magic; int written; + FcChar8 *sysroot = FcConfigGetSysRoot (); + + if (!dir) + return FcFalse; /* * Write it to the first directory in the list which is writable @@ -871,7 +925,7 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) list = FcStrListCreate (config->cacheDirs); if (!list) - return FcFalse; + goto bail0; while ((test_dir = FcStrListNext (list))) { if (access ((char *) test_dir, W_OK) == 0) { @@ -906,12 +960,12 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) } FcStrListDone (list); if (!cache_dir) - return FcFalse; + goto bail0; FcDirCacheBasename (dir, cache_base); cache_hashed = FcStrPlus (cache_dir, cache_base); if (!cache_hashed) - return FcFalse; + goto bail0; if (FcDebug () & FC_DBG_CACHE) printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n", @@ -971,6 +1025,8 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) FcStrFree (cache_hashed); FcAtomicUnlock (atomic); FcAtomicDestroy (atomic); + if (sysroot) + FcStrFree ((FcChar8 *) dir); return FcTrue; bail5: @@ -981,6 +1037,9 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) FcAtomicDestroy (atomic); bail1: FcStrFree (cache_hashed); + bail0: + if (sysroot) + FcStrFree ((FcChar8 *) dir); return FcFalse; } @@ -1021,6 +1080,7 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose) { FcChar8 *file_name; const FcChar8 *target_dir; + FcChar8 *sysroot = FcConfigGetSysRoot (); if (ent->d_name[0] == '.') continue; @@ -1047,7 +1107,13 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose) } else { - target_dir = FcCacheDir (cache); + target_dir = FcCacheDir args1(cache); + if (!target_dir) + { + ret = FcFalse; + FcStrFree (file_name); + break; + } if (stat ((char *) target_dir, &target_stat) < 0) { if (verbose || FcDebug () & FC_DBG_CACHE) @@ -1066,6 +1132,8 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose) } } FcStrFree (file_name); + if (sysroot) + FcStrFree ((FcChar8 *) target_dir); } closedir (d); @@ -1075,17 +1143,23 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose) return ret; } -/* - * Hokey little macro trick to permit the definitions of C functions - * with the same name as CPP macros - */ -#define args1(x) (x) -#define args2(x,y) (x,y) - const FcChar8 * FcCacheDir args1(const FcCache *c) { - return FcCacheDir (c); + FcChar8 *sysroot = FcConfigGetSysRoot (); + FcChar8 *dir = FcCacheDir (c); + FcChar8 *new_dir = NULL; + + if (sysroot) + { + new_dir = FcStrPlus (sysroot, dir); + if (!new_dir) + return NULL; + + return new_dir; + } + + return dir; } FcFontSet * @@ -1114,7 +1188,20 @@ FcCacheCopySet args1(const FcCache *c) const FcChar8 * FcCacheSubdir args2(const FcCache *c, int i) { - return FcCacheSubdir (c, i); + FcChar8 *sysroot = FcConfigGetSysRoot (); + FcChar8 *subdir = FcCacheSubdir (c, i); + FcChar8 *new_subdir; + + if (sysroot) + { + new_subdir = FcStrPlus (sysroot, subdir); + if (!new_subdir) + return NULL; + + return new_subdir; + } + + return subdir; } int diff --git a/src/fccfg.c b/src/fccfg.c index 12d7e1a..38d16c5 100644 --- a/src/fccfg.c +++ b/src/fccfg.c @@ -33,6 +33,7 @@ #endif static FcConfig *_fcConfig; /* MT-safe */ +static FcChar8 *_FcConfigSysRoot = NULL; static FcConfig * FcConfigEnsure (void) @@ -1832,6 +1833,18 @@ FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file) #endif strcat ((char *) path, (char *) file); + if (_FcConfigSysRoot && + strncmp ((const char*) _FcConfigSysRoot, (const char*) path, + strlen ((const char *) _FcConfigSysRoot))) + { + FcChar8 *new_path = FcStrPlus (_FcConfigSysRoot, path); + FcStrFree (path); + if (!new_path) + return 0; + + path = new_path; + } + if (access ((char *) path, R_OK) == 0) return path; @@ -2299,6 +2312,25 @@ FcConfigAcceptFont (FcConfig *config, return FcFalse; return FcTrue; } + + +FcPublic FcChar8 * +FcConfigSetSysRoot (const FcChar8 *sysroot) +{ + if (_FcConfigSysRoot) + FcStrFree(_FcConfigSysRoot); + + _FcConfigSysRoot = FcStrCopyFilename(sysroot); + + return _FcConfigSysRoot; +} + +FcPublic FcChar8 * +FcConfigGetSysRoot (void) +{ + return _FcConfigSysRoot; +} + #define __fccfg__ #include "fcaliastail.h" #undef __fccfg__ diff --git a/src/fcfreetype.c b/src/fcfreetype.c index faf3c35..019409f 100644 --- a/src/fcfreetype.c +++ b/src/fcfreetype.c @@ -1122,6 +1122,7 @@ FcFreeTypeQueryFace (const FT_Face face, FcChar8 *style = 0; int st; + FcChar8 *sysroot = FcConfigGetSysRoot(); pat = FcPatternCreate (); if (!pat) @@ -1338,6 +1339,9 @@ FcFreeTypeQueryFace (const FT_Face face, ++nstyle; } + if (sysroot) + file += strlen ((const char*) sysroot); + if (!nfamily) { FcChar8 *start, *end; diff --git a/src/fcstr.c b/src/fcstr.c index 414d6dd..b464812 100644 --- a/src/fcstr.c +++ b/src/fcstr.c @@ -1141,12 +1141,26 @@ FcStrSetAdd (FcStrSet *set, const FcChar8 *s) FcBool FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s) { + FcChar8 *full; + FcChar8 *sysroot = FcConfigGetSysRoot(); FcChar8 *new = FcStrCopyFilename (s); if (!new) return FcFalse; - if (!_FcStrSetAppend (set, new)) + + if (sysroot && strncmp ((const char *) sysroot, (const char *) new, + strlen ((const char*) sysroot))) { - FcStrFree (new); + full = FcStrPlus(sysroot, new); + FcStrFree(new); + if (!full) + return FcFalse; + } + else + full = new; + + if (!_FcStrSetAppend (set, full)) + { + FcStrFree (full); return FcFalse; } return FcTrue; -- 1.7.9.5 _______________________________________________ Fontconfig mailing list Fontconfig@xxxxxxxxxxxxxxxxxxxxx http://lists.freedesktop.org/mailman/listinfo/fontconfig