]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccache.c
Fix flipped return value on unlink. (Reported by Mike Fabian)
[fontconfig.git] / src / fccache.c
index d001e2b255bec1d13a94d7336dc7ad7cd9d3f6bc..7ccb5295013f3a946146d0af273406faca06855b 100644 (file)
@@ -46,7 +46,7 @@ FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
 static FcBool
 FcDirCacheConsume (int fd, FcFontSet *set);
 
-static FcBool
+FcBool
 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
 
 static int
@@ -517,6 +517,7 @@ FcCacheCopyOld (int fd, int fd_orig, off_t start)
     return FcFalse;
 }
 
+/* Does not check that the cache has the appropriate arch section. */
 FcBool
 FcDirCacheValid (const FcChar8 *dir)
 {
@@ -533,6 +534,7 @@ FcDirCacheValid (const FcChar8 *dir)
         FcStrFree (cache_file);
         return FcFalse;
     }
+
     FcStrFree (cache_file);
     /*
      * If the directory has been modified more recently than
@@ -543,6 +545,45 @@ FcDirCacheValid (const FcChar8 *dir)
     return FcTrue;
 }
 
+/* Assumes that the cache file in 'dir' exists.
+ * Checks that the cache has the appropriate arch section. */
+FcBool
+FcDirCacheHasCurrentArch (const FcChar8 *dir)
+{
+    FcChar8     *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+    int        fd;
+    off_t      current_arch_start;
+    char       *current_arch_machine_name;
+
+    current_arch_machine_name = FcCacheMachineSignature();
+    fd = open ((char *)cache_file, O_RDONLY);
+    if (fd == -1)
+        return FcFalse;
+
+    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
+    close (fd);
+
+    if (current_arch_start < 0)
+        return FcFalse;
+    
+    return FcTrue;
+}
+
+FcBool
+FcDirCacheUnlink (const FcChar8 *dir)
+{
+    FcChar8     *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+
+    if (unlink ((char *)cache_file) != 0)
+    {
+       FcStrFree (cache_file);
+        return FcFalse;
+    }
+
+    FcStrFree (cache_file);
+    return FcTrue;
+}
+
 static int
 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache, 
                 FcStrList *list, FcFontSet * set)
@@ -651,7 +692,7 @@ FcCacheRead (FcConfig *config, FcGlobalCache * cache)
 }
 
 /* read serialized state from the cache file */
-static FcBool
+FcBool
 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
 {
     char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
@@ -910,7 +951,7 @@ FcCacheMachineSignature ()
 }
 
 static int banks_ptr = 0, banks_alloc = 0;
-static int * bankId = 0;
+static int * bankId = 0, * bankIdx = 0;
 
 static FcBool
 FcCacheHaveBank (int bank)
@@ -930,28 +971,37 @@ FcCacheHaveBank (int bank)
 int
 FcCacheBankToIndex (int bank)
 {
-    static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1;
-    int i;
-    int * b;
-
-    if (bank == lastBank)
-       return lastIndex;
+    int i, j;
 
     for (i = 0; i < banks_ptr; i++)
-       if (bankId[i] == bank)
-           return i;
+       if (bankId[bankIdx[i]] == bank)
+       {
+           int t = bankIdx[i];
+
+           for (j = i; j > 0; j--)
+               bankIdx[j] = bankIdx[j-1];
+           bankIdx[0] = t;
+           return t;
+       }
 
     if (banks_ptr >= banks_alloc)
     {
+       int * b, * bidx;
        b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
        if (!b)
            return -1;
-
        bankId = b;
+
+       bidx = realloc (bankIdx, (banks_alloc + 4) * sizeof(int));
+       if (!bidx)
+           return -1;
+       bankIdx = bidx;
+
        banks_alloc += 4;
     }
 
     i = banks_ptr++;
     bankId[i] = bank;
+    bankIdx[i] = i;
     return i;
 }