src/backup/datastore.rs: add new helpers to load blobs and verify chunks

This commit is contained in:
Dietmar Maurer 2020-06-24 06:58:14 +02:00
parent 1090fd4424
commit 60f9a6ea8f
2 changed files with 31 additions and 11 deletions

View File

@ -1,5 +1,4 @@
use std::collections::{HashSet, HashMap};
use std::convert::TryFrom;
use anyhow::{bail, format_err, Error};
use futures::*;
@ -12,7 +11,7 @@ use proxmox::api::{
RpcEnvironment, RpcEnvironmentType, Permission, UserInformation};
use proxmox::api::router::SubdirMap;
use proxmox::api::schema::*;
use proxmox::tools::fs::{file_get_contents, replace_file, CreateOptions};
use proxmox::tools::fs::{replace_file, CreateOptions};
use proxmox::try_block;
use proxmox::{http_err, identity, list_subdirs_api_method, sortable};
@ -42,15 +41,7 @@ fn check_backup_owner(store: &DataStore, group: &BackupGroup, userid: &str) -> R
fn read_backup_index(store: &DataStore, backup_dir: &BackupDir) -> Result<Vec<BackupContent>, Error> {
let mut path = store.base_path();
path.push(backup_dir.relative_path());
path.push(MANIFEST_BLOB_NAME);
let raw_data = file_get_contents(&path)?;
let index_size = raw_data.len() as u64;
let blob = DataBlob::from_raw(raw_data)?;
let manifest = BackupManifest::try_from(blob)?;
let (manifest, index_size) = store.load_manifest(backup_dir)?;
let mut result = Vec::new();
for item in manifest.files() {

View File

@ -2,6 +2,7 @@ use std::collections::{HashSet, HashMap};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::convert::TryFrom;
use anyhow::{bail, format_err, Error};
use lazy_static::lazy_static;
@ -134,6 +135,10 @@ impl DataStore {
Ok(out)
}
pub fn name(&self) -> &str {
self.chunk_store.name()
}
pub fn base_path(&self) -> PathBuf {
self.chunk_store.base_path()
}
@ -470,4 +475,28 @@ impl DataStore {
) -> Result<(bool, u64), Error> {
self.chunk_store.insert_chunk(chunk, digest)
}
pub fn verify_stored_chunk(&self, digest: &[u8; 32], expected_chunk_size: u64) -> Result<(), Error> {
let blob = self.chunk_store.read_chunk(digest)?;
blob.verify_crc()?;
blob.verify_unencrypted(expected_chunk_size as usize, digest)?;
Ok(())
}
pub fn load_blob(&self, backup_dir: &BackupDir, filename: &str) -> Result<(DataBlob, u64), Error> {
let mut path = self.base_path();
path.push(backup_dir.relative_path());
path.push(filename);
let raw_data = proxmox::tools::fs::file_get_contents(&path)?;
let raw_size = raw_data.len() as u64;
let blob = DataBlob::from_raw(raw_data)?;
Ok((blob, raw_size))
}
pub fn load_manifest(&self, backup_dir: &BackupDir) -> Result<(BackupManifest, u64), Error> {
let (blob, raw_size) = self.load_blob(backup_dir, MANIFEST_BLOB_NAME)?;
let manifest = BackupManifest::try_from(blob)?;
Ok((manifest, raw_size))
}
}