]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccache.c
Update fc-cat to handle subdir lists in global cache file.
[fontconfig.git] / src / fccache.c
index 688da877ef1e66f45c2cb8a4e491498982389b65..3dd7eae3ba790c2025853a54d2cb9bd19df1eb9e 100644 (file)
@@ -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;
@@ -216,7 +218,8 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
 
     cache->updated = FcFalse;
 
-    FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
+    if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
+       return;
     if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
        return;
 
@@ -227,8 +230,9 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
         goto bail_and_destroy;
 
     lseek (cache->fd, current_arch_start, SEEK_SET);
-    FcCacheReadString (cache->fd, candidate_arch_machine_name, 
-                       sizeof (candidate_arch_machine_name));
+    if (!FcCacheReadString (cache->fd, candidate_arch_machine_name, 
+                           sizeof (candidate_arch_machine_name)))
+       goto bail_and_destroy;
     if (strlen(candidate_arch_machine_name) == 0)
        goto bail_and_destroy;
 
@@ -236,8 +240,8 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
     {
        off_t targ;
 
-       FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
-       if (!strlen(name_buf))
+       if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) || 
+           !strlen(name_buf))
            break;
 
        /* Directory must be older than the global cache file; also
@@ -264,6 +268,17 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
        d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
        d->ent = 0;
        d->offset = lseek (cache->fd, 0, SEEK_CUR);
+
+       d->subdirs = FcStrSetCreate();
+       do
+       {
+           if (!FcCacheReadString (cache->fd, subdirName, 
+                                   sizeof (subdirName)) ||
+               !strlen (subdirName))
+               break;
+           FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
+       } while (1);
+
        if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
            goto bail1;
        targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
@@ -304,13 +319,15 @@ FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, con
     if (cache->fd == -1)
        return FcFalse;
 
-    dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir);
+    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)
        {
            lseek (cache->fd, d->offset, SEEK_SET);
-           if (!FcDirCacheConsume (cache->fd, dir, set, config))
+           if (!FcDirCacheConsume (cache->fd, d->name, set, config))
                return FcFalse;
             if (strcmp (d->name, dir) == 0)
                ret = FcTrue;
@@ -322,11 +339,13 @@ FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, con
 
 FcBool
 FcGlobalCacheUpdate (FcGlobalCache  *cache,
+                    FcStrSet       *dirs,
                     const char     *name,
                     FcFontSet      *set,
                     FcConfig       *config)
 {
-    FcGlobalCacheDir * d;
+    FcGlobalCacheDir    *d;
+    int                        i;
 
     name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)name);
     for (d = cache->dirs; d; d = d->next)
@@ -349,6 +368,9 @@ 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;
 }
 
@@ -357,7 +379,7 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
                   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;
@@ -382,6 +404,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);
 
@@ -393,7 +416,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;
@@ -416,10 +444,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))
@@ -432,6 +463,11 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
            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);
@@ -693,8 +729,7 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
            return FcTrue;
        }
 
-       FcCacheReadString (fd, name_buf, sizeof (name_buf));
-       if (!strlen(name_buf))
+       if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
        {
            FcStrFree ((FcChar8 *)cache_hashed);
            goto bail;
@@ -900,7 +935,10 @@ FcDirCacheOpen (const FcChar8 *dir)
 
        cache_hashed = FcDirCacheHashName (cache_file, collisions++);
        if (!cache_hashed)
+       {
+           FcStrFree ((FcChar8 *)cache_file);
            return -1;
+       }
 
        if (fd > 0)
            close (fd);
@@ -908,9 +946,11 @@ FcDirCacheOpen (const FcChar8 *dir)
        FcStrFree ((FcChar8 *)cache_hashed);
 
        if (fd == -1)
+       {
+           FcStrFree ((FcChar8 *)cache_file);
            return -1;
-       FcCacheReadString (fd, name_buf, sizeof (name_buf));
-       if (!strlen(name_buf))
+       }
+       if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
            goto bail;
 
        name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
@@ -920,11 +960,13 @@ FcDirCacheOpen (const FcChar8 *dir)
            continue;
        }
        FcStrFree (name_buf_dir);
-       found = c.st_ino == dir_stat.st_ino;
+       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;
 }
@@ -954,7 +996,7 @@ FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *
                           sizeof (candidate_arch_machine_name)) == 0)
        goto bail1;
 
-    while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
+    while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
         FcStrSetAdd (dirs, (FcChar8 *)subdirName);
 
     if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
@@ -1094,11 +1136,12 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
        fd = open(cache_hashed, O_RDONLY);
        if (fd == -1)
            break;
-       FcCacheReadString (fd, name_buf, sizeof (name_buf));
-       close (fd);
-
-       if (!strlen(name_buf))
+       if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
+       {
+           close (fd);
            break;
+       }
+       close (fd);
 
        if (strcmp (name_buf, cache_file) != 0)
            continue;
@@ -1151,7 +1194,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;
@@ -1200,6 +1248,7 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
     if (!FcAtomicReplaceOrig(atomic))
         goto bail5;
     FcStrFree ((FcChar8 *)cache_hashed);
+    FcStrFree ((FcChar8 *)cache_file);
     FcAtomicUnlock (atomic);
     FcAtomicDestroy (atomic);
     return FcTrue;
@@ -1216,7 +1265,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: