Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set file permissions in Unix and still working on Windows

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading