use super::{
BackupLayoutGuard, JournalLockError, OperationPlanPersistenceError, PersistenceError,
immutable_record, read_operation_plan,
};
use crate::model::{
artifacts::ArtifactChecksumRecord,
operation_plan::OperationPlanRecord,
restore_safety::{
MAX_RESTORE_SAFETY_REQUIREMENT_BYTES, RestoreSafetyRequirementError,
RestoreSafetyRequirementRecord,
},
};
use thiserror::Error;
pub fn create_restore_safety_requirement(
layout: &BackupLayoutGuard,
source_layout: &BackupLayoutGuard,
plan: &OperationPlanRecord,
source: &OperationPlanRecord,
record: &RestoreSafetyRequirementRecord,
) -> Result<(), RestoreSafetyPersistenceError> {
record.validate_plans(plan, source)?;
read_operation_plan(layout, &plan.digest())?;
read_operation_plan(source_layout, &source.digest())?;
immutable_record::create(
layout,
"restore-safety-requirement.json",
MAX_RESTORE_SAFETY_REQUIREMENT_BYTES,
record,
)
}
pub fn read_restore_safety_requirement(
layout: &BackupLayoutGuard,
source_layout: &BackupLayoutGuard,
plan: &OperationPlanRecord,
source: &OperationPlanRecord,
expected: &ArtifactChecksumRecord,
) -> Result<RestoreSafetyRequirementRecord, RestoreSafetyPersistenceError> {
read_operation_plan(layout, &plan.digest())?;
read_operation_plan(source_layout, &source.digest())?;
immutable_record::read_with(
layout,
"restore-safety-requirement.json",
MAX_RESTORE_SAFETY_REQUIREMENT_BYTES,
|record: &RestoreSafetyRequirementRecord| {
record.validate_plans(plan, source)?;
if &record.digest() != expected {
return Err(RestoreSafetyPersistenceError::DigestMismatch);
}
Ok(())
},
)
}
#[derive(Debug, Error)]
pub enum RestoreSafetyPersistenceError {
#[error("restore safety requirement digest mismatch")]
DigestMismatch,
#[error(transparent)]
Requirement(#[from] RestoreSafetyRequirementError),
#[error(transparent)]
Plan(#[from] OperationPlanPersistenceError),
#[error(transparent)]
Lock(#[from] JournalLockError),
#[error(transparent)]
Persistence(#[from] PersistenceError),
}
#[cfg(all(test, unix))]
mod tests;