src/pxar/decoder.rs: rustfmt fixups

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
This commit is contained in:
Christian Ebner 2019-09-03 13:17:02 +02:00 committed by Dietmar Maurer
parent 3fa71727ee
commit 3626ac611f
1 changed files with 38 additions and 24 deletions

View File

@ -12,7 +12,6 @@ use std::path::{Path, PathBuf};
use std::ffi::OsString; use std::ffi::OsString;
pub struct DirectoryEntry { pub struct DirectoryEntry {
start: u64, start: u64,
end: u64, end: u64,
@ -30,10 +29,8 @@ pub struct Decoder<R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> {
const HEADER_SIZE: u64 = std::mem::size_of::<PxarHeader>() as u64; const HEADER_SIZE: u64 = std::mem::size_of::<PxarHeader>() as u64;
const GOODBYE_ITEM_SIZE: u64 = std::mem::size_of::<PxarGoodbyeItem>() as u64; const GOODBYE_ITEM_SIZE: u64 = std::mem::size_of::<PxarGoodbyeItem>() as u64;
impl <R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> { impl<R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> {
pub fn new(mut reader: R, callback: F) -> Result<Self, Error> { pub fn new(mut reader: R, callback: F) -> Result<Self, Error> {
let root_end = reader.seek(SeekFrom::End(0))?; let root_end = reader.seek(SeekFrom::End(0))?;
Ok(Self { Ok(Self {
@ -61,11 +58,7 @@ impl <R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> {
Ok(pos) Ok(pos)
} }
pub fn restore( pub fn restore(&mut self, dir: &DirectoryEntry, path: &Path) -> Result<(), Error> {
&mut self,
dir: &DirectoryEntry,
path: &Path,
) -> Result<(), Error> {
let start = dir.start; let start = dir.start;
self.seek(SeekFrom::Start(start))?; self.seek(SeekFrom::Start(start))?;
@ -76,7 +69,6 @@ impl <R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> {
} }
fn read_directory_entry(&mut self, start: u64, end: u64) -> Result<DirectoryEntry, Error> { fn read_directory_entry(&mut self, start: u64, end: u64) -> Result<DirectoryEntry, Error> {
self.seek(SeekFrom::Start(start))?; self.seek(SeekFrom::Start(start))?;
let head: PxarHeader = self.inner.read_item()?; let head: PxarHeader = self.inner.read_item()?;
@ -106,7 +98,6 @@ impl <R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> {
} }
pub fn list_dir(&mut self, dir: &DirectoryEntry) -> Result<Vec<DirectoryEntry>, Error> { pub fn list_dir(&mut self, dir: &DirectoryEntry) -> Result<Vec<DirectoryEntry>, Error> {
let start = dir.start; let start = dir.start;
let end = dir.end; let end = dir.end;
@ -121,31 +112,50 @@ impl <R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> {
let item: PxarGoodbyeItem = self.inner.read_item()?; let item: PxarGoodbyeItem = self.inner.read_item()?;
if item.hash != PXAR_GOODBYE_TAIL_MARKER { if item.hash != PXAR_GOODBYE_TAIL_MARKER {
bail!("missing goodbye tail marker for object [{}..{}]", start, end); bail!(
"missing goodbye tail marker for object [{}..{}]",
start,
end
);
} }
let goodbye_table_size = item.size; let goodbye_table_size = item.size;
if goodbye_table_size < (HEADER_SIZE + GOODBYE_ITEM_SIZE) { if goodbye_table_size < (HEADER_SIZE + GOODBYE_ITEM_SIZE) {
bail!("short goodbye table size for object [{}..{}]", start, end); bail!("short goodbye table size for object [{}..{}]", start, end);
} }
let goodbye_inner_size = goodbye_table_size - HEADER_SIZE - GOODBYE_ITEM_SIZE; let goodbye_inner_size = goodbye_table_size - HEADER_SIZE - GOODBYE_ITEM_SIZE;
if (goodbye_inner_size % GOODBYE_ITEM_SIZE) != 0 { if (goodbye_inner_size % GOODBYE_ITEM_SIZE) != 0 {
bail!("wrong goodbye inner table size for entry [{}..{}]", start, end); bail!(
"wrong goodbye inner table size for entry [{}..{}]",
start,
end
);
} }
let goodbye_start = end - goodbye_table_size; let goodbye_start = end - goodbye_table_size;
if item.offset != (goodbye_start - start) { if item.offset != (goodbye_start - start) {
println!("DEBUG: {} {}", u64::from_le(item.offset), goodbye_start - start); println!(
bail!("wrong offset in goodbye tail marker for entry [{}..{}]", start, end); "DEBUG: {} {}",
u64::from_le(item.offset),
goodbye_start - start
);
bail!(
"wrong offset in goodbye tail marker for entry [{}..{}]",
start,
end
);
} }
self.seek(SeekFrom::Start(goodbye_start))?; self.seek(SeekFrom::Start(goodbye_start))?;
let head: PxarHeader = self.inner.read_item()?; let head: PxarHeader = self.inner.read_item()?;
if head.htype != PXAR_GOODBYE { if head.htype != PXAR_GOODBYE {
bail!("wrong goodbye table header type for entry [{}..{}]", start, end); bail!(
"wrong goodbye table header type for entry [{}..{}]",
start,
end
);
} }
if head.size != goodbye_table_size { if head.size != goodbye_table_size {
@ -154,18 +164,24 @@ impl <R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> {
let mut range_list = Vec::new(); let mut range_list = Vec::new();
for i in 0..goodbye_inner_size/GOODBYE_ITEM_SIZE { for i in 0..goodbye_inner_size / GOODBYE_ITEM_SIZE {
let item: PxarGoodbyeItem = self.inner.read_item()?; let item: PxarGoodbyeItem = self.inner.read_item()?;
if item.offset > (goodbye_start - start) { if item.offset > (goodbye_start - start) {
bail!("goodbye entry {} offset out of range [{}..{}] {} {} {}", bail!(
i, start, end, item.offset, goodbye_start, start); "goodbye entry {} offset out of range [{}..{}] {} {} {}",
i,
start,
end,
item.offset,
goodbye_start,
start
);
} }
let item_start = goodbye_start - item.offset; let item_start = goodbye_start - item.offset;
let item_end = item_start + item.size; let item_end = item_start + item.size;
if item_end > goodbye_start { if item_end > goodbye_start {
bail!("goodbye entry {} end out of range [{}..{}]", bail!("goodbye entry {} end out of range [{}..{}]", i, start, end);
i, start, end);
} }
range_list.push((item_start, item_end)); range_list.push((item_start, item_end));
@ -188,13 +204,11 @@ impl <R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> {
prefix: &mut PathBuf, prefix: &mut PathBuf,
dir: &DirectoryEntry, dir: &DirectoryEntry,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut list = self.list_dir(dir)?; let mut list = self.list_dir(dir)?;
list.sort_unstable_by(|a, b| a.filename.cmp(&b.filename)); list.sort_unstable_by(|a, b| a.filename.cmp(&b.filename));
for item in &list { for item in &list {
prefix.push(item.filename.clone()); prefix.push(item.filename.clone());
let mode = item.entry.mode as u32; let mode = item.entry.mode as u32;