diff --git a/CHANGELOG.md b/CHANGELOG.md index f4b20ad..0e534e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ Categories Used: - Fix tar extraction count when --quiet [\#824](https://github.com/ouch-org/ouch/pull/824) ([marcospb19](https://github.com/marcospb19)) - Fix 7z BadSignature error when compressing and then listing [\#819](https://github.com/ouch-org/ouch/pull/819) ([tommady](https://github.com/tommady)) +- Fix Unpacking with merge flag failed without --dir flag [\#826](https://github.com/ouch-org/ouch/pull/826) ([tommady](https://github.com/tommady)) ### Tweaks diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index f5dc712..33a046f 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -413,7 +413,9 @@ fn smart_unpack( }; // Rename the temporary directory to the archive name, which is output_file_path - fs::rename(&previous_path, &new_path)?; + if fs::rename(&previous_path, &new_path).is_err() { + utils::copy_dir(&previous_path, &new_path)?; + }; info_accessible(format!( "Successfully moved \"{}\" to \"{}\"", nice_directory_display(&previous_path), diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 0f55252..f5a23fc 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -209,3 +209,23 @@ pub fn try_infer_extension(path: &Path) -> Option { None } } + +/// Copy the src directory into the dst directory recursively +pub fn copy_dir(src: &Path, dst: &Path) -> crate::Result<()> { + if !src.exists() || !dst.exists() { + return Err(crate::Error::NotFound { + error_title: "source or destination directory does not exist".to_string(), + }); + } + + for entry in fs::read_dir(src)? { + let entry = entry?; + let ty = entry.file_type()?; + if ty.is_dir() { + copy_dir(entry.path().as_path(), dst.join(entry.file_name()).as_path())?; + } else { + fs::copy(entry.path(), dst.join(entry.file_name()).as_path())?; + } + } + Ok(()) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 444cf1f..060a7ed 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -18,7 +18,7 @@ pub use self::{ EscapedPathDisplay, }, fs::{ - cd_into_same_dir_as, create_dir_if_non_existent, is_path_stdin, remove_file_or_dir, + cd_into_same_dir_as, copy_dir, create_dir_if_non_existent, is_path_stdin, remove_file_or_dir, rename_for_available_filename, resolve_path_conflict, try_infer_extension, }, question::{