Merge pull request #201 from nichmor/fix/soft-links-should-remain-the-same

fix: soft links should remain the same
This commit is contained in:
Chris Hennick 2024-07-18 17:26:17 +00:00 committed by GitHub
commit 6106a2bf0b
Signed by: DevComp
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View file

@ -1083,7 +1083,7 @@ impl<R: Read + Seek> ZipArchive<R> {
}
let symlink_target = if file.is_symlink() && (cfg!(unix) || cfg!(windows)) {
let mut target = Vec::with_capacity(file.size() as usize);
file.read_exact(&mut target)?;
file.read_to_end(&mut target)?;
Some(target)
} else {
None
@ -1097,8 +1097,7 @@ impl<R: Read + Seek> ZipArchive<R> {
{
use std::os::unix::ffi::OsStringExt;
let target = OsString::from_vec(target);
let target_path = directory.as_ref().join(target);
std::os::unix::fs::symlink(target_path, outpath.as_path())?;
std::os::unix::fs::symlink(&target, outpath.as_path())?;
}
#[cfg(windows)]
{

Binary file not shown.

20
tests/extract_symlink.rs Normal file
View file

@ -0,0 +1,20 @@
#[test]
#[cfg(all(unix, feature = "_deflate-any"))]
fn extract_should_respect_links() {
use std::{fs, io, path::PathBuf, str::FromStr};
use tempdir::TempDir;
use zip::ZipArchive;
let mut v = Vec::new();
v.extend_from_slice(include_bytes!("data/pandoc_soft_links.zip"));
let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file");
let temp_dir = TempDir::new("pandoc_soft_links").unwrap();
archive.extract(&temp_dir).unwrap();
let symlink_path = temp_dir.path().join("pandoc-3.2-arm64/bin/pandoc-lua");
// Read the target of the symbolic link
let target_path = fs::read_link(&symlink_path).unwrap();
assert_eq!(target_path, PathBuf::from_str("pandoc").unwrap());
}