2019-10-12 15:58:08 +00:00
|
|
|
use failure::*;
|
|
|
|
use std::convert::TryFrom;
|
2019-12-31 14:23:41 +00:00
|
|
|
use std::path::Path;
|
2019-10-12 15:58:08 +00:00
|
|
|
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
|
|
|
use crate::backup::BackupDir;
|
|
|
|
|
|
|
|
pub const MANIFEST_BLOB_NAME: &str = "index.json.blob";
|
|
|
|
|
2019-12-31 14:23:41 +00:00
|
|
|
pub struct FileInfo {
|
|
|
|
pub filename: String,
|
|
|
|
pub size: u64,
|
|
|
|
pub csum: [u8; 32],
|
2019-10-12 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BackupManifest {
|
|
|
|
snapshot: BackupDir,
|
|
|
|
files: Vec<FileInfo>,
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:23:41 +00:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum ArchiveType {
|
|
|
|
FixedIndex,
|
|
|
|
DynamicIndex,
|
|
|
|
Blob,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn archive_type<P: AsRef<Path>>(
|
|
|
|
archive_name: P,
|
|
|
|
) -> Result<ArchiveType, Error> {
|
|
|
|
|
|
|
|
let archive_name = archive_name.as_ref();
|
|
|
|
let archive_type = match archive_name.extension().and_then(|ext| ext.to_str()) {
|
|
|
|
Some("didx") => ArchiveType::DynamicIndex,
|
|
|
|
Some("fidx") => ArchiveType::FixedIndex,
|
|
|
|
Some("blob") => ArchiveType::Blob,
|
|
|
|
_ => bail!("unknown archive type: {:?}", archive_name),
|
|
|
|
};
|
|
|
|
Ok(archive_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-12 15:58:08 +00:00
|
|
|
impl BackupManifest {
|
|
|
|
|
|
|
|
pub fn new(snapshot: BackupDir) -> Self {
|
|
|
|
Self { files: Vec::new(), snapshot }
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:23:41 +00:00
|
|
|
pub fn add_file(&mut self, filename: String, size: u64, csum: [u8; 32]) -> Result<(), Error> {
|
|
|
|
let _archive_type = archive_type(&filename)?; // check type
|
2019-10-12 15:58:08 +00:00
|
|
|
self.files.push(FileInfo { filename, size, csum });
|
2019-12-31 14:23:41 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn files(&self) -> &[FileInfo] {
|
|
|
|
&self.files[..]
|
2019-10-12 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 08:09:12 +00:00
|
|
|
fn lookup_file_info(&self, name: &str) -> Result<&FileInfo, Error> {
|
|
|
|
|
|
|
|
let info = self.files.iter().find(|item| item.filename == name);
|
|
|
|
|
|
|
|
match info {
|
|
|
|
None => bail!("manifest does not contain file '{}'", name),
|
|
|
|
Some(info) => Ok(info),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verify_file(&self, name: &str, csum: &[u8; 32], size: u64) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let info = self.lookup_file_info(name)?;
|
|
|
|
|
|
|
|
if size != info.size {
|
|
|
|
bail!("wrong size for file '{}' ({} != {}", name, info.size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if csum != &info.csum {
|
|
|
|
bail!("wrong checksum for file '{}'", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-12 15:58:08 +00:00
|
|
|
pub fn into_json(self) -> Value {
|
|
|
|
json!({
|
|
|
|
"backup-type": self.snapshot.group().backup_type(),
|
|
|
|
"backup-id": self.snapshot.group().backup_id(),
|
|
|
|
"backup-time": self.snapshot.backup_time().timestamp(),
|
|
|
|
"files": self.files.iter()
|
|
|
|
.fold(Vec::new(), |mut acc, info| {
|
|
|
|
acc.push(json!({
|
|
|
|
"filename": info.filename,
|
|
|
|
"size": info.size,
|
|
|
|
"csum": proxmox::tools::digest_to_hex(&info.csum),
|
|
|
|
}));
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Value> for BackupManifest {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(data: Value) -> Result<Self, Error> {
|
|
|
|
|
2019-10-13 07:39:21 +00:00
|
|
|
use crate::tools::{required_string_property, required_integer_property, required_array_property};
|
|
|
|
|
2019-10-13 08:09:12 +00:00
|
|
|
proxmox::tools::try_block!({
|
2019-10-14 10:28:27 +00:00
|
|
|
let backup_type = required_string_property(&data, "backup-type")?;
|
|
|
|
let backup_id = required_string_property(&data, "backup-id")?;
|
|
|
|
let backup_time = required_integer_property(&data, "backup-time")?;
|
2019-10-12 15:58:08 +00:00
|
|
|
|
2019-10-13 08:09:12 +00:00
|
|
|
let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
|
2019-10-12 15:58:08 +00:00
|
|
|
|
2019-12-31 14:23:41 +00:00
|
|
|
let mut manifest = BackupManifest::new(snapshot);
|
|
|
|
|
2019-10-13 08:09:12 +00:00
|
|
|
for item in required_array_property(&data, "files")?.iter() {
|
|
|
|
let filename = required_string_property(item, "filename")?.to_owned();
|
|
|
|
let csum = required_string_property(item, "csum")?;
|
|
|
|
let csum = proxmox::tools::hex_to_digest(csum)?;
|
|
|
|
let size = required_integer_property(item, "size")? as u64;
|
2019-12-31 14:23:41 +00:00
|
|
|
manifest.add_file(filename, size, csum)?;
|
2019-10-13 08:09:12 +00:00
|
|
|
}
|
2019-12-31 14:23:41 +00:00
|
|
|
Ok(manifest)
|
2019-10-13 08:09:12 +00:00
|
|
|
}).map_err(|err: Error| format_err!("unable to parse backup manifest - {}", err))
|
2019-10-12 15:58:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|