]> git.wh0rd.org - fontconfig.git/commitdiff
Fix up fc-cache and fc-cat for no global cache changes.
authorKeith Packard <keithp@neko.keithp.com>
Mon, 28 Aug 2006 05:24:39 +0000 (22:24 -0700)
committerKeith Packard <keithp@neko.keithp.com>
Mon, 28 Aug 2006 05:24:39 +0000 (22:24 -0700)
fc-cache and fc-cat use internal (fcint.h) APIs that have
changed with the elimination of the global cache.

fc-cache/fc-cache.c
fc-cat/fc-cat.c
src/fccache.c
src/fcdir.c
src/fcint.h

index 281f8359029bbbfc7810f055bba4b466b1f06e07..134d33dbb8d3244e33c8ce0fe931708738206ed4 100644 (file)
@@ -219,7 +219,7 @@ scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool
        if (really_force)
            FcDirCacheUnlink (dir, config);
 
-       if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
+       if (!FcDirScanConfig (set, subdirs, FcConfigGetBlanks (config), dir, force, config))
        {
            fprintf (stderr, "\"%s\": error scanning\n", dir);
            FcFontSetDestroy (set);
index cf3632325f4fd7a1e924e429cf2db1d566ed0d9a..36f8bd9865047549fd1399cd969ffaa0b2a3dc9c 100644 (file)
@@ -167,127 +167,37 @@ usage (char *program)
     exit (1);
 }
 
-static FcBool 
-FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char *cache_file)
-{
-    char               name_buf[8192];
-    int fd;
-    char * current_arch_machine_name;
-    char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
-    char               subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
-    off_t current_arch_start = 0;
-
-    if (!cache_file)
-       goto bail;
-
-    current_arch_machine_name = FcCacheMachineSignature();
-    fd = open(cache_file, O_RDONLY);
-    if (fd == -1)
-       goto bail;
-
-    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
-    if (current_arch_start < 0)
-       goto bail1;
-
-    lseek (fd, current_arch_start, SEEK_SET);
-    if (FcCacheReadString (fd, candidate_arch_machine_name, 
-                          sizeof (candidate_arch_machine_name)) == 0)
-       goto bail1;
-
-    while (1) 
-    {
-       char * dir;
-       FcCacheReadString (fd, name_buf, sizeof (name_buf));
-       if (!strlen(name_buf))
-           break;
-       printf ("fc-cat: printing global cache contents for dir %s\n", 
-               name_buf);
-
-       do
-       {
-           if (!FcCacheReadString (fd, subdirName, 
-                                   sizeof (subdirName)) ||
-               !strlen (subdirName))
-               break;
-           /* then don't do anything with subdirName. */
-       } while (1);
-
-       if (!FcDirCacheConsume (fd, name_buf, set, 0))
-           goto bail1;
-
-       dir = malloc (strlen (name_buf) + 2);
-       if (!dir)
-           goto bail1;
-
-       strcpy (dir, name_buf);
-       strcat (dir, "/");
-
-       FcCachePrintSet (set, dirs, dir);
-       free (dir);
-
-       FcFontSetDestroy (set);
-       set = FcFontSetCreate();
-    }
-
- bail1:
-    close (fd);
- bail:
-    return FcFalse;
-}
-
 /* read serialized state from the cache file */
 static char *
 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
 {
+    FILE *file;
     int fd;
-    char * current_arch_machine_name;
-    off_t current_arch_start = 0;
     char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
-    static char name_buf[8192], *dir;
+    static char name_buf[8192];
     FcChar8 * ls;
     char * buf;
+    int i;
 
     if (!cache_file)
         goto bail;
 
-    current_arch_machine_name = FcCacheMachineSignature();
-    fd = open(cache_file, O_RDONLY);
-    if (fd == -1)
+    file = fopen(cache_file, "rb");
+    if (file == NULL)
         goto bail;
 
-    FcCacheReadString (fd, name_buf, sizeof (name_buf));
-    if (!strlen (name_buf))
-       goto bail;
-    if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0)
-       goto bail;
+    if (!FcDirCacheConsume (file, set, dirs, NULL, name_buf))
+       goto bail1;
+    
+    fclose (file);
+    
     printf ("fc-cat: printing directory cache for cache which would be named %s\n", 
            name_buf);
 
-    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
-    if (current_arch_start < 0)
-        goto bail1;
-
-    while ((buf = FcCacheReadString (fd, subdirName, sizeof (subdirName))) 
-          && *buf)
-        FcStrSetAdd (dirs, (FcChar8 *)subdirName);
-
-    dir = strdup(name_buf);
-    ls = FcStrLastSlash ((FcChar8 *)dir);
-    if (ls)
-       *ls = 0;
-
-    if (!FcDirCacheConsume (fd, dir, set, 0))
-       goto bail2;
-    free (dir);
-
-    close(fd);
     return name_buf;
 
- bail2:
-    free (dir);
-
  bail1:
-    close (fd);
+    fclose (file);
  bail:
     return 0;
 }
@@ -425,7 +335,7 @@ main (int argc, char **argv)
     {
         char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i], 
                                                (FcChar8 *)"/dummy");
-        if (!FcDirScanConfig (fs, dirs, 0, 0, 
+        if (!FcDirScanConfig (fs, dirs, 0, 
                               (const FcChar8 *)argv[i], FcFalse, config))
             fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
         else
@@ -439,10 +349,7 @@ main (int argc, char **argv)
        FcCachePrintSet (fs, dirs, name_buf);
     else
     {
-        FcStrSetDestroy (dirs);
-        dirs = FcStrSetCreate ();
-        if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
-            ;
+       printf ("nothing to do\n");
     }
 
     FcStrSetDestroy (dirs);
index 295ee7750abc21ac215a395b9839f33287dbee36..a67725341a1a442e5eb82532fcf8c0b5aff13b45 100644 (file)
@@ -132,7 +132,7 @@ FcDirCacheValid (const FcChar8 *dir, FcConfig *config)
 
     file = FcDirCacheOpen (config, dir, NULL);
 
-    if (file != NULL)
+    if (file == NULL)
        return FcFalse;
     fclose (file);
 
@@ -334,26 +334,31 @@ FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, FcChar8 **cache_path)
 
 /* read serialized state from the cache file */
 FcBool
-FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, 
-               const FcChar8 *dir, FcConfig *config)
+FcDirCacheConsume (FILE *file, FcFontSet *set, FcStrSet *dirs,
+                  const FcChar8 *dir, char *dirname)
 {
-    FILE       *file;
     FcCache    metadata;
     void       *current_dir_block;
     char       subdir_name[FC_MAX_FILE_LEN + 1 + 12 + 1];
-
-    file = FcDirCacheOpen (config, dir, NULL);
-    if (file == NULL)
-       goto bail;
+    int                i;
 
     if (fread(&metadata, sizeof(FcCache), 1, file) != 1)
-       goto bail1;
+       goto bail;
     if (metadata.magic != FC_CACHE_MAGIC)
-        goto bail1;
+        goto bail;
 
-    while (FcCacheReadString (file, subdir_name, sizeof (subdir_name)) && 
-          strlen (subdir_name) > 0)
-        FcStrSetAdd (dirs, (FcChar8 *)subdir_name);
+    /* skip directory name; it's just for fc-cat */
+    if (!FcCacheReadString (file, subdir_name, sizeof (subdir_name)))
+       goto bail;
+    
+    if (dirname)
+       strcpy (dirname, subdir_name);
+
+    for (i = 0; i < metadata.subdirs; i++) {
+       if (!FcCacheReadString (file, subdir_name, sizeof (subdir_name)))
+           goto bail;
+       FcStrSetAdd (dirs, (FcChar8 *)subdir_name);
+    }
 
     if (metadata.count)
     {
@@ -362,52 +367,73 @@ FcDirCacheRead (FcFontSet * set, FcStrSet * dirs,
        current_dir_block = mmap (0, metadata.count, 
                                  PROT_READ, MAP_SHARED, fd, metadata.pos);
        if (current_dir_block == MAP_FAILED)
-           goto bail1;
+           goto bail;
 #elif defined(_WIN32)
        {
            HANDLE hFileMap;
 
            hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL);
            if (hFileMap == NULL)
-               goto bail1;
+               goto bail;
 
            current_dir_block = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, metadata.pos, metadata.count);
            if (current_dir_block == NULL)
            {
                CloseHandle (hFileMap);
-               goto bail1;
+               goto bail;
            }
        }
 #else
        if (lseek (fd, metatdata.pos, SEEK_SET) == -1)
-           goto bail1;
+           goto bail;
 
        current_dir_block = malloc (metadata.count);
        if (!current_dir_block)
-           goto bail1;
+           goto bail;
 
        /* could also use CreateMappedViewOfFile under MinGW... */
        if (read (fd, current_dir_block, metadata.count) != metadata.count)
        {
            free (current_dir_block);
-           goto bail1;
+           goto bail;
        }
 #endif
        FcCacheAddBankDir (metadata.bank, (char *) dir);
        if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
-           goto bail1;
+           goto bail;
     }
