]> git.proxmox.com Git - proxmox-backup.git/commitdiff
add and implement chunk_from_offset for IndexFile
authorStefan Reiter <s.reiter@proxmox.com>
Wed, 22 Jul 2020 13:56:21 +0000 (15:56 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 22 Jul 2020 15:28:49 +0000 (17:28 +0200)
Necessary for byte-wise seeking through chunks in an index.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
src/backup/dynamic_index.rs
src/backup/fixed_index.rs
src/backup/index.rs

index 4907fe1f88e12580e5ec8c05f0be25be6581e0ab..887b7cf3c3163f1f2784559a6de7e84091da261b 100644 (file)
@@ -216,6 +216,24 @@ impl IndexFile for DynamicIndexReader {
             digest: self.index[pos].digest.clone(),
         })
     }
+
+    fn chunk_from_offset(&self, offset: u64) -> Option<(usize, u64)> {
+        let end_idx = self.index.len() - 1;
+        let end = self.chunk_end(end_idx);
+        let found_idx = self.binary_search(0, 0, end_idx, end, offset);
+        let found_idx = match found_idx {
+            Ok(i) => i,
+            Err(_) => return None
+        };
+
+        let found_start = if found_idx == 0 {
+            0
+        } else {
+            self.chunk_end(found_idx - 1)
+        };
+
+        Some((found_idx, offset - found_start))
+    }
 }
 
 struct CachedChunk {
index 73d0dad0f788da43ca728073e20baccff22514cd..b7e785d637ed0a4bd1fd05723112837c29a4a124 100644 (file)
@@ -219,6 +219,17 @@ impl IndexFile for FixedIndexReader {
 
         (csum, chunk_end)
     }
+
+    fn chunk_from_offset(&self, offset: u64) -> Option<(usize, u64)> {
+        if offset >= self.size {
+            return None;
+        }
+
+        Some((
+            (offset / self.chunk_size as u64) as usize,
+            offset % self.chunk_size as u64
+        ))
+    }
 }
 
 pub struct FixedIndexWriter {
index efdf3b5408b7b616a76b9531e37871f5331efbf2..2eab8524d128cac031698be975b0e2465bf5fade 100644 (file)
@@ -22,6 +22,9 @@ pub trait IndexFile {
     fn index_bytes(&self) -> u64;
     fn chunk_info(&self, pos: usize) -> Option<ChunkReadInfo>;
 
+    /// Get the chunk index and the relative offset within it for a byte offset
+    fn chunk_from_offset(&self, offset: u64) -> Option<(usize, u64)>;
+
     /// Compute index checksum and size
     fn compute_csum(&self) -> ([u8; 32], u64);