feat: function to set file readonly

This commit is contained in:
Gabriele Musco 2023-06-15 17:54:38 +02:00
parent ab1f4c1093
commit 9229d082ee
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE

View file

@ -1,6 +1,6 @@
use std::{
env,
fs::{create_dir_all, OpenOptions},
fs::{self, create_dir_all, OpenOptions},
io::BufWriter,
path::Path,
};
@ -53,3 +53,16 @@ pub fn get_data_dir() -> String {
),
}
}
pub fn set_file_radonly(path_s: &String, readonly: bool) {
let path = Path::new(&path_s);
if !path.is_file() {
println!("WARN: trying to set readonly on a file that does not exist");
return;
}
let mut perms = fs::metadata(path)
.expect_dialog("Could not get metadata for file")
.permissions();
perms.set_readonly(readonly);
fs::set_permissions(path, perms).expect_dialog("Could not set permissions for file")
}