use super::{
BackupLayoutGuard, JournalLockError, OperationPlanPersistenceError, PersistenceError,
immutable_record, 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())?;
immutable_record::create(
layout,
"consistency-requirement.json",
MAX_CONSISTENCY_REQUIREMENT_BYTES,
record,
)
}
pub fn read_consistency_requirement(
layout: &BackupLayoutGuard,
plan: &OperationPlanRecord,
expected: &ArtifactChecksumRecord,
) -> Result<ConsistencyRequirementRecord, ConsistencyPersistenceError> {
read_operation_plan(layout, &plan.digest())?;
immutable_record::read_with(
layout,
"consistency-requirement.json",
MAX_CONSISTENCY_REQUIREMENT_BYTES,
|record: &ConsistencyRequirementRecord| {
record.validate_plan(plan)?;
if &record.digest() != expected {
return Err(ConsistencyPersistenceError::DigestMismatch);
}
Ok(())
},
)
}
#[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;