From 7a630e21b373dd039c874aed528c3ae8f151fc27 Mon Sep 17 00:00:00 2001 From: Jack Fletcher Date: Mon, 24 Jan 2022 18:13:33 +0000 Subject: [PATCH] Sync changes from upstream --- .github/workflows/ci.yaml | 23 ++++++++++- CODE_OF_CONDUCT.md | 1 - Cargo.toml | 11 +++--- README.md | 7 ++-- benches/read_entry.rs | 80 ++++++++++++--------------------------- examples/extract.rs | 3 +- examples/extract_lorem.rs | 2 +- examples/file_info.rs | 3 +- examples/stdin_info.rs | 3 +- examples/write_dir.rs | 6 +-- examples/write_sample.rs | 4 +- tests/issue_234.rs | 31 +++++++++++++++ 12 files changed, 97 insertions(+), 77 deletions(-) create mode 100644 tests/issue_234.rs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 83741894..6f0e4b9d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,7 +16,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macOS-latest, windows-latest] - rust: [stable, 1.36.0] + rust: [stable, 1.54.0] steps: - uses: actions/checkout@master @@ -39,6 +39,25 @@ jobs: command: test args: --all + clippy: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly + override: true + components: clippy + + - name: clippy + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --all-targets --all-features -- -D warnings + check_fmt_and_docs: name: Checking fmt and docs runs-on: ubuntu-latest @@ -54,4 +73,4 @@ jobs: run: cargo fmt --all -- --check - name: Docs - run: cargo doc \ No newline at end of file + run: cargo doc diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 845634eb..2290ec2b 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -1,4 +1,3 @@ - # Contributor Covenant Code of Conduct ## Our Pledge diff --git a/Cargo.toml b/Cargo.toml index 1afd00d3..34af3ea2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,16 +12,15 @@ edition = "2018" [dependencies] flate2 = { version = "1.0.0", default-features = false, optional = true } -time = { version = "0.1", optional = true } +time = { version = "0.3", features = ["formatting", "macros" ], optional = true } byteorder = "1.3" bzip2 = { version = "0.4", optional = true } -crc32fast = "1.0" -thiserror = "1.0" -zstd = { version = "0.8", optional = true } +crc32fast = "1.1.1" +zstd = { version = "0.10", optional = true } [dev-dependencies] -criterion = "0.3" -rand = "0.7" +bencher = "0.1" +getrandom = "0.2" walkdir = "2" [features] diff --git a/README.md b/README.md index e1e3385d..be471f6d 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ zip-rs [![Build Status](https://img.shields.io/github/workflow/status/zip-rs/zip/CI)](https://github.com/zip-rs/zip/actions?query=branch%3Amaster+workflow%3ACI) [![Crates.io version](https://img.shields.io/crates/v/zip.svg)](https://crates.io/crates/zip) +[![Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/rQ7H9cSsF4) [Documentation](https://docs.rs/zip/0.5.13/zip/) @@ -17,7 +18,7 @@ Supported compression formats: * stored (i.e. none) * deflate * bzip2 -* zstd (in progress...) +* zstd Currently unsupported zip extensions: @@ -43,7 +44,7 @@ zip = { version = "0.5", default-features = false } The features available are: -* `deflate`: Enables the deflate compression algorithm, which is the default for zipfiles. +* `deflate`: Enables the deflate compression algorithm, which is the default for zip files. * `bzip2`: Enables the BZip2 compression algorithm. * `time`: Enables features using the [time](https://github.com/rust-lang-deprecated/time) crate. * `zstd`: Enables the Zstandard compression algorithm. @@ -53,7 +54,7 @@ All of these are enabled by default. MSRV ---- -Our current Minimum Supported Rust Version is **1.36.0**. When adding features, +Our current Minimum Supported Rust Version is **1.54.0**. When adding features, we will follow these guidelines: - We will always support the latest four minor Rust versions. This gives you a 6 diff --git a/benches/read_entry.rs b/benches/read_entry.rs index bb91d6dc..af9affe3 100644 --- a/benches/read_entry.rs +++ b/benches/read_entry.rs @@ -1,75 +1,43 @@ -use criterion::{criterion_group, criterion_main}; -use criterion::{BenchmarkId, Criterion}; +use bencher::{benchmark_group, benchmark_main}; use std::io::{Cursor, Read, Write}; -use rand::Rng; -use zip::{CompressionMethod, ZipArchive, ZipWriter}; +use bencher::Bencher; +use getrandom::getrandom; +use zip::{ZipArchive, ZipWriter}; -fn generate_random_archive(size: usize, method: Option) -> Vec { +fn generate_random_archive(size: usize) -> Vec { let data = Vec::new(); let mut writer = ZipWriter::new(Cursor::new(data)); - let options = zip::write::FileOptions::default() - .compression_method(method.unwrap_or(CompressionMethod::Stored)); + let options = + zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored); writer.start_file("random.dat", options).unwrap(); - - // Generate some random data. let mut bytes = vec![0u8; size]; - rand::thread_rng().fill(bytes.as_mut_slice()); - + getrandom(&mut bytes).unwrap(); writer.write_all(&bytes).unwrap(); writer.finish().unwrap().into_inner() } -fn read_entry(bench: &mut Criterion) { +fn read_entry(bench: &mut Bencher) { let size = 1024 * 1024; + let bytes = generate_random_archive(size); + let mut archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap(); - // - let mut group = bench.benchmark_group("read_entry"); + bench.iter(|| { + let mut file = archive.by_name("random.dat").unwrap(); + let mut buf = [0u8; 1024]; + loop { + let n = file.read(&mut buf).unwrap(); + if n == 0 { + break; + } + } + }); - // - for method in CompressionMethod::supported_methods().iter() { - group.bench_with_input(BenchmarkId::from_parameter(method), method, |bench, method| { - let bytes = generate_random_archive(size, Some(*method)); - - bench.iter(|| { - let mut archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap(); - let mut file = archive.by_name("random.dat").unwrap(); - let mut buf = [0u8; 1024]; - - let mut total_bytes = 0; - - loop { - let n = file.read(&mut buf).unwrap(); - total_bytes += n; - if n == 0 { - return total_bytes; - } - } - }); - }); - } + bench.bytes = size as u64; } -fn write_random_archive(bench: &mut Criterion) { - let size = 1024 * 1024; - - // - let mut group = bench.benchmark_group("write_random_archive"); - - // - for method in CompressionMethod::supported_methods().iter() { - group.bench_with_input(BenchmarkId::from_parameter(method), method, |b, method| { - b.iter(|| { - generate_random_archive(size, Some(*method)); - }) - }); - } - - group.finish(); -} - -criterion_group!(benches, read_entry, write_random_archive); -criterion_main!(benches); +benchmark_group!(benches, read_entry); +benchmark_main!(benches); diff --git a/examples/extract.rs b/examples/extract.rs index 05c5a4aa..b02eb4cd 100644 --- a/examples/extract.rs +++ b/examples/extract.rs @@ -59,5 +59,6 @@ fn real_main() -> i32 { } } } - return 0; + + 0 } diff --git a/examples/extract_lorem.rs b/examples/extract_lorem.rs index 89e33ef9..a34a04f4 100644 --- a/examples/extract_lorem.rs +++ b/examples/extract_lorem.rs @@ -27,5 +27,5 @@ fn real_main() -> i32 { file.read_to_string(&mut contents).unwrap(); println!("{}", contents); - return 0; + 0 } diff --git a/examples/file_info.rs b/examples/file_info.rs index 315b5c38..824278df 100644 --- a/examples/file_info.rs +++ b/examples/file_info.rs @@ -49,5 +49,6 @@ fn real_main() -> i32 { ); } } - return 0; + + 0 } diff --git a/examples/stdin_info.rs b/examples/stdin_info.rs index 606944ce..10d7aa8b 100644 --- a/examples/stdin_info.rs +++ b/examples/stdin_info.rs @@ -30,5 +30,6 @@ fn real_main() -> i32 { } } } - return 0; + + 0 } diff --git a/examples/write_dir.rs b/examples/write_dir.rs index a78bc43e..8cc561ff 100644 --- a/examples/write_dir.rs +++ b/examples/write_dir.rs @@ -59,7 +59,7 @@ fn real_main() -> i32 { } } - return 0; + 0 } fn zip_dir( @@ -92,7 +92,7 @@ where f.read_to_end(&mut buffer)?; zip.write_all(&*buffer)?; buffer.clear(); - } else if name.as_os_str().len() != 0 { + } else if !name.as_os_str().is_empty() { // Only if not root! Avoids path spec / warning // and mapname conversion failed error on unzip println!("adding dir {:?} as {:?} ...", path, name); @@ -116,7 +116,7 @@ fn doit( let path = Path::new(dst_file); let file = File::create(&path).unwrap(); - let walkdir = WalkDir::new(src_dir.to_string()); + let walkdir = WalkDir::new(src_dir); let it = walkdir.into_iter(); zip_dir(&mut it.filter_map(|e| e.ok()), src_dir, file, method)?; diff --git a/examples/write_sample.rs b/examples/write_sample.rs index 4ef5ce34..b5749509 100644 --- a/examples/write_sample.rs +++ b/examples/write_sample.rs @@ -18,7 +18,7 @@ fn real_main() -> i32 { Err(e) => println!("Error: {:?}", e), } - return 0; + 0 } fn doit(filename: &str) -> zip::result::ZipResult<()> { @@ -42,7 +42,7 @@ fn doit(filename: &str) -> zip::result::ZipResult<()> { Ok(()) } -const LOREM_IPSUM : &'static [u8] = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tellus elit, tristique vitae mattis egestas, ultricies vitae risus. Quisque sit amet quam ut urna aliquet +const LOREM_IPSUM : &[u8] = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tellus elit, tristique vitae mattis egestas, ultricies vitae risus. Quisque sit amet quam ut urna aliquet molestie. Proin blandit ornare dui, a tempor nisl accumsan in. Praesent a consequat felis. Morbi metus diam, auctor in auctor vel, feugiat id odio. Curabitur ex ex, dictum quis auctor quis, suscipit id lorem. Aliquam vestibulum dolor nec enim vehicula, porta tristique augue tincidunt. Vivamus ut gravida est. Sed pellentesque, dolor vitae tristique consectetur, neque lectus pulvinar dui, sed feugiat purus diam id lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per diff --git a/tests/issue_234.rs b/tests/issue_234.rs new file mode 100644 index 00000000..bd01d1d0 --- /dev/null +++ b/tests/issue_234.rs @@ -0,0 +1,31 @@ +use zip::result::ZipError; + +const BUF: &[u8] = &[ + 0, 80, 75, 1, 2, 127, 120, 0, 3, 3, 75, 80, 232, 3, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 7, 0, 0, 0, + 0, 65, 0, 1, 0, 0, 0, 4, 0, 0, 224, 255, 0, 255, 255, 255, 255, 255, 255, 20, 39, 221, 221, + 221, 221, 221, 221, 205, 221, 221, 221, 42, 221, 221, 221, 221, 221, 221, 221, 221, 38, 34, 34, + 219, 80, 75, 5, 6, 0, 0, 0, 0, 5, 96, 0, 1, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 234, 236, 124, + 221, 221, 37, 221, 221, 221, 221, 221, 129, 4, 0, 0, 221, 221, 80, 75, 1, 2, 127, 120, 0, 4, 0, + 0, 2, 127, 120, 0, 79, 75, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, + 234, 0, 0, 0, 3, 8, 4, 232, 3, 0, 0, 0, 255, 255, 255, 255, 1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 3, 0, + 221, 209, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 58, 58, 42, 75, 9, 2, 127, + 120, 0, 99, 99, 99, 99, 99, 99, 94, 7, 0, 0, 0, 0, 0, 0, 213, 213, 213, 213, 213, 213, 213, + 213, 213, 7, 0, 0, 211, 211, 211, 211, 124, 236, 99, 99, 99, 94, 7, 0, 0, 0, 0, 0, 0, 213, 213, + 213, 213, 213, 213, 213, 213, 213, 7, 0, 0, 211, 211, 211, 211, 124, 236, 234, 0, 0, 0, 3, 8, + 0, 0, 0, 12, 0, 0, 0, 0, 0, 3, 0, 0, 0, 7, 0, 0, 0, 0, 0, 58, 58, 58, 42, 175, 221, 253, 221, + 221, 221, 221, 221, 80, 75, 9, 2, 127, 120, 0, 99, 99, 99, 99, 99, 99, 94, 7, 0, 0, 0, 0, 0, 0, + 213, 213, 213, 213, 213, 213, 213, 213, 213, 7, 0, 0, 211, 211, 211, 211, 124, 236, 221, 221, + 221, 221, 221, 80, 75, 9, 2, 127, 120, 0, 99, 99, 99, 99, 99, 99, 94, 7, 0, 0, 0, 0, 0, 0, 213, + 213, 213, 213, 213, 213, 213, 213, 213, 7, 0, 0, 211, 211, 211, 211, 124, 236, +]; + +#[test] +fn invalid_header() { + let reader = std::io::Cursor::new(&BUF); + let archive = zip::ZipArchive::new(reader); + match archive { + Err(ZipError::InvalidArchive(_)) => {} + value => panic!("Unexpected value: {:?}", value), + } +}