Skip to content

Commit 9340bf4

Browse files
committed
mv: support moving folder containing symlinks to different filesystem
1 parent 4ee588e commit 9340bf4

2 files changed

Lines changed: 71 additions & 9 deletions

File tree

src/uu/mv/src/mv.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,13 @@ fn copy_dir_contents_recursive(
10941094
}
10951095
#[cfg(not(unix))]
10961096
{
1097-
fs::copy(&from_path, &to_path)?;
1097+
if from_path.is_symlink() {
1098+
// Copy a symlink file (no-follow).
1099+
rename_symlink_fallback(&from_path, &to_path)?;
1100+
} else {
1101+
// Copy a regular file.
1102+
fs::copy(&from_path, &to_path)?;
1103+
}
10981104
}
10991105

11001106
// Print verbose message for file
@@ -1137,14 +1143,19 @@ fn copy_file_with_hardlinks_helper(
11371143
return Ok(());
11381144
}
11391145

1140-
// Regular file copy
1141-
#[cfg(all(unix, not(any(target_os = "macos", target_os = "redox"))))]
1142-
{
1143-
fs::copy(from, to).and_then(|_| fsxattr::copy_xattrs(&from, &to))?;
1144-
}
1145-
#[cfg(any(target_os = "macos", target_os = "redox"))]
1146-
{
1147-
fs::copy(from, to)?;
1146+
if from.is_symlink() {
1147+
// Copy a symlink file (no-follow).
1148+
rename_symlink_fallback(from, to)?;
1149+
} else {
1150+
// Copy a regular file.
1151+
#[cfg(all(unix, not(any(target_os = "macos", target_os = "redox"))))]
1152+
{
1153+
fs::copy(from, to).and_then(|_| fsxattr::copy_xattrs(&from, &to))?;
1154+
}
1155+
#[cfg(any(target_os = "macos", target_os = "redox"))]
1156+
{
1157+
fs::copy(from, to)?;
1158+
}
11481159
}
11491160

11501161
Ok(())

tests/by-util/test_mv.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,57 @@ fn test_mv_symlink_into_target() {
621621
ucmd.arg("dir-link").arg("dir").succeeds();
622622
}
623623

624+
#[cfg(all(unix, not(target_os = "android")))]
625+
#[test]
626+
fn test_mv_broken_symlink_to_another_fs() {
627+
let scene = TestScenario::new(util_name!());
628+
629+
scene.fixtures.mkdir("foo");
630+
631+
let output = scene
632+
.cmd("sudo")
633+
.env("PATH", env!("PATH"))
634+
.args(&["-E", "--non-interactive", "ls"])
635+
.run();
636+
println!("test output: {output:?}");
637+
638+
let mount = scene
639+
.cmd("sudo")
640+
.env("PATH", env!("PATH"))
641+
.args(&[
642+
"-E",
643+
"--non-interactive",
644+
"mount",
645+
"none",
646+
"-t",
647+
"tmpfs",
648+
"foo",
649+
])
650+
.run();
651+
652+
if !mount.succeeded() {
653+
print!("Test skipped; requires root user");
654+
return;
655+
}
656+
657+
scene.fixtures.mkdir("bar");
658+
scene.fixtures.symlink_file("nonexistent", "bar/baz");
659+
660+
scene
661+
.ucmd()
662+
.arg("bar")
663+
.arg("foo")
664+
.succeeds()
665+
.no_stderr()
666+
.no_stdout();
667+
668+
scene
669+
.cmd("sudo")
670+
.env("PATH", env!("PATH"))
671+
.args(&["-E", "--non-interactive", "umount", "foo"])
672+
.succeeds();
673+
}
674+
624675
#[test]
625676
#[cfg(all(unix, not(target_os = "android")))]
626677
fn test_mv_hardlink_to_symlink() {

0 commit comments

Comments
 (0)