ic_backup/ops/persistence/consistency/
mod.rs1use super::{
4 BackupLayoutGuard, JournalLockError, OperationPlanPersistenceError, PersistenceError,
5 immutable_record, read_operation_plan,
6};
7use crate::model::{
8 artifacts::ArtifactChecksumRecord,
9 consistency::{
10 ConsistencyRequirementError, ConsistencyRequirementRecord,
11 MAX_CONSISTENCY_REQUIREMENT_BYTES,
12 },
13 operation_plan::OperationPlanRecord,
14};
15use thiserror::Error;
16
17pub fn create_consistency_requirement(
24 layout: &BackupLayoutGuard,
25 plan: &OperationPlanRecord,
26 record: &ConsistencyRequirementRecord,
27) -> Result<(), ConsistencyPersistenceError> {
28 record.validate_plan(plan)?;
29 read_operation_plan(layout, &plan.digest())?;
30 immutable_record::create(
31 layout,
32 "consistency-requirement.json",
33 MAX_CONSISTENCY_REQUIREMENT_BYTES,
34 record,
35 )
36}
37pub fn read_consistency_requirement(
44 layout: &BackupLayoutGuard,
45 plan: &OperationPlanRecord,
46 expected: &ArtifactChecksumRecord,
47) -> Result<ConsistencyRequirementRecord, ConsistencyPersistenceError> {
48 read_operation_plan(layout, &plan.digest())?;
49 immutable_record::read_with(
50 layout,
51 "consistency-requirement.json",
52 MAX_CONSISTENCY_REQUIREMENT_BYTES,
53 |record: &ConsistencyRequirementRecord| {
54 record.validate_plan(plan)?;
55 if &record.digest() != expected {
56 return Err(ConsistencyPersistenceError::DigestMismatch);
57 }
58 Ok(())
59 },
60 )
61}
62#[derive(Debug, Error)]
64pub enum ConsistencyPersistenceError {
65 #[error("consistency requirement digest mismatch")]
67 DigestMismatch,
68 #[error(transparent)]
70 Requirement(#[from] ConsistencyRequirementError),
71 #[error(transparent)]
73 Plan(#[from] OperationPlanPersistenceError),
74 #[error(transparent)]
76 Lock(#[from] JournalLockError),
77 #[error(transparent)]
79 Persistence(#[from] PersistenceError),
80}
81
82#[cfg(all(test, unix))]
83mod tests;