Function nc::unlinkat

source ·
pub unsafe fn unlinkat<P: AsRef<Path>>(
    dfd: i32,
    filename: P,
    flag: i32,
) -> Result<(), Errno>
Expand description

Delete a name and possibly the file it refers to.

§Examples

let path = "/tmp/nc-unlinkat";
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());
// /tmp folder is not empty, so this call always returns error.
let ret = unsafe { nc::unlinkat(nc::AT_FDCWD, path, nc::AT_REMOVEDIR) };
assert!(ret.is_err());
let ret = unsafe { nc::unlinkat(nc::AT_FDCWD, path, 0) };
assert!(ret.is_ok());
Examples found in repository?
examples/unlink.rs (line 16)
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());
}
More examples
Hide additional examples
examples/attr.rs (line 14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    let path = "/tmp/nc-ioctl";
    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 mut attr: i32 = 0;
    let cmd = nc::FS_IOC_GETFLAGS;
    let ret = unsafe { nc::ioctl(fd, cmd, &mut attr as *mut i32 as *const _) };
    assert!(ret.is_ok());
    println!("attr: {}", attr);

    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/mount.rs (line 18)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    let target_dir = "/tmp/nc-mount";
    let ret = unsafe { nc::mkdirat(nc::AT_FDCWD, target_dir, 0o755) };
    assert!(ret.is_ok());

    let src_dir = "/etc";
    let fs_type = "";
    let mount_flags = nc::MS_BIND | nc::MS_RDONLY;
    let data = std::ptr::null_mut();
    let ret = unsafe { nc::mount(src_dir, target_dir, fs_type, mount_flags, data) };
    assert!(ret.is_ok());
    let ret = unsafe { nc::umount2(target_dir, nc::UMOUNT_NOFOLLOW) };
    assert!(ret.is_ok());
    let ret = unsafe { nc::unlinkat(nc::AT_FDCWD, target_dir, nc::AT_REMOVEDIR) };
    assert!(ret.is_ok());
}