]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcdir.c
Add test for validity of directory caches that somehow got lost (reported
[fontconfig.git] / src / fcdir.c
index cf0b154e338c842fcf37138c47ce3304f08ae632..5452b60ca6f91daf2c6da51610faac751e7fbb29 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $XFree86: xc/lib/fontconfig/src/fcdir.c,v 1.2 2002/02/15 06:01:28 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
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/types.h>
-#include <dirent.h>
-#include <stdlib.h>
-#include <string.h>
 #include "fcint.h"
+#include <dirent.h>
+
+FcBool
+FcFileIsDir (const FcChar8 *file)
+{
+    struct stat            statb;
 
-#define FC_INVALID_FONT_FILE "."
+    if (stat ((const char *) file, &statb) != 0)
+       return FcFalse;
+    return S_ISDIR(statb.st_mode);
+}
 
 FcBool
-FcFileScan (FcFontSet      *set,
-           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;
-    int                    count;
+    int                        id;
+    FcPattern          *font;
+    FcBool             ret = FcTrue;
+    FcBool             isDir;
+    int                        count = 0;
     
+    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)
+       font = 0;
+       /*
+        * Nothing in the cache, scan the file
+        */
+       if (FcDebug () & FC_DBG_SCAN)
        {
-           /* "." means the file doesn't contain a font */
-           if (strcmp ((const char *) name, FC_INVALID_FONT_FILE) != 0)
-           {
-               font = FcNameParse (name);
-               if (font)
-                   FcPatternAddString (font, FC_FILE, file);
-           }
-           else
-               font = 0;
+           printf ("\tScanning file %s...", file);
+           fflush (stdout);
        }
-       else
+       font = FcFreeTypeQuery (file, id, blanks, &count);
+       if (FcDebug () & FC_DBG_SCAN)
+           printf ("done\n");
+       isDir = FcFalse;
+       if (!font && FcFileIsDir (file))
        {
-           if (FcDebug () & FC_DBG_SCAN)
-           {
-               printf ("\tScanning file %s...", file);
-               fflush (stdout);
-           }
-           font = FcFreeTypeQuery (file, id, blanks, &count);
-           if (FcDebug () & FC_DBG_SCAN)
-               printf ("done\n");
-           if (!force && cache)
-           {
-               if (font)
-               {
-                   FcChar8     *unparse;
-
-                   unparse = FcNameUnparse (font);
-                   if (unparse)
-                   {
-                       (void) FcFileCacheUpdate (cache, file, id, unparse);
-                       free (unparse);
-                   }
-               }
-               else
-               {
-                   /* negative cache files not containing fonts */
-                   FcFileCacheUpdate (cache, file, id, (FcChar8 *) FC_INVALID_FONT_FILE);
-               }
-           }
+           isDir = FcTrue;
+           ret = FcStrSetAdd (dirs, file);
        }
-       if (font)
+       /*
+        * Add the font
+        */
+       if (font && (!config || FcConfigAcceptFont (config, font)))
        {
            if (!FcFontSetAdd (set, font))
            {
@@ -101,78 +88,134 @@ FcFileScan (FcFontSet         *set,
                ret = FcFalse;
            }
        }
+       else if (font)
+           FcPatternDestroy (font);
        id++;
     } while (font && ret && id < count);
     return ret;
 }
 
 FcBool
-FcDirScan (FcFontSet       *set,
-          FcFileCache      *cache,
-          FcBlanks         *blanks,
-          const FcChar8    *dir,
-          FcBool           force)
+FcFileScan (FcFontSet      *set,
+           FcStrSet        *dirs,
+           FcGlobalCache   *cache,
+           FcBlanks        *blanks,
+           const FcChar8   *file,
+           FcBool          force)
 {
-    DIR                    *d;
-    struct dirent   *e;
-    FcChar8        *file;
-    FcChar8        *base;
-    FcBool         ret = FcTrue;
+    return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0);
+}
+
+/*
+ * Scan 'dir', adding font files to 'set' and
+ * subdirectories to 'dirs'
+ */
+
+FcBool
+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;
+    FcFontSet          *tmpSet;
+    int                        i;
+
+    if (config && !FcConfigAcceptFilename (config, dir))
+       return FcTrue;
+
+    if (!force)
+    {
+       /*
+        * Check ~/.fonts.cache-<version> file
+        */
+       if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config))
+           return FcTrue;
 
-    file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + 256 + 1);
+       if (FcDirCacheValid (dir) && FcDirCacheRead (set, dirs, dir))
+           return FcTrue;
+    }
+    
+    /* 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 (!force)
-    {
-       strcpy ((char *) base, FC_DIR_CACHE_FILE);
-       
-       if (FcFileCacheReadDir (set, file))
-       {
-           free (file);
-           return FcTrue;
-       }
-    }
     
+    if (FcDebug () & FC_DBG_SCAN)
+       printf ("\tScanning dir %s\n", dir);
+       
     d = opendir ((char *) dir);
     if (!d)
     {
        free (file);
+       /* Don't complain about missing directories */
+       if (errno == ENOENT)
+           return FcTrue;
        return FcFalse;
     }
+
+    tmpSet = FcFontSetCreate();
+    if (!tmpSet)
+    {  
+       free (file);
+       return FcFalse;
+    }
+
     while (ret && (e = readdir (d)))
     {
-       if (e->d_name[0] != '.')
+       if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
        {
            strcpy ((char *) base, (char *) e->d_name);
-           FcFileScan (set, cache, blanks, file, force);
+           ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config);
        }
     }
     free (file);
     closedir (d);
+    /*
+     * Now that the directory has been scanned,
+     * add the cache entry 
+     */
+    if (ret && cache)
+       FcGlobalCacheUpdate (cache, (char *)dir, tmpSet);
+
+    for (i = 0; i < tmpSet->nfont; i++)
+       FcFontSetAdd (set, tmpSet->fonts[i]);
+
+    if (tmpSet->fonts)
+    {
+       FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *));
+       free (tmpSet->fonts);
+    }
+    FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
+    free (tmpSet);
+       
     return ret;
 }
 
 FcBool
-FcDirSave (FcFontSet *set, 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;
-
-    strcpy ((char *) file, (char *) dir);
-    strcat ((char *) file, "/");
-    base = file + strlen ((char *) file);
-    strcpy ((char *) base, FC_DIR_CACHE_FILE);
-    ret = FcFileCacheWriteDir (set, file);
-    free (file);
-    return ret;
+    return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
 }
 
+FcBool
+FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
+{
+    return FcDirCacheWrite (set, dirs, dir);
+}