use std::path::Path;
use crate::core::error::{Error, Result};
pub fn atomic_write(path: &Path, body: &[u8]) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| Error::Io {
path: parent.to_path_buf(),
source: e,
})?;
}
let tmp = match path.file_name() {
Some(name) => {
let mut t = name.to_os_string();
t.push(".tmp");
path.with_file_name(t)
}
None => path.with_extension("tmp"),
};
std::fs::write(&tmp, body).map_err(|e| Error::Io {
path: tmp.clone(),
source: e,
})?;
std::fs::rename(&tmp, path).map_err(|e| Error::Io {
path: path.to_path_buf(),
source: e,
})
}