Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public constructor for PaxExtensions #271

Merged
merged 1 commit into from Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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