summaryrefslogtreecommitdiff
path: root/src/fccache.c
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2018-10-29 16:39:05 -0700
committerAkira TAGOH <akira@tagoh.org>2019-04-03 12:02:09 +0000
commitc4324f54ee16e648ba91f3e9c66af13ab3b1754c (patch)
treedd64c4ac0bc6d628bf39cafabe1950578c2dfdbd /src/fccache.c
parent4cde12bfda1316e6d5464a2d9607d15322ef8024 (diff)
downloadfontconfig-c4324f54ee16e648ba91f3e9c66af13ab3b1754c.tar.gz
Replace UUID file mechanism with per-directory 'map' attribute [v2]
The UUID files would be placed in each font directory to provide the unique cache name, independent of path, for that directory. The UUID files are undesireable for a couple of reasons: 1) They must be placed in the font directories to be useful. This requires modifying the font directories themselves, introducing potential visible timestamp changes when running multiple applications, and makes the cache processing inconsistent between applications with permission to write to the font directories and applications without such permission. 2) The UUID contents were generated randomly, which makes the font cache not reproducible across multiple runs. One proposed fix for 2) is to make the UUID dependent on the font directory path, but once we do that, we can simply use the font directory path itself as the key as the original MD5-based font cache naming mechanism did. The goal of the UUID file mechanism was to fix startup time of flatpaks; as the font path names inside the flatpak did not match the font path names in the base system, the font cache would need to be reconstructed the first time the flatpak was launched. The new mechanism for doing this is to allow each '<dir>' element in the configuration include a 'map' attribute. When looking for a cache file for a particular directory, if the directory name starts with the contents of the <dir> element, that portion of the name will be replaced with the value of the 'map' attribute. Outside of the flatpak, nothing need change -- fontconfig will build cache files using real directory names. Inside the flatpak, the custom fonts.conf file will now include mappings such as this: <dir map="/usr/share/fonts">/run/host/fonts</dir> When scanning the directory /run/host/fonts/ttf, fontconfig will use the name /usr/share/fonts/ttf as the source for building the cache file name. The existing FC_FILE replacement code used for the UUID-based implementation continues to correctly adapt font path names seen by applications. v2: Leave FcDirCacheCreateUUID stub around to avoid removing public API function. Document 'map' attribute of <dir> element in fontconfig-user.sgml Suggested-by: Akira TAGOH <akira@tagoh.org> Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'src/fccache.c')
-rw-r--r--src/fccache.c210
1 files changed, 14 insertions, 196 deletions
diff --git a/src/fccache.c b/src/fccache.c
index fa3451e..c728553 100644
--- a/src/fccache.c
+++ b/src/fccache.c
@@ -51,103 +51,7 @@ FcDirCacheCreateUUID (FcChar8 *dir,
FcBool force,
FcConfig *config)
{
- FcBool ret = FcTrue;
-#ifndef _WIN32
- const FcChar8 *sysroot = FcConfigGetSysRoot (config);
- FcChar8 *target;
- FcChar8 *uuidname;
-
- if (sysroot)
- target = FcStrBuildFilename (sysroot, dir, NULL);
- else
- target = FcStrdup (dir);
- uuidname = FcStrBuildFilename (target, ".uuid", NULL);
-
- if (!uuidname)
- {
- FcStrFree (target);
- return FcFalse;
- }
-
- if (force || access ((const char *) uuidname, F_OK) < 0)
- {
- FcAtomic *atomic;
- int fd;
- uuid_t uuid;
- char out[37];
- FcBool (* hash_add) (FcHashTable *, void*, void*);
- struct stat statb;
- struct timeval times[2];
-
- if (FcStat (target, &statb) != 0)
- {
- ret = FcFalse;
- goto bail1;
- }
- atomic = FcAtomicCreate (uuidname);
- if (!atomic)
- {
- ret = FcFalse;
- goto bail1;
- }
- if (!FcAtomicLock (atomic))
- {
- ret = FcFalse;
- goto bail2;
- }
- fd = FcOpen ((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
- if (fd == -1)
- {
- ret = FcFalse;
- goto bail3;
- }
- uuid_generate_random (uuid);
- if (force)
- hash_add = FcHashTableReplace;
- else
- hash_add = FcHashTableAdd;
- if (!hash_add (config->uuid_table, target, uuid))
- {
- ret = FcFalse;
- FcAtomicDeleteNew (atomic);
- close (fd);
- goto bail3;
- }
- uuid_unparse (uuid, out);
- if (FcDebug () & FC_DBG_CACHE)
- printf ("FcDirCacheCreateUUID %s: %s\n", uuidname, out);
- write (fd, out, strlen (out));
- close (fd);
- FcAtomicReplaceOrig (atomic);
- bail3:
- FcAtomicUnlock (atomic);
- bail2:
- FcAtomicDestroy (atomic);
-
- if (ret)
- {
- /* revert mtime of the directory */
- times[0].tv_sec = statb.st_atime;
- times[1].tv_sec = statb.st_mtime;
-#ifdef HAVE_STRUCT_STAT_ST_MTIM
- times[0].tv_usec = statb.st_atim.tv_nsec / 1000;
- times[1].tv_usec = statb.st_mtim.tv_nsec / 1000;
-#else
- times[0].tv_usec = 0;
- times[1].tv_usec = 0;
-#endif
- if (utimes ((const char *) target, times) != 0)
- {
- fprintf (stderr, "Unable to revert mtime: %s\n", target);
- }
- }
- }
-bail1:
- FcStrFree (uuidname);
- FcStrFree (target);
-#endif
-
- return ret;
+ return FcTrue;
}
FcBool
@@ -187,7 +91,6 @@ FcDirCacheDeleteUUID (const FcChar8 *dir,
{
fprintf (stderr, "Unable to revert mtime: %s\n", d);
}
- FcHashTableRemove (config->uuid_table, target);
}
FcStrFree (target);
bail:
@@ -197,59 +100,6 @@ bail:
return ret;
}
-#ifndef _WIN32
-static void
-FcDirCacheReadUUID (FcChar8 *dir,
- FcConfig *config)
-{
- void *u;
- uuid_t uuid;
- const FcChar8 *sysroot = FcConfigGetSysRoot (config);
- FcChar8 *target;
-
- if (sysroot)
- target = FcStrBuildFilename (sysroot, dir, NULL);
- else
- target = FcStrdup (dir);
-
- if (!FcHashTableFind (config->uuid_table, target, &u))
- {
- FcChar8 *uuidname = FcStrBuildFilename (target, ".uuid", NULL);
- int fd;
-
- if ((fd = FcOpen ((char *) uuidname, O_RDONLY)) >= 0)
- {
- char suuid[37];
- ssize_t len;
-
- memset (suuid, 0, sizeof (suuid));
- len = read (fd, suuid, 36);
- if (len != -1)
- {
- suuid[len] = 0;
- memset (uuid, 0, sizeof (uuid));
- if (uuid_parse (suuid, uuid) == 0)
- {
- if (FcDebug () & FC_DBG_CACHE)
- printf ("FcDirCacheReadUUID %s -> %s\n", uuidname, suuid);
- FcHashTableAdd (config->uuid_table, target, uuid);
- }
- }
- close (fd);
- }
- else
- {
- if (FcDebug () & FC_DBG_CACHE)
- printf ("FcDirCacheReadUUID Unable to read %s\n", uuidname);
- }
- FcStrFree (uuidname);
- }
- else
- FcHashUuidFree (u);
- FcStrFree (target);
-}
-#endif
-
struct MD5Context {
FcChar32 buf[4];
FcChar32 bits[2];
@@ -300,18 +150,26 @@ static const char bin2hex[] = { '0', '1', '2', '3',
'c', 'd', 'e', 'f' };
static FcChar8 *
-FcDirCacheBasenameMD5 (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN])
+FcDirCacheBasenameMD5 (FcConfig *config, const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN])
{
+ FcChar8 *new_dir;
unsigned char hash[16];
FcChar8 *hex_hash;
int cnt;
struct MD5Context ctx;
+ new_dir = FcConfigMapFontPath(config, dir);
+ if (new_dir)
+ dir = new_dir;
+
MD5Init (&ctx);
MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
MD5Final (hash, &ctx);
+ if (new_dir)
+ FcStrFree(new_dir);
+
cache_base[0] = '/';
hex_hash = cache_base + 1;
for (cnt = 0; cnt < 16; ++cnt)
@@ -325,31 +183,6 @@ FcDirCacheBasenameMD5 (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN])
return cache_base;
}
-#ifndef _WIN32
-static FcChar8 *
-FcDirCacheBasenameUUID (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN], FcConfig *config)
-{
- void *u;
- FcChar8 *target;
- const FcChar8 *sysroot = FcConfigGetSysRoot (config);
-
- if (sysroot)
- target = FcStrBuildFilename (sysroot, dir, NULL);
- else
- target = FcStrdup (dir);
- if (FcHashTableFind (config->uuid_table, target, &u))
- {
- uuid_unparse (u, (char *) cache_base);
- strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
- FcHashUuidFree (u);
- FcStrFree (target);
- return cache_base;
- }
- FcStrFree (target);
- return NULL;
-}
-#endif
-
FcBool
FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
{
@@ -359,10 +192,7 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
FcChar8 *cache_dir;
const FcChar8 *sysroot = FcConfigGetSysRoot (config);
-#ifndef _WIN32
- if (!FcDirCacheBasenameUUID (dir, cache_base, config))
-#endif
- FcDirCacheBasenameMD5 (dir, cache_base);
+ FcDirCacheBasenameMD5 (config, dir, cache_base);
list = FcStrListCreate (config->cacheDirs);
if (!list)
@@ -439,10 +269,7 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
}
FcStrFree (d);
-#ifndef _WIN32
- if (!FcDirCacheBasenameUUID (dir, cache_base, config))
-#endif
- FcDirCacheBasenameMD5 (dir, cache_base);
+ FcDirCacheBasenameMD5 (config, dir, cache_base);
list = FcStrListCreate (config->cacheDirs);
if (!list)
@@ -1070,9 +897,6 @@ FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
{
FcCache *cache = NULL;
-#ifndef _WIN32
- FcDirCacheReadUUID ((FcChar8 *) dir, config);
-#endif
if (!FcDirCacheProcess (config, dir,
FcDirCacheMapHelper,
&cache, cache_file))
@@ -1377,10 +1201,7 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
if (!cache_dir)
return FcFalse;
-#ifndef _WIN32
- if (!FcDirCacheBasenameUUID (dir, cache_base, config))
-#endif
- FcDirCacheBasenameMD5 (dir, cache_base);
+ FcDirCacheBasenameMD5 (config, dir, cache_base);
cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
FcStrFree (cache_dir);
if (!cache_hashed)
@@ -1577,10 +1398,7 @@ FcDirCacheLock (const FcChar8 *dir,
const FcChar8 *sysroot = FcConfigGetSysRoot (config);
int fd = -1;
-#ifndef _WIN32
- if (!FcDirCacheBasenameUUID (dir, cache_base, config))
-#endif
- FcDirCacheBasenameMD5 (dir, cache_base);
+ FcDirCacheBasenameMD5 (config, dir, cache_base);
list = FcStrListCreate (config->cacheDirs);
if (!list)
return -1;