From b82a101c86f15132b0ac2b708596e8579f1c7f1b Mon Sep 17 00:00:00 2001 From: Christian Ebner Date: Wed, 30 Oct 2019 14:38:05 +0100 Subject: [PATCH] catalog: store current writer position in CatalogBlobWriter Counts the bytes written by the CatalogBlobWriter in order to obtain the stream position, needed to get offset to reference catalog items. Signed-off-by: Christian Ebner --- src/backup/catalog_blob.rs | 54 ++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/backup/catalog_blob.rs b/src/backup/catalog_blob.rs index 695261a0..478c1a1f 100644 --- a/src/backup/catalog_blob.rs +++ b/src/backup/catalog_blob.rs @@ -15,20 +15,21 @@ use super::{DataBlobWriter, DataBlobReader, CryptConfig}; pub struct CatalogBlobWriter { writer: DataBlobWriter, level: usize, + pos: u64, } impl CatalogBlobWriter { pub fn new_compressed(writer: W) -> Result { let writer = DataBlobWriter::new_compressed(writer)?; - Ok(Self { writer, level: 0 }) + Ok(Self { writer, level: 0, pos: 0 }) } pub fn new_signed_compressed(writer: W, config: Arc) -> Result { let writer = DataBlobWriter::new_signed_compressed(writer, config)?; - Ok(Self { writer, level: 0 }) + Ok(Self { writer, level: 0, pos: 0 }) } pub fn new_encrypted_compressed(writer: W, config: Arc) -> Result { let writer = DataBlobWriter::new_encrypted_compressed(writer, config)?; - Ok(Self { writer, level: 0 }) + Ok(Self { writer, level: 0, pos: 0 }) } pub fn finish(self) -> Result { self.writer.finish() @@ -38,9 +39,9 @@ impl CatalogBlobWriter { impl BackupCatalogWriter for CatalogBlobWriter { fn start_directory(&mut self, name: &CStr) -> Result<(), Error> { - self.writer.write_all(&[CatalogEntryType::Directory as u8])?; - self.writer.write_all(name.to_bytes_with_nul())?; - self.writer.write_all(b"{")?; + self.write_all(&[CatalogEntryType::Directory as u8])?; + self.write_all(name.to_bytes_with_nul())?; + self.write_all(b"{")?; self.level += 1; Ok(()) } @@ -49,56 +50,63 @@ impl BackupCatalogWriter for CatalogBlobWriter { if self.level == 0 { bail!("got unexpected end_directory level 0"); } - self.writer.write_all(b"}")?; + self.write_all(b"}")?; self.level -= 1; Ok(()) } fn add_file(&mut self, name: &CStr, size: u64, mtime: u64) -> Result<(), Error> { - self.writer.write_all(&[CatalogEntryType::File as u8])?; - self.writer.write_all(&size.to_le_bytes())?; - self.writer.write_all(&mtime.to_le_bytes())?; - self.writer.write_all(name.to_bytes_with_nul())?; + self.write_all(&[CatalogEntryType::File as u8])?; + self.write_all(&size.to_le_bytes())?; + self.write_all(&mtime.to_le_bytes())?; + self.write_all(name.to_bytes_with_nul())?; Ok(()) } fn add_symlink(&mut self, name: &CStr) -> Result<(), Error> { - self.writer.write_all(&[CatalogEntryType::Symlink as u8])?; - self.writer.write_all(name.to_bytes_with_nul())?; + self.write_all(&[CatalogEntryType::Symlink as u8])?; + self.write_all(name.to_bytes_with_nul())?; Ok(()) } fn add_hardlink(&mut self, name: &CStr) -> Result<(), Error> { - self.writer.write_all(&[CatalogEntryType::Hardlink as u8])?; - self.writer.write_all(name.to_bytes_with_nul())?; + self.write_all(&[CatalogEntryType::Hardlink as u8])?; + self.write_all(name.to_bytes_with_nul())?; Ok(()) } fn add_block_device(&mut self, name: &CStr) -> Result<(), Error> { - self.writer.write_all(&[CatalogEntryType::BlockDevice as u8])?; - self.writer.write_all(name.to_bytes_with_nul())?; + self.write_all(&[CatalogEntryType::BlockDevice as u8])?; + self.write_all(name.to_bytes_with_nul())?; Ok(()) } fn add_char_device(&mut self, name: &CStr) -> Result<(), Error> { - self.writer.write_all(&[CatalogEntryType::CharDevice as u8])?; - self.writer.write_all(name.to_bytes_with_nul())?; + self.write_all(&[CatalogEntryType::CharDevice as u8])?; + self.write_all(name.to_bytes_with_nul())?; Ok(()) } fn add_fifo(&mut self, name: &CStr) -> Result<(), Error> { - self.writer.write_all(&[CatalogEntryType::Fifo as u8])?; - self.writer.write_all(name.to_bytes_with_nul())?; + self.write_all(&[CatalogEntryType::Fifo as u8])?; + self.write_all(name.to_bytes_with_nul())?; Ok(()) } fn add_socket(&mut self, name: &CStr) -> Result<(), Error> { - self.writer.write_all(&[CatalogEntryType::Socket as u8])?; - self.writer.write_all(name.to_bytes_with_nul())?; + self.write_all(&[CatalogEntryType::Socket as u8])?; + self.write_all(name.to_bytes_with_nul())?; Ok(()) } } +impl CatalogBlobWriter { + fn write_all(&mut self, data: &[u8]) -> Result<(), Error> { + self.writer.write_all(data)?; + self.pos += u64::try_from(data.len())?; + Ok(()) + } +} pub struct CatalogBlobReader { reader: BufReader>,