use std::fs::{self, File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
fn temporary_path(path: &Path) -> PathBuf {
let name = path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default();
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |elapsed| elapsed.as_nanos());
path.with_file_name(format!(".{name}.{}{nanos:x}", std::process::id()))
}
fn stage_replacement(path: &Path, temporary: &Path, contents: &str) -> std::io::Result<()> {
let permissions = fs::metadata(path)?.permissions();
let mut handle: File = OpenOptions::new()
.write(true)
.create_new(true)
.open(temporary)?;
handle.set_permissions(permissions)?;
handle.write_all(contents.as_bytes())?;
handle.sync_all()
}
pub(crate) fn replace_file(path: &Path, (original, contents): (&str, &str)) -> Result<(), String> {
let failure = |error: std::io::Error| format!("cannot write {}: {error}", path.display());
if fs::read_to_string(path).map_err(failure)? != original {
return Err(format!(
"{} changed while contract generate ran; nothing was written, run it again",
path.display()
));
}
let temporary = temporary_path(path);
let written =
stage_replacement(path, &temporary, contents).and_then(|()| fs::rename(&temporary, path));
let _ = fs::remove_file(&temporary);
written.map_err(failure)
}