]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/tape/helpers/snapshot_reader.rs
add pbs-tools subcrate
[proxmox-backup.git] / src / tape / helpers / snapshot_reader.rs
index c8deb58a325c5ae73dea3da0ecf8de705a719831..780439532ed23a496838c0a14fb1edc97dc49d29 100644 (file)
@@ -6,8 +6,9 @@ use std::fs::File;
 use anyhow::{bail, Error};
 use nix::dir::Dir;
 
+use pbs_tools::fs::lock_dir_noblock_shared;
+
 use crate::{
-    tools::fs::lock_dir_noblock_shared,
     backup::{
         DataStore,
         BackupDir,
@@ -26,6 +27,7 @@ use crate::{
 /// This make it easy to iterate over all used chunks and files.
 pub struct SnapshotReader {
     snapshot: BackupDir,
+    datastore_name: String,
     file_list: Vec<String>,
     locked_dir: Dir,
 }
@@ -42,11 +44,13 @@ impl SnapshotReader {
             "snapshot",
             "locked by another operation")?;
 
+        let datastore_name = datastore.name().to_string();
+
         let manifest = match datastore.load_manifest(&snapshot) {
             Ok((manifest, _)) => manifest,
             Err(err) => {
                 bail!("manifest load error on datastore '{}' snapshot '{}' - {}",
-                      datastore.name(), snapshot, err);
+                      datastore_name, snapshot, err);
             }
         };
 
@@ -60,7 +64,7 @@ impl SnapshotReader {
             file_list.push(CLIENT_LOG_BLOB_NAME.to_string());
         }
 
-        Ok(Self { snapshot, file_list, locked_dir })
+        Ok(Self { snapshot, datastore_name, file_list, locked_dir })
     }
 
     /// Return the snapshot directory
@@ -68,6 +72,11 @@ impl SnapshotReader {
         &self.snapshot
     }
 
+    /// Return the datastore name
+    pub fn datastore_name(&self) -> &str {
+        &self.datastore_name
+    }
+
     /// Returns the list of files the snapshot refers to.
     pub fn file_list(&self) -> &Vec<String> {
         &self.file_list
@@ -96,11 +105,10 @@ impl SnapshotReader {
 /// Note: The iterator returns a `Result`, and the iterator state is
 /// undefined after the first error. So it make no sense to continue
 /// iteration after the first error.
-#[derive(Clone)]
 pub struct SnapshotChunkIterator<'a> {
     snapshot_reader: &'a SnapshotReader,
     todo_list: Vec<String>,
-    current_index: Option<(Arc<Box<dyn IndexFile>>, usize)>,
+    current_index: Option<(Arc<Box<dyn IndexFile + Send>>, usize, Vec<(usize, u64)>)>,
 }
 
 impl <'a> Iterator for SnapshotChunkIterator<'a> {
@@ -112,20 +120,26 @@ impl <'a> Iterator for SnapshotChunkIterator<'a> {
                 if self.current_index.is_none() {
                     if let Some(filename) = self.todo_list.pop() {
                         let file = self.snapshot_reader.open_file(&filename)?;
-                        let index: Box<dyn IndexFile> = match archive_type(&filename)? {
+                        let index: Box<dyn IndexFile + Send> = match archive_type(&filename)? {
                             ArchiveType::FixedIndex => Box::new(FixedIndexReader::new(file)?),
                             ArchiveType::DynamicIndex => Box::new(DynamicIndexReader::new(file)?),
                             _ => bail!("SnapshotChunkIterator: got unknown file type - internal error"),
                         };
-                        self.current_index = Some((Arc::new(index), 0));
+
+                        let datastore =
+                            DataStore::lookup_datastore(self.snapshot_reader.datastore_name())?;
+                        let order = datastore.get_chunks_in_order(&index, |_| false, |_| Ok(()))?;
+
+                        self.current_index = Some((Arc::new(index), 0, order));
                     } else {
                         return Ok(None);
                     }
                 }
-                let (index, pos) = self.current_index.take().unwrap();
-                if pos < index.index_count() {
-                    let digest = *index.index_digest(pos).unwrap();
-                    self.current_index = Some((index, pos + 1));
+                let (index, pos, list) = self.current_index.take().unwrap();
+                if pos < list.len() {
+                    let (real_pos, _) = list[pos];
+                    let digest = *index.index_digest(real_pos).unwrap();
+                    self.current_index = Some((index, pos + 1, list));
                     return Ok(Some(digest));
                 } else {
                     // pop next index