From e4439025838f894ff8a105cb4b203a564d1054a3 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 31 Jul 2020 07:19:14 +0200 Subject: [PATCH] src/backup/datastore.rs: add helpers to load/store manifest We want this to modify the manifest "unprotected" data, for example to add upload statistics, notes, ... --- src/backup/datastore.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index af964f64..f3c44179 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -7,6 +7,9 @@ use std::convert::TryFrom; use anyhow::{bail, format_err, Error}; use lazy_static::lazy_static; use chrono::{DateTime, Utc}; +use serde_json::Value; + +use proxmox::tools::fs::{replace_file, CreateOptions}; use super::backup_info::{BackupGroup, BackupGroupGuard, BackupDir, BackupInfo}; use super::chunk_store::ChunkStore; @@ -557,7 +560,8 @@ impl DataStore { DataBlob::load_from_reader(&mut file) }).map_err(|err| format_err!("unable to load blob '{:?}' - {}", path, err)) } - + + pub fn load_chunk(&self, digest: &[u8; 32]) -> Result { let (chunk_path, digest_str) = self.chunk_store.chunk_path(digest); @@ -572,7 +576,7 @@ impl DataStore { err, )) } - + pub fn load_manifest( &self, backup_dir: &BackupDir, @@ -583,4 +587,32 @@ impl DataStore { let manifest = BackupManifest::try_from(blob)?; Ok((manifest, crypt_mode, raw_size)) } + + pub fn load_manifest_json( + &self, + backup_dir: &BackupDir, + ) -> Result { + let blob = self.load_blob(backup_dir, MANIFEST_BLOB_NAME)?; + let manifest_data = blob.decode(None)?; + let manifest: Value = serde_json::from_slice(&manifest_data[..])?; + Ok(manifest) + } + + pub fn store_manifest( + &self, + backup_dir: &BackupDir, + manifest: Value, + ) -> Result<(), Error> { + let manifest = serde_json::to_string_pretty(&manifest)?; + let blob = DataBlob::encode(manifest.as_bytes(), None, true)?; + let raw_data = blob.raw_data(); + + let mut path = self.base_path(); + path.push(backup_dir.relative_path()); + path.push(MANIFEST_BLOB_NAME); + + replace_file(&path, raw_data, CreateOptions::new())?; + + Ok(()) + } }