You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 60048ab
Browse filesBrowse the repository at this point in the historyBrowse files
Rollup merge of #162027 - valentynkit:fs-rename-noreplace, r=joshtriplett
std: add `fs::rename_noreplace`
Tracking issue: #161427
Accepted ACP: rust-lang/libs-team#131
## Summary
Add `fs::rename_noreplace`. Unlike `fs::rename`, the destination is never overwritten, instead returns `ErrorKind::AlreadyExists` if `to` already exists.
Platform-specific APIs used:
- [`renameat2`](https://man7.org/linux/man-pages/man2/renameat2.2.html) with `RENAME_NOREPLACE` flag on Linux and Android
- `renamex_np` with `RENAME_EXCL` on Apple
- [`MoveFileExW`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefileexw) without the `MOVEFILE_REPLACE_EXISTING` flag on Windows. Extracted functionality into `rename_inner` to avoid duplication for `rename` and `rename_noreplace`
- On other Unix platforms and on WASI, and on Linux, Android and Apple when the kernel or the filesystem doesn't support it, `link` followed by `unlink` is used instead.
- Platforms with neither return `Unsupported`.
Linux and Android case, has different error handling than Apple because the platforms report these conditions differently. On Linux the syscall may not exist at all (`ENOSYS`), and `EINVAL` covers "filesystem doesn't support the flag" and flag misuse (incorrect combinations of flags, as example). On Apple `renamex_np` is called directly, and Darwin use `ENOTSUP` for not supported case, and keeps `EINVAL` only for misuse combinations.
> EINVAL An invalid flag was specified in flags.
> EINVAL Both RENAME_NOREPLACE and RENAME_EXCHANGE were specified in flags.
> EINVAL The filesystem does not support one of the flags in flags.
[`renameat2`](https://man7.org/linux/man-pages/man2/renameat2.2.html)
## Open questions
1. `ENOSYS` on Linux and Android could be cached, unlike `EINVAL` case which on the same machine, when using different filesystem may return different result. In this PR I deliberately avoided adding caching to avoid premature optimization, but could add it if you think it worth it.
2. The `link` + `unlink` fallback can't move directories: `link` returns `EPERM`. Should it report `Unsupported` there instead?
r? libs
0 commit comments