Skip to main content

zagens_runtime_adapters/
util.rs

1//! Small shared helpers for adapters modules.
2
3use std::path::Path;
4
5/// Write `contents` to `path` atomically via a temp file in the same directory.
6pub fn write_atomic(path: &Path, contents: &[u8]) -> std::io::Result<()> {
7    let parent = path.parent().ok_or_else(|| {
8        std::io::Error::new(
9            std::io::ErrorKind::InvalidInput,
10            format!("path has no parent directory: {}", path.display()),
11        )
12    })?;
13    let mut tmp = tempfile::NamedTempFile::new_in(parent)?;
14    std::io::Write::write_all(&mut tmp, contents)?;
15    tmp.as_file().sync_all()?;
16    tmp.persist(path)?;
17    Ok(())
18}