Feature gate: #![feature(rename_noreplace)]
This is a tracking issue for std::fs::rename_noreplace.
It works similarly to std::fs::rename except that an existing destination is never overwritten, so the call fails with ErrorKind::AlreadyExists.
Should use appropriate OS primitives for platforms that have them, and use link/unlink approach as fallback on platforms that don't support it.
Platforms that have neither a rename-without-replace syscall nor hardlinks will return ErrorKind::Unsupported, unless alternative approach exists on those platforms.
Docs must be clear that this prevents TOCTOU races, but makes no atomicity or crash-safety guarantee.
Public API
// std::fs
pub fn rename_noreplace<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
Steps / History
(Remember to update the S-tracking-* label when checking boxes.)
Unresolved Questions
Feature gate:
#![feature(rename_noreplace)]This is a tracking issue for
std::fs::rename_noreplace.It works similarly to
std::fs::renameexcept that an existing destination is never overwritten, so the call fails withErrorKind::AlreadyExists.Should use appropriate OS primitives for platforms that have them, and use link/unlink approach as fallback on platforms that don't support it.
Platforms that have neither a rename-without-replace syscall nor hardlinks will return
ErrorKind::Unsupported, unless alternative approach exists on those platforms.Docs must be clear that this prevents TOCTOU races, but makes no atomicity or crash-safety guarantee.
Public API
Steps / History
(Remember to update the
S-tracking-*label when checking boxes.)std::fs::rename_noreplacelibs-team#131fs::rename_noreplace#162027:renameat2)renamex_np)MoveFileExW)link+unlinkfallbackErrorKind::UnsupportedUnresolved Questions
linkatwill fail withEPERM, so they can't be moved there at all. The ACP suggests plainrename, so destination "must either not exist or must be an empty directory", so an empty destination directory would still be removed, or am I missing anything? Is that acceptable, or should directories error in such case?Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