I need to execute the equivalent of chattr i myfile.txt
in rust.
Since the call for chmod
is fs::set_permissions()
I was expecting something like fs::set_attributes()
to also exists, but I cannot find it in the docs.
Is there an std function to set (linux) file attributes?
CodePudding user response:
There is nothing in the standard library for this. The i
attribute, and file attributes in general, are very system specific, and are outside the scope for a portable standard library.
Internally, the chattr
uses the FS_IOC_SETFLAGS
ioctl
. You may have to implement code that uses it yourself, using a crate like nix can help.
CodePudding user response:
Use std::os::unix::fs::PermissionsExt
.
Example from the stdlib:
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();
permissions.set_mode(0o644); // Read/write for owner and read for others.
assert_eq!(permissions.mode(), 0o644);
Ok(())
}