]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccache.c
fc-cache: add a --root option
[fontconfig.git] / src / fccache.c
index c38a7050edd837cba58165ab42614ec7224d85df..e9f57f4ed736cf009dc2388b175457a1e73e4b49 100644 (file)
@@ -87,7 +87,7 @@ typedef __int64 INT64;
  */
 
 int
-FcStat (const FcChar8 *file, struct stat *statb)
+FcStat (FcConfig *config, const FcChar8 *file, struct stat *statb)
 {
     WIN32_FILE_ATTRIBUTE_DATA wfad;
     char full_path_name[MAX_PATH];
@@ -136,9 +136,18 @@ FcStat (const FcChar8 *file, struct stat *statb)
 #else
 
 int
-FcStat (const FcChar8 *file, struct stat *statb)
+FcStat (FcConfig *config, const FcChar8 *file, struct stat *statb)
 {
-  return stat ((char *) file, statb);
+    int ret;
+    FcChar8 *fullFile = FcConfigGetRootPlus (config, file);
+
+    if (fullFile)
+       file = fullFile;
+    ret = stat ((char *) file, statb);
+    if (fullFile)
+       FcStrFree (fullFile);
+
+    return ret;
 }
 
 #endif
@@ -204,15 +213,21 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
 }
 
 static int
-FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
+FcDirCacheOpenFile (FcConfig *config, const FcChar8 *cache_file, struct stat *file_stat)
 {
     int        fd;
+    FcChar8 *fullFile;
 
 #ifdef _WIN32
-    if (FcStat (cache_file, file_stat) < 0)
+    if (FcStat (config, cache_file, file_stat) < 0)
         return -1;
 #endif
-    fd = open((char *) cache_file, O_RDONLY | O_BINARY);
+    fullFile = FcConfigGetRootPlus (config, cache_file);
+    if (fullFile)
+       cache_file = fullFile;
+    fd = open ((char *) cache_file, O_RDONLY | O_BINARY);
+    if (fullFile)
+       FcStrFree (fullFile);
     if (fd < 0)
        return fd;
 #ifndef _WIN32
@@ -232,7 +247,7 @@ FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
  */
 static FcBool
 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
-                  FcBool (*callback) (int fd, struct stat *fd_stat,
+                  FcBool (*callback) (FcConfig *config, int fd, struct stat *fd_stat,
                                       struct stat *dir_stat, void *closure),
                   void *closure, FcChar8 **cache_file_ret)
 {
@@ -243,7 +258,7 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
     struct stat file_stat, dir_stat;
     FcBool     ret = FcFalse;
 
-    if (FcStat (dir, &dir_stat) < 0)
+    if (FcStat (config, dir, &dir_stat) < 0)
         return FcFalse;
 
     FcDirCacheBasename (dir, cache_base);
@@ -257,9 +272,9 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
         FcChar8        *cache_hashed = FcStrPlus (cache_dir, cache_base);
         if (!cache_hashed)
            break;
-        fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
+        fd = FcDirCacheOpenFile (config, cache_hashed, &file_stat);
         if (fd >= 0) {
-           ret = (*callback) (fd, &file_stat, &dir_stat, closure);
+           ret = (*callback) (config, fd, &file_stat, &dir_stat, closure);
            close (fd);
            if (ret)
            {
@@ -519,13 +534,13 @@ FcCacheFini (void)
 }
 
 static FcBool
-FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
+FcCacheTimeValid (FcConfig *config, FcCache *cache, struct stat *dir_stat)
 {
     struct stat        dir_static;
 
     if (!dir_stat)
     {
-       if (FcStat (FcCacheDir (cache), &dir_static) < 0)
+       if (FcStat (config, FcCacheDir (cache), &dir_static) < 0)
            return FcFalse;
        dir_stat = &dir_static;
     }
@@ -539,7 +554,7 @@ FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
  * Map a cache file into memory
  */
 static FcCache *
-FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
+FcDirCacheMapFd (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat)
 {
     FcCache    *cache;
     FcBool     allocated = FcFalse;
@@ -549,7 +564,7 @@ FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
     cache = FcCacheFindByStat (fd_stat);
     if (cache)
     {
-       if (FcCacheTimeValid (cache, dir_stat))
+       if (FcCacheTimeValid (config, cache, dir_stat))
            return cache;
        FcDirCacheUnload (cache);
        cache = NULL;
@@ -597,7 +612,7 @@ FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
     if (cache->magic != FC_CACHE_MAGIC_MMAP ||
        cache->version < FC_CACHE_CONTENT_VERSION ||
        cache->size != fd_stat->st_size ||
-       !FcCacheTimeValid (cache, dir_stat) ||
+       !FcCacheTimeValid (config, cache, dir_stat) ||
        !FcCacheInsert (cache, fd_stat))
     {
        if (allocated)
@@ -636,9 +651,9 @@ FcDirCacheUnload (FcCache *cache)
 }
 
 static FcBool
-FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
+FcDirCacheMapHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
 {
-    FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
+    FcCache *cache = FcDirCacheMapFd (config, fd, fd_stat, dir_stat);
 
     if (!cache)
        return FcFalse;
@@ -660,6 +675,12 @@ FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
 
 FcCache *
 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
+{
+    return FcDirCacheLoadFile2 (cache_file, FcConfigGetCurrent (), file_stat);
+}
+
+FcCache *
+FcDirCacheLoadFile2 (const FcChar8 *cache_file, FcConfig *config, struct stat *file_stat)
 {
     int        fd;
     FcCache *cache;
@@ -667,10 +688,10 @@ FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
 
     if (!file_stat)
        file_stat = &my_file_stat;
-    fd = FcDirCacheOpenFile (cache_file, file_stat);
+    fd = FcDirCacheOpenFile (config, cache_file, file_stat);
     if (fd < 0)
        return NULL;
-    cache = FcDirCacheMapFd (fd, file_stat, NULL);
+    cache = FcDirCacheMapFd (config, fd, file_stat, NULL);
     close (fd);
     return cache;
 }
@@ -680,7 +701,7 @@ FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
  * the magic number and the size field
  */
 static FcBool
-FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
+FcDirCacheValidateHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
 {
     FcBool  ret = FcTrue;
     FcCache    c;
@@ -854,6 +875,8 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
     FcStrList      *list;
     FcChar8        *cache_dir = NULL;
     FcChar8        *test_dir;
+    FcChar8        *full_test_dir;
+    FcBool         free_test_dir;
     FcCacheSkip     *skip;
     struct stat     cache_stat;
     int                    magic;
@@ -866,7 +889,19 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
     list = FcStrListCreate (config->cacheDirs);
     if (!list)
        return FcFalse;
+    free_test_dir = FcFalse;
     while ((test_dir = FcStrListNext (list))) {
+       if (free_test_dir)
+           FcStrFree (full_test_dir);
+       free_test_dir = FcFalse;
+
+       full_test_dir = FcConfigGetRootPlus (config, test_dir);
+       if (full_test_dir)
+       {
+           free_test_dir = FcTrue;
+           test_dir = full_test_dir;
+       }
+
        if (access ((char *) test_dir, W_OK|X_OK) == 0)
        {
            cache_dir = test_dir;
@@ -896,12 +931,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",
@@ -911,7 +946,7 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
     if (!atomic)
        goto bail1;
 
-    if (!FcAtomicLock (atomic))
+    if (!FcAtomicLock2 (config, atomic))
        goto bail3;
 
     fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
@@ -948,7 +983,7 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
      */
     if (cache->size < FC_CACHE_MIN_MMAP &&
        (skip = FcCacheFindByAddr (cache)) &&
-       FcStat (cache_hashed, &cache_stat))
+       FcStat (config, cache_hashed, &cache_stat))
     {
        skip->cache_dev = cache_stat.st_dev;
        skip->cache_ino = cache_stat.st_ino;
@@ -968,6 +1003,9 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
     FcAtomicDestroy (atomic);
  bail1:
     FcStrFree (cache_hashed);
+ bail0:
+    if (free_test_dir)
+       FcStrFree (full_test_dir);
     return FcFalse;
 }