use super::{
BackupLayoutGuard, JournalLock, JournalLockError, OperationPlanPersistenceError,
PersistenceError, create_json_durable, read_json, read_operation_plan,
};
use crate::model::{
artifacts::ArtifactChecksumRecord,
consistency::{
ConsistencyRequirementError, ConsistencyRequirementRecord,
MAX_CONSISTENCY_REQUIREMENT_BYTES,
},
operation_plan::OperationPlanRecord,
};
use thiserror::Error;
pub fn create_consistency_requirement(
layout: &BackupLayoutGuard,
plan: &OperationPlanRecord,
record: &ConsistencyRequirementRecord,
) -> Result<(), ConsistencyPersistenceError> {
record.validate_plan(plan)?;
read_operation_plan(layout, &plan.digest())?;
let path = layout.root().join("consistency-requirement.json");
let _lock = JournalLock::acquire(&path)?;
check_size(record)?;
create_json_durable(&path, record)?;
Ok(())
}
pub fn read_consistency_requirement(
layout: &BackupLayoutGuard,
plan: &OperationPlanRecord,
expected: &ArtifactChecksumRecord,
) -> Result<ConsistencyRequirementRecord, ConsistencyPersistenceError> {
read_operation_plan(layout, &plan.digest())?;
let path = layout.root().join("consistency-requirement.json");
let _lock = JournalLock::acquire(&path)?;
let record: ConsistencyRequirementRecord = read_json(&path, MAX_CONSISTENCY_REQUIREMENT_BYTES)?;
check_size(&record)?;
record.validate_plan(plan)?;
if &record.digest() != expected {
return Err(ConsistencyPersistenceError::DigestMismatch);
}
Ok(record)
}
fn check_size(record: &ConsistencyRequirementRecord) -> Result<(), PersistenceError> {
super::json::check_json_size(record, MAX_CONSISTENCY_REQUIREMENT_BYTES)
}
#[derive(Debug, Error)]
pub enum ConsistencyPersistenceError {
#[error("consistency requirement digest mismatch")]
DigestMismatch,
#[error(transparent)]
Requirement(#[from] ConsistencyRequirementError),
#[error(transparent)]
Plan(#[from] OperationPlanPersistenceError),
#[error(transparent)]
Lock(#[from] JournalLockError),
#[error(transparent)]
Persistence(#[from] PersistenceError),
}
#[cfg(all(test, unix))]
mod tests;