From d351a8ef7bc91c66ec17724b073147c271784913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Sun, 21 Mar 2021 04:23:00 -0300 Subject: [PATCH] decompressors.tar: now working! --- src/decompressors/tar.rs | 42 +++++++++++++++++++++++++++++----------- src/main.rs | 41 ++++++++++++--------------------------- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/decompressors/tar.rs b/src/decompressors/tar.rs index b2b1465..94eb59f 100644 --- a/src/decompressors/tar.rs +++ b/src/decompressors/tar.rs @@ -1,33 +1,53 @@ -use std::{fs, path::{Path, PathBuf}}; - -use crate::file::File; -use crate::error::OuchResult; +use std::{ + fs, + path::{Path, PathBuf}, +}; use colored::Colorize; +use tar; + +use crate::error::OuchResult; +use crate::file::File; + pub struct Decompressor {} impl Decompressor { - pub fn decompress(from: &File, into: &Option) -> OuchResult<()> { + pub fn decompress(from: &File, into: &Option) -> OuchResult<()> { let destination_path = match into { Some(output) => { // Must be None according to the way command-line arg. parsing in Ouch works assert_eq!(output.extension, None); - + Path::new(&output.path) } - None => Path::new(".") + None => Path::new("."), }; if !destination_path.exists() { - println!("{}: attempting to create folder {:?}.", "info".yellow(), &destination_path); + println!( + "{}: attempting to create folder {:?}.", + "info".yellow(), + &destination_path + ); std::fs::create_dir_all(destination_path)?; - println!("{}: directory {:#?} created.", "info".yellow(), fs::canonicalize(&destination_path)?); + println!( + "{}: directory {:#?} created.", + "info".yellow(), + fs::canonicalize(&destination_path)? + ); } + let file = fs::File::open(&from.path)?; + let mut archive = tar::Archive::new(file); + for file in archive.entries().unwrap() { + let mut file = file?; - + file.unpack_in(destination_path)?; + + // TODO: save all unpacked files into a 'transaction' metadata + } Ok(()) } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 0674463..fca88cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::{convert::TryFrom, fs::File, path::{Path, PathBuf}}; use colored::Colorize; +use error::{Error, OuchResult}; use tar::Archive; mod cli; @@ -12,34 +13,16 @@ mod evaluator; mod decompressors; -fn main() { - // let matches = cli::get_matches(); - // match cli::Command::try_from(matches) { - // Ok(command) => { - // let mut eval = evaluator::Evaluator::new(command); - // eval.evaluate(); - // } - // Err(err) => { - // print!("{}: {}\n", "error".red(), err); - // } - // } - - - // Testing tar unarchival - let file = File::open("ouch.tar").unwrap(); - let mut a = Archive::new(file); - - for file in a.entries().unwrap() { - // Make sure there wasn't an I/O error - let mut file = file.unwrap(); - - let path = if let Ok(path) = file.path() { - path - } else { - continue; - }; - let name = path.to_string_lossy(); - - file.unpack(format!("{}", name)).unwrap(); +fn main() -> OuchResult<()>{ + let matches = cli::get_matches(); + match cli::Command::try_from(matches) { + Ok(command) => { + let mut eval = evaluator::Evaluator::new(command); + eval.evaluate()?; + } + Err(err) => { + print!("{}: {}\n", "error".red(), err); + } } + Ok(()) }