]> git.wh0rd.org - fontconfig.git/commitdiff
Stop trampling the directory name when writing out caches. (with Mike
authorPatrick Lam <plam@MIT.EDU>
Thu, 26 Jan 2006 16:09:12 +0000 (16:09 +0000)
committerPatrick Lam <plam@MIT.EDU>
Thu, 26 Jan 2006 16:09:12 +0000 (16:09 +0000)
    Fabian:) Beef up FcConfigNormalizeFontDir to scan subdirs when
    necessary. Don't scan directories that can't be normalized.

ChangeLog
src/fccache.c
src/fccfg.c
src/fcdir.c
src/fcxml.c

index cad05d36486960785b3b36772105697c1d71ae72..328b0fb4d3e2da78a47c96301ef54838289b1eab 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2006-01-26  Patirck Lam  <plam@mit.edu>
+       * src/fccache.c (FcGlobalCacheSave, FcDirCacheWrite):
+       * src/fccfg.c (FcConfigAddFontDirSubdirs, FcConfigNormalizeFontDir):
+       * src/fcdir.c (FcDirScanConfig):
+       * src/fcxml.c (FcEndElement):
+
+       Stop trampling the directory name when writing out caches.
+       (with Mike Fabian:) Beef up FcConfigNormalizeFontDir to scan
+       subdirs when necessary.  Don't scan directories that can't be
+       normalized.
+
 2006-01-25  Patrick Lam  <plam@mit.edu>
        * src/fccache.c (FcDirCacheOpen, FcDirCacheWrite):
        * src/fccfg.c (FcConfigEvaluate):
index 742e9aa950efa23b91827598f0a546a48a5b2b8c..349bc9ab500a69a12e32d5b107af1afbca873417 100644 (file)
@@ -382,6 +382,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 +394,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;
@@ -419,7 +425,6 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
     }
     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))
@@ -1159,7 +1164,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;
index 2135d67f790316cdf4aaa5525ccac98b2d5f4342..6e5174b505df51090ea4d8c5deae6b12eff6f3f9 100644 (file)
@@ -22,6 +22,8 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <dirent.h>
+#include <sys/types.h>
 #include "fcint.h"
 
 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
@@ -388,6 +390,41 @@ FcConfigAddFontDir (FcConfig           *config,
     return FcStrSetAddFilename (config->fontDirs, d);
 }
 
+static FcBool
+FcConfigAddFontDirSubdirs (FcConfig        *config,
+                          const FcChar8   *d)
+{
+    DIR *dir;
+    struct dirent *e;
+    FcChar8 *subdir;
+    
+    if (!(dir = opendir ((char *) d)))
+       return FcFalse;
+    if (!(subdir = (FcChar8 *) malloc (strlen ((char *) d) + FC_MAX_FILE_LEN + 2)))
+    {
+       fprintf (stderr, "out of memory");
+       return FcFalse;
+    }
+    while ((e = readdir (dir)))
+    {
+       if (strcmp (e->d_name, ".") && strcmp (e->d_name, "..") &&
+           strlen (e->d_name) < FC_MAX_FILE_LEN)
+       {
+           strcpy ((char *)subdir, (char *)d);
+           strcat ((char *)subdir, "/");
+           strcat ((char *)subdir, e->d_name);
+           if (FcFileIsDir (subdir))
+           {
+               FcConfigAddFontDir (config, subdir);
+               FcConfigAddFontDirSubdirs (config, subdir);
+           }
+       }
+    }
+    free (subdir);
+    closedir (dir);
+    return FcTrue;
+}
+
 const FcChar8 *
 FcConfigNormalizeFontDir (FcConfig     *config, 
                          const FcChar8 *d)
@@ -409,6 +446,21 @@ FcConfigNormalizeFontDir (FcConfig         *config,
        if (di == s.st_ino && dd == s.st_dev)
            return config->fontDirs->strs[n];
     }
+
+    /* Ok, we didn't find it in fontDirs; let's add subdirs.... */
+    for (n = 0; n < config->fontDirs->num; n++)
+       FcConfigAddFontDirSubdirs (config, config->fontDirs->strs[n]);
+
+    /* ... and try again. */
+    for (n = 0; n < config->fontDirs->num; n++)
+    {
+       if (stat ((char *)config->fontDirs->strs[n], &s) == -1)
+           continue;
+       if (di == s.st_ino && dd == s.st_dev)
+           return config->fontDirs->strs[n];
+    }
+
+    /* if it fails, then really give up. */
     return 0;
 }
 
index 6db0b8bddf3c15b96de1be0b3476817fe85de5d4..adb39fb1fe62b45fb94ac35d3d3075304c5fbdb7 100644 (file)
@@ -132,7 +132,11 @@ FcDirScanConfig (FcFontSet *set,
        return FcTrue;
 
     if (config)
-        FcConfigAddFontDir (config, dir);
+       dir = FcConfigNormalizeFontDir (config, dir);
+
+    /* refuse to scan a directory that can't be normalized. */
+    if (!dir)
+       return FcFalse;
 
     if (!force)
     {
index 0aaec1db85663172a0a55a981a5b69c0080b09ee..e65ccbfdfce589a763350526cf4d5d75606e1424 100644 (file)
@@ -2050,7 +2050,7 @@ FcEndElement(void *userData, const XML_Char *name)
        if (!FcStrUsesHome (data) || FcConfigHome ())
        {
            if (!FcConfigAddDir (parse->config, data))
-               FcConfigMessage (parse, FcSevereError, "out of memory");
+               FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data);
        }
        FcStrFree (data);
        break;