use super::json::check_json_size;
use super::{
BackupLayoutGuard, JournalLock, JournalLockError, OperationPlanPersistenceError,
PersistenceError, create_json_durable, read_json, 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())?;
let path = layout.root().join("restore-safety-requirement.json");
let _lock = JournalLock::acquire(&path)?;
check_json_size(record, MAX_RESTORE_SAFETY_REQUIREMENT_BYTES)?;
create_json_durable(&path, record)?;
Ok(())
}
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())?;
let path = layout.root().join("restore-safety-requirement.json");
let _lock = JournalLock::acquire(&path)?;
let record: RestoreSafetyRequirementRecord =
read_json(&path, MAX_RESTORE_SAFETY_REQUIREMENT_BYTES)?;
check_json_size(&record, MAX_RESTORE_SAFETY_REQUIREMENT_BYTES)?;
record.validate_plans(plan, source)?;
if &record.digest() != expected {
return Err(RestoreSafetyPersistenceError::DigestMismatch);
}
Ok(record)
}
#[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;