faccess icon indicating copy to clipboard operation
faccess copied to clipboard

Add function `PathExt::removable`

Open marcospb19 opened this issue 2 years ago • 4 comments

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?

marcospb19 avatar Jun 02 '23 16:06 marcospb19

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 avatar Jun 02 '23 17:06 marcospb19

@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 avatar Sep 07 '24 23:09 schneems

@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)

marcospb19 avatar Oct 22 '24 02:10 marcospb19