use std::{
io::Write,
path::{Path, PathBuf},
};
pub const FAILPOINT_ENV: &str = "GRIZ_FAILPOINT";
pub fn failpoint(name: &str) {
if std::env::var(FAILPOINT_ENV).is_ok_and(|armed| armed == name) {
std::process::abort();
}
}
pub fn failpoint_result(name: &str) -> std::io::Result<()> {
if std::env::var(FAILPOINT_ENV).is_ok_and(|armed| armed == name) {
return Err(std::io::Error::other(format!("failpoint: {name}")));
}
Ok(())
}
#[must_use]
pub fn temp_sibling(path: &Path) -> PathBuf {
let name = path
.file_name()
.map_or_else(|| "griz".into(), |name| name.to_string_lossy().into_owned());
path.with_file_name(format!(
".{name}.griz-{}.tmp",
uuid::Uuid::now_v7().simple()
))
}
pub struct StagedFile {
path: PathBuf,
committed: bool,
}
impl StagedFile {
pub fn commit(mut self, path: &Path) -> std::io::Result<()> {
std::fs::rename(&self.path, path)?;
self.committed = true;
Ok(())
}
}
impl Drop for StagedFile {
fn drop(&mut self) {
if !self.committed {
let _ = std::fs::remove_file(&self.path);
}
}
}
pub fn stage(path: &Path, text: &str) -> std::io::Result<StagedFile> {
let permissions = existing_permissions(path)?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let temp = temp_sibling(path);
let mut file = create_staged(&temp, permissions.as_ref())?;
let staged = StagedFile {
path: temp,
committed: false,
};
failpoint("after_stage_create");
let result = write_staged(&mut file, text, permissions);
drop(file);
result?;
Ok(staged)
}
fn write_staged(
file: &mut std::fs::File,
text: &str,
permissions: Option<std::fs::Permissions>,
) -> std::io::Result<()> {
file.write_all(text.as_bytes())?;
failpoint_result("after_stage_write")?;
if let Some(permissions) = permissions {
file.set_permissions(permissions)?;
}
file.sync_all()
}
fn existing_permissions(path: &Path) -> std::io::Result<Option<std::fs::Permissions>> {
match std::fs::metadata(path) {
Ok(metadata) => Ok(Some(metadata.permissions())),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(error) => Err(error),
}
}
fn create_staged(
path: &Path,
permissions: Option<&std::fs::Permissions>,
) -> std::io::Result<std::fs::File> {
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
if let Some(permissions) = permissions {
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
options.mode(permissions.mode());
}
#[cfg(not(unix))]
let _ = permissions;
options.open(path)
}
pub fn write_atomic(path: &Path, text: &str) -> std::io::Result<()> {
stage(path, text)?.commit(path)
}