faccess
faccess copied to clipboard
Add function `PathExt::removable`
In Unix, a file is removable if you have write access to the parent directory.
So you can write something like:
fn removable(path: AsRef<Path>) -> Result<bool> {
match path.as_ref().parent() {
Some(parent) => parent.writable(),
None => Ok(false),
}
}
Not sure about Windows tho, what do you think?
Sorry, you return bool and not Result<bool>:
fn removable(path: AsRef<Path>) -> Result<bool> {
path.as_ref().parent().map(|parent| parent.writable()).unwrap_or(false)
}
@marcospb19 I like the idea. I'm trying to use this library to map desired operations to human readable reasons why they are or aren't possible. For example, instead of simply "may I access path X" telling me that it's not accessible, I want it to tell me it's because the path does't exist and that's because it's parent directory doesn't exist (etc.). The goal would be to eventually hook into something like https://docs.rs/fs-err/latest/fs_err/ and give really detailed error messages (behind a feature flag) so when a file operation fails it gives you enough context around the state of the disk to know how to fix it.
So I've also need to map a similar set of logic that you've described here for deletion and I had question on this, what do you do for when the user asks if they can remove the root path? It has no parent. It might be unwise to delete it, but I think it's technically possible (i've not tried for obvious reasons). Is that your understanding or would something prevent it?
@schneems I really like the fs-err wrapper idea, I always use this crate.
So I've also need to map a similar set of logic that you've described here for deletion and I had question on this, what do you do for when the user asks if they can remove the root path? It has no parent. It might be unwise to delete it, but I think it's technically possible (i've not tried for obvious reasons). Is that your understanding or would something prevent it?
In the code snippets I wrote unwrap_or(false), so I always assumed the root directory isn't removable, I think it's actually impossible. (sorry for the late response)