From f2bf17fde9c681a0443680fede140e081ba4d01b Mon Sep 17 00:00:00 2001 From: "Florian RICHER (MrDev023)" Date: Sat, 24 Jul 2021 17:06:10 +0200 Subject: [PATCH] Add tar support --- Cargo.lock | 34 +++++++++++++++++++ Cargo.toml | 4 ++- src/common/utils/extractor/mod.rs | 18 ++++++++++ src/common/utils/extractor/tar.rs | 15 ++++++++ .../utils/{extractor.rs => extractor/zip.rs} | 10 +++--- src/windows/fvm/mod.rs | 3 +- 6 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 src/common/utils/extractor/mod.rs create mode 100644 src/common/utils/extractor/tar.rs rename src/common/utils/{extractor.rs => extractor/zip.rs} (80%) diff --git a/Cargo.lock b/Cargo.lock index 7a9fd11..5d43008 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,11 +38,13 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" name = "autoconfig" version = "0.1.0" dependencies = [ + "flate2", "reqwest", "serde", "serde_derive", "serde_json", "structopt", + "tar", "zip", ] @@ -158,6 +160,18 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "filetime" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "winapi", +] + [[package]] name = "flate2" version = "1.0.20" @@ -864,6 +878,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tar" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d779dc6aeff029314570f666ec83f19df7280bb36ef338442cfa8c604021b80" +dependencies = [ + "filetime", + "libc", + "xattr", +] + [[package]] name = "tempfile" version = "3.2.0" @@ -1195,6 +1220,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "xattr" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "244c3741f4240ef46274860397c7c74e50eb23624996930e484c16679633a54c" +dependencies = [ + "libc", +] + [[package]] name = "zip" version = "0.5.13" diff --git a/Cargo.toml b/Cargo.toml index 245e6fc..f3a3628 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,6 @@ reqwest = { version = "0.11.4", features = ["blocking"] } serde = "1.0" serde_json = "1.0" serde_derive = "1.0" -zip = "0.5" \ No newline at end of file +zip = "0.5" +tar = "0.4" +flate2 = "1.0" \ No newline at end of file diff --git a/src/common/utils/extractor/mod.rs b/src/common/utils/extractor/mod.rs new file mode 100644 index 0000000..ed2d5e1 --- /dev/null +++ b/src/common/utils/extractor/mod.rs @@ -0,0 +1,18 @@ +use std::path::{PathBuf}; + +mod zip; +mod tar; + +pub fn extract_file(path: &PathBuf, outdir: &str) -> Result<(), String> { + let file_extension = path.extension() + .ok_or(format!("Failed to get extension"))? + .to_str() + .ok_or(format!("Failed to convert extension to &str"))?; + match file_extension { + "zip" => Ok(zip::extract_file(&path, outdir).ok_or(format!("Extract failed"))?), + "bz2" => Ok(zip::extract_file(&path, outdir).ok_or(format!("Extract failed"))?), + "tar" => Ok(tar::extract_file(&path, outdir).ok_or(format!("Extract failed"))?), + "gz" => Ok(tar::extract_file(&path, outdir).ok_or(format!("Extract failed"))?), + _ => Err(format!("Format not supported")) + } +} \ No newline at end of file diff --git a/src/common/utils/extractor/tar.rs b/src/common/utils/extractor/tar.rs new file mode 100644 index 0000000..c45855c --- /dev/null +++ b/src/common/utils/extractor/tar.rs @@ -0,0 +1,15 @@ +use std::{fs::File, path::{Path, PathBuf}}; +use flate2::read::GzDecoder; +use tar::Archive; +use crate::common; + + +pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> { + let file = File::open(path).ok()?; + let tar = GzDecoder::new(file); + let mut archive = Archive::new(tar); + let path = Path::new(common::TEMP_FOLDER).join(outdir); + archive.unpack(path).ok()?; + + Some(()) +} \ No newline at end of file diff --git a/src/common/utils/extractor.rs b/src/common/utils/extractor/zip.rs similarity index 80% rename from src/common/utils/extractor.rs rename to src/common/utils/extractor/zip.rs index 5f76f03..304dfb3 100644 --- a/src/common/utils/extractor.rs +++ b/src/common/utils/extractor/zip.rs @@ -5,7 +5,7 @@ use std::{fs::{ }, io, path::{self, PathBuf}}; pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> { - let file = File::open(&path).unwrap(); + let file = File::open(&path).ok()?; let mut archive = match zip::ZipArchive::new(file) { Ok(archive) => archive, Err(err) => { @@ -23,7 +23,7 @@ pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> { if (&*file.name()).ends_with('/') { println!("File {} extracted to \"{}\"", i, output_path.display()); - create_dir_all(&output_path).unwrap(); + create_dir_all(&output_path).ok()?; } else { println!( "File {} extracted to \"{}\" ({} bytes)", @@ -33,11 +33,11 @@ pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> { ); if let Some(p) = output_path.parent() { if !p.exists() { - create_dir_all(&p).unwrap(); + create_dir_all(&p).ok()?; } } - let mut outfile = File::create(&output_path).unwrap(); - io::copy(&mut file, &mut outfile).unwrap(); + let mut outfile = File::create(&output_path).ok()?; + io::copy(&mut file, &mut outfile).ok()?; } } diff --git a/src/windows/fvm/mod.rs b/src/windows/fvm/mod.rs index c58b80b..7f3daab 100644 --- a/src/windows/fvm/mod.rs +++ b/src/windows/fvm/mod.rs @@ -16,8 +16,7 @@ pub fn install() -> Result<(), String> { let file = downloader::download_file(&url, &filename) .ok_or(format!("Failed to download file"))?; - extractor::extract_file(&file, "fvm") - .ok_or(format!("Failed to extract file"))?; + extractor::extract_file(&file, "fvm")?; println!("{}", url);