How do I set file permissions in Linux while still allowing the program to run on a Windows machine?
On a Unix machine I can do like this:
use std::os::unix::fs::PermissionsExt;
fs::set_permissions("/path", fs::Permissions::from_mode(0o655)).unwrap();
But this will not compile on Windows.
The goal is to disallow other users on a Unix system to read the content of the file.
>Solution :
The easiest way to handle this is probably with configuration attributes.
For instance, you might want something like this:
#[cfg(target_family = "unix")]
fn set_permissions(path: &str) {
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(path, fs::Permissions::from_mode(0o655)).unwrap();
}
#[cfg(not(target_family = "unix"))]
fn set_permissions(path: &str) {}
More examples here and reference documentation here.