use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
pub(crate) fn write_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> {
let temp = path.with_extension(format!(
"tmp.{}.{}",
std::process::id(),
TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
));
let mut options = OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut file = options.open(&temp)?;
if let Err(error) = file
.write_all(bytes)
.and_then(|()| file.sync_all())
.and_then(|()| fs::rename(&temp, path))
{
let _ = fs::remove_file(&temp);
return Err(error);
}
if let Some(parent) = path.parent() {
File::open(parent)?.sync_all()?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn replaces_the_file_and_leaves_no_temp_file_on_failure() {
let dir = std::env::temp_dir().join(format!("shipslip-private-{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
let path = dir.join("data.json");
write_atomic(&path, b"one").unwrap();
write_atomic(&path, b"two").unwrap();
assert_eq!(fs::read(&path).unwrap(), b"two");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(
fs::metadata(&path).unwrap().permissions().mode() & 0o777,
0o600
);
}
let blocked = dir.join("blocked.json");
fs::create_dir_all(blocked.join("child")).unwrap();
assert!(write_atomic(&blocked, b"x").is_err());
let names: Vec<_> = fs::read_dir(&dir)
.unwrap()
.map(|entry| entry.unwrap().file_name().into_string().unwrap())
.collect();
let _ = fs::remove_dir_all(&dir);
assert!(
!names.iter().any(|name| name.contains(".tmp.")),
"{names:?}"
);
}
}