diff --git a/Cargo.lock b/Cargo.lock index c95299d..fe839b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -519,6 +519,12 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "difflib" version = "0.4.0" @@ -818,6 +824,15 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "jiff" version = "0.2.8" @@ -1081,17 +1096,20 @@ dependencies = [ "filetime_creation", "flate2", "fs-err", + "glob", "gzp", "ignore", "infer", "insta", "is_executable", + "itertools", "libc", "linked-hash-map", "lz4_flex", "num_cpus", "once_cell", "parse-display", + "pretty_assertions", "proptest", "rand 0.8.5", "rayon", @@ -1246,6 +1264,16 @@ dependencies = [ "termtree", ] +[[package]] +name = "pretty_assertions" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.93" @@ -2047,6 +2075,12 @@ dependencies = [ "lzma-sys", ] +[[package]] +name = "yansi" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" + [[package]] name = "zerocopy" version = "0.7.35" diff --git a/Cargo.toml b/Cargo.toml index 3577a2c..17dd326 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ brotli = "7.0.0" bstr = { version = "1.10.0", default-features = false, features = ["std"] } bytesize = "1.3.0" bzip2 = "0.4.4" -bzip3 = { version = "0.9.0", features = ["bundled"] , optional = true } +bzip3 = { version = "0.9.0", features = ["bundled"], optional = true } clap = { version = "4.5.20", features = ["derive", "env"] } filetime_creation = "0.2" flate2 = { version = "1.0.30", default-features = false } @@ -58,9 +58,12 @@ clap_mangen = "0.2.24" [dev-dependencies] assert_cmd = "2.0.14" +glob = "0.3.2" infer = "0.16.0" insta = { version = "1.40.0", features = ["filters"] } +itertools = "0.14.0" parse-display = "0.9.1" +pretty_assertions = "1.4.1" proptest = "1.5.0" rand = { version = "0.8.5", default-features = false, features = [ "small_rng", @@ -88,4 +91,5 @@ inherits = "release" lto = false opt-level = 2 incremental = true -codegen-units = 16 +codegen-units = 32 +strip = false diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index a108c16..6c254ad 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -132,7 +132,7 @@ pub fn decompress_file(options: DecompressOptions) -> crate::Result<()> { Snappy => Box::new(snap::read::FrameDecoder::new(decoder)), Zstd => Box::new(zstd::stream::Decoder::new(decoder)?), Brotli => Box::new(brotli::Decompressor::new(decoder, BUFFER_CAPACITY)), - Tar | Zip | Rar | SevenZip => unreachable!(), + Tar | Zip | Rar | SevenZip => decoder, }; Ok(decoder) }; diff --git a/src/commands/list.rs b/src/commands/list.rs index b3405b4..ef7a5f5 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -36,7 +36,6 @@ pub fn list_archive_contents( let zip_archive = zip::ZipArchive::new(reader)?; let files = crate::archive::zip::list_archive(zip_archive, password); list::list_files(archive_path, files, list_options)?; - return Ok(()); } @@ -46,7 +45,7 @@ pub fn list_archive_contents( // Grab previous decoder and wrap it inside of a new one let chain_reader_decoder = - |format: &CompressionFormat, decoder: Box| -> crate::Result> { + |format: CompressionFormat, decoder: Box| -> crate::Result> { let decoder: Box = match format { Gzip => Box::new(flate2::read::GzDecoder::new(decoder)), Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)), @@ -62,16 +61,22 @@ pub fn list_archive_contents( Snappy => Box::new(snap::read::FrameDecoder::new(decoder)), Zstd => Box::new(zstd::stream::Decoder::new(decoder)?), Brotli => Box::new(brotli::Decompressor::new(decoder, BUFFER_CAPACITY)), - Tar | Zip | Rar | SevenZip => unreachable!(), + Tar | Zip | Rar | SevenZip => unreachable!("should be treated by caller"), }; Ok(decoder) }; - for format in formats.iter().skip(1).rev() { + let mut misplaced_archive_format = None; + for &format in formats.iter().skip(1).rev() { + if format.archive_format() { + misplaced_archive_format = Some(format); + break; + } reader = chain_reader_decoder(format, reader)?; } - let files: Box>> = match formats[0] { + let archive_format = misplaced_archive_format.unwrap_or(formats[0]); + let files: Box>> = match archive_format { Tar => Box::new(crate::archive::tar::list_archive(tar::Archive::new(reader))), Zip => { if formats.len() > 1 { @@ -120,7 +125,7 @@ pub fn list_archive_contents( Box::new(archive::sevenz::list_archive(archive_path, password)?) } Gzip | Bzip | Bzip3 | Lz4 | Lzma | Snappy | Zstd | Brotli => { - panic!("Not an archive! This should never happen, if it does, something is wrong with `CompressionFormat::is_archive()`. Please report this error!"); + unreachable!("Not an archive, should be validated before calling this function."); } }; diff --git a/src/extension.rs b/src/extension.rs index 9e40b28..76d2426 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -60,8 +60,8 @@ impl Extension { /// Checks if the first format in `compression_formats` is an archive pub fn is_archive(&self) -> bool { - // Safety: we check that `compression_formats` is not empty in `Self::new` - self.compression_formats[0].is_archive_format() + // Index Safety: we check that `compression_formats` is not empty in `Self::new` + self.compression_formats[0].archive_format() } } @@ -103,7 +103,7 @@ pub enum CompressionFormat { impl CompressionFormat { /// Currently supported archive formats are .tar (and aliases to it) and .zip - fn is_archive_format(&self) -> bool { + pub fn archive_format(&self) -> bool { // Keep this match like that without a wildcard `_` so we don't forget to update it match self { Tar | Zip | Rar | SevenZip => true, @@ -147,14 +147,13 @@ fn to_extension(ext: &[u8]) -> Option { )) } -fn split_extension(name: &mut &[u8]) -> Option { +fn split_extension_at_end(name: &[u8]) -> Option<(&[u8], Extension)> { let (new_name, ext) = name.rsplit_once_str(b".")?; if matches!(new_name, b"" | b"." | b"..") { return None; } let ext = to_extension(ext)?; - *name = new_name; - Some(ext) + Some((new_name, ext)) } pub fn parse_format_flag(input: &OsStr) -> crate::Result> { @@ -188,7 +187,7 @@ pub fn parse_format_flag(input: &OsStr) -> crate::Result> { /// Extracts extensions from a path. /// -/// Returns both the remaining path and the list of extension objects +/// Returns both the remaining path and the list of extension objects. pub fn separate_known_extensions_from_name(path: &Path) -> (&Path, Vec) { let mut extensions = vec![]; @@ -196,9 +195,12 @@ pub fn separate_known_extensions_from_name(path: &Path) -> (&Path, Vec, _>>() + .unwrap(); + let decompression_expected = format!("{}/out/{}", temp_dir.path().display(), files[1]); + assert_eq!(decompressed_files, [Path::new(&decompression_expected)]); + } +} + +#[test] +fn reading_nested_archives_with_two_archive_extensions_interleaved() { + let archive_formats = ["tar", "zip", "7z"].into_iter(); + + for (first_archive, second_archive) in archive_formats.clone().cartesian_product(archive_formats.rev()) { + let temp_dir = tempdir().unwrap(); + let in_dir = |path: &str| format!("{}/{}", temp_dir.path().display(), path); + + fs::write(in_dir("a.txt"), "contents").unwrap(); + + let files = [ + "a.txt", + &format!("c.{first_archive}"), + &format!("d.{first_archive}.zst"), + &format!("e.{first_archive}.zst.{second_archive}"), + &format!("f.{first_archive}.zst.{second_archive}.lz4"), + ]; + let transformations = [first_archive, "zst", second_archive, "lz4"]; + let compressed_path = in_dir(files.last().unwrap()); + + for (window, format) in files.windows(2).zip(transformations.iter()) { + let [a, b] = [window[0], window[1]].map(in_dir); + crate::utils::cargo_bin() + .args(["compress", &a, &b, "--format", format]) + .assert() + .success(); + } + + // // TODO: uncomment after fixing the 7z BadSignature error [4, 34, 77, 24, 96, 64] + // crate::utils::cargo_bin() + // .args(["list", &compressed_path, "--yes"]) + // .assert() + // .success(); + + crate::utils::cargo_bin() + .args(["decompress", &compressed_path, "--dir", &in_dir("out"), "--yes"]) + .assert() + .success(); + + let decompressed_files = glob::glob(&format!("{}/*", in_dir("out"))) + .unwrap() + .collect::, _>>() + .unwrap(); + let decompression_expected = format!("{}/out/{}", temp_dir.path().display(), files[2]); + assert_eq!(decompressed_files, [Path::new(&decompression_expected)]); + } +}