From b9ee86efe1b220756d87eba4e69d0bf8f4785665 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 18 Dec 2020 12:10:31 +0100 Subject: [PATCH] tape: use SnapshotReader to create snapshot archive --- src/tape/helpers/snapshot_reader.rs | 8 +++++++- src/tape/snapshot_archive.rs | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/tape/helpers/snapshot_reader.rs b/src/tape/helpers/snapshot_reader.rs index 6ee17c16..a2531874 100644 --- a/src/tape/helpers/snapshot_reader.rs +++ b/src/tape/helpers/snapshot_reader.rs @@ -25,6 +25,7 @@ use crate::{ /// /// This make it easy to iterate over all used chunks and files. pub struct SnapshotReader { + snapshot: BackupDir, file_list: Vec, locked_dir: Dir, } @@ -59,7 +60,12 @@ impl SnapshotReader { file_list.push(CLIENT_LOG_BLOB_NAME.to_string()); } - Ok(Self { file_list, locked_dir }) + Ok(Self { snapshot, file_list, locked_dir }) + } + + /// Return the snapshot directory + pub fn snapshot(&self) -> &BackupDir { + &self.snapshot } /// Returns the list of files the snapshot refers to. diff --git a/src/tape/snapshot_archive.rs b/src/tape/snapshot_archive.rs index a6c9c7c6..5dc81706 100644 --- a/src/tape/snapshot_archive.rs +++ b/src/tape/snapshot_archive.rs @@ -1,7 +1,6 @@ use std::io::{Read, Write}; use std::pin::Pin; use std::task::{Context, Poll}; -use std::path::{Path, PathBuf}; use proxmox::{ sys::error::SysError, @@ -10,6 +9,7 @@ use proxmox::{ use crate::tape::{ TapeWrite, + SnapshotReader, file_formats::{ PROXMOX_TAPE_BLOCK_SIZE, PROXMOX_BACKUP_SNAPSHOT_ARCHIVE_MAGIC_1_0, @@ -27,11 +27,12 @@ use crate::tape::{ /// backup task must rewrite the whole file on the next media). pub fn tape_write_snapshot_archive<'a>( writer: &mut (dyn TapeWrite + 'a), - snapshot: &str, - base_path: &Path, - file_list: &[String], + snapshot_reader: &SnapshotReader, ) -> Result, std::io::Error> { + let snapshot = snapshot_reader.snapshot().to_string(); + let file_list = snapshot_reader.file_list(); + let header_data = snapshot.as_bytes().to_vec(); let header = MediaContentHeader::new( @@ -52,17 +53,16 @@ pub fn tape_write_snapshot_archive<'a>( let mut encoder = pxar::encoder::sync::Encoder::new(PxarTapeWriter::new(writer), &root_metadata)?; for filename in file_list.iter() { - let mut path = PathBuf::from(base_path); - path.push(filename); - let mut file = std::fs::File::open(&path)?; + let mut file = snapshot_reader.open_file(filename) + .map_err(|err| proxmox::io_format_err!("open file '{}' failed - {}", filename, err))?; let metadata = file.metadata()?; let file_size = metadata.len(); let metadata: pxar::Metadata = metadata.into(); if !metadata.is_regular_file() { - proxmox::io_bail!("path {:?} is not a regular file", path); + proxmox::io_bail!("file '{}' is not a regular file", filename); } let mut remaining = file_size; @@ -70,14 +70,14 @@ pub fn tape_write_snapshot_archive<'a>( while remaining != 0 { let got = file.read(&mut file_copy_buffer[..])?; if got as u64 > remaining { - proxmox::io_bail!("file {:?} changed while reading", path); + proxmox::io_bail!("file '{}' changed while reading", filename); } out.write_all(&file_copy_buffer[..got])?; remaining -= got as u64; } if remaining > 0 { - proxmox::io_bail!("file {:?} shrunk while reading", path); + proxmox::io_bail!("file '{}' shrunk while reading", filename); } } encoder.finish()?;