]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccache.c
Add documentation for FcConfigNormalizeFontDir.
[fontconfig.git] / src / fccache.c
index e68259f1f0d9b1aa60f24faec75b5cfb0ee486c6..924494f24ee07637fc982d43f912f8a633e3d599 100644 (file)
@@ -37,7 +37,7 @@
 #define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
 
 static int
-FcDirCacheOpen (char * cache_file);
+FcDirCacheOpen (const FcChar8 * dir);
 
 static char *
 FcDirCacheHashName (char * cache_file, int collisions);
@@ -157,6 +157,7 @@ FcCacheWriteString (int fd, const char *chars)
 static void
 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
 {
+    FcStrSetDestroy (d->subdirs);
     FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
     free (d->name);
     FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
@@ -206,6 +207,7 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
     off_t              current_arch_start;
 
     struct stat        cache_stat, dir_stat;
+    char               subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
 
     if (stat ((char *) cache_file, &cache_stat) < 0)
         return;
@@ -264,6 +266,11 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
        d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
        d->ent = 0;
        d->offset = lseek (cache->fd, 0, SEEK_CUR);
+
+       d->subdirs = FcStrSetCreate();
+       while (strlen(FcCacheReadString (cache->fd, subdirName, sizeof (subdirName))) > 0)
+           FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
+
        if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
            goto bail1;
        targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
@@ -304,6 +311,9 @@ FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, con
     if (cache->fd == -1)
        return FcFalse;
 
+    if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
+       return FcFalse; /* non-existing directory */
+
     for (d = cache->dirs; d; d = d->next)
     {
        if (strncmp (d->name, dir, strlen(dir)) == 0)
@@ -321,11 +331,15 @@ FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, con
 
 FcBool
 FcGlobalCacheUpdate (FcGlobalCache  *cache,
+                    FcStrSet       *dirs,
                     const char     *name,
-                    FcFontSet      *set)
+                    FcFontSet      *set,
+                    FcConfig       *config)
 {
-    FcGlobalCacheDir * d;
+    FcGlobalCacheDir    *d;
+    int                        i;
 
+    name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)name);
     for (d = cache->dirs; d; d = d->next)
     {
        if (strcmp(d->name, name) == 0)
@@ -346,14 +360,18 @@ FcGlobalCacheUpdate (FcGlobalCache  *cache,
     d->name = (char *)FcStrCopy ((FcChar8 *)name);
     d->ent = FcDirCacheProduce (set, &d->metadata);
     d->offset = 0;
+    d->subdirs = FcStrSetCreate();
+    for (i = 0; i < dirs->num; i++)
+       FcStrSetAdd (d->subdirs, dirs->strs[i]);
     return FcTrue;
 }
 
 FcBool
 FcGlobalCacheSave (FcGlobalCache    *cache,
-                  const FcChar8    *cache_file)
+                  const FcChar8    *cache_file,
+                  FcConfig         *config)
 {
-    int                        fd, fd_orig;
+    int                        fd, fd_orig, i;
     FcGlobalCacheDir   *dir;
     FcAtomic           *atomic;
     off_t              current_arch_start = 0, truncate_to;
@@ -378,6 +396,7 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
               S_IRUSR | S_IWUSR);
     if (fd == -1)
        goto bail2;
+    FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
 
     fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
 
@@ -389,7 +408,12 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
                                                 current_arch_machine_name);
 
     if (current_arch_start < 0)
-       current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
+    {
+       off_t i = lseek(fd_orig, 0, SEEK_END);
+       if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
+           i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
+       current_arch_start = FcCacheNextOffset (i);
+    }
 
     if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
        goto bail3;
@@ -412,10 +436,13 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
        truncate_to += sizeof (FcCache);
        truncate_to = FcCacheNextOffset (truncate_to);
        truncate_to += dir->metadata.count;
+
+       for (i = 0; i < dir->subdirs->size; i++)
+           truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
+       truncate_to ++;
     }
     truncate_to -= current_arch_start;
 
