From cdd1b530be86ee14cfd10c4d3af12e21c9f4108d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 15 Oct 2022 21:08:17 -0300 Subject: [PATCH 1/2] fix not overwriting a folder when compressing --- src/commands/compress.rs | 1 - src/commands/decompress.rs | 11 ++++------- src/commands/list.rs | 1 - src/commands/mod.rs | 9 ++++----- src/utils/mod.rs | 2 +- src/utils/question.rs | 2 +- 6 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/commands/compress.rs b/src/commands/compress.rs index 6f5e0be..7313448 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -114,7 +114,6 @@ pub fn compress_files( if !formats.is_empty() { warn_user_about_loading_zip_in_memory(); - // give user the option to continue compressing after warning is shown if !user_wants_to_continue(output_path, question_policy, QuestionAction::Compression)? { return Ok(false); } diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 66ec78e..02e5d9f 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -104,12 +104,10 @@ pub fn decompress_file( Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { reader = chain_reader_decoder(&first_extension, reader)?; - let writer = utils::create_or_ask_overwrite(&output_file_path, question_policy)?; - if writer.is_none() { - // Means that the user doesn't want to overwrite - return Ok(()); - } - let mut writer = writer.unwrap(); + let mut writer = match utils::ask_to_create_file(&output_file_path, question_policy)? { + Some(file) => file, + None => return Ok(()), + }; if is_running_in_accessible_mode() { io::copy(&mut reader, &mut writer)?; @@ -139,7 +137,6 @@ pub fn decompress_file( if formats.len() > 1 { warn_user_about_loading_zip_in_memory(); - // give user the option to continue decompressing after warning is shown if !user_wants_to_continue(input_file_path, question_policy, QuestionAction::Decompression)? { return Ok(()); } diff --git a/src/commands/list.rs b/src/commands/list.rs index dc28be5..7af8557 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -67,7 +67,6 @@ pub fn list_archive_contents( if formats.len() > 1 { warn_user_about_loading_zip_in_memory(); - // give user the option to continue decompressing after warning is shown if !user_wants_to_continue(archive_path, question_policy, QuestionAction::Decompression)? { return Ok(()); } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index ea6d58e..d05e643 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -162,12 +162,11 @@ pub fn run( return Err(error.into()); } - if output_path.exists() && !utils::user_wants_to_overwrite(&output_path, question_policy)? { - // User does not want to overwrite this file, skip and return without any errors - return Ok(()); - } + let output_file = match utils::ask_to_create_file(&output_path, question_policy)? { + Some(writer) => writer, + None => return Ok(()), + }; - let output_file = fs::File::create(&output_path)?; let compress_result = compress_files( files, formats, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 05e325c..e6808bf 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -15,7 +15,7 @@ pub use fs::{ cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, is_symlink, try_infer_extension, }; pub use question::{ - create_or_ask_overwrite, user_wants_to_continue, user_wants_to_overwrite, QuestionAction, QuestionPolicy, + ask_to_create_file, user_wants_to_continue, user_wants_to_overwrite, QuestionAction, QuestionPolicy, }; pub use utf8::{get_invalid_utf8_paths, is_invalid_utf8}; diff --git a/src/utils/question.rs b/src/utils/question.rs index 84b0fb9..837384e 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -54,7 +54,7 @@ pub fn user_wants_to_overwrite(path: &Path, question_policy: QuestionPolicy) -> /// Create the file if it doesn't exist and if it does then ask to overwrite it. /// If the user doesn't want to overwrite then we return [`Ok(None)`] -pub fn create_or_ask_overwrite(path: &Path, question_policy: QuestionPolicy) -> Result> { +pub fn ask_to_create_file(path: &Path, question_policy: QuestionPolicy) -> Result> { match fs::OpenOptions::new().write(true).create_new(true).open(path) { Ok(w) => Ok(Some(w)), Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { From effe0b30b87801fbaee36d65a28d0b66189f7dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 15 Oct 2022 21:38:57 -0300 Subject: [PATCH 2/2] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87a17c6..eb60d39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ Categories Used: - Remove single quotes from clap doc comments [\#251](https://github.com/ouch-org/ouch/pull/251) ([jcgruenhage](https://github.com/jcgruenhage)) - Fix incorrect warnings for decompression [\#270](https://github.com/ouch-org/ouch/pull/270) ([figsoda](https://github.com/figsoda)) - Fix infinite compression if output file is inside the input folder [\#288](https://github.com/ouch-org/ouch/pull/288) ([figsoda](https://github.com/figsoda)) +- Fix not overwriting a folder when compressing [\#295](https://github.com/ouch-org/ouch/pull/295) ([marcospb19](https://github.com/marcospb19)) ### Improvements