Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support for the operations in UnliftIO.IO.File? #50

Open
sjakobi opened this issue Nov 18, 2019 · 3 comments
Open

Windows support for the operations in UnliftIO.IO.File? #50

sjakobi opened this issue Nov 18, 2019 · 3 comments

Comments

@sjakobi
Copy link

sjakobi commented Nov 18, 2019

Most of the file operations in UnliftIO.IO.File have disclaimers like this:

This function behaves the same as withBinaryFile on Windows platforms.

I presume that this means that atomicity and durability are not implemented on Windows.

If so, could the same guarantees be implemented on Windows too?

@snoyberg
Copy link
Member

I spoke with @lehins about this a while ago. We didn't know how to add this support correctly on Windows. We'd really need a Windows API expert to step up to the plate with a good PR.

@lehins
Copy link
Contributor

lehins commented Nov 19, 2019

From what I know none of the functions in Windows API documentation are stating that a file rename can be done atomically (except MoveFileTransacted, which might get deprecated), but at the same time it doesn't say the opposite either. There are things sprinkled online stating that it can be done, for example here is a comment that I found very interesting: golang/go#8914 (comment)

There are two place in the Microsoft related documents that say that ReplaceFile is atomic:

Under Windows, the ReplaceFile() call is used to atomically replace one file with another.

The needed atomicity here is that the changes either are completely applied or not applied at all, as an inconsistent state would render the file corrupt. A common approach is to write the document to a new file, then replace the original file with the new one. One method to do this is with the ReplaceFile API.

In order to get this done properly a bit of research would be needed, but from what I've seen so far it is not the ReplaceFIle, but MoveFileEx is the best candidate for implementing atomic writes. It looks like even durable writes could be made possible with MOVEFILE_WRITE_THROUGH flag.

In fact looking at the implementation of renamePath on windows it already does the sensible thing and is documented as atomic in haddock. So implementing a write to temporary file in same directory followed by renamePath could be all that is necessary to get atomicity.

@sjakobi
Copy link
Author

sjakobi commented Nov 19, 2019

Thanks for the quick response! :)

Just atomicity on Windows would already be a big step forward for me – I actually don't need durability right now. The renamePath-based approach sounds great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants