Skip to content

Commit ae94b34

Browse files
authored
chore: Fix clippy and cargo fmt warnings (#310)
* chore: Fix new clippy warning (iter().any(==) to contains) Signed-off-by: Chris Hennick <[email protected]> * Fix build Signed-off-by: Chris Hennick <[email protected]> * fix: Windows type mismatch Signed-off-by: Chris Hennick <[email protected]> * Fix: need to convert to OsStr first, then to Path Signed-off-by: Chris Hennick <[email protected]> * Fix another Clippy lint Signed-off-by: Chris Hennick <[email protected]> * Fix clippy lint: simplify to pop()? Signed-off-by: Chris Hennick <[email protected]> * style: fix cargo fmt Signed-off-by: Chris Hennick <[email protected]> * Fix: more as_path() uses that Clippy didn't detect Signed-off-by: Chris Hennick <[email protected]> * style: Fix cargo fmt Signed-off-by: Chris Hennick <[email protected]> * style: cargo fmt decided line was still too long Signed-off-by: Chris Hennick <[email protected]> * chore: Fix type mismatch Signed-off-by: Chris Hennick <[email protected]> * Chore: update argument type for make_symlink Signed-off-by: Chris Hennick <[email protected]> * Fix: std::str::from_utf8 needs to be qualified Signed-off-by: Chris Hennick <[email protected]> * Fix: make_symlink needs access to the IndexMap keys * style: Conditionalize an import * style: Fix a clippy warning and a fmt whitespace discrepancy * fix: Remove stray "continue", and validate symlinks as UTF-8 on all platforms --------- Signed-off-by: Chris Hennick <[email protected]>
1 parent a2e062f commit ae94b34

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

src/path.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ pub(crate) fn simplified_components(input: &Path) -> Option<Vec<&OsStr>> {
1212
match component {
1313
Component::Prefix(_) | Component::RootDir => return None,
1414
Component::ParentDir => {
15-
if out.pop().is_none() {
16-
return None;
17-
}
15+
out.pop()?;
1816
}
1917
Component::Normal(_) => out.push(component.as_os_str()),
2018
Component::CurDir => (),

src/read.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -449,41 +449,40 @@ pub(crate) fn make_reader(
449449
))))
450450
}
451451

452-
pub(crate) fn make_symlink(outpath: &PathBuf, target: Vec<u8>) -> ZipResult<()> {
452+
pub(crate) fn make_symlink<T>(
453+
outpath: &Path,
454+
target: &[u8],
455+
#[allow(unused)] existing_files: &IndexMap<Box<str>, T>,
456+
) -> ZipResult<()> {
457+
let Ok(target_str) = std::str::from_utf8(target) else {
458+
return Err(ZipError::InvalidArchive("Invalid UTF-8 as symlink target"));
459+
};
460+
453461
#[cfg(not(any(unix, windows)))]
454462
{
455-
let output = File::create(outpath.as_path());
463+
let output = File::create(outpath);
456464
output.write_all(target)?;
457-
continue;
458465
}
459-
460-
let Ok(target) = String::from_utf8(target) else {
461-
return Err(ZipError::InvalidArchive("Invalid UTF-8 as symlink target"));
462-
};
463-
let target = Path::new(&target);
464-
465466
#[cfg(unix)]
466467
{
467-
std::os::unix::fs::symlink(target, outpath.as_path())?;
468+
std::os::unix::fs::symlink(Path::new(&target_str), outpath)?;
468469
}
469470
#[cfg(windows)]
470471
{
471-
let Ok(target) = String::from_utf8(target) else {
472-
return Err(ZipError::InvalidArchive("Invalid UTF-8 as symlink target"));
473-
};
474-
let target = target.into_boxed_str();
475-
let target_is_dir_from_archive = self.shared.files.contains_key(&target) && is_dir(&target);
472+
let target = Path::new(OsStr::new(&target_str));
473+
let target_is_dir_from_archive =
474+
existing_files.contains_key(target_str) && is_dir(target_str);
476475
let target_is_dir = if target_is_dir_from_archive {
477476
true
478-
} else if let Ok(meta) = std::fs::metadata(&target) {
477+
} else if let Ok(meta) = std::fs::metadata(target) {
479478
meta.is_dir()
480479
} else {
481480
false
482481
};
483482
if target_is_dir {
484-
std::os::windows::fs::symlink_dir(target, outpath.as_path())?;
483+
std::os::windows::fs::symlink_dir(target, outpath)?;
485484
} else {
486-
std::os::windows::fs::symlink_file(target, outpath.as_path())?;
485+
std::os::windows::fs::symlink_file(target, outpath)?;
487486
}
488487
}
489488
Ok(())
@@ -816,7 +815,7 @@ impl<R: Read + Seek> ZipArchive<R> {
816815
drop(file);
817816

818817
if let Some(target) = symlink_target {
819-
make_symlink(&outpath, target)?;
818+
make_symlink(&outpath, &target, &self.shared.files)?;
820819
continue;
821820
}
822821
let mut file = self.by_index(i)?;

src/read/stream.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::fs;
2-
use std::io::{self, Read};
3-
use std::path::{Path, PathBuf};
4-
51
use super::{
62
central_header_to_zip_file_inner, make_symlink, read_zipfile_from_stream, ZipCentralEntryBlock,
7-
ZipError, ZipFile, ZipFileData, ZipResult,
3+
ZipFile, ZipFileData, ZipResult,
84
};
95
use crate::spec::FixedSizeBlock;
6+
use indexmap::IndexMap;
7+
use std::fs;
8+
use std::io::{self, Read};
9+
use std::path::{Path, PathBuf};
1010

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

6667
if file.is_symlink() {
6768
let mut target = Vec::with_capacity(file.size() as usize);
6869
file.read_to_end(&mut target)?;
69-
make_symlink(&outpath, target)?;
70+
make_symlink(&outpath, &target, &self.1)?;
7071
return Ok(());
7172
}
7273

@@ -87,6 +88,7 @@ impl<R: Read> ZipStreamReader<R> {
8788
) -> ZipResult<()> {
8889
#[cfg(unix)]
8990
{
91+
use super::ZipError;
9092
let filepath = metadata
9193
.enclosed_name()
9294
.ok_or(ZipError::InvalidArchive("Invalid file path"))?;
@@ -103,7 +105,10 @@ impl<R: Read> ZipStreamReader<R> {
103105
}
104106
}
105107

106-
self.visit(&mut Extractor(directory.as_ref().canonicalize()?))
108+
self.visit(&mut Extractor(
109+
directory.as_ref().canonicalize()?,
110+
IndexMap::new(),
111+
))
107112
}
108113
}
109114

src/write.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,7 @@ impl ExtendedFileOptions {
347347
{
348348
use crate::unstable::LittleEndianReadExt;
349349
let header_id = data.read_u16_le()?;
350-
if EXTRA_FIELD_MAPPING
351-
.iter()
352-
.any(|&mapped| mapped == header_id)
353-
{
350+
if EXTRA_FIELD_MAPPING.contains(&header_id) {
354351
return Err(ZipError::Io(io::Error::new(
355352
io::ErrorKind::Other,
356353
format!(

0 commit comments

Comments
 (0)