Trait std::os::unix::fs::PermissionsExt

1.1.0 · source ·
pub trait PermissionsExt {
    // Required methods
    fn mode(&self) -> u32;
    fn set_mode(&mut self, mode: u32);
    fn from_mode(mode: u32) -> Self;
}
Available on Unix only.
Expand description

特定于 Unix 的 fs::Permissions 扩展。

Required Methods§

source

fn mode(&self) -> u32

返回包含该文件的标准 Unix 权限的底层原始 st_mode 位。

Examples
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 permissions = metadata.permissions();

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

fn set_mode(&mut self, mode: u32)

设置此权限集的底层原始位。

Examples
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); // 所有者支持读写,其他人只能读。
    assert_eq!(permissions.mode(), 0o644);
    Ok(())
}
Run
source

fn from_mode(mode: u32) -> Self

从给定的 Unix 权限位集中创建 Permissions 的新实例。

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

// 所有者支持读写,其他人只能读。
let permissions = Permissions::from_mode(0o644);
assert_eq!(permissions.mode(), 0o644);
Run

Implementors§