Setmode() not working

Not sure what I do wrong.

My system is a Linux system.

From PermissionsExt in std::os::unix::fs - Rust I have this sample (slightly modified to use different perms and to give some output)

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

fn main() -> std::io::Result<()> {
    let f = File::create("foo.txt")?;
    let metadata = f.metadata()?;
    let mut permissions = metadata.permissions();
    println!("permissions (old): {:o}", permissions.mode());

    permissions.set_mode(0o666); // Read/write for owner and read for others.
    assert_eq!(permissions.mode(), 0o666);

    println!("permissions (new): {:o}", permissions.mode());
    Ok(())
}

Running this program looks good from an output point of view.

But the file looks like this

.rw-r--r--     0 manfred manfred 18 Jan 10:57   foo.txt

What is my mistake?

I suspect you're only changing the in-memory metadata structure, and need to call File::set_permissions to push the new permissions out to the filesystem.

2 Likes

Yes, you are right. When I add

    f.set_permissions(permissions)?;

before the Ok(()) line it works fine.