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 a method to write a link name without canonicalization #274

Merged
merged 1 commit into from Dec 14, 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
12 changes: 12 additions & 0 deletions src/header.rs
Expand Up @@ -435,6 +435,18 @@ impl Header {
})
}

/// Sets the link name for this header without any transformation.
///
/// This function is like [`Self::set_link_name`] but accepts an arbitrary byte array.
/// Hence it will not perform any canonicalization, such as replacing duplicate `//` with `/`.
pub fn set_link_name_literal<P: AsRef<[u8]>>(&mut self, p: P) -> io::Result<()> {
self._set_link_name_literal(p.as_ref())
}

fn _set_link_name_literal(&mut self, bytes: &[u8]) -> io::Result<()> {
copy_into(&mut self.as_old_mut().linkname, bytes)
}

/// Returns the mode bits for this file
///
/// May return an error if the field is corrupted.
Expand Down
22 changes: 22 additions & 0 deletions tests/all.rs
Expand Up @@ -935,6 +935,28 @@ fn long_linkname_gnu() {
}
}

#[test]
fn linkname_literal() {
for t in [tar::EntryType::Symlink, tar::EntryType::Link] {
let mut b = Builder::new(Vec::<u8>::new());
let mut h = Header::new_gnu();
h.set_entry_type(t);
h.set_size(0);
let path = "usr/lib/systemd/systemd-sysv-install";
let target = "../../..//sbin/chkconfig";
h.set_link_name_literal(target).unwrap();
t!(b.append_data(&mut h, path, std::io::empty()));

let contents = t!(b.into_inner());
let mut a = Archive::new(&contents[..]);

let e = &t!(t!(a.entries()).next().unwrap());
assert_eq!(e.header().entry_type(), t);
assert_eq!(e.path().unwrap().to_str().unwrap(), path);
assert_eq!(e.link_name().unwrap().unwrap().to_str().unwrap(), target);
}
}

#[test]
fn encoded_long_name_has_trailing_nul() {
let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
Expand Down