Skip to content
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

Add function PathExt::removable #3

Open
marcospb19 opened this issue Jun 2, 2023 · 3 comments
Open

Add function PathExt::removable #3

marcospb19 opened this issue Jun 2, 2023 · 3 comments

Comments

@marcospb19
Copy link

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
Copy link
Author

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

@schneems
Copy link

schneems commented Sep 7, 2024

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

@marcospb19
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants