Re: Win32 rename()/unlink() questions

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Mike Mascari <mascarm(at)mascari(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Win32 rename()/unlink() questions
Date: 2002-09-19 20:24:01
Message-ID: 200209192024.g8JKO1g10337@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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 |
MOVEFILE_COPY_ALLOWED))

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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/movefile.asp

> > 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:
>
> FILE_FLAG_DELETE_ON_CLOSE
>
> 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
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2002-09-19 20:27:39 Re: PGXLOG variable worthwhile?
Previous Message Oleg Bartunov 2002-09-19 20:19:15 Re: Inconsistent Conversion Names