*** pgsql/src/backend/access/transam/xlog.c	22 Jun 2006 20:43:20 -0000	1.222.2.4
--- pgsql/src/backend/access/transam/xlog.c	10 Jan 2007 09:39:47 -0000
***************
*** 1969,1975 ****
   * caller must *not* hold the lock at call.
   *
   * Returns TRUE if file installed, FALSE if not installed because of
!  * exceeding max_advance limit.  (Any other kind of failure causes ereport().)
   */
  static bool
  InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
--- 1969,1977 ----
   * caller must *not* hold the lock at call.
   *
   * Returns TRUE if file installed, FALSE if not installed because of
!  * exceeding max_advance limit.  On Windows, we also return FALSE if we
!  * can't rename the file into place because someone's got it open.
!  * (Any other kind of failure causes ereport().)
   */
  static bool
  InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
***************
*** 2024,2033 ****
--- 2026,2050 ----
  	unlink(tmppath);
  #else
  	if (rename(tmppath, path) < 0)
+ 	{
+ #ifdef WIN32
+ #if !defined(__CYGWIN__)
+ 		if (GetLastError() == ERROR_ACCESS_DENIED)
+ #else
+ 		if (errno == EACCES)
+ #endif
+ 		{
+ 			if (use_lock)
+ 				LWLockRelease(ControlFileLock);
+ 			return false;
+ 		}
+ #endif /* WIN32 */
+ 
  		ereport(ERROR,
  				(errcode_for_file_access(),
  				 errmsg("could not rename file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m",
  						tmppath, path, *log, *seg)));
+ 	}
  #endif
  
  	if (use_lock)
*** pgsql/src/port/dirmod.c	15 Oct 2005 02:49:51 -0000	1.41
--- pgsql/src/port/dirmod.c	10 Jan 2007 09:39:50 -0000
***************
*** 117,159 ****
  	int			loops = 0;
  
  	/*
! 	 * We need these loops because even though PostgreSQL uses flags that
  	 * allow rename while the file is open, other applications might have
! 	 * these files open without those flags.
  	 */
  #if defined(WIN32) && !defined(__CYGWIN__)
  	while (!MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING))
- #endif
- #ifdef __CYGWIN__
- 		while (rename(from, to) < 0)
- #endif
- 		{
- #if defined(WIN32) && !defined(__CYGWIN__)
- 			if (GetLastError() != ERROR_ACCESS_DENIED)
- #endif
- #ifdef __CYGWIN__
- 				if (errno != EACCES)
- #endif
- 					/* set errno? */
- 					return -1;
- 			pg_usleep(100000);	/* us */
- 			if (loops == 30)
- #ifndef FRONTEND
- 				elog(LOG, "could not rename file \"%s\" to \"%s\", continuing to try",
- 					 from, to);
  #else
! 				fprintf(stderr, _("could not rename file \"%s\" to \"%s\", continuing to try\n"),
! 						from, to);
  #endif
! 			loops++;
! 		}
! 
! 	if (loops > 30)
! #ifndef FRONTEND
! 		elog(LOG, "completed rename of file \"%s\" to \"%s\"", from, to);
  #else
! 		fprintf(stderr, _("completed rename of file \"%s\" to \"%s\"\n"), from, to);
  #endif
  	return 0;
  }
  
--- 117,144 ----
  	int			loops = 0;
  
  	/*
! 	 * We need to loop because even though PostgreSQL uses flags that
  	 * allow rename while the file is open, other applications might have
! 	 * the file open without those flags.  However, we won't wait
! 	 * indefinitely for someone else to close the file.
  	 */
  #if defined(WIN32) && !defined(__CYGWIN__)
  	while (!MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING))
  #else
! 	while (rename(from, to) < 0)
  #endif
! 	{
! #if defined(WIN32) && !defined(__CYGWIN__)
! 		if (GetLastError() != ERROR_ACCESS_DENIED)
  #else
! 		if (errno != EACCES)
  #endif
+ 			/* set errno? */
+ 			return -1;
+ 		if (++loops > 300)		/* time out after 30 sec */
+ 			return -1;
+ 		pg_usleep(100000);		/* us */
+ 	}
  	return 0;
  }
  
***************
*** 167,199 ****
  	int			loops = 0;
  
  	/*
! 	 * We need these loops because even though PostgreSQL uses flags that
  	 * allow unlink while the file is open, other applications might have
! 	 * these files open without those flags.
  	 */
  	while (unlink(path))
  	{
  		if (errno != EACCES)
  			/* set errno? */
  			return -1;
  		pg_usleep(100000);		/* us */
- 		if (loops == 30)
- #ifndef FRONTEND
- 			elog(LOG, "could not remove file \"%s\", continuing to try",
- 				 path);
- #else
- 			fprintf(stderr, _("could not remove file \"%s\", continuing to try\n"),
- 					path);
- #endif
- 		loops++;
  	}
- 
- 	if (loops > 30)
- #ifndef FRONTEND
- 		elog(LOG, "completed removal of file \"%s\"", path);
- #else
- 		fprintf(stderr, _("completed removal of file \"%s\"\n"), path);
- #endif
  	return 0;
  }
  
--- 152,171 ----
  	int			loops = 0;
  
  	/*
! 	 * We need to loop because even though PostgreSQL uses flags that
  	 * allow unlink while the file is open, other applications might have
! 	 * the file open without those flags.  However, we won't wait
! 	 * indefinitely for someone else to close the file.
  	 */
  	while (unlink(path))
  	{
  		if (errno != EACCES)
  			/* set errno? */
  			return -1;
+ 		if (++loops > 300)		/* time out after 30 sec */
+ 			return -1;
  		pg_usleep(100000);		/* us */
  	}
  	return 0;
  }
  
