diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:36 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:36 +0000 |
commit | e02c5b5930c2c9ba3e5423fe12e2ef0155017297 (patch) | |
tree | fd60ebbbb5299e16e5fca8c773ddb74f764760db /vendor/tar-0.4.38/src/pax.rs | |
parent | Adding debian version 1.73.0+dfsg1-1. (diff) | |
download | rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.tar.xz rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.zip |
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tar-0.4.38/src/pax.rs')
-rw-r--r-- | vendor/tar-0.4.38/src/pax.rs | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/vendor/tar-0.4.38/src/pax.rs b/vendor/tar-0.4.38/src/pax.rs new file mode 100644 index 000000000..80ca3e9b4 --- /dev/null +++ b/vendor/tar-0.4.38/src/pax.rs @@ -0,0 +1,117 @@ +use std::io; +use std::slice; +use std::str; + +use crate::other; + +/// An iterator over the pax extensions in an archive entry. +/// +/// This iterator yields structures which can themselves be parsed into +/// key/value pairs. +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_size(a: &[u8]) -> Option<u64> { + for extension in PaxExtensions::new(a) { + let current_extension = match extension { + Ok(ext) => ext, + Err(_) => return None, + }; + if current_extension.key() != Ok("size") { + continue; + } + + let value = match current_extension.value() { + Ok(value) => value, + Err(_) => return None, + }; + let size = match value.parse::<u64>() { + Ok(size) => size, + Err(_) => return None, + }; + return Some(size); + } + None +} + +impl<'entry> Iterator for PaxExtensions<'entry> { + type Item = io::Result<PaxExtension<'entry>>; + + fn next(&mut self) -> Option<io::Result<PaxExtension<'entry>>> { + let line = match self.data.next() { + Some(line) if line.is_empty() => return None, + Some(line) => line, + None => return None, + }; + + Some( + line.iter() + .position(|b| *b == b' ') + .and_then(|i| { + str::from_utf8(&line[..i]) + .ok() + .and_then(|len| len.parse::<usize>().ok().map(|j| (i + 1, j))) + }) + .and_then(|(kvstart, reported_len)| { + if line.len() + 1 == reported_len { + line[kvstart..] + .iter() + .position(|b| *b == b'=') + .map(|equals| (kvstart, equals)) + } else { + None + } + }) + .map(|(kvstart, equals)| PaxExtension { + key: &line[kvstart..kvstart + equals], + value: &line[kvstart + equals + 1..], + }) + .ok_or_else(|| other("malformed pax extension")), + ) + } +} + +impl<'entry> PaxExtension<'entry> { + /// Returns the key for this key/value pair parsed as a string. + /// + /// May fail if the key isn't actually utf-8. + pub fn key(&self) -> Result<&'entry str, str::Utf8Error> { + str::from_utf8(self.key) + } + + /// Returns the underlying raw bytes for the key of this key/value pair. + pub fn key_bytes(&self) -> &'entry [u8] { + self.key + } + + /// Returns the value for this key/value pair parsed as a string. + /// + /// May fail if the value isn't actually utf-8. + pub fn value(&self) -> Result<&'entry str, str::Utf8Error> { + str::from_utf8(self.value) + } + + /// Returns the underlying raw bytes for this value of this key/value pair. + pub fn value_bytes(&self) -> &'entry [u8] { + self.value + } +} |