Skip to content

Commit aeb27cc

Browse files
authored
Fix unpacking with merge flag failing without --dir flag (#826)
Signed-off-by: tommady <[email protected]>
1 parent 5939fe5 commit aeb27cc

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Categories Used:
3737

3838
- Fix tar extraction count when --quiet [\#824](https://github.com/ouch-org/ouch/pull/824) ([marcospb19](https://github.com/marcospb19))
3939
- Fix 7z BadSignature error when compressing and then listing [\#819](https://github.com/ouch-org/ouch/pull/819) ([tommady](https://github.com/tommady))
40+
- Fix unpacking with merge flag failing without --dir flag [\#826](https://github.com/ouch-org/ouch/pull/826) ([tommady](https://github.com/tommady))
4041

4142
### Tweaks
4243

src/commands/decompress.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,9 @@ fn smart_unpack(
413413
};
414414

415415
// Rename the temporary directory to the archive name, which is output_file_path
416-
fs::rename(&previous_path, &new_path)?;
416+
if fs::rename(&previous_path, &new_path).is_err() {
417+
utils::rename_recursively(&previous_path, &new_path)?;
418+
};
417419
info_accessible(format!(
418420
"Successfully moved \"{}\" to \"{}\"",
419421
nice_directory_display(&previous_path),

src/utils/fs.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,23 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
209209
None
210210
}
211211
}
212+
213+
/// Rename the src directory into the dst directory recursively
214+
pub fn rename_recursively(src: &Path, dst: &Path) -> crate::Result<()> {
215+
if !src.exists() || !dst.exists() {
216+
return Err(crate::Error::NotFound {
217+
error_title: "source or destination directory does not exist".to_string(),
218+
});
219+
}
220+
221+
for entry in fs::read_dir(src)? {
222+
let entry = entry?;
223+
let ty = entry.file_type()?;
224+
if ty.is_dir() {
225+
rename_recursively(entry.path().as_path(), dst.join(entry.file_name()).as_path())?;
226+
} else {
227+
fs::rename(entry.path(), dst.join(entry.file_name()).as_path())?;
228+
}
229+
}
230+
Ok(())
231+
}

src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use self::{
1919
},
2020
fs::{
2121
cd_into_same_dir_as, create_dir_if_non_existent, is_path_stdin, remove_file_or_dir,
22-
rename_for_available_filename, resolve_path_conflict, try_infer_extension,
22+
rename_for_available_filename, rename_recursively, resolve_path_conflict, try_infer_extension,
2323
},
2424
question::{
2525
ask_to_create_file, user_wants_to_continue, user_wants_to_overwrite, FileConflitOperation, QuestionAction,

0 commit comments

Comments
 (0)