-    FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
     sprintf (header, "%8x ", (int)truncate_to);
     strcat (header, current_arch_machine_name);
     if (!FcCacheWriteString (fd, header))
@@ -425,7 +452,14 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
     {
         if (dir->name)
         {
-            FcCacheWriteString (fd, dir->name);
+           const char * d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
+
+            FcCacheWriteString (fd, d);
+
+           for (i = 0; i < dir->subdirs->size; i++)
+               FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
+           FcCacheWriteString (fd, "");
+           
             write (fd, &dir->metadata, sizeof(FcCache));
             lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
             write (fd, dir->ent, dir->metadata.count);
@@ -593,26 +627,20 @@ FcCacheCopyOld (int fd, int fd_orig, off_t start)
 FcBool
 FcDirCacheValid (const FcChar8 *dir)
 {
-    FcChar8    *cache_file;
     struct stat file_stat, dir_stat;
     int        fd;
 
     if (stat ((char *) dir, &dir_stat) < 0)
         return FcFalse;
 
-    cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
-    if (!cache_file)
-       return FcFalse;
-
-    fd = FcDirCacheOpen ((char *)cache_file);
+    fd = FcDirCacheOpen (dir);
 
     if (fd < 0)
        goto bail;
     if (fstat (fd, &file_stat) < 0)
-       goto bail1;
+       goto bail;
 
     close (fd);
-    FcStrFree (cache_file);
 
     /*
      * If the directory has been modified more recently than
@@ -623,10 +651,8 @@ FcDirCacheValid (const FcChar8 *dir)
 
     return FcTrue;
 
- bail1:
-    close (fd);
  bail:
-    FcStrFree (cache_file);
+    close (fd);
     return FcFalse;
 }
 
@@ -635,16 +661,11 @@ FcDirCacheValid (const FcChar8 *dir)
 FcBool
 FcDirCacheHasCurrentArch (const FcChar8 *dir)
 {
-    char       *cache_file;
     int        fd;
     off_t      current_arch_start;
     char       *current_arch_machine_name;
 
-    cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
-    if (!cache_file)
-       return FcFalse;
-
-    fd = FcDirCacheOpen (cache_file);
+    fd = FcDirCacheOpen (dir);
     if (fd < 0)
        goto bail;
 
@@ -658,19 +679,23 @@ FcDirCacheHasCurrentArch (const FcChar8 *dir)
     return FcTrue;
 
  bail:
-    free (cache_file);
     return FcFalse;
 }
 
 FcBool
-FcDirCacheUnlink (const FcChar8 *dir)
+FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
 {
-    char       *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
-    char       *cache_hashed;
+    char       *cache_file;
+    char       *cache_hashed = 0;
     int                fd, collisions;
     struct stat        cache_stat;
     char       name_buf[FC_MAX_FILE_LEN];
 
+    dir = FcConfigNormalizeFontDir (config, dir);
+    cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+    if (!cache_file)
+       return FcFalse;
+
     /* First remove normal cache file. */
     if (stat ((char *) cache_file, &cache_stat) == 0 &&
        unlink ((char *)cache_file) != 0)
@@ -680,6 +705,9 @@ FcDirCacheUnlink (const FcChar8 *dir)
     fd = -1; collisions = 0;
     do
     {
+       if (cache_hashed)
+           FcStrFree ((FcChar8 *)cache_hashed);
+
        cache_hashed = FcDirCacheHashName (cache_file, collisions++);
        if (!cache_hashed)
            goto bail;
@@ -695,7 +723,10 @@ FcDirCacheUnlink (const FcChar8 *dir)
 
        FcCacheReadString (fd, name_buf, sizeof (name_buf));
        if (!strlen(name_buf))
+       {
+           FcStrFree ((FcChar8 *)cache_hashed);
            goto bail;
+       }
     } while (strcmp (name_buf, cache_file) != 0);
 
     FcStrFree ((FcChar8 *)cache_file);
@@ -870,21 +901,37 @@ FcDirCacheHashName (char * cache_file, int collisions)
  * This would fail in the unlikely event of a collision and subsequent
  * removal of the file which originally caused the collision. */
 static int
-FcDirCacheOpen (char *cache_file)
+FcDirCacheOpen (const FcChar8 *dir)
 {
+    FcBool     found;
     int                fd = -1, collisions = 0;
-    char       *cache_hashed;
+    char       *cache_file, *cache_hashed;
     char       name_buf[FC_MAX_FILE_LEN];
+    struct stat dir_stat;
+
+    cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+    if (!cache_file)
+       return -1;
 
     fd = open(cache_file, O_RDONLY);
     if (fd != -1)
        return fd;
 
+    if (stat ((char *)dir, &dir_stat) == -1)
+       return -1;
+
+    found = FcFalse;
     do
     {
+       struct stat c;
+       FcChar8 * name_buf_dir;
+
        cache_hashed = FcDirCacheHashName (cache_file, collisions++);
        if (!cache_hashed)
+       {
+           FcStrFree ((FcChar8 *)cache_file);
            return -1;
+       }
 
        if (fd > 0)
            close (fd);
@@ -892,14 +939,28 @@ FcDirCacheOpen (char *cache_file)
        FcStrFree ((FcChar8 *)cache_hashed);
 
        if (fd == -1)
+       {
+           FcStrFree ((FcChar8 *)cache_file);
            return -1;
+       }
        FcCacheReadString (fd, name_buf, sizeof (name_buf));
        if (!strlen(name_buf))
            goto bail;
-    } while (strcmp (name_buf, cache_file) != 0);
+
+       name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
+       if (stat ((char *)name_buf_dir, &c) == -1)
+       {
+           FcStrFree (name_buf_dir);
+           continue;
+       }
+       FcStrFree (name_buf_dir);
+       found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
+    } while (!found);
+    FcStrFree ((FcChar8 *)cache_file);
     return fd;
 
  bail:
+    FcStrFree ((FcChar8 *)cache_file);
     close (fd);
     return -1;
 }
@@ -908,18 +969,13 @@ FcDirCacheOpen (char *cache_file)
 FcBool
 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
 {
-    char       *cache_file;
     int        fd;
     char       *current_arch_machine_name;
     char       candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
     off_t      current_arch_start = 0;
     char       subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
 
-    cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
-    if (!cache_file)
-       return FcFalse;
-
-    fd = FcDirCacheOpen (cache_file);
+    fd = FcDirCacheOpen (dir);
     if (fd < 0)
        goto bail;
 
@@ -941,13 +997,11 @@ FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *
        goto bail1;
        
     close(fd);
-    free (cache_file);
     return FcTrue;
 
  bail1:
     close (fd);
  bail:
-    free (cache_file);
     return FcFalse;
 }
 
@@ -966,6 +1020,8 @@ FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
     {
        pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
        lseek (fd, pos, SEEK_SET);
+       if (config)
+           FcConfigAddFontDir (config, (FcChar8 *)dir);
        return FcTrue;
     }
 
@@ -1131,7 +1187,12 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
             FcCacheSkipToArch(fd_orig, current_arch_machine_name);
 
     if (current_arch_start < 0)
-       current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
+    {
+       off_t i = lseek(fd_orig, 0, SEEK_END);
+       if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
+           i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
+       current_arch_start = FcCacheNextOffset (i);
+    }
 
     if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
        goto bail4;
@@ -1175,10 +1236,12 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
     if (ftruncate (fd, current_arch_start + truncate_to) == -1)
        goto bail5;
 
+    free (header);
     close(fd);
     if (!FcAtomicReplaceOrig(atomic))
         goto bail5;
     FcStrFree ((FcChar8 *)cache_hashed);
+    FcStrFree ((FcChar8 *)cache_file);
     FcAtomicUnlock (atomic);
     FcAtomicDestroy (atomic);
     return FcTrue;
@@ -1195,7 +1258,7 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
     FcStrFree ((FcChar8 *)cache_hashed);
  bail0:
     unlink ((char *)cache_file);
-    free (cache_file);
+    FcStrFree ((FcChar8 *)cache_file);
     if (current_dir_block)
         free (current_dir_block);
  bail: