Is file.rename() atomic?

Marco Leise Marco.Leise at gmx.de
Fri Dec 13 07:40:05 PST 2013


Am Thu, 12 Dec 2013 18:08:12 +0100
schrieb "Jacek Furmankiewicz" <jacek99 at 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



More information about the Digitalmars-d-learn mailing list