Function nc::openat

source ·
pub unsafe fn openat<P: AsRef<Path>>(
    dirfd: i32,
    filename: P,
    flags: i32,
    mode: mode_t
) -> Result<i32, Errno>
Expand description

Open and possibly create a file within a directory.

§Example

let path = "/etc/passwd";
let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_RDONLY, 0) };
assert!(ret.is_ok());
let fd = ret.unwrap();
let ret = unsafe { nc::close(fd) };
assert!(ret.is_ok());
Examples found in repository?
examples/file.rs (line 23)
22
23
24
25
26
27
28
29
30
31
    pub fn open(&mut self) -> Result<(), nc::Errno> {
        let ret = unsafe { nc::openat(nc::AT_FDCWD, self.path.to_str().unwrap(), nc::O_RDONLY, 0) };
        match ret {
            Ok(fd) => {
                self.fd = fd;
                Ok(())
            }
            Err(errno) => Err(errno),
        }
    }
More examples
Hide additional examples
examples/unlink.rs (line 11)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    #[cfg(feature = "std")]
    let path = std::path::Path::new("/tmp/nc-unlink");
    #[cfg(not(feature = "std"))]
    let path = "/tmp/nc.unlink";

    let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_WRONLY | nc::O_CREAT, 0o644) };
    assert!(ret.is_ok());
    let fd = ret.unwrap();
    let ret = unsafe { nc::close(fd) };
    assert!(ret.is_ok());
    let ret = unsafe { nc::unlinkat(nc::AT_FDCWD, path, 0) };
    assert!(ret.is_ok());
}
examples/pty.rs (line 21)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fn open_pty() -> Result<(i32, i32), nc::Errno> {
    #[cfg(any(target_os = "linux", target_os = "android"))]
    let pty_fd = unsafe { nc::openat(nc::AT_FDCWD, "/dev/ptmx", nc::O_RDWR, 0)? };
    #[cfg(target_os = "freebsd")]
    let pty_fd = unsafe { nc::open("/dev/ptmx", nc::O_RDWR, 0)? };

    println!("pty_fd: {}", pty_fd);

    let sname = ptsname(pty_fd)?;
    println!("sname: {}", sname);
    unlockpt(pty_fd)?;

    #[cfg(any(target_os = "linux", target_os = "android"))]
    let tty_fd = unsafe { nc::openat(nc::AT_FDCWD, &sname, nc::O_RDWR | nc::O_NOCTTY, 0)? };

    #[cfg(target_os = "freebsd")]
    let tty_fd = unsafe { nc::open(&sname, nc::O_RDWR | nc::O_NOCTTY, 0)? };

    println!("tty_fd: {}", tty_fd);
    Ok((pty_fd, tty_fd))
}
examples/open.rs (lines 19-24)
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
fn main() -> Result<(), nc::Errno> {
    let path = "/tmp/hello.rs";

    #[cfg(target_os = "freebsd")]
    let fd = unsafe {
        nc::open(
            path,
            nc::O_CREAT | nc::O_RDWR,
            nc::S_IRUSR | nc::S_IWUSR | nc::S_IRGRP | nc::S_IROTH,
        )?
    };

    #[cfg(any(target_os = "linux", target_os = "android"))]
    let fd = unsafe {
        nc::openat(
            nc::AT_FDCWD,
            path,
            nc::O_CREAT | nc::O_RDWR,
            nc::S_IRUSR | nc::S_IWUSR | nc::S_IRGRP | nc::S_IROTH,
        )?
    };

    let msg = "fn main() { println!(\"Hello, world\");}";

    let n_write = unsafe { nc::write(fd, msg.as_ptr() as usize, msg.len()) };
    assert!(n_write.is_ok());
    let ret = unsafe { nc::close(fd) };
    assert!(ret.is_ok());
    Ok(())
}
examples/read.rs (line 7)
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
fn main() -> Result<(), nc::Errno> {
    #[cfg(any(target_os = "linux", target_os = "android"))]
    let fd = unsafe { nc::openat(nc::AT_FDCWD, "/etc/passwd", nc::O_RDONLY, 0)? };

    #[cfg(target_os = "freebsd")]
    let fd = unsafe { nc::open("/etc/passwd", nc::O_RDONLY, 0)? };

    let mut buf: [u8; 256] = [0; 256];
    loop {
        let n_read = unsafe { nc::read(fd, buf.as_mut_ptr() as usize, buf.len()) };
        match n_read {
            Ok(n) => {
                if n == 0 {
                    break;
                }
                // FIXME(Shaohua): Read buf with len(n).
                if let Ok(s) = std::str::from_utf8(&buf) {
                    print!("s: {}", s);
                } else {
                    eprintln!("Failed to read buf as UTF-8!");
                    break;
                }
            }
            Err(errno) => {
                eprintln!("Failed to read, got errno: {}", errno);
                break;
            }
        }
    }

    unsafe { nc::close(fd) }
}
examples/dir.rs (line 7)
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
fn main() {
    let path = "/etc";
    let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_DIRECTORY, 0) };
    assert!(ret.is_ok());
    let fd = ret.unwrap();

    const BUF_SIZE: usize = 4 * 1024;
    loop {
        // TODO(Shaohua): Only allocate one buf block.
        let mut buf: Vec<u8> = vec![0; BUF_SIZE];
        let ret = unsafe { nc::getdents64(fd, buf.as_mut_ptr() as usize, BUF_SIZE) };
        assert!(ret.is_ok());

        let buf_box = buf.into_boxed_slice();
        let buf_box_ptr = Box::into_raw(buf_box) as *mut u8 as usize;
        let nread = ret.unwrap() as usize;
        if nread == 0 {
            break;
        }

        let mut bpos: usize = 0;
        while bpos < nread {
            let d = (buf_box_ptr + bpos) as *mut nc::linux_dirent64_t;
            let d_ref = unsafe { &(*d) };
            let mut name_vec: Vec<u8> = vec![];
            // TODO(Shaohua): Calculate string len of name.
            for i in 0..nc::PATH_MAX {
                let c = d_ref.d_name[i as usize];
                if c == 0 {
                    break;
                }
                name_vec.push(c);
            }
            let name = String::from_utf8(name_vec).unwrap();
            println!("name: {}", name);

            bpos += d_ref.d_reclen as usize;
        }
    }

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