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.
This commit is contained in:
Patrick Lam 2006-01-26 16:09:12 +00:00
parent 575a37b797
commit f468f568b4
5 changed files with 82 additions and 5 deletions

View File

@ -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> 2006-01-25 Patrick Lam <plam@mit.edu>
* src/fccache.c (FcDirCacheOpen, FcDirCacheWrite): * src/fccache.c (FcDirCacheOpen, FcDirCacheWrite):
* src/fccfg.c (FcConfigEvaluate): * src/fccfg.c (FcConfigEvaluate):

View File

@ -382,6 +382,7 @@ FcGlobalCacheSave (FcGlobalCache *cache,
S_IRUSR | S_IWUSR); S_IRUSR | S_IWUSR);
if (fd == -1) if (fd == -1)
goto bail2; goto bail2;
FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY); fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
@ -393,7 +394,12 @@ FcGlobalCacheSave (FcGlobalCache *cache,
current_arch_machine_name); current_arch_machine_name);
if (current_arch_start < 0) 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)) if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
goto bail3; goto bail3;
@ -419,7 +425,6 @@ FcGlobalCacheSave (FcGlobalCache *cache,
} }
truncate_to -= current_arch_start; truncate_to -= current_arch_start;
FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
sprintf (header, "%8x ", (int)truncate_to); sprintf (header, "%8x ", (int)truncate_to);
strcat (header, current_arch_machine_name); strcat (header, current_arch_machine_name);
if (!FcCacheWriteString (fd, header)) if (!FcCacheWriteString (fd, header))
@ -1159,7 +1164,12 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
FcCacheSkipToArch(fd_orig, current_arch_machine_name); FcCacheSkipToArch(fd_orig, current_arch_machine_name);
if (current_arch_start < 0) 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)) if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
goto bail4; goto bail4;

View File

@ -22,6 +22,8 @@
* PERFORMANCE OF THIS SOFTWARE. * PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <dirent.h>
#include <sys/types.h>
#include "fcint.h" #include "fcint.h"
#if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT)) #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
@ -388,6 +390,41 @@ FcConfigAddFontDir (FcConfig *config,
return FcStrSetAddFilename (config->fontDirs, d); 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 * const FcChar8 *
FcConfigNormalizeFontDir (FcConfig *config, FcConfigNormalizeFontDir (FcConfig *config,
const FcChar8 *d) const FcChar8 *d)
@ -409,6 +446,21 @@ FcConfigNormalizeFontDir (FcConfig *config,
if (di == s.st_ino && dd == s.st_dev) if (di == s.st_ino && dd == s.st_dev)
return config->fontDirs->strs[n]; 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; return 0;
} }

View File

@ -132,7 +132,11 @@ FcDirScanConfig (FcFontSet *set,
return FcTrue; return FcTrue;
if (config) 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) if (!force)
{ {

View File

@ -2050,7 +2050,7 @@ FcEndElement(void *userData, const XML_Char *name)
if (!FcStrUsesHome (data) || FcConfigHome ()) if (!FcStrUsesHome (data) || FcConfigHome ())
{ {
if (!FcConfigAddDir (parse->config, data)) if (!FcConfigAddDir (parse->config, data))
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data);
} }
FcStrFree (data); FcStrFree (data);
break; break;