use std::path::{Path, PathBuf};
use crate::{Error, token};
pub(crate) struct TempDir(PathBuf);
impl TempDir {
pub(crate) fn new() -> Result<Self, Error> {
let path = std::env::temp_dir().join(format!("hazine-{}", token::generate(12)?));
std::fs::create_dir(&path)?;
let dir = TempDir(path);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&dir.0, std::fs::Permissions::from_mode(0o700))?;
}
Ok(dir)
}
pub(crate) fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
if let Err(error) = std::fs::remove_dir_all(&self.0) {
tracing::warn!(path = %self.0.display(), %error, "cannot remove temporary directory");
}
}
}