Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are permissions applied to a file using set_mode?

Tags:

rust

If my understanding is correct, the following code should produce an executable file. However it doesn't; it gets created, but the permissions specified aren't applied. What am I doing wrong?

use std::fs;
use std::os::unix::PermissionsExt;


fn main() {
    fs::File::create("somefile").unwrap()
        .metadata().unwrap()
        .permissions()
        .set_mode(0o770);
}
like image 709
urubi Avatar asked Dec 03 '22 17:12

urubi


2 Answers

Use OpenOptions:

use std::fs;
use std::os::unix::OpenOptionsExt;

fn main() {
    fs::OpenOptions::new()
        .create(true)
        .write(true)
        .mode(0o770)
        .open("somefile")
        .unwrap();
}
like image 56
Renato Zannon Avatar answered Dec 05 '22 07:12

Renato Zannon


You can also use set_permissions for existing path

use std::fs;
use std::os::unix::fs::PermissionsExt;

fn main(){
     fs::set_permissions("/path", fs::Permissions::from_mode(0o655)).unwrap();
}
like image 33
Karthik Nedunchezhiyan Avatar answered Dec 05 '22 07:12

Karthik Nedunchezhiyan