Mike Mascari wrote:
> Bruce Momjian wrote:
> > I am working with several groups getting the Win32 port ready for 7.4
> > and I have a few questions:
> > 
> > What is the standard workaround for the fact that rename() isn't atomic
> > on Win32?  Do we need to create our own locking around the
> > reading/writing of files that are normally updated in place using
> > rename()?
> Visual C++ comes with the source to Microsoft's C library:
> rename() calls MoveFile() which will error if:
> 1. The target file exists
> 2. The source file is in use
> MoveFileEx() (not available on 95/98) can overwrite the target 
> file if it exists. The Apache APR portability library uses 
> MoveFileEx() to rename files if under NT/XP/2K vs. a sequence of :
> 1. CreateFile() to test for target file existence
> 2. DeleteFile() to remove the target file
> 3. MoveFile() to rename the old file to new
> under Windows 95/98. Of course, some other process could create 
> the target file between 2 and 3, so their rename() would just 
> error out in that situation. I haven't tested it, but I recall 
> reading somewhere that MoveFileEx() has the ability to rename an 
> opened file. I'm 99% sure MoveFile() will fail if the source 
> file is open.

OK, I downloaded APR and see in apr_file_rename():

        if (MoveFileEx(frompath, topath, MOVEFILE_REPLACE_EXISTING |

Looking at the entire APR function, they have lots of tests so it works
on Win9X and wide characters.  I think we will just use the APR as a
guide in implementing the things we need.  I think MoveFileEx() is the
proper way to go;  any other solution requires loop tests for rename.

I see the MoveFileEx manual page at:


> > Second, when you unlink() a file on Win32, do applications continue
> > accessing the old file contents if they had the file open before the
> > unlink?
> > 
> unlink() just calls DeleteFile() which will error if:
> 1. The target file is in use
> CreateFile() has the option:
> which might be able to be used to simulate traditional unlink() 
> behavior.

No, that flag isn't going to help us.  I wonder what MoveFileEx does if
the target file exists _and_ is open by another user?  I don't see any
loop in that Win32 rename() routine, and I looked at the Unix version of
apr_file_rename and its just a straight rename() call.

  Bruce Momjian                        |  http://candle.pha.pa.us
  [EMAIL PROTECTED]               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to [EMAIL PROTECTED] so that your
message can get through to the mailing list cleanly

Reply via email to