2019-03-15 08:36:05 +00:00
|
|
|
//! *pxar* format decoder for seekable files
|
|
|
|
//!
|
|
|
|
//! This module contain the code to decode *pxar* archive files.
|
|
|
|
|
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use super::format_definition::*;
|
|
|
|
use super::sequential_decoder::*;
|
|
|
|
|
2019-03-15 09:18:28 +00:00
|
|
|
use std::io::{Read, Seek, SeekFrom};
|
2019-03-15 08:36:05 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2019-03-15 09:18:28 +00:00
|
|
|
use std::ffi::OsString;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-08-13 12:50:12 +00:00
|
|
|
pub struct DirectoryEntry {
|
2019-03-15 08:36:05 +00:00
|
|
|
start: u64,
|
|
|
|
end: u64,
|
|
|
|
pub filename: OsString,
|
2019-08-02 13:19:34 +00:00
|
|
|
pub entry: PxarEntry,
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 12:14:29 +00:00
|
|
|
impl DirectoryEntry {
|
|
|
|
pub(crate) fn start_offset(&self) -> u64 {
|
|
|
|
self.start
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn end_offset(&self) -> u64 {
|
|
|
|
self.end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 08:36:05 +00:00
|
|
|
// This one needs Read+Seek
|
2019-08-13 12:50:13 +00:00
|
|
|
pub struct Decoder<R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> {
|
|
|
|
inner: SequentialDecoder<R, F>,
|
2019-03-15 08:36:05 +00:00
|
|
|
root_start: u64,
|
|
|
|
root_end: u64,
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
const HEADER_SIZE: u64 = std::mem::size_of::<PxarHeader>() as u64;
|
2019-08-20 11:40:17 +00:00
|
|
|
const GOODBYE_ITEM_SIZE: u64 = std::mem::size_of::<PxarGoodbyeItem>() as u64;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-09-03 11:17:02 +00:00
|
|
|
impl<R: Read + Seek, F: Fn(&Path) -> Result<(), Error>> Decoder<R, F> {
|
2019-08-13 12:50:13 +00:00
|
|
|
pub fn new(mut reader: R, callback: F) -> Result<Self, Error> {
|
2019-03-15 08:36:05 +00:00
|
|
|
let root_end = reader.seek(SeekFrom::End(0))?;
|
|
|
|
|
|
|
|
Ok(Self {
|
2019-08-02 13:19:33 +00:00
|
|
|
inner: SequentialDecoder::new(reader, super::flags::DEFAULT, callback),
|
2019-03-15 08:36:05 +00:00
|
|
|
root_start: 0,
|
|
|
|
root_end: root_end,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-20 11:40:18 +00:00
|
|
|
pub fn root(&mut self) -> Result<DirectoryEntry, Error> {
|
|
|
|
self.seek(SeekFrom::Start(0))?;
|
|
|
|
let header: PxarHeader = self.inner.read_item()?;
|
|
|
|
check_ca_header::<PxarEntry>(&header, PXAR_ENTRY)?;
|
|
|
|
let entry: PxarEntry = self.inner.read_item()?;
|
|
|
|
Ok(DirectoryEntry {
|
2019-03-15 08:36:05 +00:00
|
|
|
start: self.root_start,
|
|
|
|
end: self.root_end,
|
|
|
|
filename: OsString::new(), // Empty
|
2019-08-20 11:40:18 +00:00
|
|
|
entry: entry,
|
|
|
|
})
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error> {
|
|
|
|
let pos = self.inner.get_reader_mut().seek(pos)?;
|
|
|
|
Ok(pos)
|
|
|
|
}
|
|
|
|
|
2019-09-03 12:14:29 +00:00
|
|
|
pub(crate) fn root_end_offset(&self) -> u64 {
|
|
|
|
self.root_end
|
|
|
|
}
|
|
|
|
|
2019-09-03 11:17:02 +00:00
|
|
|
pub fn restore(&mut self, dir: &DirectoryEntry, path: &Path) -> Result<(), Error> {
|
2019-03-15 08:36:05 +00:00
|
|
|
let start = dir.start;
|
|
|
|
|
|
|
|
self.seek(SeekFrom::Start(start))?;
|
|
|
|
|
2019-07-16 16:19:44 +00:00
|
|
|
self.inner.restore(path, &Vec::new())?;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-13 12:50:12 +00:00
|
|
|
fn read_directory_entry(&mut self, start: u64, end: u64) -> Result<DirectoryEntry, Error> {
|
2019-03-15 08:36:05 +00:00
|
|
|
self.seek(SeekFrom::Start(start))?;
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.inner.read_item()?;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
if head.htype != PXAR_FILENAME {
|
2019-03-15 08:36:05 +00:00
|
|
|
bail!("wrong filename header type for object [{}..{}]", start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
let entry_start = start + head.size;
|
|
|
|
|
|
|
|
let filename = self.inner.read_filename(head.size)?;
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.inner.read_item()?;
|
2019-08-20 11:40:19 +00:00
|
|
|
if head.htype == PXAR_FORMAT_HARDLINK {
|
|
|
|
let (_, offset) = self.inner.read_hardlink(head.size)?;
|
|
|
|
return self.read_directory_entry(start - offset, end);
|
|
|
|
}
|
2019-08-02 13:19:34 +00:00
|
|
|
check_ca_header::<PxarEntry>(&head, PXAR_ENTRY)?;
|
|
|
|
let entry: PxarEntry = self.inner.read_item()?;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-08-13 12:50:12 +00:00
|
|
|
Ok(DirectoryEntry {
|
2019-03-15 08:36:05 +00:00
|
|
|
start: entry_start,
|
|
|
|
end: end,
|
|
|
|
filename: filename,
|
|
|
|
entry,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-13 12:50:12 +00:00
|
|
|
pub fn list_dir(&mut self, dir: &DirectoryEntry) -> Result<Vec<DirectoryEntry>, Error> {
|
2019-03-15 08:36:05 +00:00
|
|
|
let start = dir.start;
|
|
|
|
let end = dir.end;
|
|
|
|
|
|
|
|
//println!("list_dir1: {} {}", start, end);
|
|
|
|
|
|
|
|
if (end - start) < (HEADER_SIZE + GOODBYE_ITEM_SIZE) {
|
|
|
|
bail!("detected short object [{}..{}]", start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.seek(SeekFrom::Start(end - GOODBYE_ITEM_SIZE))?;
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let item: PxarGoodbyeItem = self.inner.read_item()?;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
if item.hash != PXAR_GOODBYE_TAIL_MARKER {
|
2019-09-03 11:17:02 +00:00
|
|
|
bail!(
|
|
|
|
"missing goodbye tail marker for object [{}..{}]",
|
|
|
|
start,
|
|
|
|
end
|
|
|
|
);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let goodbye_table_size = item.size;
|
|
|
|
if goodbye_table_size < (HEADER_SIZE + GOODBYE_ITEM_SIZE) {
|
|
|
|
bail!("short goodbye table size for object [{}..{}]", start, end);
|
|
|
|
}
|
|
|
|
let goodbye_inner_size = goodbye_table_size - HEADER_SIZE - GOODBYE_ITEM_SIZE;
|
|
|
|
if (goodbye_inner_size % GOODBYE_ITEM_SIZE) != 0 {
|
2019-09-03 11:17:02 +00:00
|
|
|
bail!(
|
|
|
|
"wrong goodbye inner table size for entry [{}..{}]",
|
|
|
|
start,
|
|
|
|
end
|
|
|
|
);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let goodbye_start = end - goodbye_table_size;
|
|
|
|
|
|
|
|
if item.offset != (goodbye_start - start) {
|
2019-09-03 11:17:02 +00:00
|
|
|
println!(
|
|
|
|
"DEBUG: {} {}",
|
|
|
|
u64::from_le(item.offset),
|
|
|
|
goodbye_start - start
|
|
|
|
);
|
|
|
|
bail!(
|
|
|
|
"wrong offset in goodbye tail marker for entry [{}..{}]",
|
|
|
|
start,
|
|
|
|
end
|
|
|
|
);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.seek(SeekFrom::Start(goodbye_start))?;
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.inner.read_item()?;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
if head.htype != PXAR_GOODBYE {
|
2019-09-03 11:17:02 +00:00
|
|
|
bail!(
|
|
|
|
"wrong goodbye table header type for entry [{}..{}]",
|
|
|
|
start,
|
|
|
|
end
|
|
|
|
);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if head.size != goodbye_table_size {
|
|
|
|
bail!("wrong goodbye table size for entry [{}..{}]", start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut range_list = Vec::new();
|
|
|
|
|
2019-09-03 11:17:02 +00:00
|
|
|
for i in 0..goodbye_inner_size / GOODBYE_ITEM_SIZE {
|
2019-08-02 13:19:34 +00:00
|
|
|
let item: PxarGoodbyeItem = self.inner.read_item()?;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
|
|
|
if item.offset > (goodbye_start - start) {
|
2019-09-03 11:17:02 +00:00
|
|
|
bail!(
|
|
|
|
"goodbye entry {} offset out of range [{}..{}] {} {} {}",
|
|
|
|
i,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
item.offset,
|
|
|
|
goodbye_start,
|
|
|
|
start
|
|
|
|
);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
let item_start = goodbye_start - item.offset;
|
|
|
|
let item_end = item_start + item.size;
|
|
|
|
if item_end > goodbye_start {
|
2019-09-03 11:17:02 +00:00
|
|
|
bail!("goodbye entry {} end out of range [{}..{}]", i, start, end);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
range_list.push((item_start, item_end));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
for (item_start, item_end) in range_list {
|
|
|
|
let entry = self.read_directory_entry(item_start, item_end)?;
|
|
|
|
//println!("ENTRY: {} {} {:?}", item_start, item_end, entry.filename);
|
|
|
|
result.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_filenames<W: std::io::Write>(
|
|
|
|
&mut self,
|
|
|
|
output: &mut W,
|
|
|
|
prefix: &mut PathBuf,
|
2019-08-13 12:50:12 +00:00
|
|
|
dir: &DirectoryEntry,
|
2019-03-15 08:36:05 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut list = self.list_dir(dir)?;
|
|
|
|
|
|
|
|
list.sort_unstable_by(|a, b| a.filename.cmp(&b.filename));
|
|
|
|
|
|
|
|
for item in &list {
|
|
|
|
prefix.push(item.filename.clone());
|
|
|
|
|
|
|
|
let mode = item.entry.mode as u32;
|
|
|
|
|
|
|
|
let ifmt = mode & libc::S_IFMT;
|
|
|
|
|
2019-03-15 09:18:28 +00:00
|
|
|
writeln!(output, "{:?}", prefix)?;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
|
|
|
if ifmt == libc::S_IFDIR {
|
|
|
|
self.print_filenames(output, prefix, item)?;
|
|
|
|
} else if ifmt == libc::S_IFREG {
|
|
|
|
} else if ifmt == libc::S_IFLNK {
|
|
|
|
} else if ifmt == libc::S_IFBLK {
|
|
|
|
} else if ifmt == libc::S_IFCHR {
|
|
|
|
} else {
|
|
|
|
bail!("unknown item mode/type for {:?}", prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|