]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcdir.c
Overhaul the serialization system to create one mmapable file per directory
[fontconfig.git] / src / fcdir.c
index cc8a9b1dfa1d1dc8fa3873164ebd8b5843aee1d3..4f69f228bc0688fb98fb65df81b13d3a597f3fb1 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $XFree86: xc/lib/fontconfig/src/fcdir.c,v 1.5 2002/05/21 17:06:22 keithp Exp $
+ * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $
  *
- * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
+ * Copyright © 2000 Keith Packard
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -25,7 +25,7 @@
 #include "fcint.h"
 #include <dirent.h>
 
-static FcBool
+FcBool
 FcFileIsDir (const FcChar8 *file)
 {
     struct stat            statb;
@@ -36,46 +36,82 @@ FcFileIsDir (const FcChar8 *file)
 }
 
 FcBool
-FcFileScan (FcFontSet      *set,
-           FcStrSet        *dirs,
-           FcFileCache     *cache,
-           FcBlanks        *blanks,
-           const FcChar8   *file,
-           FcBool          force)
+FcFileScanConfig (FcFontSet    *set,
+                 FcStrSet      *dirs,
+                 FcGlobalCache *cache,
+                 FcBlanks      *blanks,
+                 const FcChar8 *file,
+                 FcBool        force,
+                 FcConfig      *config)
 {
-    int                    id;
-    FcChar8        *name;
-    FcPattern      *font;
-    FcBool         ret = FcTrue;
-    FcBool         isDir;
-    int                    count;
+    int                        id;
+#if 0
+    FcChar8            *name;
+    FcGlobalCacheFile  *cache_file;
+    FcGlobalCacheDir   *cache_dir;
+#endif
+    FcPattern          *font;
+    FcBool             ret = FcTrue;
+    FcBool             isDir;
+    int                        count = 0;
+    FcBool             need_scan;
     
+    if (config && !FcConfigAcceptFilename (config, file))
+       return FcTrue;
+
+    if (force)
+       cache = 0;
     id = 0;
     do
     {
-       if (!force && cache)
-           name = FcFileCacheFind (cache, file, id, &count);
-       else
-           name = 0;
-       if (name)
+       need_scan = FcTrue;
+       font = 0;
+#if 0
+       /*
+        * Check the cache
+        */
+       if (cache)
        {
-           /* "." means the file doesn't contain a font */
-           if (FcStrCmp (name, FC_FONT_FILE_INVALID) == 0)
-               font = 0;
-           else if (FcStrCmp (name, FC_FONT_FILE_DIR) == 0)
+           if ((cache_file = FcGlobalCacheFileGet (cache, file, id, &count)))
            {
-               ret = FcStrSetAdd (dirs, file);
-               font = 0;
+               /*
+                * Found a cache entry for the file
+                */
+               if (FcGlobalCacheCheckTime (file, &cache_file->info))
+               {
+                   name = cache_file->name;
+                   need_scan = FcFalse;
+                   FcGlobalCacheReferenced (cache, &cache_file->info);
+                   /* "." means the file doesn't contain a font */
+                   if (FcStrCmp (name, FC_FONT_FILE_INVALID) != 0)
+                   {
+                       font = FcNameParse (name);
+                       if (font)
+                           if (!FcPatternAddString (font, FC_FILE, file))
+                               ret = FcFalse;
+                   }
+               }
            }
-           else
+           else if ((cache_dir = FcGlobalCacheDirGet (cache, file,
+                                                      strlen ((const char *) file),
+                                                      FcFalse)))
            {
-               font = FcNameParse (name);
-               if (font)
-                   if (!FcPatternAddString (font, FC_FILE, file))
+               if (FcGlobalCacheCheckTime (cache_dir->info.file, 
+                                           &cache_dir->info))
+               {
+                   font = 0;
+                   need_scan = FcFalse;
+                   FcGlobalCacheReferenced (cache, &cache_dir->info);
+                   if (!FcStrSetAdd (dirs, file))
                        ret = FcFalse;
+               }
            }
        }
-       else
+#endif
+       /*
+        * Nothing in the cache, scan the file
+        */
+       if (need_scan)
        {
            if (FcDebug () & FC_DBG_SCAN)
            {
@@ -91,36 +127,27 @@ FcFileScan (FcFontSet          *set,
                isDir = FcTrue;
                ret = FcStrSetAdd (dirs, file);
            }
-           if (!force && cache)
+#if 0
+           /*
+            * Update the cache
+            */
+           if (cache && font)
            {
-               if (font)
-               {
-                   FcChar8     *unparse;
+               FcChar8 *unparse;
 
-                   unparse = FcNameUnparse (font);
-                   if (unparse)
-                   {
-                       (void) FcFileCacheUpdate (cache, file, id, unparse);
-                       free (unparse);
-                   }
-               }
-               else
+               unparse = FcNameUnparse (font);
+               if (unparse)
                {
-                   if (isDir)
-                   {
-                       FcFileCacheUpdate (cache, file, id, (FcChar8 *) 
-                                          FC_FONT_FILE_DIR);
-                   }
-                   else
-                   {
-                       /* negative cache files not containing fonts */
-                       FcFileCacheUpdate (cache, file, id, (FcChar8 *) 
-                                          FC_FONT_FILE_INVALID);
-                   }
+                   (void) FcGlobalCacheUpdate (cache, file, id, unparse);
+                   FcStrFree (unparse);
                }
            }
+#endif
        }
-       if (font)
+       /*
+        * Add the font
+        */
+       if (font && (!config || FcConfigAcceptFont (config, font)))
        {
            if (!FcFontSetAdd (set, font))
            {
@@ -129,64 +156,82 @@ FcFileScan (FcFontSet         *set,
                ret = FcFalse;
            }
        }
+       else if (font)
+           FcPatternDestroy (font);
        id++;
     } while (font && ret && id < count);
     return ret;
 }
 
 FcBool
-FcDirCacheValid (const FcChar8 *dir)
+FcFileScan (FcFontSet      *set,
+           FcStrSet        *dirs,
+           FcGlobalCache   *cache,
+           FcBlanks        *blanks,
+           const FcChar8   *file,
+           FcBool          force)
 {
-    FcChar8 *path;
-    FcBool  ret;
-
-    path = (FcChar8 *) malloc (strlen ((const char *) dir) + 1 + 
-                              strlen ((const char *) FC_DIR_CACHE_FILE) + 1);
-    if (!path)
-       return FcFalse;
-    strcpy ((char *) path, (const char *) dir);
-    strcat ((char *) path, (const char *) "/");
-    strcat ((char *) path, (const char *) FC_DIR_CACHE_FILE);
-    ret = FcFileCacheValid (path);
-    free (path);
-    return ret;
+    return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0);
 }
 
-#define FC_MAX_FILE_LEN            4096
+/*
+ * Scan 'dir', adding font files to 'set' and
+ * subdirectories to 'dirs'
+ */
 
 FcBool
-FcDirScan (FcFontSet       *set,
-          FcStrSet         *dirs,
-          FcFileCache      *cache,
-          FcBlanks         *blanks,
-          const FcChar8    *dir,
-          FcBool           force)
+FcDirScanConfig (FcFontSet     *set,
+                FcStrSet       *dirs,
+                FcGlobalCache  *cache,
+                FcBlanks       *blanks,
+                const FcChar8  *dir,
+                FcBool         force,
+                FcConfig       *config)
 {
-    DIR                    *d;
-    struct dirent   *e;
-    FcChar8        *file;
-    FcChar8        *base;
-    FcBool         ret = FcTrue;
+    DIR                        *d;
+    struct dirent      *e;
+    FcChar8            *file;
+    FcChar8            *base;
+    FcBool             ret = FcTrue;
 
-    file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
-    if (!file)
-       return FcFalse;
+    if (config && !FcConfigAcceptFilename (config, dir))
+       return FcTrue;
 
-    strcpy ((char *) file, (char *) dir);
-    strcat ((char *) file, "/");
-    base = file + strlen ((char *) file);
     if (!force)
     {
-       strcpy ((char *) base, FC_DIR_CACHE_FILE);
-       
-       if (FcFileCacheReadDir (set, dirs, file))
+#if 0
+       /*
+        * Check fonts.cache-<version> file
+        */
+       if (FcDirCacheReadDir (set, dirs, dir, config))
        {
-           free (file);
+           if (cache)
+               FcGlobalCacheReferenceSubdir (cache, dir);
            return FcTrue;
        }
+    
+       /*
+        * Check ~/.fonts.cache-<version> file
+        */
+       if (cache && FcGlobalCacheScanDir (set, dirs, cache, dir, config))
+           return FcTrue;
+#endif
     }
     
+    /* freed below */
+    file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
+    if (!file)
+       return FcFalse;
+
+    strcpy ((char *) file, (char *) dir);
+    strcat ((char *) file, "/");
+    base = file + strlen ((char *) file);
+    
+    if (FcDebug () & FC_DBG_SCAN)
+       printf ("\tScanning dir %s\n", dir);
+       
     d = opendir ((char *) dir);
+    
     if (!d)
     {
        free (file);
@@ -200,30 +245,46 @@ FcDirScan (FcFontSet          *set,
        if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
        {
            strcpy ((char *) base, (char *) e->d_name);
-           ret = FcFileScan (set, dirs, cache, blanks, file, force);
+           ret = FcFileScanConfig (set, dirs, cache, blanks, file, force, config);
        }
     }
     free (file);
     closedir (d);
+    /*
+     * Now that the directory has been scanned,
+     * add the cache entry 
+     */
+#if 0
+    if (ret && cache)
+       FcGlobalCacheUpdate (cache, dir, 0, 0);
+#endif
+       
     return ret;
 }
 
 FcBool
-FcDirSave (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
+FcDirScan (FcFontSet       *set,
+          FcStrSet         *dirs,
+          FcGlobalCache    *cache,
+          FcBlanks         *blanks,
+          const FcChar8    *dir,
+          FcBool           force)
 {
-    FcChar8        *file;
-    FcChar8        *base;
-    FcBool         ret;
-    
-    file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + 256 + 1);
-    if (!file)
-       return FcFalse;
+    return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
+}
 
-    strcpy ((char *) file, (char *) dir);
-    strcat ((char *) file, "/");
-    base = file + strlen ((char *) file);
-    strcpy ((char *) base, FC_DIR_CACHE_FILE);
-    ret = FcFileCacheWriteDir (set, dirs, file);
-    free (file);
-    return ret;
+FcBool
+FcDirSave (FcFontSet *set, const FcChar8 *dir)
+{
+    static int rand_state = 0;
+    int bank;
+
+    if (!rand_state) 
+       rand_state = time(0L);
+    bank = rand_r(&rand_state);
+
+    while (FcCacheHaveBank(bank))
+       bank = rand_r(&rand_state);
+
+    return FcDirCacheWrite (bank, set, dir);
 }