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