From d72ca9eeae6bb627db285eaaba2b20ccfa14627d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Tue, 23 Mar 2021 03:10:25 -0300 Subject: [PATCH] Tar compression seemingly working --- src/compressors/compressor.rs | 2 +- src/compressors/mod.rs | 3 ++- src/compressors/tar.rs | 32 ++++++++++++++++++++++---------- src/error.rs | 8 ++++++++ src/evaluator.rs | 32 +++++++++++++++++++++++++++++--- src/main.rs | 19 +------------------ 6 files changed, 63 insertions(+), 33 deletions(-) diff --git a/src/compressors/compressor.rs b/src/compressors/compressor.rs index 729d770..5998bef 100644 --- a/src/compressors/compressor.rs +++ b/src/compressors/compressor.rs @@ -14,5 +14,5 @@ pub enum Entry { } pub trait Compressor { - fn compress(&self, from: Vec) -> OuchResult>; + fn compress(&self, from: Entry) -> OuchResult>; } \ No newline at end of file diff --git a/src/compressors/mod.rs b/src/compressors/mod.rs index 2d18d41..739a34e 100644 --- a/src/compressors/mod.rs +++ b/src/compressors/mod.rs @@ -2,4 +2,5 @@ mod tar; mod compressor; pub use compressor::{Compressor}; -pub use self::tar::TarCompressor; \ No newline at end of file +pub use self::tar::TarCompressor; +pub use self::compressor::Entry; \ No newline at end of file diff --git a/src/compressors/tar.rs b/src/compressors/tar.rs index 2e800b3..dc18c13 100644 --- a/src/compressors/tar.rs +++ b/src/compressors/tar.rs @@ -4,7 +4,9 @@ use colored::Colorize; use tar::{Builder, Header}; use walkdir::WalkDir; -use crate::{compressors::Compressor, error::{Error, OuchResult}, file::{self, File}}; +use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File}; + +use super::compressor::Entry; pub struct TarCompressor {} @@ -33,19 +35,19 @@ impl TarCompressor { Ok(b.into_inner()?) } - fn make_archive_from_files(input_files: Vec) -> OuchResult> { + fn make_archive_from_files(input_filenames: Vec) -> OuchResult> { let buf = Vec::new(); let mut b = Builder::new(buf); - for file in input_files { - for entry in WalkDir::new(&file) { - let entry = entry.unwrap(); + for filename in input_filenames { + for entry in WalkDir::new(&filename) { + let entry = entry?; let path = entry.path(); if path.is_dir() { continue; } - b.append_file(path, &mut fs::File::open(path).unwrap()).unwrap(); + b.append_file(path, &mut fs::File::open(path)?)?; } } @@ -54,9 +56,19 @@ impl TarCompressor { } impl Compressor for TarCompressor { - fn compress(&self, from: Vec) -> OuchResult> { - Ok( - TarCompressor::make_archive_from_files(from)? - ) + fn compress(&self, from: Entry) -> OuchResult> { + + match from { + Entry::Files(filenames) => { + Ok( + Self::make_archive_from_files(filenames)? + ) + }, + Entry::InMemory(file) => { + Ok( + Self::make_archive_from_memory(file)? + ) + } + } } } \ No newline at end of file diff --git a/src/error.rs b/src/error.rs index 63c6ba4..9cd2223 100644 --- a/src/error.rs +++ b/src/error.rs @@ -86,4 +86,12 @@ impl From for Error { NifErr::IOError(io_err) => Self::from(io_err) } } +} + +impl From for Error { + fn from(err: walkdir::Error) -> Self { + eprintln!("{}: {}", "error".red(), err); + + Self::InvalidInput + } } \ No newline at end of file diff --git a/src/evaluator.rs b/src/evaluator.rs index 8aee8a8..9b715dc 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -2,7 +2,7 @@ use std::{ffi::OsStr, fs, io::Write, path::PathBuf}; use colored::Colorize; -use crate::{compressors::TarCompressor, decompressors::TarDecompressor}; +use crate::{compressors::{Entry, TarCompressor}, decompressors::TarDecompressor}; use crate::decompressors::ZipDecompressor; use crate::{ cli::{Command, CommandKind}, @@ -155,8 +155,34 @@ impl Evaluator { Ok(()) } - fn compress_files(files: Vec, output: File) -> error::OuchResult<()> { - let (first_decompressor, second_decompressor) = Self::get_compressor(&output)?; + fn compress_files(files: Vec, mut output: File) -> error::OuchResult<()> { + let (first_compressor, second_compressor) = Self::get_compressor(&output)?; + + + let output_path = output.path.clone(); + + let bytes = match first_compressor { + Some(first_compressor) => { + let mut entry = Entry::Files(files); + let bytes = first_compressor.compress(entry)?; + + output.contents = Some(bytes); + + entry = Entry::InMemory(output); + + second_compressor.compress(entry)? + }, + None => { + let entry = Entry::Files(files); + second_compressor.compress(entry)? + } + }; + + println!("{}: writing to {:?}. ({} bytes)", "info".yellow(), &output_path, bytes.len()); + fs::write( + output_path, + bytes)?; + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 02a8020..02e4375 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,23 +29,6 @@ fn main() -> error::OuchResult<()>{ print_error(err) } } - - // let compressor = TarCompressor {}; - - // let file = File { - // path: PathBuf::from("target"), - // contents: None, - // extension: None, - // }; - - // let ok = compressor.compress(vec![file])?; - - // let ok = match ok { - // CompressionResult::TarArchive(bytes) => bytes, - // _ => unreachable!() - // }; - - // fs::write(Path::new("great.tar"), ok)?; - + Ok(()) }