Skip to content

Commit 2627841

Browse files
committed
mv --backup=simple a b on hard links should not fail
touch a ln a b ./target/debug/coreutils mv --backup=simple a b GNU: tests/mv/hard-4.sh
1 parent 8b75f37 commit 2627841

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/uu/mv/src/mv.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,16 @@ fn handle_two_paths(source: &Path, target: &Path, b: &Behavior) -> UResult<()> {
250250
}
251251

252252
if source.eq(target) || is_hardlink(source, target) {
253-
if source.eq(Path::new(".")) || source.ends_with("/.") || source.is_file() {
254-
return Err(
255-
MvError::SameFile(source.quote().to_string(), target.quote().to_string()).into(),
256-
);
257-
} else {
258-
return Err(MvError::SelfSubdirectory(source.display().to_string()).into());
253+
if b.backup != BackupMode::SimpleBackup {
254+
if source.eq(Path::new(".")) || source.ends_with("/.") || source.is_file() {
255+
return Err(MvError::SameFile(
256+
source.quote().to_string(),
257+
target.quote().to_string(),
258+
)
259+
.into());
260+
} else {
261+
return Err(MvError::SelfSubdirectory(source.display().to_string()).into());
262+
}
259263
}
260264
}
261265

tests/by-util/test_mv.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,23 @@ fn test_mv_same_hardlink() {
382382
.stderr_is(format!("mv: '{file_a}' and '{file_b}' are the same file\n",));
383383
}
384384

385+
#[test]
386+
#[cfg(unix)]
387+
fn test_mv_same_hardlink_backup_simple() {
388+
let (at, mut ucmd) = at_and_ucmd!();
389+
let file_a = "test_mv_same_file_a";
390+
let file_b = "test_mv_same_file_b";
391+
at.touch(file_a);
392+
393+
at.hard_link(file_a, file_b);
394+
395+
at.touch(file_a);
396+
ucmd.arg(file_a)
397+
.arg(file_b)
398+
.arg("--backup=simple")
399+
.succeeds();
400+
}
401+
385402
#[test]
386403
fn test_mv_same_file_not_dot_dir() {
387404
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)