Function nc::stat

source ·
pub unsafe fn stat<P: AsRef<Path>>(
    filename: P,
    statbuf: &mut stat_t
) -> Result<(), Errno>
Expand description

Get file status about a file.

§Example

let path = "/etc/passwd";
let mut stat = nc::stat_t::default();
let ret = unsafe { nc::stat(path, &mut stat) };
assert!(ret.is_ok());
// Check fd is a regular file.
assert_eq!((stat.st_mode & nc::S_IFMT), nc::S_IFREG);
Examples found in repository?
examples/stat.rs (line 16)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let mut statbuf = nc::stat_t::default();
    let filepath = "/dev/fd/0";
    #[cfg(target_os = "linux")]
    let ret = {
        #[cfg(not(any(
            target_arch = "aarch64",
            target_arch = "loongarch64",
            target_arch = "riscv64"
        )))]
        unsafe {
            nc::stat(filepath, &mut statbuf)
        }

        #[cfg(any(
            target_arch = "aarch64",
            target_arch = "loongarch64",
            target_arch = "riscv64"
        ))]
        unsafe {
            nc::fstatat(nc::AT_FDCWD, filepath, &mut statbuf, 0)
        }
    };
    #[cfg(any(target_os = "android", target_os = "freebsd"))]
    let ret = unsafe { nc::fstatat(nc::AT_FDCWD, filepath, &mut statbuf, 0) };

    match ret {
        Ok(_) => {
            println!("s: {:?}", statbuf);
        }
        Err(errno) => {
            eprintln!(
                "Failed to get file status, got errno: {}",
                nc::strerror(errno)
            );
        }
    }
}