Create wrapper pgwin32_safestat() and redefine stat() to it
authorMagnus Hagander <magnus@hagander.net>
Thu, 10 Apr 2008 16:59:42 +0000 (16:59 +0000)
committerMagnus Hagander <magnus@hagander.net>
Thu, 10 Apr 2008 16:59:42 +0000 (16:59 +0000)
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.

src/include/port.h
src/port/dirmod.c

index c9d3e48f0e8232ba91abf3753fdf1b47ebfdc179..46d08c956a004c339e4150d5080caf93602a1ef0 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/port.h,v 1.106.2.3 2008/02/29 15:31:37 mha Exp $
+ * $PostgreSQL: pgsql/src/include/port.h,v 1.106.2.4 2008/04/10 16:59:42 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -290,6 +290,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);
index 7c34fee3237fa82be0d6b0c064cc30be79d2219e..e80d9bdbee1f495c405564e2ef21024f3292e76c 100644 (file)
@@ -10,7 +10,7 @@
  * Win32 (NT, Win2k, XP).  replace() doesn't work on Win95/98/Me.
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/port/dirmod.c,v 1.44.2.1 2006/12/04 22:24:04 momjian Exp $
+ *   $PostgreSQL: pgsql/src/port/dirmod.c,v 1.44.2.2 2008/04/10 16:59:42 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -447,3 +447,37 @@ report_and_fail:
    fnames_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