Location
std::fs::symlink_metadata or std::fs::set_permissions
Summary
fn main() -> std::io::Result<()> {
use std::os::unix::fs::PermissionsExt;
let path = std::path::PathBuf::from("bar"); // `bar` is a symlink
let meta = std::fs::symlink_metadata(&path)?;
let mut permissions = meta.permissions();
println!("current mode: {:o}", permissions.mode());
permissions.set_mode(0o707);
println!("new mode: {:o}", permissions.mode());
std::fs::set_permissions(&path, permissions)?; // applied permission to original file
let meta = std::fs::symlink_metadata(&path)?;
let mut permissions = meta.permissions();
println!("reloaded mode: {:o}", permissions.mode());
Ok(())
}
current mode: 120755
new mode: 707
reloaded mode: 120755
Since I could obtain metadata for either original file or symlink, I expected there must be a way to apply permissions to symlink. But symlink_metadata revealed not to have a pair of set_permissions.
By the design if it is intended or lack of API, it could be documented somewhere like symlink_metadata or set_permissions.
On the other hand, metadata is obtainable from either file or symlink. Then I expected set_permission to be placed on metadata object if metadata includes symlink information.
e.g. metadata.set_permissions(path, permissions)
Location
std::fs::symlink_metadataorstd::fs::set_permissionsSummary
Since I could obtain metadata for either original file or symlink, I expected there must be a way to apply permissions to symlink. But
symlink_metadatarevealed not to have a pair ofset_permissions.By the design if it is intended or lack of API, it could be documented somewhere like
symlink_metadataorset_permissions.On the other hand, metadata is obtainable from either file or symlink. Then I expected
set_permissionto be placed on metadata object if metadata includes symlink information.e.g.
metadata.set_permissions(path, permissions)