+
+    return FcTrue;
+
+ bail:
+    return FcFalse;
+}
+
+FcBool
+FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, 
+               const FcChar8 *dir, FcConfig *config)
+{
+    FILE    *file;
+
+    file = FcDirCacheOpen (config, dir, NULL);
+    if (file == NULL)
+       goto bail;
+
+    if (!FcDirCacheConsume (file, set, dirs, dir, NULL))
+       goto bail1;
+
     if (config)
        FcConfigAddFontDir (config, (FcChar8 *)dir);
-
-    fclose(file);
+    
+    fclose (file);
     return FcTrue;
 
- bail1:
+bail1:
     fclose (file);
- bail:
+bail:
     return FcFalse;
 }
+    
 
 static void *
 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
@@ -566,6 +592,7 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *c
      * Compute file header length -- the FcCache followed by the subdir names
      */
     header_len = sizeof (FcCache);
+    header_len += strlen ((char *) dir) + 1;
     for (i = 0; i < dirs->size; i++)
        header_len += strlen ((char *)dirs->strs[i]) + 1;
     
@@ -581,6 +608,8 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *c
        goto bail5;
     }
     
+    FcCacheWriteString (fd, (char *) dir);
+    
     for (i = 0; i < dirs->size; i++)
         FcCacheWriteString (fd, (char *)dirs->strs[i]);
 
index 62314a325984452c437d9987e6aad0b2d2c3a4ff..c15b7277b10216a9959847f60215150e91546620 100644 (file)
@@ -220,14 +220,11 @@ FcDirScanConfig (FcFontSet        *set,
     for (i = 0; i < filelist->num; i++)
        FcFileScanFontConfig (tmpSet, blanks, filelist->strs[i], config);
     
-    if (FcShouldWriteFiles ())
-    {
-       /*
-        * Now that the directory has been scanned,
-        * write out the cache file
-        */
-       FcDirCacheWrite (tmpSet, dirlist, dir, config);
-    }
+    /*
+     * Now that the directory has been scanned,
+     * write out the cache file
+     */
+    FcDirCacheWrite (tmpSet, dirlist, dir, config);
 
     /*
      * Add the discovered fonts to our internal non-cache list
index 494552ef44b241be96b9a75e479414e9e272ea07..cb137d389615646d802285ad37a09437a53867d3 100644 (file)
@@ -422,6 +422,10 @@ FcCacheRead (FcConfig *config);
 FcBool
 FcDirCacheWrite (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config);
 
+FcBool
+FcDirCacheConsume (FILE *file, FcFontSet *set, FcStrSet *dirs,
+                  const FcChar8 *dir, char *dirname);
+    
 FcBool
 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config);