2019-03-15 08:36:05 +00:00
|
|
|
//! *pxar* format decoder for seekable files
|
|
|
|
//!
|
|
|
|
//! This module contain the code to decode *pxar* archive files.
|
|
|
|
|
2019-09-05 15:27:58 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-11-05 16:11:04 +00:00
|
|
|
use std::ffi::{OsString, OsStr};
|
2019-09-05 15:27:58 +00:00
|
|
|
use std::io::{Read, Seek, SeekFrom};
|
|
|
|
use std::path::{Path, PathBuf};
|
2019-11-05 16:11:04 +00:00
|
|
|
use std::os::unix::ffi::OsStrExt;
|
2019-09-05 15:27:58 +00:00
|
|
|
|
2019-03-15 08:36:05 +00:00
|
|
|
use failure::*;
|
2019-09-05 15:27:58 +00:00
|
|
|
use libc;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-12-06 12:13:18 +00:00
|
|
|
use super::binary_search_tree::search_binary_tree_by;
|
2019-03-15 08:36:05 +00:00
|
|
|
use super::format_definition::*;
|
2019-11-05 12:45:04 +00:00
|
|
|
use super::sequential_decoder::SequentialDecoder;
|
|
|
|
use super::match_pattern::MatchPattern;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-09-05 15:27:58 +00:00
|
|
|
use proxmox::tools::io::ReadExt;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-08-13 12:50:12 +00:00
|
|
|
pub struct DirectoryEntry {
|
2019-11-05 16:02:53 +00:00
|
|
|
/// Points to the `PxarEntry` of the directory
|
2019-03-15 08:36:05 +00:00
|
|
|
start: u64,
|
2019-11-05 16:02:53 +00:00
|
|
|
/// Points past the goodbye table tail
|
2019-03-15 08:36:05 +00:00
|
|
|
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-11-26 09:45:11 +00:00
|
|
|
/// Trait to create ReadSeek Decoder trait objects.
|
|
|
|
trait ReadSeek: Read + Seek {}
|
|
|
|
impl <R: Read + Seek> ReadSeek for R {}
|
|
|
|
|
2019-03-15 08:36:05 +00:00
|
|
|
// This one needs Read+Seek
|
2019-11-26 09:45:11 +00:00
|
|
|
pub struct Decoder {
|
2019-12-16 08:58:26 +00:00
|
|
|
inner: SequentialDecoder<Box<dyn ReadSeek + Send>>,
|
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-11-26 09:45:11 +00:00
|
|
|
impl Decoder {
|
2019-12-16 08:58:26 +00:00
|
|
|
pub fn new<R: Read + Seek + Send + 'static>(mut reader: R) -> Result<Self, Error> {
|
2019-03-15 08:36:05 +00:00
|
|
|
let root_end = reader.seek(SeekFrom::End(0))?;
|
2019-12-16 08:58:26 +00:00
|
|
|
let boxed_reader: Box<dyn ReadSeek + 'static + Send> = Box::new(reader);
|
2019-11-26 09:45:11 +00:00
|
|
|
let inner = SequentialDecoder::new(boxed_reader, super::flags::DEFAULT);
|
2019-11-26 08:56:48 +00:00
|
|
|
|
|
|
|
Ok(Self { inner, root_start: 0, root_end })
|
|
|
|
}
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-12-16 08:58:26 +00:00
|
|
|
pub fn set_callback<F: Fn(&Path) -> Result<(), Error> + Send + 'static>(&mut self, callback: F ) {
|
2019-11-26 08:56:48 +00:00
|
|
|
self.inner.set_callback(callback);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
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-09-11 10:06:59 +00:00
|
|
|
entry,
|
2019-08-20 11:40:18 +00:00
|
|
|
})
|
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-11-05 12:45:04 +00:00
|
|
|
/// Restore the subarchive starting at `dir` to the provided target `path`.
|
|
|
|
///
|
|
|
|
/// Only restore the content matched by the MatchPattern `pattern`.
|
|
|
|
/// An empty Vec `pattern` means restore all.
|
|
|
|
pub fn restore(&mut self, dir: &DirectoryEntry, path: &Path, pattern: &Vec<MatchPattern>) -> Result<(), Error> {
|
2019-03-15 08:36:05 +00:00
|
|
|
let start = dir.start;
|
|
|
|
self.seek(SeekFrom::Start(start))?;
|
2019-11-05 12:45:04 +00:00
|
|
|
self.inner.restore(path, pattern)?;
|
2019-03-15 08:36:05 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-09 16:27:23 +00:00
|
|
|
pub(crate) 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)?;
|
2019-09-05 11:17:40 +00:00
|
|
|
// TODO: Howto find correct end offset for hardlink target?
|
|
|
|
// This is a bit tricky since we cannot find correct end in an efficient
|
|
|
|
// way, on the other hand it doesn't really matter (for now) since target
|
|
|
|
// is never a directory and end is not used in such cases.
|
2019-08-20 11:40:19 +00:00
|
|
|
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,
|
2019-09-11 10:06:59 +00:00
|
|
|
end,
|
|
|
|
filename,
|
2019-03-15 08:36:05 +00:00
|
|
|
entry,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-09 16:27:24 +00:00
|
|
|
/// Return the goodbye table based on the provided end offset.
|
|
|
|
///
|
|
|
|
/// Get the goodbye table entries and the start and end offsets of the
|
|
|
|
/// items they reference.
|
|
|
|
/// If the start offset is provided, we use that to check the consistency of
|
|
|
|
/// the data, else the start offset calculated based on the goodbye tail is
|
|
|
|
/// used.
|
|
|
|
pub(crate) fn goodbye_table(
|
|
|
|
&mut self,
|
|
|
|
start: Option<u64>,
|
|
|
|
end: u64,
|
|
|
|
) -> Result<Vec<(PxarGoodbyeItem, u64, u64)>, Error> {
|
2019-03-15 08:36:05 +00:00
|
|
|
self.seek(SeekFrom::Start(end - GOODBYE_ITEM_SIZE))?;
|
|
|
|
|
2019-09-09 16:27:24 +00:00
|
|
|
let tail: PxarGoodbyeItem = self.inner.read_item()?;
|
|
|
|
if tail.hash != PXAR_GOODBYE_TAIL_MARKER {
|
|
|
|
bail!("missing goodbye tail marker for object at offset {}", end);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 16:27:24 +00:00
|
|
|
// If the start offset was provided, we use and check based on that.
|
|
|
|
// If not, we rely on the offset calculated from the goodbye table entry.
|
|
|
|
let start = start.unwrap_or(end - tail.offset - tail.size);
|
|
|
|
let goodbye_table_size = tail.size;
|
2019-03-15 08:36:05 +00:00
|
|
|
if goodbye_table_size < (HEADER_SIZE + GOODBYE_ITEM_SIZE) {
|
|
|
|
bail!("short goodbye table size for object [{}..{}]", start, end);
|
|
|
|
}
|
2019-09-09 16:27:24 +00:00
|
|
|
|
2019-03-15 08:36:05 +00:00
|
|
|
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;
|
2019-09-09 16:27:24 +00:00
|
|
|
if tail.offset != (goodbye_start - start) {
|
2019-09-03 11:17:02 +00:00
|
|
|
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()?;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-09-09 16:27:24 +00:00
|
|
|
let mut gb_entries = 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
|
|
|
}
|
2019-09-09 16:27:24 +00:00
|
|
|
gb_entries.push((item, item_start, item_end));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(gb_entries)
|
|
|
|
}
|
2019-03-15 08:36:05 +00:00
|
|
|
|
2019-09-09 16:27:24 +00:00
|
|
|
pub fn list_dir(&mut self, dir: &DirectoryEntry) -> Result<Vec<DirectoryEntry>, Error> {
|
|
|
|
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);
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut result = vec![];
|
2019-09-09 16:27:24 +00:00
|
|
|
let goodbye_table = self.goodbye_table(Some(start), end)?;
|
|
|
|
for (_, item_start, item_end) in goodbye_table {
|
2019-03-15 08:36:05 +00:00
|
|
|
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
|
|
|
|
2019-09-05 11:17:40 +00:00
|
|
|
match ifmt {
|
|
|
|
libc::S_IFDIR => self.print_filenames(output, prefix, item)?,
|
|
|
|
libc::S_IFREG | libc::S_IFLNK | libc::S_IFBLK | libc::S_IFCHR => {}
|
|
|
|
_ => bail!("unknown item mode/type for {:?}", prefix),
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prefix.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-05 15:27:58 +00:00
|
|
|
|
2019-11-05 16:11:04 +00:00
|
|
|
/// Lookup the item identified by `filename` in the provided `DirectoryEntry`.
|
|
|
|
///
|
|
|
|
/// Calculates the hash of the filename and searches for matching entries in
|
|
|
|
/// the goodbye table of the provided `DirectoryEntry`.
|
|
|
|
/// If found, also the filename is compared to avoid hash collision.
|
|
|
|
/// If the filename does not match, the search resumes with the next entry in
|
|
|
|
/// the goodbye table.
|
|
|
|
/// If there is no entry with matching `filename`, `Ok(None)` is returned.
|
|
|
|
pub fn lookup(
|
|
|
|
&mut self,
|
|
|
|
dir: &DirectoryEntry,
|
|
|
|
filename: &OsStr,
|
2019-11-14 16:42:15 +00:00
|
|
|
) -> Result<Option<(DirectoryEntry, PxarAttributes, u64)>, Error> {
|
2019-11-05 16:11:04 +00:00
|
|
|
let gbt = self.goodbye_table(Some(dir.start), dir.end)?;
|
|
|
|
let hash = compute_goodbye_hash(filename.as_bytes());
|
|
|
|
|
2019-12-06 12:13:18 +00:00
|
|
|
let mut start_idx = 0;
|
|
|
|
let mut skip_multiple = 0;
|
2019-11-05 16:11:04 +00:00
|
|
|
loop {
|
|
|
|
// Search for the next goodbye entry with matching hash.
|
2019-12-06 12:13:18 +00:00
|
|
|
let idx = search_binary_tree_by(
|
|
|
|
start_idx,
|
|
|
|
gbt.len(),
|
|
|
|
skip_multiple,
|
|
|
|
|idx| hash.cmp(&gbt[idx].0.hash),
|
|
|
|
);
|
|
|
|
let (_item, start, end) = match idx {
|
|
|
|
Some(idx) => &gbt[idx],
|
2019-11-05 16:11:04 +00:00
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
// At this point it is not clear if the item is a directory or not,
|
|
|
|
// this has to be decided based on the entry mode.
|
|
|
|
// `Decoder`s attributes function accepts both, offsets pointing to
|
|
|
|
// the start of an item (PXAR_FILENAME) or the GOODBYE_TAIL_MARKER in
|
|
|
|
// case of directories, so the use of start offset is fine for both
|
|
|
|
// cases.
|
2019-11-14 16:42:15 +00:00
|
|
|
let (entry_name, entry, attr, payload_size) = self.attributes(*start)?;
|
2019-11-05 16:11:04 +00:00
|
|
|
|
|
|
|
// Possible hash collision, need to check if the found entry is indeed
|
|
|
|
// the filename to lookup.
|
|
|
|
if entry_name == filename {
|
|
|
|
let dir_entry = DirectoryEntry {
|
|
|
|
start: *start + HEADER_SIZE + entry_name.len() as u64 + 1,
|
|
|
|
end: *end,
|
|
|
|
filename: entry_name,
|
|
|
|
entry,
|
|
|
|
};
|
2019-11-14 16:42:15 +00:00
|
|
|
return Ok(Some((dir_entry, attr, payload_size)));
|
2019-11-05 16:11:04 +00:00
|
|
|
}
|
2019-12-06 12:13:18 +00:00
|
|
|
// Hash collision, check the next entry in the goodbye table by starting
|
|
|
|
// from given index but skipping one more match (so hash at index itself).
|
|
|
|
start_idx = idx.unwrap();
|
|
|
|
skip_multiple = 1;
|
2019-11-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 15:27:58 +00:00
|
|
|
/// Get attributes for the archive item located at `offset`.
|
|
|
|
///
|
|
|
|
/// Returns the entry, attributes and the payload size for the item.
|
|
|
|
/// For regular archive itmes a `PXAR_FILENAME` or a `PXAR_ENTRY` header is
|
|
|
|
/// expected at `offset`.
|
|
|
|
/// For directories, `offset` might also (but not necessarily) point at the
|
|
|
|
/// directories `PXAR_GOODBYE_TAIL_MARKER`. This is not mandatory and it can
|
|
|
|
/// also directly point to its `PXAR_FILENAME` or `PXAR_ENTRY`, thereby
|
|
|
|
/// avoiding an additional seek.
|
2019-09-11 15:25:03 +00:00
|
|
|
pub fn attributes(&mut self, offset: u64) -> Result<(OsString, PxarEntry, PxarAttributes, u64), Error> {
|
2019-09-05 15:27:58 +00:00
|
|
|
self.seek(SeekFrom::Start(offset))?;
|
|
|
|
|
|
|
|
let mut marker: u64 = self.inner.read_item()?;
|
|
|
|
if marker == PXAR_GOODBYE_TAIL_MARKER {
|
|
|
|
let dir_offset: u64 = self.inner.read_item()?;
|
|
|
|
let gb_size: u64 = self.inner.read_item()?;
|
|
|
|
let distance = i64::try_from(dir_offset + gb_size)?;
|
|
|
|
self.seek(SeekFrom::Current(0 - distance))?;
|
|
|
|
marker = self.inner.read_item()?;
|
|
|
|
}
|
|
|
|
|
2019-09-11 15:25:03 +00:00
|
|
|
let filename = if marker == PXAR_FILENAME {
|
2019-09-05 15:27:58 +00:00
|
|
|
let size: u64 = self.inner.read_item()?;
|
2019-09-11 15:25:03 +00:00
|
|
|
let filename = self.inner.read_filename(size)?;
|
2019-09-05 15:27:58 +00:00
|
|
|
marker = self.inner.read_item()?;
|
2019-09-11 15:25:03 +00:00
|
|
|
filename
|
|
|
|
} else {
|
|
|
|
OsString::new()
|
|
|
|
};
|
2019-09-09 16:27:25 +00:00
|
|
|
|
|
|
|
if marker == PXAR_FORMAT_HARDLINK {
|
|
|
|
let size: u64 = self.inner.read_item()?;
|
|
|
|
let (_, diff) = self.inner.read_hardlink(size)?;
|
2019-11-27 15:27:55 +00:00
|
|
|
// Make sure to return the original filename,
|
|
|
|
// not the one read from the hardlink.
|
|
|
|
let (_, entry, xattr, file_size) = self.attributes(offset - diff)?;
|
|
|
|
return Ok((filename, entry, xattr, file_size));
|
2019-09-09 16:27:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 15:27:58 +00:00
|
|
|
if marker != PXAR_ENTRY {
|
|
|
|
bail!("Expected PXAR_ENTRY, found 0x{:x?}", marker);
|
|
|
|
}
|
|
|
|
let _size: u64 = self.inner.read_item()?;
|
|
|
|
let entry: PxarEntry = self.inner.read_item()?;
|
|
|
|
let (header, xattr) = self.inner.read_attributes()?;
|
|
|
|
let file_size = match header.htype {
|
|
|
|
PXAR_PAYLOAD => header.size - HEADER_SIZE,
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
|
2019-09-11 15:25:03 +00:00
|
|
|
Ok((filename, entry, xattr, file_size))
|
2019-09-05 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Opens the file by validating the given `offset` and returning its attrs,
|
|
|
|
/// xattrs and size.
|
2019-09-11 15:25:03 +00:00
|
|
|
pub fn open(&mut self, offset: u64) -> Result<(OsString, PxarEntry, PxarAttributes, u64), Error> {
|
2019-09-05 15:27:58 +00:00
|
|
|
self.attributes(offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the payload of the file given by `offset`.
|
|
|
|
///
|
|
|
|
/// This will read the file by first seeking to `offset` within the archive,
|
|
|
|
/// check if there is indeed a valid item with payload and then read `size`
|
|
|
|
/// bytes of content starting from `data_offset`.
|
|
|
|
/// If EOF is reached before reading `size` bytes, the reduced buffer is
|
|
|
|
/// returned.
|
|
|
|
pub fn read(&mut self, offset: u64, size: usize, data_offset: u64) -> Result<Vec<u8>, Error> {
|
|
|
|
self.seek(SeekFrom::Start(offset))?;
|
2019-09-16 14:59:31 +00:00
|
|
|
let head: PxarHeader = self.inner.read_item()?;
|
|
|
|
if head.htype != PXAR_FILENAME {
|
|
|
|
bail!("Expected PXAR_FILENAME, encountered 0x{:x?}", head.htype);
|
|
|
|
}
|
|
|
|
let _filename = self.inner.read_filename(head.size)?;
|
2019-09-05 15:27:58 +00:00
|
|
|
|
|
|
|
let head: PxarHeader = self.inner.read_item()?;
|
2019-11-27 15:27:56 +00:00
|
|
|
if head.htype == PXAR_FORMAT_HARDLINK {
|
|
|
|
let (_, diff) = self.inner.read_hardlink(head.size)?;
|
|
|
|
return self.read(offset - diff, size, data_offset);
|
|
|
|
}
|
2019-09-05 15:27:58 +00:00
|
|
|
check_ca_header::<PxarEntry>(&head, PXAR_ENTRY)?;
|
|
|
|
let _: PxarEntry = self.inner.read_item()?;
|
|
|
|
|
|
|
|
let (header, _) = self.inner.read_attributes()?;
|
|
|
|
if header.htype != PXAR_PAYLOAD {
|
|
|
|
bail!("Expected PXAR_PAYLOAD, found 0x{:x?}", header.htype);
|
|
|
|
}
|
|
|
|
|
|
|
|
let payload_size = header.size - HEADER_SIZE;
|
|
|
|
if data_offset >= payload_size {
|
|
|
|
return Ok(Vec::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
let len = if data_offset + u64::try_from(size)? > payload_size {
|
|
|
|
usize::try_from(payload_size - data_offset)?
|
|
|
|
} else {
|
|
|
|
size
|
|
|
|
};
|
|
|
|
self.inner.skip_bytes(usize::try_from(data_offset)?)?;
|
|
|
|
let data = self.inner.get_reader_mut().read_exact_allocated(len)?;
|
|
|
|
|
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the target of a hardlink in the archive.
|
|
|
|
pub fn read_link(&mut self, offset: u64) -> Result<(PathBuf, PxarEntry), Error> {
|
|
|
|
self.seek(SeekFrom::Start(offset))?;
|
|
|
|
let mut header: PxarHeader = self.inner.read_item()?;
|
2019-09-09 16:27:30 +00:00
|
|
|
if header.htype != PXAR_FILENAME {
|
|
|
|
bail!("Expected PXAR_FILENAME, encountered 0x{:x?}", header.htype);
|
|
|
|
}
|
|
|
|
let _filename = self.inner.read_filename(header.size)?;
|
|
|
|
|
|
|
|
header = self.inner.read_item()?;
|
2019-09-05 15:27:58 +00:00
|
|
|
check_ca_header::<PxarEntry>(&header, PXAR_ENTRY)?;
|
|
|
|
let entry: PxarEntry = self.inner.read_item()?;
|
|
|
|
|
|
|
|
header = self.inner.read_item()?;
|
|
|
|
if header.htype != PXAR_SYMLINK {
|
|
|
|
bail!("Expected PXAR_SYMLINK, encountered 0x{:x?}", header.htype);
|
|
|
|
}
|
|
|
|
let target = self.inner.read_link(header.size)?;
|
|
|
|
|
|
|
|
Ok((target, entry))
|
|
|
|
}
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|