Function nc::fstat

source ·
pub unsafe fn fstat(fd: i32, statbuf: &mut stat_t) -> Result<(), Errno>
Expand description

Get file status about a file descriptor.

§example

let path = "/tmp";
// Open folder directly.
let fd = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_PATH, 0) };
assert!(fd.is_ok());
let fd = fd.unwrap();
let mut stat = nc::stat_t::default();
let ret = unsafe { nc::fstat(fd, &mut stat) };
assert!(ret.is_ok());
// Check fd is a directory.
assert_eq!((stat.st_mode & nc::S_IFMT), nc::S_IFDIR);
let ret = unsafe { nc::close(fd) };
assert!(ret.is_ok());
Examples found in repository?
examples/mmap.rs (line 12)
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
43
44
45
46
47
48
49
50
51
52
53
fn main() {
    let path = "/etc/passwd";
    let fd = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_RDONLY, 0o644) };
    assert!(fd.is_ok());
    let fd = fd.unwrap();

    let mut sb = nc::stat_t::default();
    let ret = unsafe { nc::fstat(fd, &mut sb) };
    assert!(ret.is_ok());

    let offset: usize = 0;
    let length: usize = sb.st_size as usize - offset;
    // Offset for mmap must be page aligned.
    let pa_offset: usize = offset & !(nc::PAGE_SIZE - 1);
    let map_length = length + offset - pa_offset;

    #[cfg(target_arch = "arm")]
    let addr = unsafe {
        nc::mmap2(
            0, // 0 as NULL
            map_length,
            nc::PROT_READ,
            nc::MAP_PRIVATE,
            fd,
            pa_offset as nc::off_t,
        )
    };
    #[cfg(not(target_arch = "arm"))]
    let addr = unsafe {
        nc::mmap(
            0, // 0 as NULL
            map_length,
            nc::PROT_READ,
            nc::MAP_PRIVATE,
            fd,
            pa_offset as nc::off_t,
        )
    };
    assert!(addr.is_ok());

    let addr = addr.unwrap();
    let n_write = unsafe { nc::write(1, addr + offset - pa_offset, length) };
    assert!(n_write.is_ok());
    assert_eq!(n_write, Ok(length as nc::ssize_t));
    let ret = unsafe { nc::munmap(addr, map_length) };
    assert!(ret.is_ok());
    let ret = unsafe { nc::close(fd) };
    assert!(ret.is_ok());
}
More examples
Hide additional examples
examples/sendfile.rs (line 42)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    println!("S_IRUSR: {}", nc::S_IRUSR);
    println!("S_IRGRP: {}", nc::S_IRGRP);

    #[cfg(any(target_os = "linux", target_os = "android"))]
    let in_fd = unsafe {
        nc::openat(nc::AT_FDCWD, "/etc/passwd", nc::O_RDONLY, 0).expect("Failed to open file!")
    };

    #[cfg(target_os = "freebsd")]
    let in_fd = unsafe {
        nc::openat(nc::AT_FDCWD, "/etc/passwd", nc::O_RDONLY, 0).expect("Failed to open file!")
    };

    #[cfg(any(target_os = "linux", target_os = "android"))]
    let out_fd = unsafe {
        nc::openat(
            nc::AT_FDCWD,
            "/tmp/passwd.copy",
            nc::O_WRONLY | nc::O_CREAT,
            nc::S_IRUSR | nc::S_IWUSR | nc::S_IRGRP | nc::S_IROTH,
        )
        .expect("Failed to open passwd copy file")
    };

    #[cfg(target_os = "freebsd")]
    let out_fd = unsafe {
        nc::openat(
            nc::AT_FDCWD,
            "/tmp/passwd.copy",
            nc::O_WRONLY | nc::O_CREAT,
            nc::S_IRUSR | nc::S_IWUSR | nc::S_IRGRP | nc::S_IROTH,
        )
        .expect("Failed to open passwd copy file")
    };

    let mut stat = nc::stat_t::default();
    unsafe { nc::fstat(in_fd, &mut stat).expect("Failed to get file stat!") };
    println!("stat: {:?}", stat);

    let count = stat.st_blksize as usize;
    println!("count: {}", count);
    let mut offset = 0;
    let nread = unsafe {
        nc::sendfile(out_fd, in_fd, &mut offset, count).expect("Failed to call sendfile()")
    };
    println!("nbytes: {}", nread);

    unsafe {
        let _ = nc::close(in_fd);
        let _ = nc::close(out_fd);
    }
}