Skip to main content

ic_backup/ops/persistence/effect_graph/
mod.rs

1//! Immutable bounded graph publication and exact original local graph admission.
2
3use super::{
4    BackupLayoutGuard, JournalLock, JournalLockError, PersistenceError, create_json_durable,
5    read_json,
6};
7use crate::model::{
8    artifacts::ArtifactChecksumRecord,
9    effect_graph::{EffectGraphRecord, MAX_EFFECT_GRAPH_BYTES},
10};
11use thiserror::Error;
12
13/// Durably create `effect-graph.json` under layout exclusion without replacing evidence.
14///
15/// # Errors
16/// Rejects excessive canonical bytes, existing/unsafe entries, replaced roots and IO/locks.
17pub fn create_effect_graph(
18    layout: &BackupLayoutGuard,
19    record: &EffectGraphRecord,
20) -> Result<(), EffectGraphPersistenceError> {
21    layout.check_root()?;
22    let path = layout.root().join("effect-graph.json");
23    let _lock = JournalLock::acquire(&path)?;
24    check_size(record)?;
25    create_json_durable(&path, record)?;
26    Ok(())
27}
28
29/// Admit retained bounded graph under its original exact expected digest, using local IO only.
30///
31/// Lost creation replies reconcile by reading the exact graph. This neither checks
32/// remote completion nor grants effects, fresh authority or plan acceptance.
33///
34/// # Errors
35/// Rejects unsafe/missing/oversized/invalid graphs, digest mismatch, replaced roots and locks.
36pub fn read_effect_graph(
37    layout: &BackupLayoutGuard,
38    expected: &ArtifactChecksumRecord,
39) -> Result<EffectGraphRecord, EffectGraphPersistenceError> {
40    layout.check_root()?;
41    let path = layout.root().join("effect-graph.json");
42    let _lock = JournalLock::acquire(&path)?;
43    let record: EffectGraphRecord = read_json(&path, MAX_EFFECT_GRAPH_BYTES)?;
44    check_size(&record)?;
45    if &record.digest() != expected {
46        return Err(EffectGraphPersistenceError::DigestMismatch);
47    }
48    Ok(record)
49}
50fn check_size(record: &EffectGraphRecord) -> Result<(), PersistenceError> {
51    super::json::check_json_size(record, MAX_EFFECT_GRAPH_BYTES)
52}
53
54/// Typed declared graph identity or immutable local persistence rejection.
55#[derive(Debug, Error)]
56pub enum EffectGraphPersistenceError {
57    /// Retained graph differs from the original exact declared digest.
58    #[error("effect graph digest mismatch")]
59    DigestMismatch,
60    /// Cooperating layout/journal ownership failed.
61    #[error(transparent)]
62    Lock(#[from] JournalLockError),
63    /// Bounded JSON, graph admission or durable filesystem access failed.
64    #[error(transparent)]
65    Persistence(#[from] PersistenceError),
66}
67
68#[cfg(all(test, unix))]
69mod tests;