[pbs-devel] [PATCH v4 proxmox-backup 24/26] catalog: fetch offset and size for files and refs
Christian Ebner
c.ebner at proxmox.com
Thu Nov 9 19:46:12 CET 2023
Allows to fetch the pxar archive offsets and file size for regular files
and appendix referenced files.
Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
Changes since version 2:
- no present in version 2
pbs-datastore/src/catalog.rs | 70 ++++++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git a/pbs-datastore/src/catalog.rs b/pbs-datastore/src/catalog.rs
index 220313c6..fe076a94 100644
--- a/pbs-datastore/src/catalog.rs
+++ b/pbs-datastore/src/catalog.rs
@@ -1,3 +1,4 @@
+use std::collections::BTreeMap;
use std::ffi::{CStr, CString, OsStr};
use std::fmt;
use std::io::{Read, Seek, SeekFrom, Write};
@@ -1118,6 +1119,75 @@ impl<R: Read + Seek> CatalogReader<R> {
Ok(res)
}
+
+ /// Get all File and AppendixRef entries with their pxar archive offset and size
+ pub fn fetch_offsets(&mut self) -> Result<BTreeMap<u64, u64>, Error> {
+ let root = self.root()?;
+ let mut list = BTreeMap::new();
+ match root {
+ DirEntry {
+ attr: DirEntryAttribute::Directory { start },
+ ..
+ } => self.fetch_offsets_from_dir(std::path::Path::new("./"), start, &mut list, None)?,
+ _ => bail!("unexpected root entry type, not a directory!"),
+ }
+ Ok(list)
+ }
+
+ fn fetch_offsets_from_dir(
+ &mut self,
+ prefix: &std::path::Path,
+ start: u64,
+ list: &mut BTreeMap<u64, u64>,
+ appendix_start: Option<AppendixStartOffset>,
+ ) -> Result<(), Error> {
+ let data = self.read_raw_dirinfo_block(start)?;
+
+ DirInfo::parse(
+ &data,
+ self.magic,
+ |etype, name_bytes, offset, size, _mtime, _ctime, link_offset| {
+ let mut path = std::path::PathBuf::from(prefix);
+ let name: &OsStr = OsStrExt::from_bytes(name_bytes);
+ path.push(name);
+
+ match etype {
+ CatalogEntryType::Archive => {
+ if offset > start {
+ bail!("got wrong archive offset ({} > {})", offset, start);
+ }
+ let pos = start - offset;
+ let appendix_start = self.appendix_offset(name_bytes)?;
+ self.fetch_offsets_from_dir(&path, pos, list, appendix_start)?;
+ }
+ CatalogEntryType::Directory => {
+ if offset > start {
+ bail!("got wrong directory offset ({} > {})", offset, start);
+ }
+ let pos = start - offset;
+ self.fetch_offsets_from_dir(&path, pos, list, appendix_start)?;
+ }
+ CatalogEntryType::AppendixRef => {
+ if let Some(Offset::AppendixRefOffset { offset }) = link_offset {
+ if let Some(appendix_start) = appendix_start {
+ list.insert(appendix_start.raw() + offset, size);
+ } else {
+ bail!("missing required appendix start offset");
+ }
+ }
+ }
+ CatalogEntryType::File => {
+ if let Some(Offset::FileOffset { offset }) = link_offset {
+ list.insert(offset, size);
+ }
+ }
+ _ => {}
+ }
+ Ok(true)
+ },
+ )?;
+ Ok(())
+ }
}
/// Serialize i64 as short, variable length byte sequence
--
2.39.2
More information about the pbs-devel
mailing list