Skip to content

Commit a3b4272

Browse files
committed
refactor(mv): simplify rename_symlink_fallback to use copy_symlink
Replace the duplicated symlink handling logic in rename_symlink_fallback functions across Unix, Windows, and other platforms with a call to the existing copy_symlink function. This reduces code duplication and centralizes symlink copying logic while maintaining the same functionality of copying the symlink and then removing the original file.
1 parent fc32a78 commit a3b4272

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

src/uu/mv/src/mv.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -968,42 +968,20 @@ fn rename_fifo_fallback(_from: &Path, _to: &Path) -> io::Result<()> {
968968
/// symlinks return an error.
969969
#[cfg(unix)]
970970
fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
971-
let from_meta = from.symlink_metadata()?;
972-
let path_symlink_points_to = fs::read_link(from)?;
973-
unix::fs::symlink(path_symlink_points_to, to)?;
974-
#[cfg(not(any(target_os = "macos", target_os = "redox")))]
975-
{
976-
let _ = copy_xattrs_if_supported(from, to);
977-
}
978-
try_preserve_ownership(&from_meta, to, false);
971+
copy_symlink(from, to)?;
979972
fs::remove_file(from)
980973
}
981974

982975
#[cfg(windows)]
983976
fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
984-
let path_symlink_points_to = fs::read_link(from)?;
985-
if path_symlink_points_to.exists() {
986-
if path_symlink_points_to.is_dir() {
987-
windows::fs::symlink_dir(&path_symlink_points_to, to)?;
988-
} else {
989-
windows::fs::symlink_file(&path_symlink_points_to, to)?;
990-
}
991-
fs::remove_file(from)
992-
} else {
993-
Err(io::Error::new(
994-
io::ErrorKind::NotFound,
995-
translate!("mv-error-dangling-symlink"),
996-
))
997-
}
977+
copy_symlink(from, to)?;
978+
fs::remove_file(from)
998979
}
999980

1000981
#[cfg(not(any(windows, unix)))]
1001982
fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
1002-
let path_symlink_points_to = fs::read_link(from)?;
1003-
Err(io::Error::new(
1004-
io::ErrorKind::Other,
1005-
translate!("mv-error-no-symlink-support"),
1006-
))
983+
copy_symlink(from, to)?;
984+
fs::remove_file(from)
1007985
}
1008986

1009987
/// Copy the given symlink to the given destination without dereferencing.
@@ -1012,9 +990,13 @@ fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
1012990
fn copy_symlink(from: &Path, to: &Path) -> io::Result<()> {
1013991
let from_meta = from.symlink_metadata()?;
1014992
let path_symlink_points_to = fs::read_link(from)?;
1015-
unix::fs::symlink(path_symlink_points_to, to).map(|_| {
1016-
try_preserve_ownership(&from_meta, to, false);
1017-
})
993+
unix::fs::symlink(path_symlink_points_to, to)?;
994+
#[cfg(not(any(target_os = "macos", target_os = "redox")))]
995+
{
996+
let _ = copy_xattrs_if_supported(from, to);
997+
}
998+
try_preserve_ownership(&from_meta, to, false);
999+
Ok(())
10181000
}
10191001

10201002
#[cfg(windows)]
@@ -1231,7 +1213,7 @@ fn copy_dir_contents_recursive(
12311213
{
12321214
if from_path.is_symlink() {
12331215
// Copy a symlink file (no-follow).
1234-
rename_symlink_fallback(&from_path, &to_path)?;
1216+
copy_symlink(&from_path, &to_path)?;
12351217
} else {
12361218
// Copy a regular file.
12371219
fs::copy(&from_path, &to_path)?;
@@ -1282,8 +1264,8 @@ fn copy_file_with_hardlinks_helper(
12821264

12831265
if from_meta.file_type().is_symlink() {
12841266
// Copy a symlink file (no-follow).
1285-
rename_symlink_fallback(from, to)?;
1286-
} else if is_fifo(from.symlink_metadata()?.file_type()) {
1267+
copy_symlink(from, to)?;
1268+
} else if is_fifo(from_meta.file_type()) {
12871269
make_fifo(to)?;
12881270
} else {
12891271
// Copy a regular file.

0 commit comments

Comments
 (0)