66 *
77 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88 *
9- * $PostgreSQL: pgsql/src/port/open.c,v 1.22 2007/11/30 11:16:43 mha Exp $
9+ * $PostgreSQL: pgsql/src/port/open.c,v 1.23 2007/12/20 20:27:53 mha Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
1313
1414#ifdef WIN32
1515
16- #include "c.h"
16+ #ifndef FRONTEND
17+ #include "postgres.h"
18+ #else
19+ #include "postgres_fe.h"
20+ #endif
1721
1822#include <windows.h>
1923#include <fcntl.h>
5862pgwin32_open (const char * fileName ,int fileFlags ,...)
5963{
6064int fd ;
61- HANDLE h ;
65+ HANDLE h = INVALID_HANDLE_VALUE ;
6266SECURITY_ATTRIBUTES sa ;
67+ int loops = 0 ;
6368
6469/* Check that we can handle the request */
6570assert ((fileFlags & ((O_RDONLY |O_WRONLY |O_RDWR ) |O_APPEND |
@@ -71,7 +76,7 @@ pgwin32_open(const char *fileName, int fileFlags,...)
7176sa .bInheritHandle = TRUE;
7277sa .lpSecurityDescriptor = NULL ;
7378
74- if ((h = CreateFile (fileName ,
79+ while ((h = CreateFile (fileName ,
7580/* cannot use O_RDONLY, as it == 0 */
7681 (fileFlags & O_RDWR ) ? (GENERIC_WRITE |GENERIC_READ ) :
7782 ((fileFlags & O_WRONLY ) ?GENERIC_WRITE :GENERIC_READ ),
@@ -88,7 +93,32 @@ pgwin32_open(const char *fileName, int fileFlags,...)
8893((fileFlags & O_DSYNC ) ?FILE_FLAG_WRITE_THROUGH :0 ),
8994NULL ))== INVALID_HANDLE_VALUE )
9095{
91- _dosmaperr (GetLastError ());
96+ /*
97+ * Sharing violation or locking error can indicate antivirus, backup
98+ * or similar software that's locking the file. Try again for 30 seconds
99+ * before giving up.
100+ */
101+ DWORD err = GetLastError ();
102+ if (err == ERROR_SHARING_VIOLATION ||
103+ err == ERROR_LOCK_VIOLATION )
104+ {
105+ pg_usleep (100000 );
106+ loops ++ ;
107+
108+ #ifndef FRONTEND
109+ if (loops == 50 )
110+ ereport (LOG ,
111+ (errmsg ("could not open file \"%s\": %s" ,fileName ,
112+ (err == ERROR_SHARING_VIOLATION )?_ ("sharing violation" ):_ ("lock violation" )),
113+ errdetail ("Continuing to retry for 30 seconds." ),
114+ errhint ("You may have antivirus, backup or similar software interfering with the database." )));
115+ #endif
116+
117+ if (loops < 300 )
118+ continue ;
119+ }
120+
121+ _dosmaperr (err );
92122return -1 ;
93123}
94124