summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Hagander2008-04-10 16:59:42 +0000
committerMagnus Hagander2008-04-10 16:59:42 +0000
commita32bc36a66a92b94fdef509ba8f17dac4f14f525 (patch)
tree96f8a1f26233bb595748722a026d4a31300a186b
parentf9292f2ed1ae377a705be1a5a129778194dfbc92 (diff)
Create wrapper pgwin32_safestat() and redefine stat() to it
on win32, because the stat() function in the runtime cannot be trusted to always update the st_size field. Per report and research by Sergey Zubkovsky.
-rw-r--r--src/include/port.h13
-rw-r--r--src/port/dirmod.c34
2 files changed, 47 insertions, 0 deletions
diff --git a/src/include/port.h b/src/include/port.h
index bb83d02749..f9a200f76f 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -298,6 +298,19 @@ extern FILE *pgwin32_fopen(const char *, const char *);
#define popen(a,b) _popen(a,b)
#define pclose(a) _pclose(a)
+/*
+ * stat() is not guaranteed to set the st_size field on win32, so we
+ * redefine it to our own implementation that is.
+ *
+ * We must pull in sys/stat.h here so the system header definition
+ * goes in first, and we redefine that, and not the other way around.
+ */
+extern int pgwin32_safestat(const char *path, struct stat *buf);
+#if !defined(FRONTEND) && !defined(_DIRMOD_C)
+#include <sys/stat.h>
+#define stat(a,b) pgwin32_safestat(a,b)
+#endif
+
/* Missing rand functions */
extern long lrand48(void);
extern void srand48(long seed);
diff --git a/src/port/dirmod.c b/src/port/dirmod.c
index eeba9a6139..0a64cab0a2 100644
--- a/src/port/dirmod.c
+++ b/src/port/dirmod.c
@@ -447,3 +447,37 @@ report_and_fail:
pgfnames_cleanup(filenames);
return false;
}
+
+
+#ifdef WIN32
+/*
+ * The stat() function in win32 is not guaranteed to update the st_size
+ * field when run. So we define our own version that uses the Win32 API
+ * to update this field.
+ */
+#undef stat
+int
+pgwin32_safestat(const char *path, struct stat *buf)
+{
+ int r;
+ WIN32_FILE_ATTRIBUTE_DATA attr;
+
+ r = stat(path, buf);
+ if (r < 0)
+ return r;
+
+ if (!GetFileAttributesEx(path, GetFileExInfoStandard, &attr))
+ {
+ _dosmaperr(GetLastError());
+ return -1;
+ }
+
+ /*
+ * XXX no support for large files here, but we don't do that in
+ * general on Win32 yet.
+ */
+ buf->st_size = attr.nFileSizeLow;
+
+ return 0;
+}
+#endif