fs: rename_exclusive (renameat2 + Windows impl)#415
Draft
joshuamegnauth54 wants to merge 1 commit into
Draft
Conversation
Closes: bytecodealliance#406 POSIX's `rename` atomically replaces targets. However, if a caller wants to optionally replace a target, they have to check if the target exists and then rename onto the target. This sequence of events is subject to TOCTOU where the target may be created between the check and the call to rename(). `renameat2` for some of the Unixes and MoveFileExW on Windows both support atomically checking if a target exists on rename. `renameat2` is supported on Linux, FreeBSD 16 (not exposed yet in libc), Redox, and macOS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #406
POSIX's
renameatomically replaces targets. However, if a caller wants to optionally replace a target, they have to check if the target exists and then rename onto the target. This sequence of events is subject to TOCTOU where the target may be created between the check and the call to rename().renameat2for some of the Unixes and MoveFileExW on Windows both support atomically checking if a target exists on rename.renameat2is supported on Linux, FreeBSD 16 (not exposed yet in libc), Redox, and macOS.I tested this on Linux but not Windows. I wrote the Windows code by looking up examples and using Microsoft's documentation.
rename_excl_uncheckedreuses the existingrename_uncheckedcode but callsrenameat2instead on Unix. I copied Rustix's feature gates for Unix. FreeBSD 16-CURRENT supportsrenameat2, so it can be enabled whenever it's released and Rust's libc and Rustix gains wrappers for it.The Windows version supports moving files across volumes. It seemed like the correct thing to do and
MoveFileExWsupports it natively. The problem is that file security isn't moved/copied, so it has to manually stored and then restored. The Windows code is significantly shorter without this feature. It's only one call toMoveFileExWwithout it. 😹Anyway, I'm relying on CI to yell at me if the Windows code is incorrect. I tried to be really careful as well as write a lot of comments to document my thinking. The Unix code is straightforward, though.
Windows sources: