Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ pub(crate) fn simplified_components(input: &Path) -> Option<Vec<&OsStr>> {
match component {
Component::Prefix(_) | Component::RootDir => return None,
Component::ParentDir => {
if out.pop().is_none() {
return None;
}
out.pop()?;
}
Component::Normal(_) => out.push(component.as_os_str()),
Component::CurDir => (),
Expand Down
37 changes: 18 additions & 19 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,41 +449,40 @@ pub(crate) fn make_reader(
))))
}

pub(crate) fn make_symlink(outpath: &PathBuf, target: Vec<u8>) -> ZipResult<()> {
pub(crate) fn make_symlink<T>(
outpath: &Path,
target: &[u8],
#[allow(unused)] existing_files: &IndexMap<Box<str>, T>,
) -> ZipResult<()> {
let Ok(target_str) = std::str::from_utf8(target) else {
return Err(ZipError::InvalidArchive("Invalid UTF-8 as symlink target"));
};

#[cfg(not(any(unix, windows)))]
{
let output = File::create(outpath.as_path());
let output = File::create(outpath);
output.write_all(target)?;
continue;
}

let Ok(target) = String::from_utf8(target) else {
return Err(ZipError::InvalidArchive("Invalid UTF-8 as symlink target"));
};
let target = Path::new(&target);

#[cfg(unix)]
{
std::os::unix::fs::symlink(target, outpath.as_path())?;
std::os::unix::fs::symlink(Path::new(&target_str), outpath)?;
}
#[cfg(windows)]
{
let Ok(target) = String::from_utf8(target) else {
return Err(ZipError::InvalidArchive("Invalid UTF-8 as symlink target"));
};
let target = target.into_boxed_str();
let target_is_dir_from_archive = self.shared.files.contains_key(&target) && is_dir(&target);
let target = Path::new(OsStr::new(&target_str));
let target_is_dir_from_archive =
existing_files.contains_key(target_str) && is_dir(target_str);
let target_is_dir = if target_is_dir_from_archive {
true
} else if let Ok(meta) = std::fs::metadata(&target) {
} else if let Ok(meta) = std::fs::metadata(target) {
meta.is_dir()
} else {
false
};
if target_is_dir {
std::os::windows::fs::symlink_dir(target, outpath.as_path())?;
std::os::windows::fs::symlink_dir(target, outpath)?;
} else {
std::os::windows::fs::symlink_file(target, outpath.as_path())?;
std::os::windows::fs::symlink_file(target, outpath)?;
}
}
Ok(())
Expand Down Expand Up @@ -816,7 +815,7 @@ impl<R: Read + Seek> ZipArchive<R> {
drop(file);

if let Some(target) = symlink_target {
make_symlink(&outpath, target)?;
make_symlink(&outpath, &target, &self.shared.files)?;
continue;
}
let mut file = self.by_index(i)?;
Expand Down
21 changes: 13 additions & 8 deletions src/read/stream.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::fs;
use std::io::{self, Read};
use std::path::{Path, PathBuf};

use super::{
central_header_to_zip_file_inner, make_symlink, read_zipfile_from_stream, ZipCentralEntryBlock,
ZipError, ZipFile, ZipFileData, ZipResult,
ZipFile, ZipFileData, ZipResult,
};
use crate::spec::FixedSizeBlock;
use indexmap::IndexMap;
use std::fs;
use std::io::{self, Read};
use std::path::{Path, PathBuf};

/// Stream decoder for zip.
#[derive(Debug)]
Expand Down Expand Up @@ -57,16 +57,17 @@ impl<R: Read> ZipStreamReader<R> {
/// Extraction is not atomic; If an error is encountered, some of the files
/// may be left on disk.
pub fn extract<P: AsRef<Path>>(self, directory: P) -> ZipResult<()> {
struct Extractor(PathBuf);
struct Extractor(PathBuf, IndexMap<Box<str>, ()>);
impl ZipStreamVisitor for Extractor {
fn visit_file(&mut self, file: &mut ZipFile<'_>) -> ZipResult<()> {
self.1.insert(file.name().into(), ());
let mut outpath = self.0.clone();
file.safe_prepare_path(&self.0, &mut outpath)?;

if file.is_symlink() {
let mut target = Vec::with_capacity(file.size() as usize);
file.read_to_end(&mut target)?;
make_symlink(&outpath, target)?;
make_symlink(&outpath, &target, &self.1)?;
return Ok(());
}

Expand All @@ -87,6 +88,7 @@ impl<R: Read> ZipStreamReader<R> {
) -> ZipResult<()> {
#[cfg(unix)]
{
use super::ZipError;
let filepath = metadata
.enclosed_name()
.ok_or(ZipError::InvalidArchive("Invalid file path"))?;
Expand All @@ -103,7 +105,10 @@ impl<R: Read> ZipStreamReader<R> {
}
}

self.visit(&mut Extractor(directory.as_ref().canonicalize()?))
self.visit(&mut Extractor(
directory.as_ref().canonicalize()?,
IndexMap::new(),
))
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,7 @@ impl ExtendedFileOptions {
{
use crate::unstable::LittleEndianReadExt;
let header_id = data.read_u16_le()?;
if EXTRA_FIELD_MAPPING
.iter()
.any(|&mapped| mapped == header_id)
{
if EXTRA_FIELD_MAPPING.contains(&header_id) {
return Err(ZipError::Io(io::Error::new(
io::ErrorKind::Other,
format!(
Expand Down