use super::*;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct CommitIntent {
pub subject_id: RecordId,
pub written: bool,
}
impl CommitIntent {
pub(crate) fn write_to_file(&self, path: &Path) -> Result<(), LogError> {
use std::io::Write;
let bytes =
canonical_json(self).map_err(|e| LogError::Io(std::io::Error::other(e.to_string())))?;
let tmp_path = path.with_extension("intent.tmp");
{
let mut f = std::fs::File::create(&tmp_path)?;
f.write_all(&bytes)?;
f.sync_all()?;
}
std::fs::rename(&tmp_path, path)?;
sync_parent_dir(path)?;
Ok(())
}
pub(crate) fn clear_file(path: &Path) -> Result<(), LogError> {
if path.exists() {
std::fs::remove_file(path)?;
sync_parent_dir(path)?;
}
Ok(())
}
}
fn sync_parent_dir(path: &Path) -> Result<(), LogError> {
#[cfg(unix)]
if let Some(parent) = path.parent() {
std::fs::File::open(parent)?.sync_all()?;
}
#[cfg(not(unix))]
let _ = path;
Ok(())
}