use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use crate::error::ForgeError;
pub(crate) fn unique_suffix() -> String {
static SEQUENCE: AtomicU64 = AtomicU64::new(0);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let sequence = SEQUENCE.fetch_add(1, Ordering::Relaxed);
format!("{}-{nanos}-{sequence}", process::id())
}
pub fn atomic_write(path: &Path, bytes: &[u8]) -> Result<(), ForgeError> {
let parent = path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or(Path::new("."));
let tmp = parent.join(format!(".utf8-forge-tmp-{}", unique_suffix()));
let mut f = fs::File::create_new(&tmp)?;
let write_result = (|| -> std::io::Result<()> {
f.write_all(bytes)?;
match fs::metadata(path) {
Ok(meta) => f.set_permissions(meta.permissions())?,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(e),
}
f.sync_all()?;
drop(f);
fs::rename(&tmp, path)?;
if let Ok(dir) = fs::File::open(parent) {
let _ = dir.sync_all();
}
Ok(())
})();
match write_result {
Ok(()) => Ok(()),
Err(e) => {
let _ = fs::remove_file(&tmp);
Err(ForgeError::Io(e))
}
}
}
pub fn backup_original(
root: &Path,
path: &Path,
backup_dir: &Path,
original: &[u8],
) -> Result<(), ForgeError> {
let rel = path.strip_prefix(root).map_err(|_| {
ForgeError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"file {} is not under root {}",
path.display(),
root.display()
),
))
})?;
let target: PathBuf = backup_dir.join(rel);
if let Some(parent) = target.parent() {
fs::create_dir_all(parent)?;
}
atomic_write(&target, original)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn atomic_write_replaces_content_and_cleans_up_temps() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("a.txt");
fs::write(&p, b"old").unwrap();
atomic_write(&p, b"new-bytes").unwrap();
assert_eq!(fs::read(&p).unwrap(), b"new-bytes");
let leftovers: Vec<_> = fs::read_dir(dir.path())
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| {
e.file_name()
.to_string_lossy()
.starts_with(".utf8-forge-tmp")
})
.collect();
assert!(leftovers.is_empty(), "temp files must be renamed away");
}
#[test]
fn atomic_write_creates_file_when_missing() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("sub").join("b.txt");
assert!(atomic_write(&p, b"x").is_err());
let p2 = dir.path().join("c.txt");
atomic_write(&p2, b"x").unwrap();
assert_eq!(fs::read(&p2).unwrap(), b"x");
}
#[test]
fn failed_rename_cleans_up_temp_and_keeps_destination() {
let dir = tempfile::tempdir().unwrap();
let destination = dir.path().join("occupied");
fs::create_dir(&destination).unwrap();
fs::write(destination.join("original"), b"old").unwrap();
assert!(atomic_write(&destination, b"replacement").is_err());
assert_eq!(fs::read(destination.join("original")).unwrap(), b"old");
assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1);
}
#[test]
fn concurrent_writes_produce_complete_content_without_temp_leftovers() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("shared.cpp");
std::thread::scope(|scope| {
let handles: Vec<_> = (0..16u8)
.map(|value| {
let path = &path;
scope.spawn(move || atomic_write(path, &vec![value; 4096]))
})
.collect();
for handle in handles {
handle.join().unwrap().unwrap();
}
});
let bytes = fs::read(&path).unwrap();
assert_eq!(bytes.len(), 4096);
assert!(bytes[0] < 16);
assert!(bytes.iter().all(|byte| *byte == bytes[0]));
assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1);
}
#[test]
fn backup_mirrors_relative_structure() {
let root = tempfile::tempdir().unwrap();
let backup = tempfile::tempdir().unwrap();
let src = root.path().join("GameServer").join("Channel.cpp");
fs::create_dir_all(src.parent().unwrap()).unwrap();
fs::write(&src, b"original").unwrap();
backup_original(root.path(), &src, backup.path(), b"original").unwrap();
let backed = backup.path().join("GameServer").join("Channel.cpp");
assert_eq!(fs::read(&backed).unwrap(), b"original");
}
#[test]
fn backup_rejects_path_outside_root() {
let root = tempfile::tempdir().unwrap();
let backup = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let src = outside.path().join("x.cpp");
fs::write(&src, b"x").unwrap();
let res = backup_original(root.path(), &src, backup.path(), b"x");
assert!(res.is_err());
}
}