I know that any I/O operation can fail, but I'm trying to reduce the possibilities of any errors.
Some context, I'm writing another Dotfiles Manager (it's in a early stage), and I need to check if I can delete files before I do delete them, because if one of them fail, my program would leave the folder in a messed up state.
Here's my current hack for this, I'll check if I have permissions to write
to a file, using append to leave contents as they are:
use std::{fs::OpenOptions, path::Path};
pub fn can_i_delete_it(path: impl AsRef<Path>) -> bool {
let file = OpenOptions::new()
.create(false)
.append(true)
.open(path.as_ref());
let ok = file.is_ok();
ok
}
If I can write to a file, I can delete it too, is this really guaranteed to work with files
? What about directories?
The problem I found by using fs::Metadata
is that it gives me the mode permission bits
but that's all, I wanted a more straight forward way than reading groups and users around.