use crate::runtime::CONTEXT;
use crate::runtime::driver::op::{Completable, CqeResult, Op};
use std::ffi::CString;
use std::io;
use std::path::Path;
pub(crate) struct Unlink {
pub(crate) path: CString,
}
impl Op<Unlink> {
pub(crate) fn unlink_dir(path: &Path) -> io::Result<Op<Unlink>> {
Self::unlink(path, libc::AT_REMOVEDIR)
}
pub(crate) fn unlink_file(path: &Path) -> io::Result<Op<Unlink>> {
Self::unlink(path, 0)
}
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| {
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(|_| ())
}
}