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 <c.ebner@proxmox.com>
This commit is contained in:
Christian Ebner 2019-10-30 14:38:05 +01:00 committed by Dietmar Maurer
parent 62ee2eb405
commit b82a101c86

View File

@ -15,20 +15,21 @@ use super::{DataBlobWriter, DataBlobReader, CryptConfig};
pub struct CatalogBlobWriter<W: Write + Seek> { pub struct CatalogBlobWriter<W: Write + Seek> {
writer: DataBlobWriter<W>, writer: DataBlobWriter<W>,
level: usize, level: usize,
pos: u64,
} }
impl <W: Write + Seek> CatalogBlobWriter<W> { impl <W: Write + Seek> CatalogBlobWriter<W> {
pub fn new_compressed(writer: W) -> Result<Self, Error> { pub fn new_compressed(writer: W) -> Result<Self, Error> {
let writer = DataBlobWriter::new_compressed(writer)?; 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<CryptConfig>) -> Result<Self, Error> { pub fn new_signed_compressed(writer: W, config: Arc<CryptConfig>) -> Result<Self, Error> {
let writer = DataBlobWriter::new_signed_compressed(writer, config)?; 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<CryptConfig>) -> Result<Self, Error> { pub fn new_encrypted_compressed(writer: W, config: Arc<CryptConfig>) -> Result<Self, Error> {
let writer = DataBlobWriter::new_encrypted_compressed(writer, config)?; 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<W, Error> { pub fn finish(self) -> Result<W, Error> {
self.writer.finish() self.writer.finish()
@ -38,9 +39,9 @@ impl <W: Write + Seek> CatalogBlobWriter<W> {
impl <W: Write + Seek> BackupCatalogWriter for CatalogBlobWriter<W> { impl <W: Write + Seek> BackupCatalogWriter for CatalogBlobWriter<W> {
fn start_directory(&mut self, name: &CStr) -> Result<(), Error> { fn start_directory(&mut self, name: &CStr) -> Result<(), Error> {
self.writer.write_all(&[CatalogEntryType::Directory as u8])?; self.write_all(&[CatalogEntryType::Directory as u8])?;
self.writer.write_all(name.to_bytes_with_nul())?; self.write_all(name.to_bytes_with_nul())?;
self.writer.write_all(b"{")?; self.write_all(b"{")?;
self.level += 1; self.level += 1;
Ok(()) Ok(())
} }
@ -49,56 +50,63 @@ impl <W: Write + Seek> BackupCatalogWriter for CatalogBlobWriter<W> {
if self.level == 0 { if self.level == 0 {
bail!("got unexpected end_directory level 0"); bail!("got unexpected end_directory level 0");
} }
self.writer.write_all(b"}")?; self.write_all(b"}")?;
self.level -= 1; self.level -= 1;
Ok(()) Ok(())
} }
fn add_file(&mut self, name: &CStr, size: u64, mtime: u64) -> Result<(), Error> { fn add_file(&mut self, name: &CStr, size: u64, mtime: u64) -> Result<(), Error> {
self.writer.write_all(&[CatalogEntryType::File as u8])?; self.write_all(&[CatalogEntryType::File as u8])?;
self.writer.write_all(&size.to_le_bytes())?; self.write_all(&size.to_le_bytes())?;
self.writer.write_all(&mtime.to_le_bytes())?; self.write_all(&mtime.to_le_bytes())?;
self.writer.write_all(name.to_bytes_with_nul())?; self.write_all(name.to_bytes_with_nul())?;
Ok(()) Ok(())
} }
fn add_symlink(&mut self, name: &CStr) -> Result<(), Error> { fn add_symlink(&mut self, name: &CStr) -> Result<(), Error> {
self.writer.write_all(&[CatalogEntryType::Symlink as u8])?; self.write_all(&[CatalogEntryType::Symlink as u8])?;
self.writer.write_all(name.to_bytes_with_nul())?; self.write_all(name.to_bytes_with_nul())?;
Ok(()) Ok(())
} }
fn add_hardlink(&mut self, name: &CStr) -> Result<(), Error> { fn add_hardlink(&mut self, name: &CStr) -> Result<(), Error> {
self.writer.write_all(&[CatalogEntryType::Hardlink as u8])?; self.write_all(&[CatalogEntryType::Hardlink as u8])?;
self.writer.write_all(name.to_bytes_with_nul())?; self.write_all(name.to_bytes_with_nul())?;
Ok(()) Ok(())
} }
fn add_block_device(&mut self, name: &CStr) -> Result<(), Error> { fn add_block_device(&mut self, name: &CStr) -> Result<(), Error> {
self.writer.write_all(&[CatalogEntryType::BlockDevice as u8])?; self.write_all(&[CatalogEntryType::BlockDevice as u8])?;
self.writer.write_all(name.to_bytes_with_nul())?; self.write_all(name.to_bytes_with_nul())?;
Ok(()) Ok(())
} }
fn add_char_device(&mut self, name: &CStr) -> Result<(), Error> { fn add_char_device(&mut self, name: &CStr) -> Result<(), Error> {
self.writer.write_all(&[CatalogEntryType::CharDevice as u8])?; self.write_all(&[CatalogEntryType::CharDevice as u8])?;
self.writer.write_all(name.to_bytes_with_nul())?; self.write_all(name.to_bytes_with_nul())?;
Ok(()) Ok(())
} }
fn add_fifo(&mut self, name: &CStr) -> Result<(), Error> { fn add_fifo(&mut self, name: &CStr) -> Result<(), Error> {
self.writer.write_all(&[CatalogEntryType::Fifo as u8])?; self.write_all(&[CatalogEntryType::Fifo as u8])?;
self.writer.write_all(name.to_bytes_with_nul())?; self.write_all(name.to_bytes_with_nul())?;
Ok(()) Ok(())
} }
fn add_socket(&mut self, name: &CStr) -> Result<(), Error> { fn add_socket(&mut self, name: &CStr) -> Result<(), Error> {
self.writer.write_all(&[CatalogEntryType::Socket as u8])?; self.write_all(&[CatalogEntryType::Socket as u8])?;
self.writer.write_all(name.to_bytes_with_nul())?; self.write_all(name.to_bytes_with_nul())?;
Ok(()) Ok(())
} }
} }
impl<W: Write + Seek> CatalogBlobWriter<W> {
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<R: Read + BufRead> { pub struct CatalogBlobReader<R: Read + BufRead> {
reader: BufReader<DataBlobReader<R>>, reader: BufReader<DataBlobReader<R>>,