Skip to content

Commit

Permalink
Pax header handler, switch to not owned data types
Browse files Browse the repository at this point in the history
Since we can keep the ownership of the data when calling append_pax_extensions, we can use &str and &[u8] according to the lib documentation
  • Loading branch information
gwitrand-ovh committed Oct 23, 2024
1 parent 3c86863 commit 6d47fbc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/pax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ impl<T: Write> crate::Builder<T> {
/// Takes in an iterator over the list of headers to add to convert it into a header set formatted.
///
/// Returns io::Error if an error occurs, else it returns ()
pub fn append_pax_extensions<'a>(
pub fn append_pax_extensions<'key, 'value>(
&mut self,
headers: impl IntoIterator<Item = (String, String)>,
headers: impl IntoIterator<Item = (&'key str, &'value [u8])>,
) -> Result<(), io::Error> {
// Store the headers formatted before write
let mut data: Vec<u8> = Vec::new();
Expand All @@ -172,7 +172,9 @@ impl<T: Write> crate::Builder<T> {
max_len *= 10;
}
let len = rest_len + len_len;
write!(&mut data, "{} {}={}\n", len, key, value)?;
write!(&mut data, "{} {}=", len, key)?;
data.extend_from_slice(value);
data.push(b'\n');
}

// Ignore the header append if it's empty.
Expand Down
14 changes: 8 additions & 6 deletions tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,12 +932,12 @@ fn pax_simple_write() {
let file: File = t!(File::create(&pax_path));
let mut ar: Builder<BufWriter<File>> = Builder::new(BufWriter::new(file));

let mut pax_builder: Vec<(String, String)> = Vec::new();
let key: String = "arbitrary_pax_key".to_string();
let value: String = "arbitrary_pax_value".to_string();
pax_builder.push((key, value));
let pax_extensions = [
("arbitrary_pax_key", b"arbitrary_pax_value".as_slice()),
("SCHILY.xattr.security.selinux", b"foo_t"),
];

t!(ar.append_pax_extensions(pax_builder.into_iter()));
t!(ar.append_pax_extensions(pax_extensions));
t!(ar.append_file("test2", &mut t!(File::open(&pax_path))));
t!(ar.finish());
drop(ar);
Expand All @@ -950,9 +950,11 @@ fn pax_simple_write() {
assert!(pax_headers.is_some(), "pax_headers is None");
let mut pax_headers = pax_headers.unwrap();
let pax_arbitrary = t!(pax_headers.next().unwrap());

assert_eq!(pax_arbitrary.key(), Ok("arbitrary_pax_key"));
assert_eq!(pax_arbitrary.value(), Ok("arbitrary_pax_value"));
let xattr = t!(pax_headers.next().unwrap());
assert_eq!(xattr.key().unwrap(), pax_extensions[1].0);
assert_eq!(xattr.value_bytes(), pax_extensions[1].1);

assert!(entries.next().is_none());
}
Expand Down

0 comments on commit 6d47fbc

Please sign in to comment.