From 234e0406a1c4709cf5fa4b2261d5d6fd9f6c1e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Sat, 27 Mar 2021 00:37:47 -0300 Subject: [PATCH] Don't allow `ouch` to compress the root folder --- src/compressors/zip.rs | 11 ++++++----- src/error.rs | 2 +- src/utils.rs | 12 +++++++++++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/compressors/zip.rs b/src/compressors/zip.rs index bcfea08..377ef7f 100644 --- a/src/compressors/zip.rs +++ b/src/compressors/zip.rs @@ -1,7 +1,4 @@ -use std::{ - io::{Cursor, Write}, - path::PathBuf, -}; +use std::{io::{Cursor, Write}, path::PathBuf}; use walkdir::WalkDir; @@ -57,7 +54,11 @@ impl ZipCompressor { for filename in input_filenames { let previous_location = utils::change_dir_and_return_parent(&filename)?; - let filename = filename.file_name()?; + let filename = filename + .file_name() + // Safe unwrap since the function call above would fail in scenarios + // where this unwrap would panic + .unwrap(); for entry in WalkDir::new(filename) { let entry = entry?; diff --git a/src/error.rs b/src/error.rs index 6680600..152f052 100644 --- a/src/error.rs +++ b/src/error.rs @@ -73,4 +73,4 @@ impl From for Error { eprintln!("{}: {}", "error".red(), err); Self::InvalidInput } -} +} \ No newline at end of file diff --git a/src/utils.rs b/src/utils.rs index 1ec935e..7ed0881 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -63,7 +63,17 @@ pub(crate) fn get_destination_path(dest: &Option) -> &Path { pub (crate) fn change_dir_and_return_parent(filename: &PathBuf) -> crate::Result { let previous_location = env::current_dir()?; - let parent = filename.parent().unwrap(); + + let parent = if let Some(parent) = filename.parent() { + parent + } else { + let spacing = " "; + println!("{} It seems you're trying to compress the root folder.", "[WARNING]".red()); + println!("{}This is unadvisable since ouch does compressions in-memory.", spacing); + println!("{}Use a more appropriate tool for this, such as {}.", spacing, "rsync".green()); + return Err(crate::Error::InvalidInput); + }; + env::set_current_dir(parent)?; Ok(previous_location) } \ No newline at end of file