1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Atomic file write utilities.
//!
//! Provides helpers that write to a temporary file and then rename,
//! ensuring the target file is never left in a partially-written state.
//! Temp files use a unique name per call (PID + counter) so concurrent
//! writers targeting the same path do not collide.
use anyhow::{Context, Result};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
/// Monotonic counter to ensure unique temp filenames within a process.
static TMP_COUNTER: AtomicU64 = AtomicU64::new(0);
/// Build a unique sibling temp path for the given target, e.g.
/// `logs/index.json` → `logs/.index.json.12345.0.tmp`.
fn unique_tmp_path(path: &Path) -> PathBuf {
let counter = TMP_COUNTER.fetch_add(1, Ordering::Relaxed);
let pid = std::process::id();
let file_name = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("zag-atomic");
let tmp_name = format!(".{}.{}.{}.tmp", file_name, pid, counter);
match path.parent() {
Some(parent) => parent.join(tmp_name),
None => PathBuf::from(tmp_name),
}
}
/// Write `content` to `path` atomically.
///
/// Writes to a uniquely-named sibling temp file first, then renames.
/// On Unix, `rename()` is atomic within the same filesystem, so the
/// target file is either the old version or the new one — never a
/// partial write. The unique temp name prevents concurrent writers
/// from clobbering each other's temp files.
pub fn atomic_write(path: &Path, content: &[u8]) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("Failed to create directory: {}", parent.display()))?;
}
let tmp_path = unique_tmp_path(path);
std::fs::write(&tmp_path, content)
.with_context(|| format!("Failed to write temp file: {}", tmp_path.display()))?;
std::fs::rename(&tmp_path, path).with_context(|| {
// Clean up the temp file on rename failure.
let _ = std::fs::remove_file(&tmp_path);
format!(
"Failed to rename {} -> {}",
tmp_path.display(),
path.display()
)
})?;
Ok(())
}
/// Convenience wrapper: atomically write a `&str` to `path`.
pub fn atomic_write_str(path: &Path, content: &str) -> Result<()> {
atomic_write(path, content.as_bytes())
}
#[cfg(test)]
#[path = "file_util_tests.rs"]
mod tests;