Commit 8a3dc488 authored by Tor Lillqvist's avatar Tor Lillqvist Committed by Keith Packard

Workaround for stat() brokenness in Microsoft's C library (bug 8526)

Fix a couple of longstanding problems with fontconfig on Windows that
manifest themselves especially in GIMP. The root cause to the problems is in
Microsoft's incredibly stupid stat() implementation. Basically, stat()
returns wrong timestamp fields for files on NTFS filesystems on machines
that use automatic DST switching.

See for instance and

As fccache.c now looks at more fields in the stat struct I fill in them all.
I noticed that fstat() is used only on a fd just after opening it, so on
Win32 I just call my stat() replacement before opening instead...
Implementing a good replacement for fstat() would be harder because the code
in fccache.c wants to compare inode numbers. There are no (readily
accessible) inode numbers on Win32, so I fake it with the hash of the full
file name, in the case as it is on disk. And fstat() doesn't know the full
file name, so it would be rather hard to come up with a inode number to
identify the file.

The patch also adds similar handling for the cache directory as for the fonts
directory: If a cachedir element in fonts.conf contains the magic string
"WINDOWSTEMPDIR_FONTCONFIG_CACHE" it is replaced at runtime with a path under
the machine's (or user's) temp folder as returned by GetTempPath(). I don't
want to hardcode any pathnames in a fonts.conf intended to be distributed to
end-users, most of which who wouldn't know how to edit it anyway. And
requiring an installer to edit it gets complicated.
parent 1315db01
......@@ -459,7 +459,11 @@ AC_ARG_WITH(cache-dir, [ --with-cache-dir=DIR Use DIR to store
case $fc_cachedir in
fc_cachedir=`eval echo "${localstatedir}/cache/"${PACKAGE}`
if test "$os_win32" = "yes"; then
fc_cachedir=`eval echo "${localstatedir}/cache/"${PACKAGE}`
......@@ -27,11 +27,14 @@ FC_CACHE_SRC=${top_srcdir}/fc-cache
SGML = ${FC_CACHE_SRC}/fc-cache.sgml
if OS_WIN32
-$(mkinstalldirs) "$(DESTDIR)$(fc_cachedir)"
-$(RM) -rf "$(DESTDIR)$(fc_cachedir)"
INCLUDES=-I${top_srcdir} -I${top_srcdir}/src $(FREETYPE_CFLAGS) $(WARN_CFLAGS)
......@@ -33,6 +33,7 @@
# include <unistd.h>
# include <sys/mman.h>
#elif defined(_WIN32)
# define _WIN32_WINNT 0x0500
# include <windows.h>
......@@ -53,6 +54,90 @@ static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
#define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
#ifdef _WIN32
#include <windows.h>
#ifdef __GNUC__
typedef long long INT64;
#define EPOCH_OFFSET 11644473600ll
#define EPOCH_OFFSET 11644473600i64
typedef __int64 INT64;
/* Workaround for problems in the stat() in the Microsoft C library:
* 1) stat() uses FindFirstFile() to get the file
* attributes. Unfortunately this API doesn't return correct values
* for modification time of a directory until some time after a file
* or subdirectory has been added to the directory. (This causes
* to fail, for instance.) GetFileAttributesEx() is
* better, it returns the updated timestamp right away.
* 2) stat() does some strange things related to backward
* compatibility with the local time timestamps on FAT volumes and
* daylight saving time. This causes problems after the switches
* to/from daylight saving time. See
* , especially
* comment #30, and .
* We don't need any of that, FAT and Win9x are as good as dead. So
* just use the UTC timestamps from NTFS, converted to the Unix epoch.
static int
FcStat (const char *file, struct stat *statb)
char full_path_name[MAX_PATH];
char *basename;
if (!GetFileAttributesEx (file, GetFileExInfoStandard, &wfad))
return -1;
statb->st_dev = 0;
/* Calculate a pseudo inode number as a hash of the full path name.
* Call GetLongPathName() to get the spelling of the path name as it
* is on disk.
rc = GetFullPathName (file, sizeof (full_path_name), full_path_name, &basename);
if (rc == 0 || rc > sizeof (full_path_name))
return -1;
rc = GetLongPathName (full_path_name, full_path_name, sizeof (full_path_name));
statb->st_ino = FcStringHash (full_path_name);
statb->st_mode = _S_IREAD | _S_IWRITE;
statb->st_mode |= (statb->st_mode >> 3) | (statb->st_mode >> 6);
if (wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
statb->st_mode |= _S_IFDIR;
statb->st_mode |= _S_IFREG;
statb->st_nlink = 1;
statb->st_uid = statb->st_gid = 0;
statb->st_rdev = 0;
if (wfad.nFileSizeHigh > 0)
return -1;
statb->st_size = wfad.nFileSizeLow;
statb->st_atime = (*(INT64 *)&wfad.ftLastAccessTime)/10000000 - EPOCH_OFFSET;
statb->st_mtime = (*(INT64 *)&wfad.ftLastWriteTime)/10000000 - EPOCH_OFFSET;
statb->st_ctime = statb->st_mtime;
return 0;
#define FcStat stat
static const char bin2hex[] = { '0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
......@@ -118,14 +203,20 @@ FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
int fd;
#ifdef _WIN32
if (FcStat (cache_file, file_stat) < 0)
return -1;
fd = open((char *) cache_file, O_RDONLY | O_BINARY);
if (fd < 0)
return fd;
#ifndef _WIN32
if (fstat (fd, file_stat) < 0)
close (fd);
return -1;
return fd;
......@@ -147,7 +238,7 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
struct stat file_stat, dir_stat;
FcBool ret = FcFalse;
if (stat ((char *) dir, &dir_stat) < 0)
if (FcStat ((char *) dir, &dir_stat) < 0)
return FcFalse;
FcDirCacheBasename (dir, cache_base);
......@@ -2090,6 +2090,30 @@ FcEndElement(void *userData, const XML_Char *name)
FcConfigMessage (parse, FcSevereError, "out of memory");
#ifdef _WIN32
if (strcmp (data, "WINDOWSTEMPDIR_FONTCONFIG_CACHE") == 0)
int rc;
FcStrFree (data);
data = malloc (1000);
if (!data)
FcConfigMessage (parse, FcSevereError, "out of memory");
FcMemAlloc (FC_MEM_STRING, 1000);
rc = GetTempPath (800, data);
if (rc == 0 || rc > 800)
FcConfigMessage (parse, FcSevereError, "GetWindowsDirectory failed");
FcStrFree (data);
if (data [strlen (data) - 1] != '\\')
strcat (data, "\\");
strcat (data, "fontconfig\\cache");
if (!FcStrUsesHome (data) || FcConfigHome ())
if (!FcConfigAddCacheDir (parse->config, data))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment