1use anyhow::{Context, Result};
7use std::path::Path;
8
9pub fn atomic_write(path: &Path, content: &[u8]) -> Result<()> {
15 if let Some(parent) = path.parent() {
16 std::fs::create_dir_all(parent)
17 .with_context(|| format!("Failed to create directory: {}", parent.display()))?;
18 }
19 let tmp_path = path.with_extension("tmp");
20 std::fs::write(&tmp_path, content)
21 .with_context(|| format!("Failed to write temp file: {}", tmp_path.display()))?;
22 std::fs::rename(&tmp_path, path).with_context(|| {
23 format!(
24 "Failed to rename {} -> {}",
25 tmp_path.display(),
26 path.display()
27 )
28 })?;
29 Ok(())
30}
31
32pub fn atomic_write_str(path: &Path, content: &str) -> Result<()> {
34 atomic_write(path, content.as_bytes())
35}
36
37#[cfg(test)]
38#[path = "file_util_tests.rs"]
39mod tests;