use crate::spec::types::ParityFailure;
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(loom)]
use loom::sync::Mutex as LoomMutex;
use super::hex::*;
fn write_and_commit(
tmp: &mut fs::File,
tmp_path: &std::path::Path,
path: &std::path::Path,
bytes: &[u8],
) -> io::Result<()> {
tmp.write_all(bytes)?;
tmp.sync_all()?;
match fs::hard_link(tmp_path, path) {
Ok(()) => fs::remove_file(tmp_path),
Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {
let existing = fs::read(path)?;
fs::remove_file(tmp_path)?;
if existing == bytes {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!(
"regression path already exists with different content: {}. Fix: investigate hash collision or corrupt regression file.",
path.display()
),
))
}
}
Err(err) => Err(err),
}
}