Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The pain I had with atomic writing on Windows is that Windows rename does not behave like the POSIX standard:

https://msdn.microsoft.com/en-us/library/zw5t957f.aspx

      "The old name must be the path of an existing 
       file or directory. The new name must not be 
       the name of an existing file or directory."
I ended up having to use ReplaceFile and rename if ReplaceFile failed:

    ReplaceFile(tmpFileName, fileName, NULL, 
                REPLACEFILE_IGNORE_ACL_ERRORS, NULL, NULL) == 0 &&
    rename(tmpFileName, fileName);
https://msdn.microsoft.com/en-us/library/windows/desktop/aa3...

Why Windows why?




I usually don't defend Windows, but, rename not overwriting (without an argument) seems safer as a low level API, no? Given it takes a 2 line helper to achieve the same thing in C++ [1], doesn't seem that bad.

[1] https://gitlab.com/jeffreyrogers27/AtomicWrite/blob/master/A...


Safety is usually not a design goal for low level APIs. You rather want it to be minimal, functions to be orthogonal, clean abstractions and only promise necessary semantics.

And this might be already the reason. Win32 is geared towards the broad range of developers whereas Unix comes more from systems thinking.


I just want consistency. If the POSIX standard defines a function's behavior then you should either implement it by the standard or give it a different name.


You may want to look at MoveFileEx


This is the correct answer, with MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH

(I see that also provides MOVEFILE_DELAY_UNTIL_REBOOT as a workaround for executable images that are in use)




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: