Fix things so that fopen's, not only open's, pass FILE_SHARE_DELETE
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 30 Aug 2006 18:06:27 +0000 (18:06 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 30 Aug 2006 18:06:27 +0000 (18:06 +0000)
and other special flags on Windows.  May fix intermittent 'Permission
denied' errors.  Magnus Hagander

src/include/port.h
src/port/open.c

index 05bf691f5edef6b42802aa723c81b122dfb07c0b..5f692db88653dc3e3380506ac2e3ce19ebfc5508 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.96 2006/08/18 15:47:08 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/port.h,v 1.97 2006/08/30 18:06:27 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -264,12 +264,15 @@ extern bool rmtree(char *path, bool rmtopdir);
 
 #if defined(WIN32) && !defined(__CYGWIN__)
 
-/* open() replacement to allow delete of held files and passing
- * of special options. */
+/* open() and fopen() replacements to allow deletion of open files and
+ * passing of other special options.
+ */
 extern int     pgwin32_open(const char *, int,...);
+extern FILE *pgwin32_fopen(const char *, const char *);
 
 #ifndef FRONTEND
 #define                open(a,b,c)     pgwin32_open(a,b,c)
+#define                fopen(a,b) pgwin32_fopen(a,b)
 #endif
 
 #define popen(a,b) _popen(a,b)
index 4df74d8b51b8f514353eccba45a910b851a00e52..268f2d31a24fdccb87e57978204b62820752f2b6 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/port/open.c,v 1.13 2006/06/25 00:18:24 momjian Exp $
+ * $PostgreSQL: pgsql/src/port/open.c,v 1.14 2006/08/30 18:06:27 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -19,7 +19,6 @@
 #include <fcntl.h>
 #include <assert.h>
 
-int                    pgwin32_open(const char *fileName, int fileFlags,...);
 
 static int
 openFlagsToCreateFileFlags(int openFlags)
@@ -112,4 +111,32 @@ pgwin32_open(const char *fileName, int fileFlags,...)
        return fd;
 }
 
+FILE *
+pgwin32_fopen(const char *fileName, const char *mode)
+{
+       int openmode = 0;
+       int fd;
+       
+       if (strstr(mode, "r+"))
+               openmode |= O_RDWR;
+       else if (strchr(mode, 'r'))
+               openmode |= O_RDONLY;
+       if (strstr(mode, "w+"))
+               openmode |= O_RDWR | O_CREAT | O_TRUNC;
+       else if (strchr(mode, 'w'))
+               openmode |= O_WRONLY | O_CREAT | O_TRUNC;
+       if (strchr(mode, 'a'))
+               openmode |= O_WRONLY | O_APPEND;
+
+       if (strchr(mode, 'b'))
+               openmode |= O_BINARY;
+       if (strchr(mode, 't'))
+               openmode |= O_TEXT;
+       
+       fd = pgwin32_open(fileName, openmode);
+       if (fd == -1)
+               return NULL;
+       return _fdopen(fd, mode);
+}
+
 #endif