-
Notifications
You must be signed in to change notification settings - Fork 19
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
Change std::fs::remove_dir_all
to be idempotent
#410
Comments
To be ultra clear, this is proposing changing a documented behaviour. |
Sorry, I should have mentioned that. I'll elaborate on it in the first post. |
Oh, another alternative would obviously be to create a new function doing what the initial post describes. I added it to the first post. |
Crosslinking: discussion rust-lang/rust#127576, and a proposed fix rust-lang/rust#127623 |
That's a different thing imho. It concerns the internal behaviour rather than the effect on the top-level directory. |
Not propagating internal not-found errors does seem like a decent solution because it solves
|
On Windows we ignore not found errors when trying to delete files (and I think we pretty much have to if we want to avoid spurious errors). However, we don't do that for directories. |
Ah right, I guess that this ACP is a superset of the change I linked. They should probably be considered together since it seems changing rust-lang/rust#127576 resolves the part of this that can't easily be adjusted by the user. It is trivial to ignore the top-level error if that is the desired behavior. |
Some prior discussion in #170 |
Another relevant thread: rust-lang/rust#105745. |
We discussed this in last week's libs-api meeting. The consensus was to:
This is essentially the solution implemented in rust-lang/rust#127623. While we appreciate that it can be desirable for |
Proposal
Problem statement
It is currently impossible to properly check for errors with
std::fs::remove_dir_all
, especially with concurrent calls to it. This doesn't have to be the case, as we can e.g. see instd::fs::create_dir_all
which can be called multiple times concurrently and succeed.The current documentation of
std::fs::remove_dir_all
currently guarantees thatstd::fs::remove_dir_all
is not idempotent and will return an error if the directory does not exist.Motivating examples or use cases
The Rust repository itself has skipped error checking for
std::fs::remove_dir_all
(as suggested by the documentation itself). We see snippets like the following all over the codebase:The problem is so bad that
cargo-miri
contains an incorrect version of the proposed fixed version:https://github.com/rust-lang/rust/blob/d81987661a06ae8d49a5f014f81824c655e87768/src/tools/miri/cargo-miri/src/util.rs#L289-L298
This version doesn't account for the fact that these functions might return
io::ErrorKind::NotFound
also when any file deletion fails with this error code (on Unix-like systems) or when any of the parent directories ofdir
do not exist.Note that these are just two examples from the rust repository itself, the whole ecosystem probably has more examples like this.
I believe that despite changing documented behavior, this will not break existing programs, since the end result of the directory not existing stays the same.
Solution sketch
Make concurrent calls to
std::fs::remove_dir_all
succeed. If the passed path contains more than one component, still fail withio::ErrorKind::NotFound
when any component but the last one does not exist. I.e.fs::remove_dir_all("foo/bar")
succeeds iffoo
exists butfoo/bar
does not. It'll fail if evenfoo
does not exist.Alternatives
The proposed solution cannot be written using existing APIs, except by copying the implementation from the standard library and modifying it.
Another possible solution would be to also not fail when parent directories of the passed path do not exist.
A third possible solution would be to create a new function which would have the behavior described in this API change proposal. I don't see a use case for the original function so, that's why I didn't propose this.
Links and related work
Not aware of any.
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: