use std::ffi::OsString;
use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
static TEMP_FILE_SEQUENCE: AtomicU64 = AtomicU64::new(0);
pub fn write_atomic_file(destination: &Path, contents: &[u8]) -> io::Result<()> {
let parent = destination
.parent()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "destination has no parent"))?;
let parent = if parent.as_os_str().is_empty() {
Path::new(".")
} else {
parent
};
fs::create_dir_all(parent)?;
let name = destination
.file_name()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "destination has no name"))?;
let (temporary, mut file) = create_temporary(parent, name)?;
let result = (|| {
file.write_all(contents)?;
file.sync_all()?;
drop(file);
atomic_replace(&temporary, destination)?;
sync_parent_directory(parent)?;
Ok(())
})();
if result.is_err() {
let _ = fs::remove_file(&temporary);
}
result
}
fn create_temporary(parent: &Path, name: &std::ffi::OsStr) -> io::Result<(PathBuf, File)> {
for _ in 0..32 {
let sequence = TEMP_FILE_SEQUENCE.fetch_add(1, Ordering::Relaxed);
let mut temporary_name = OsString::from(".");
temporary_name.push(name);
temporary_name.push(format!(".tmp.{}.{}", std::process::id(), sequence));
let temporary = parent.join(temporary_name);
match OpenOptions::new()
.write(true)
.create_new(true)
.open(&temporary)
{
Ok(file) => return Ok((temporary, file)),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
Err(error) => return Err(error),
}
}
Err(io::Error::new(
io::ErrorKind::AlreadyExists,
"could not allocate a unique temporary file",
))
}
#[cfg(unix)]
fn sync_parent_directory(parent: &Path) -> io::Result<()> {
File::open(parent)?.sync_all()
}
#[cfg(not(unix))]
fn sync_parent_directory(_parent: &Path) -> io::Result<()> {
Ok(())
}
#[cfg(not(windows))]
fn atomic_replace(source: &Path, destination: &Path) -> io::Result<()> {
fs::rename(source, destination)
}
#[cfg(windows)]
fn atomic_replace(source: &Path, destination: &Path) -> io::Result<()> {
use std::os::windows::ffi::OsStrExt;
use std::thread;
use std::time::Duration;
const MOVEFILE_REPLACE_EXISTING: u32 = 0x1;
const MOVEFILE_WRITE_THROUGH: u32 = 0x8;
const REPLACE_WAIT_ATTEMPTS: u32 = 100;
const REPLACE_WAIT_INTERVAL: Duration = Duration::from_millis(1);
const ERROR_ACCESS_DENIED: i32 = 5;
const ERROR_SHARING_VIOLATION: i32 = 32;
const ERROR_LOCK_VIOLATION: i32 = 33;
#[link(name = "Kernel32")]
extern "system" {
fn MoveFileExW(existing: *const u16, new: *const u16, flags: u32) -> i32;
}
let source: Vec<u16> = source.as_os_str().encode_wide().chain(Some(0)).collect();
let destination: Vec<u16> = destination
.as_os_str()
.encode_wide()
.chain(Some(0))
.collect();
for attempt in 0..REPLACE_WAIT_ATTEMPTS {
let succeeded = unsafe {
MoveFileExW(
source.as_ptr(),
destination.as_ptr(),
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH,
)
};
if succeeded != 0 {
return Ok(());
}
let error = io::Error::last_os_error();
if !matches!(
error.raw_os_error(),
Some(ERROR_ACCESS_DENIED) | Some(ERROR_SHARING_VIOLATION) | Some(ERROR_LOCK_VIOLATION)
) || attempt + 1 == REPLACE_WAIT_ATTEMPTS
{
return Err(error);
}
thread::sleep(REPLACE_WAIT_INTERVAL);
}
unreachable!("bounded replacement always returns on success or final failure")
}
#[cfg(test)]
mod tests {
use super::*;
fn temp_root() -> PathBuf {
static NEXT: AtomicU64 = AtomicU64::new(0);
std::env::temp_dir().join(format!(
"qx-core-atomic-file-{}-{}",
std::process::id(),
NEXT.fetch_add(1, Ordering::Relaxed)
))
}
#[test]
fn replacement_updates_existing_file_without_a_delete_window() {
let root = temp_root();
let path = root.join("state.json");
write_atomic_file(&path, b"old-state").unwrap();
write_atomic_file(&path, b"new-state").unwrap();
assert_eq!(fs::read(&path).unwrap(), b"new-state");
fs::remove_dir_all(root).unwrap();
}
#[test]
fn failed_replacement_preserves_existing_destination() {
let root = temp_root();
fs::create_dir_all(&root).unwrap();
let destination = root.join("state.json");
fs::create_dir(&destination).unwrap();
assert!(write_atomic_file(&destination, b"new-state").is_err());
assert!(destination.is_dir());
assert_eq!(fs::read_dir(&root).unwrap().count(), 1);
fs::remove_dir_all(root).unwrap();
}
}