Function nc::getdents64

source ·
pub unsafe fn getdents64(fd: i32, dir_buf: &mut [u8]) -> Result<ssize_t, Errno>
Expand description

Get directory entries.

§Examples

const BUF_SIZE: usize = 1 * 1024;

let path = "/etc";
let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_DIRECTORY, 0) };
assert!(ret.is_ok());
let fd = ret.unwrap();
let mut buf = [0; BUF_SIZE];

loop {
    let ret = unsafe { nc::getdents64(fd, &mut buf) };
    assert!(ret.is_ok());
    let nread = ret.unwrap() as usize;
    if nread == 0 {
        break;
    }

    let buf_ptr = buf.as_ptr() as usize;
    let mut bpos: usize = 0;

    println!("--------------- nread={nread} ---------------");
    println!("inode#    file type  d_reclen  d_off   d_name");
    while bpos < nread {
        let d = (buf_ptr + bpos) as *mut nc::linux_dirent64_t;
        let d_ref: &nc::linux_dirent64_t = unsafe { &(*d) };
        let d_type = match d_ref.d_type {
            nc::DT_REG => "regular",
            nc::DT_DIR => "directory",
            nc::DT_FIFO => "FIFO",
            nc::DT_SOCK => "socket",
            nc::DT_LNK => "symlink",
            nc::DT_BLK => "block-dev",
            nc::DT_CHR => "char-dev",
            _ => "unknown",
        };

        let name = std::str::from_utf8(d_ref.name()).unwrap();
        println!(
            "{: >8}  {:>10} {: >4} {: >12}  {}",
            d_ref.d_ino, d_type, d_ref.d_reclen, d_ref.d_off as u32, name
        );

        bpos += d_ref.d_reclen as usize;
    }
}

let ret = unsafe { nc::close(fd) };
assert!(ret.is_ok());
Examples found in repository?
examples/dir.rs (line 17)
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
58
59
60
61
62
63
64
fn main() {
    const BUF_SIZE: usize = 1024;

    let path = "/etc";
    let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_DIRECTORY, 0) };
    assert!(ret.is_ok());
    let fd = ret.unwrap();
    let mut buf = [0; BUF_SIZE];

    loop {
        let ret = unsafe { nc::getdents64(fd, &mut buf) };
        assert!(ret.is_ok());
        let nread = ret.unwrap() as usize;
        if nread == 0 {
            break;
        }

        let buf_ptr: *const u8 = buf.as_ptr();
        let mut bpos: usize = 0;

        println!("--------------- nread={nread} ---------------");
        println!("inode#    file type  d_reclen  d_off   d_name");
        while bpos < nread {
            let d = buf_ptr.wrapping_add(bpos) as *mut nc::linux_dirent_t;
            let d_ref: &nc::linux_dirent_t = unsafe { &(*d) };
            let d_type = match d_ref.d_type() {
                nc::DT_REG => "regular",
                nc::DT_DIR => "directory",
                nc::DT_FIFO => "FIFO",
                nc::DT_SOCK => "socket",
                nc::DT_LNK => "symlink",
                nc::DT_BLK => "block-dev",
                nc::DT_CHR => "char-dev",
                _ => "unknown",
            };

            if let Ok(name) = std::str::from_utf8(d_ref.name()) {
                println!(
                    "{: >8}  {:>10} {: >4} {: >12}  {}",
                    d_ref.d_ino, d_type, d_ref.d_reclen, d_ref.d_off as u32, name
                );
            } else {
                eprintln!("Invalid name: {:?}", d_ref.name());
            }

            bpos += d_ref.d_reclen as usize;
        }
    }
    let offset = offset_of!(nc::linux_dirent_t, d_name);
    println!("offset of d_name is: {}", offset);
    println!(
        "offset of d_reclen is: {}",
        offset_of!(nc::linux_dirent_t, d_reclen)
    );

    let ret = unsafe { nc::close(fd) };
    assert!(ret.is_ok());
}