src/backup/catalog.rs - Direntry::parse: abort if callback return false

This commit is contained in:
Dietmar Maurer 2019-11-12 11:21:19 +01:00
parent 7d017123fd
commit 8f24a9ea18

View File

@ -179,7 +179,7 @@ impl DirInfo {
Ok((self.name, data)) Ok((self.name, data))
} }
fn parse<C: FnMut(CatalogEntryType, &[u8], u64, u64, u64) -> Result<(), Error>>( fn parse<C: FnMut(CatalogEntryType, &[u8], u64, u64, u64) -> Result<bool, Error>>(
data: &[u8], data: &[u8],
mut callback: C, mut callback: C,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -203,19 +203,22 @@ impl DirInfo {
let name = &mut name_buf[0..name_len]; let name = &mut name_buf[0..name_len];
cursor.read_exact(name)?; cursor.read_exact(name)?;
match etype { let cont = match etype {
CatalogEntryType::Directory => { CatalogEntryType::Directory => {
let offset = catalog_decode_u64(&mut cursor)?; let offset = catalog_decode_u64(&mut cursor)?;
callback(etype, name, offset, 0, 0)?; callback(etype, name, offset, 0, 0)?
} }
CatalogEntryType::File => { CatalogEntryType::File => {
let size = catalog_decode_u64(&mut cursor)?; let size = catalog_decode_u64(&mut cursor)?;
let mtime = catalog_decode_u64(&mut cursor)?; let mtime = catalog_decode_u64(&mut cursor)?;
callback(etype, name, 0, size, mtime)?; callback(etype, name, 0, size, mtime)?
} }
_ => { _ => {
callback(etype, name, 0, 0, 0)?; callback(etype, name, 0, 0, 0)?
} }
};
if !cont {
return Ok(());
} }
} }
@ -414,7 +417,7 @@ impl <R: Read + Seek> CatalogReader<R> {
DirInfo::parse(&data, |etype, name, offset, size, mtime| { DirInfo::parse(&data, |etype, name, offset, size, mtime| {
let entry = DirEntry::new(etype, name.to_vec(), offset, size, mtime); let entry = DirEntry::new(etype, name.to_vec(), offset, size, mtime);
entry_list.push(entry); entry_list.push(entry);
Ok(()) Ok(true)
})?; })?;
Ok(entry_list) Ok(entry_list)
@ -437,13 +440,12 @@ impl <R: Read + Seek> CatalogReader<R> {
let mut item = None; let mut item = None;
DirInfo::parse(&data, |etype, name, offset, size, mtime| { DirInfo::parse(&data, |etype, name, offset, size, mtime| {
if name != filename { if name != filename {
return Ok(()); return Ok(true);
} }
let entry = DirEntry::new(etype, name.to_vec(), offset, size, mtime); let entry = DirEntry::new(etype, name.to_vec(), offset, size, mtime);
item = Some(entry); item = Some(entry);
Ok(false) // stop parsing
Ok(())
})?; })?;
match item { match item {
@ -496,7 +498,7 @@ impl <R: Read + Seek> CatalogReader<R> {
} }
} }
Ok(()) Ok(true)
}) })
} }
} }