1
2
3
4
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
use crate::runtime::CONTEXT;
use crate::runtime::driver::op::{Completable, CqeResult, Op};
use std::ffi::CString;
use std::io;
use std::path::Path;
/// Unlink a path relative to the current working directory of the caller's process.
pub(crate) struct Unlink {
pub(crate) path: CString,
}
impl Op<Unlink> {
/// Submit a request to unlink a directory with provided flags.
pub(crate) fn unlink_dir(path: &Path) -> io::Result<Op<Unlink>> {
Self::unlink(path, libc::AT_REMOVEDIR)
}
/// Submit a request to unlink a file with provided flags.
pub(crate) fn unlink_file(path: &Path) -> io::Result<Op<Unlink>> {
Self::unlink(path, 0)
}
/// Submit a request to unlink a specified path with provided flags.
pub(crate) fn unlink(path: &Path, flags: i32) -> io::Result<Op<Unlink>> {
use io_uring::{opcode, types};
let path = super::util::cstr(path)?;
CONTEXT.with(|x| {
x.handle()
.expect("Not in a runtime context")
.submit_op(Unlink { path }, |unlink| {
// Get a reference to the memory. The string will be held by the
// operation state and will not be accessed again until the operation
// completes.
let p_ref = unlink.path.as_c_str().as_ptr();
opcode::UnlinkAt::new(types::Fd(libc::AT_FDCWD), p_ref)
.flags(flags)
.build()
})
})
}
}
impl Completable for Unlink {
type Output = io::Result<()>;
fn complete(self, cqe: CqeResult) -> Self::Output {
cqe.result.map(|_| ())
}
}