windows: ReplaceFile vs MoveFileEx

1,223 views
Skip to first unread message

Manlio Perillo

unread,
Mar 31, 2016, 10:53:09 AM3/31/16
to golang-nuts
Hi.

I'm planning to port some code that use POSIX rename to Windows.
From what I can read, MoveFileEx, as used by the os package, is not atomic.

The authors of this paper:
state that ReplaceFile is the Windows version of POSIX rename.

From the documentation I can not understand if this is true.
One limitation is that it does not work on directories, but this is fine in my case.

ReplaceFile is also listed as an alternative for the Transacted API, that will probably be deprecated in a future Windows version, but this just make me more confused, since atomic or transaction is never mentioned in the ReplaceFile documentation.

Any Windows experts that can help me?


Thanks  Manlio

Konstantin Khomoutov

unread,
Mar 31, 2016, 11:17:26 AM3/31/16
to Manlio Perillo, golang-nuts
I'd not claim myself an expert, of course, but yes, ReplaceFile is what
should be used for atomic renaming of the file.

The hint to this in the MSDN docs is: «The backup file, replaced file,
and replacement file must all reside on the same volume.», which is
what makes the operation atomic -- it's performed using a filesystem
transaction.

Personally I'm using this function for in-place renames in some of
my .NET programs.

Nate Finch

unread,
Apr 1, 2016, 9:51:48 PM4/1/16
to golang-nuts

Daniel Theophanes

unread,
Apr 2, 2016, 12:52:08 AM4/2/16
to golang-nuts
os.Rename now uses MoveFileEx, which is as atomc as it gets on Windows I think.

Manlio Perillo

unread,
Apr 2, 2016, 6:07:15 AM4/2/16
to golang-nuts
Il giorno sabato 2 aprile 2016 03:51:48 UTC+2, Nate Finch ha scritto:
See https://github.com/natefinch/atomic

Thanks.

However I noted that the implementation also copy permissions.
The ReplaceFile is designed to both rename and copy file permissions atomically(?), so the package should use it instead of MoveFileEx.


Manlio 

Manlio Perillo

unread,
Apr 2, 2016, 6:10:01 AM4/2/16
to golang-nuts
Il giorno sabato 2 aprile 2016 06:52:08 UTC+2, Daniel Theophanes ha scritto:
os.Rename now uses MoveFileEx, which is as atomc as it gets on Windows I think.

The problem is precisely that there is not an authoritative answer.

A similar problem is when you want to ensure durability:

Here it seems that there is not even an authoritative answer from POSIX group...


Thanks  Manlio
Reply all
Reply to author
Forward
0 new messages