-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Describe more platform-specific behaviors of std::fs::rename
#31963
Conversation
r? @brson (rust_highfive has picked a reviewer for you, use r? to override) |
While MSDN doesn't explicitly say it is atomic, it is effectively atomic on Windows since we don't pass |
@retep998 I couldn't be sure if the operation of replacing the old, existing file can also be considered "effectively atomic". I mean, wouldn't there be a possibility that There's also an API named MoveFileTransacted, but strangely MS wants to make it obsolete... |
It is atomic in the sense that at point A the file is not at the destination, at point B the file is at at the destination, and there is no point C in between those two points where you could possibly see something else, at least for NTFS anyway. Not sure about FAT. |
@retep998 What I wondered is if there are two files named |
It should also be atomic in that sense, at least when running normally. I'm not sure if it is atomic with respect to system crashes and power failures, ideally Windows would take advantage of the change journal to rollback those incidents on next boot, but I just don't know. We'd need someone from Microsoft to confirm that. |
/cc @rust-lang/libs, are you happy with these docs? |
Seems fine to me, yes |
I removed the mention of atomicity ("Also, the operation is guaranteed to be atomic on Unix. There's currently no such guarantee on Windows."). I guess it would be better to make it unspecified, until more concrete information is found. |
Describe more platform-specific behaviors of `std::fs::rename` I did some tests myself regarding the situation when both `from` and `to` exist, and the results were: On Linux: `from` | `to` | Result ---- | ---- | ---- Directory | Directory | Ok Directory | File | Error File | Directory | Error File | File | Ok On Windows: `from` | `to` | Result ---- | ---- | ---- Directory | Directory | Error Directory | File | Ok File | Directory | Error File | File | Ok This is a bit against the official MSDN documentation, which says "(`MOVEFILE_REPLACE_EXISTING`) cannot be used if `lpNewFileName` or `lpExistingFileName` names a directory." As evidenced above, `lpExistingFileName` *can* be a directory. I also mentioned the atomicity of the operation. Fixes #31301.
I did some tests myself regarding the situation when both
from
andto
exist, and the results were:On Linux:
from
to
On Windows:
from
to
This is a bit against the official MSDN documentation, which says "(
MOVEFILE_REPLACE_EXISTING
) cannot be used iflpNewFileName
orlpExistingFileName
names a directory." As evidenced above,lpExistingFileName
can be a directory.I also mentioned the atomicity of the operation.
Fixes #31301.