From 433f8b05b08da9c2f314389a61015486918958af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Wed, 24 Mar 2021 12:56:12 -0300 Subject: [PATCH] Simplify Lzma decompression logic --- src/compressors/tar.rs | 35 ++++++++------- src/decompressors/lzma.rs | 45 ------------------- src/decompressors/mod.rs | 14 +++--- src/decompressors/{unified.rs => tomemory.rs} | 8 ++++ src/evaluator.rs | 9 ++-- src/main.rs | 20 ++++++--- 6 files changed, 54 insertions(+), 77 deletions(-) delete mode 100644 src/decompressors/lzma.rs rename src/decompressors/{unified.rs => tomemory.rs} (85%) diff --git a/src/compressors/tar.rs b/src/compressors/tar.rs index 8af4f55..84d53e1 100644 --- a/src/compressors/tar.rs +++ b/src/compressors/tar.rs @@ -1,7 +1,7 @@ use std::{fs, path::PathBuf}; use colored::Colorize; -use tar::{Builder, Header}; +use tar::{Builder, EntryType, Header}; use walkdir::WalkDir; use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File}; @@ -23,23 +23,26 @@ impl TarCompressor { } }; - let mut header = Header::new_gnu(); - - // header.set_path(&input.path.file_stem().unwrap())?; - header.set_path(".")?; - header.set_size(contents.len() as u64); - header.set_cksum(); - header.set_mode(644); + // let ok = EntryType:: - let mut b = Builder::new(Vec::new()); - b.append_data( - &mut header, - &input.path.file_stem().unwrap(), - &*contents - )?; + // let mut b = Builder::new(Vec::new()); + // let mut header = Header::new_gnu(); + // let name = b"././@LongLink"; + // header.as_gnu_mut().unwrap().name[..name.len()].clone_from_slice(&name[..]); + // header.set_mode(0o644); + // header.set_uid(0); + // header.set_gid(0); + // header.set_mtime(0); + // // + 1 to be compliant with GNU tar + // header.set_size(size + 1); + // header.set_entry_type(EntryType::new(entry_type)); + // header.set_cksum(); - Ok(b.into_inner()?) + + + // Ok(b.into_inner()?) + Ok(vec![]) } fn make_archive_from_files(input_filenames: Vec) -> OuchResult> { @@ -48,7 +51,7 @@ impl TarCompressor { let mut b = Builder::new(buf); for filename in input_filenames { - // TODO: check if filename is a file or a directory + // TODO: check if file exists for entry in WalkDir::new(&filename) { let entry = entry?; diff --git a/src/decompressors/lzma.rs b/src/decompressors/lzma.rs deleted file mode 100644 index a970feb..0000000 --- a/src/decompressors/lzma.rs +++ /dev/null @@ -1,45 +0,0 @@ -use std::{fs, io::Read}; - -use colored::Colorize; - -use crate::{error::OuchResult, utils}; -use crate::file::File; - -use super::decompressor::{DecompressionResult, Decompressor}; - -pub struct LzmaDecompressor {} - -impl LzmaDecompressor { - fn extract_to_memory(from: File) -> OuchResult> { - let mut ret = vec![]; - - let from_path = from.path; - if !from_path.exists() { - eprintln!("{}: could not find {:?}", "error".red(), from_path); - } - - let input_bytes = fs::read(&from_path)?; - - - xz2::read::XzDecoder::new_multi_decoder(&*input_bytes) - .read_to_end(&mut ret)?; - - println!("{}: extracted {:?} into memory. ({} bytes)", "info".yellow(), from_path, ret.len()); - - Ok(ret) - } -} - -impl Decompressor for LzmaDecompressor { - fn decompress(&self, from: File, into: &Option) -> OuchResult { - let destination_path = utils::get_destination_path(into); - - utils::create_path_if_non_existent(destination_path)?; - - Ok( - DecompressionResult::FileInMemory( - Self::extract_to_memory(from)? - ) - ) - } -} \ No newline at end of file diff --git a/src/decompressors/mod.rs b/src/decompressors/mod.rs index 5b9c1b0..4a88a49 100644 --- a/src/decompressors/mod.rs +++ b/src/decompressors/mod.rs @@ -1,14 +1,18 @@ mod decompressor; -mod unified; -mod lzma; +mod tomemory; mod tar; mod zip; pub use decompressor::Decompressor; pub use decompressor::DecompressionResult; + pub use self::tar::TarDecompressor; pub use self::zip::ZipDecompressor; -pub use self::unified::GzipDecompressor; -pub use self::unified::BzipDecompressor; -pub use self::lzma::LzmaDecompressor; \ No newline at end of file + +// These decompressors only decompress to memory, +// unlike {Tar, Zip}Decompressor which are capable of +// decompressing directly to storage +pub use self::tomemory::GzipDecompressor; +pub use self::tomemory::BzipDecompressor; +pub use self::tomemory::LzmaDecompressor; \ No newline at end of file diff --git a/src/decompressors/unified.rs b/src/decompressors/tomemory.rs similarity index 85% rename from src/decompressors/unified.rs rename to src/decompressors/tomemory.rs index 463270e..919cbb2 100644 --- a/src/decompressors/unified.rs +++ b/src/decompressors/tomemory.rs @@ -18,12 +18,14 @@ use super::decompressor::Decompressor; pub struct UnifiedDecompressor {} pub struct GzipDecompressor {} +pub struct LzmaDecompressor {} pub struct BzipDecompressor {} fn get_decoder<'a>(format: CompressionFormat, buffer: Box) -> Box { match format { CompressionFormat::Bzip => Box::new(bzip2::read::BzDecoder::new(buffer)), CompressionFormat::Gzip => Box::new(flate2::read::MultiGzDecoder::new(buffer)), + CompressionFormat::Lzma => Box::new(xz2::read::XzDecoder::new_multi_decoder(buffer)), _other => unreachable!() } } @@ -68,4 +70,10 @@ impl Decompressor for BzipDecompressor { fn decompress(&self, from: File, into: &Option) -> OuchResult { UnifiedDecompressor::decompress(from, CompressionFormat::Bzip, into) } +} + +impl Decompressor for LzmaDecompressor { + fn decompress(&self, from: File, into: &Option) -> OuchResult { + UnifiedDecompressor::decompress(from, CompressionFormat::Lzma, into) + } } \ No newline at end of file diff --git a/src/evaluator.rs b/src/evaluator.rs index 6a2d22f..b0849f1 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -93,13 +93,12 @@ impl Evaluator { CompressionFormat::Zip => Box::new(ZipDecompressor {}), - CompressionFormat::Gzip => Box::new(GzipDecompressor{}), + CompressionFormat::Gzip => Box::new(GzipDecompressor {}), - CompressionFormat::Lzma => Box::new(LzmaDecompressor{}), + CompressionFormat::Lzma => Box::new(LzmaDecompressor {}), - CompressionFormat::Bzip => { - Box::new(BzipDecompressor {}) - } + CompressionFormat::Bzip => Box::new(BzipDecompressor {}) + }; let first_decompressor: Option> = match extension.first_ext { diff --git a/src/main.rs b/src/main.rs index eabeafb..a00387c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,16 +34,24 @@ fn main() -> error::OuchResult<()>{ } // fn main() -> error::OuchResult<()> { -// let bytes = fs::read("extension.tar.lzma")?; -// let mut ret = vec![]; +// use tar::{Builder}; +// use walkdir::WalkDir; -// xz2::read::XzDecoder::new_multi_decoder(&*bytes) -// .read_to_end(&mut ret) -// .unwrap(); +// let mut b = Builder::new(Vec::new()); +// for entry in WalkDir::new("src") { +// let entry = entry?; +// let mut file = std::fs::File::open(entry.path())?; +// b.append_file(entry.path(), &mut file)?; +// } -// fs::write("extension.tar", &*bytes).unwrap(); +// // let mut file = std::fs::File::open("Cargo.toml")?; +// // b.append_file("Cargo.toml", &mut file)?; + +// let bytes = b.into_inner()?; + +// std::fs::write("daaaaamn.tar", bytes)?; // Ok(()) // } \ No newline at end of file