Skip to content

Commit

Permalink
Add public constructor for PaxExtensions (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
anrdnet committed Nov 10, 2021
1 parent 8333ef6 commit 5a1c8ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/entry.rs
Expand Up @@ -13,7 +13,6 @@ use crate::archive::ArchiveInner;
use crate::error::TarError;
use crate::header::bytes2path;
use crate::other;
use crate::pax::pax_extensions;
use crate::{Archive, Header, PaxExtensions};

/// A read-only view into an entry of an archive.
Expand Down Expand Up @@ -300,7 +299,7 @@ impl<'a> EntryFields<'a> {
}
None => {
if let Some(ref pax) = self.pax_extensions {
let pax = pax_extensions(pax)
let pax = PaxExtensions::new(pax)
.filter_map(|f| f.ok())
.find(|f| f.key_bytes() == b"path")
.map(|f| f.value_bytes());
Expand Down Expand Up @@ -336,7 +335,7 @@ impl<'a> EntryFields<'a> {
}
None => {
if let Some(ref pax) = self.pax_extensions {
let pax = pax_extensions(pax)
let pax = PaxExtensions::new(pax)
.filter_map(|f| f.ok())
.find(|f| f.key_bytes() == b"linkpath")
.map(|f| f.value_bytes());
Expand All @@ -358,7 +357,9 @@ impl<'a> EntryFields<'a> {
}
self.pax_extensions = Some(self.read_all()?);
}
Ok(Some(pax_extensions(self.pax_extensions.as_ref().unwrap())))
Ok(Some(PaxExtensions::new(
self.pax_extensions.as_ref().unwrap(),
)))
}

fn unpack_in(&mut self, dst: &Path) -> io::Result<bool> {
Expand Down
23 changes: 13 additions & 10 deletions src/pax.rs
Expand Up @@ -12,23 +12,26 @@ pub struct PaxExtensions<'entry> {
data: slice::Split<'entry, u8, fn(&u8) -> bool>,
}

impl<'entry> PaxExtensions<'entry> {
/// Create new pax extensions iterator from the given entry data.
pub fn new(a: &'entry [u8]) -> Self {
fn is_newline(a: &u8) -> bool {
*a == b'\n'
}
PaxExtensions {
data: a.split(is_newline),
}
}
}

/// A key/value pair corresponding to a pax extension.
pub struct PaxExtension<'entry> {
key: &'entry [u8],
value: &'entry [u8],
}

pub fn pax_extensions(a: &[u8]) -> PaxExtensions {
fn is_newline(a: &u8) -> bool {
*a == b'\n'
}
PaxExtensions {
data: a.split(is_newline),
}
}

pub fn pax_extensions_size(a: &[u8]) -> Option<u64> {
for extension in pax_extensions(a) {
for extension in PaxExtensions::new(a) {
let current_extension = match extension {
Ok(ext) => ext,
Err(_) => return None,
Expand Down

0 comments on commit 5a1c8ea

Please sign in to comment.