use dope_sys::Errno;
use dope_uring::Backend;
use o3::base::marker::ThreadBound;
use std::{
env,
path::Path,
process,
time::{self, SystemTime, SystemTimeError},
};
pub mod cleanup;
mod layout;
pub mod options;
pub mod packets;
pub mod ring;
pub mod storage;
pub use layout::Layout;
#[derive(Debug)]
pub enum DirectoryError {
Path(std::ffi::NulError),
Clock(SystemTimeError),
Syscall(Errno),
}
pub struct Directory(std::ffi::CString, ThreadBound);
impl Directory {
pub fn try_new(tag: &str) -> Result<Self, DirectoryError> {
use dope_sys::io::file::{File as _, Mode};
use std::os::unix::ffi::OsStrExt as _;
let nonce = SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.map_err(DirectoryError::Clock)?
.as_nanos();
let path = env::temp_dir().join(format!("du-{tag}-{}-{nonce}", process::id()));
let encoded =
std::ffi::CString::new(path.as_os_str().as_bytes()).map_err(DirectoryError::Path)?;
Backend::mkdir::<Errno>(&encoded, Mode::new(0o700)).map_err(DirectoryError::Syscall)?;
Ok(Self(encoded, ThreadBound::NEW))
}
pub fn path(&self) -> &Path {
use std::os::unix::ffi::OsStrExt as _;
Path::new(std::ffi::OsStr::from_bytes(self.0.to_bytes()))
}
pub fn c_path(&self) -> &std::ffi::CStr {
&self.0
}
}
impl Drop for Directory {
fn drop(&mut self) {
use dope_sys::io::file::File as _;
assert!(
Backend::rmdir_at::<Errno>(None, &self.0).is_ok(),
"fixture directory cleanup"
);
}
}