www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is file.rename() atomic?

reply "Jacek Furmankiewicz" <jacek99 gmail.com> writes:
    void rename(in char[] from, in char[] to);
         Rename file from to to. If the target file exists, it is 
overwritten.

         Throws:
         FileException on error.

Just wanted to know if this operation is atomic?
or does it depend on the underlying file system?

In short, in the file nanoseconds/milliseconds that this 
operation is occurring is it possible for someone else to be 
reading the same file and get a dirty read (i.e. with only half 
of the contents overriden, etc)?

Thanks
Dec 12 2013
next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 12 December 2013 at 17:08:13 UTC, Jacek 
Furmankiewicz wrote:
    void rename(in char[] from, in char[] to);
         Rename file from to to. If the target file exists, it 
 is overwritten.

         Throws:
         FileException on error.

 Just wanted to know if this operation is atomic?
 or does it depend on the underlying file system?

 In short, in the file nanoseconds/milliseconds that this 
 operation is occurring is it possible for someone else to be 
 reading the same file and get a dirty read (i.e. with only half 
 of the contents overriden, etc)?

 Thanks
Here's the implementation,as you can see it's just c function calls, no work is done in D: void rename(in char[] from, in char[] to) { version(Windows) { enforce(MoveFileExW(std.utf.toUTF16z(from), std.utf.toUTF16z(to), MOVEFILE_REPLACE_EXISTING), new FileException( text("Attempting to rename file ", from, " to ", to))); } else version(Posix) cenforce(core.stdc.stdio.rename(toStringz(from), toStringz(to)) == 0, to); } On a posix compliant system, I'm pretty sure this is guaranteed to be atomic as far as any other process is concerned. On windows, I don't know. A quick google suggests that there may have been a C or filesystem bug on OSX with regards to this.
Dec 12 2013
prev sibling parent Marco Leise <Marco.Leise gmx.de> writes:
Am Thu, 12 Dec 2013 18:08:12 +0100
schrieb "Jacek Furmankiewicz" <jacek99 gmail.com>:

     void rename(in char[] from, in char[] to);
          Rename file from to to. If the target file exists, it is 
 overwritten.
 
          Throws:
          FileException on error.
 
 Just wanted to know if this operation is atomic?
 or does it depend on the underlying file system?
 
 In short, in the file nanoseconds/milliseconds that this 
 operation is occurring is it possible for someone else to be 
 reading the same file and get a dirty read (i.e. with only half 
 of the contents overriden, etc)?
 
 Thanks
I don't know for sure, but it is likely you are on the safe side I guess ;) 1. the reading process probably didn't allow shared writing to the target file anyways (on Windows) 2. moving a file on the same file system is just a metadata update (giving the data a different name). 3. If all else fails, the target file is most likely deleted first, then recreated with the contents of the file to move which easily avoids what you describe. You could try MoveFileEx on an opened target file with all sharing options enabled and see what happens. -- Marco
Dec 13 2013