Unable to set permissions in RUST

Hi I have just started to program in Rust.

Here is my build.rs

use std::fs::{File, self};
use std::io::Write;
use std::os::unix::prelude::PermissionsExt;
use std::env;
use std::path::{Path, PathBuf};


fn main() {
    let out_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
    let dest_path: PathBuf = Path::new(&out_dir).join("target/release/Settings.toml");
    println!("{:?}", dest_path.as_os_str());
    let f = File::create(dest_path);
    f.as_ref()
    .unwrap()
    .write_all("largest_number = 7".as_bytes())
    .unwrap();
    set_permissions(f);
}

fn set_permissions(f: Result<File, std::io::Error>) {

    f.as_ref()
    .unwrap()
    .metadata()
    .unwrap()
    .permissions()
    .set_mode(0x777);

}

The problem I am having is even after executing cargo build the permissions of the Settings.toml file is not 777.

root@xxxx:~/code/rust/addition# ls -larth target/release/
total 5.3M
drwxrwxrwx  2 root  root  4.0K Feb 14 19:16 incremental
drwxrwxrwx  2 root  root  4.0K Feb 14 19:16 examples
-rwxrwxrwx  1 root  root     0 Feb 14 19:16 .cargo-lock
drwxrwxrwx  4 root  root  4.0K Feb 14 19:16 ..
-rwxrwxrwx  1 root  root   233 Feb 14 22:32 addition.d
drwxrwxrwx  2 root  root   20K Feb 16 20:31 deps
-rwxrwxr-x  2 vinay vinay 5.2M Feb 16 20:31 addition
drwxrwxrwx 29 root  root  4.0K Feb 16 22:02 build
drwxrwxrwx 90 root  root  4.0K Feb 16 22:02 .fingerprint
drwxrwxrwx  7 root  root  4.0K Feb 16 22:58 .
-rw-r--r--  1 root  root    18 Feb 16 23:17 Settings.toml
root@xxxx:~/code/rust/addition#

>Solution :

From Permissions::set_readonly() docs (the same apply for set_mode()):

This operation does not modify the files attributes. This only changes the in-memory value of these attributes for this Permissions instance. To modify the files attributes use the set_permissions function which commits these attribute changes to the file.

Leave a Reply