use copy dir when decompress rename failed

Signed-off-by: tommady <tommady@users.noreply.github.com>
This commit is contained in:
tommady 2025-07-16 11:53:40 +00:00
parent 5ba90f5981
commit 776413a9f9
No known key found for this signature in database
GPG Key ID: 175B664929DF2F2F
4 changed files with 33 additions and 52 deletions

View File

@ -413,7 +413,25 @@ fn smart_unpack(
}; };
// Rename the temporary directory to the archive name, which is output_file_path // 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)?;
// println!("################### {:?}", e);
// if e.kind() == io::ErrorKind::CrossesDevices || e.raw_os_error() == Some(18) {
// println!("⚠️ Rename failed due to a cross-device link. Falling back to copy-and-delete.");
// // The rename failed, so we fall back to a copy and remove.
// // This is more expensive but works across devices.
// fs::copy(&previous_path, &new_path)?; // Copy the file to the new location.
// fs::symlink_metadata(path)
// println!("✅ Successfully moved file via copy-and-delete fallback.");
// Ok(())
// } else {
// // If it's a different error (e.g., permissions), we should fail.
// println!("❌ Rename failed with an unrecoverable error.");
// Err(e)
// }
};
info_accessible(format!( info_accessible(format!(
"Successfully moved \"{}\" to \"{}\"", "Successfully moved \"{}\" to \"{}\"",
nice_directory_display(&previous_path), nice_directory_display(&previous_path),

View File

@ -209,3 +209,16 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
None None
} }
} }
pub fn copy_dir(src: &Path, dst: &Path) -> crate::Result<()> {
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(())
}

View File

@ -18,7 +18,7 @@ pub use self::{
EscapedPathDisplay, EscapedPathDisplay,
}, },
fs::{ 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, rename_for_available_filename, resolve_path_conflict, try_infer_extension,
}, },
question::{ question::{

View File

@ -870,56 +870,6 @@ fn unpack_multiple_sources_into_the_same_destination_with_merge(
assert_eq!(5, out_path.as_path().read_dir()?.count()); assert_eq!(5, out_path.as_path().read_dir()?.count());
} }
#[cfg(feature = "allow_piped_choice")]
#[test]
fn unpack_multiple_sources_into_the_same_destination_with_merge_issue_825() {
let temp_dir = tempdir().unwrap();
let root_path = temp_dir.path();
let source_path = root_path.join("parent");
fs::create_dir_all(&source_path).unwrap();
fs::File::create(source_path.join("file1.txt")).unwrap();
fs::File::create(source_path.join("file2.txt")).unwrap();
fs::File::create(source_path.join("file3.txt")).unwrap();
crate::utils::cargo_bin()
.arg("compress")
.arg(&source_path)
.arg("a.tar")
.assert()
.success();
fs::remove_dir_all(&source_path).unwrap();
fs::create_dir_all(&source_path).unwrap();
fs::File::create(source_path.join("file3.txt")).unwrap();
fs::File::create(source_path.join("file4.txt")).unwrap();
fs::File::create(source_path.join("file5.txt")).unwrap();
crate::utils::cargo_bin()
.arg("compress")
.arg(&source_path)
.arg("b.tar")
.assert()
.success();
fs::remove_dir_all(&source_path).unwrap();
crate::utils::cargo_bin()
.arg("decompress")
.arg("a.tar")
.assert()
.success();
crate::utils::cargo_bin()
.arg("decompress")
.arg("b.tar")
.write_stdin("m")
.assert()
.success();
assert_eq!(5, source_path.read_dir().unwrap().count());
}
#[test] #[test]
fn reading_nested_archives_with_two_archive_extensions_adjacent() { fn reading_nested_archives_with_two_archive_extensions_adjacent() {
let archive_formats = ["tar", "zip", "7z"].into_iter(); let archive_formats = ["tar", "zip", "7z"].into_iter();