Skip to content

Commit 8d5334a

Browse files
hamflxsylvestre
authored andcommitted
mv: fix invalid numbered backup path
1 parent c99e1c6 commit 8d5334a

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

src/uucore/src/lib/features/backup_control.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,25 +416,34 @@ pub fn get_backup_path(
416416
}
417417

418418
fn simple_backup_path(path: &Path, suffix: &str) -> PathBuf {
419-
let mut p = path.to_string_lossy().into_owned();
420-
p.push_str(suffix);
421-
PathBuf::from(p)
419+
match path.file_name() {
420+
Some(file_name) => {
421+
let mut file_name = file_name.to_os_string();
422+
file_name.push(suffix);
423+
path.with_file_name(file_name)
424+
}
425+
None => path.with_file_name(suffix),
426+
}
422427
}
423428

424429
fn numbered_backup_path(path: &Path) -> PathBuf {
430+
let file_name = path.file_name().unwrap_or_default();
425431
for i in 1_u64.. {
426-
let path_str = &format!("{}.~{}~", path.to_string_lossy(), i);
427-
let path = Path::new(path_str);
432+
let mut numbered_file_name = file_name.to_os_string();
433+
numbered_file_name.push(format!(".~{}~", i));
434+
let path = path.with_file_name(numbered_file_name);
428435
if !path.exists() {
429-
return path.to_path_buf();
436+
return path;
430437
}
431438
}
432439
panic!("cannot create backup")
433440
}
434441

435442
fn existing_backup_path(path: &Path, suffix: &str) -> PathBuf {
436-
let test_path_str = &format!("{}.~1~", path.to_string_lossy());
437-
let test_path = Path::new(test_path_str);
443+
let file_name = path.file_name().unwrap_or_default();
444+
let mut numbered_file_name = file_name.to_os_string();
445+
numbered_file_name.push(".~1~");
446+
let test_path = path.with_file_name(numbered_file_name);
438447
if test_path.exists() {
439448
numbered_backup_path(path)
440449
} else {
@@ -655,6 +664,44 @@ mod tests {
655664
let result = determine_backup_suffix(&matches);
656665
assert_eq!(result, "-v");
657666
}
667+
668+
#[test]
669+
fn test_numbered_backup_path() {
670+
assert_eq!(numbered_backup_path(&Path::new("")), PathBuf::from(".~1~"));
671+
assert_eq!(
672+
numbered_backup_path(&Path::new("/")),
673+
PathBuf::from("/.~1~")
674+
);
675+
assert_eq!(
676+
numbered_backup_path(&Path::new("/hello/world")),
677+
PathBuf::from("/hello/world.~1~")
678+
);
679+
assert_eq!(
680+
numbered_backup_path(&Path::new("/hello/world/")),
681+
PathBuf::from("/hello/world.~1~")
682+
);
683+
}
684+
685+
#[test]
686+
fn test_simple_backup_path() {
687+
assert_eq!(
688+
simple_backup_path(&Path::new(""), ".bak"),
689+
PathBuf::from(".bak")
690+
);
691+
assert_eq!(
692+
simple_backup_path(&Path::new("/"), ".bak"),
693+
PathBuf::from("/.bak")
694+
);
695+
assert_eq!(
696+
simple_backup_path(&Path::new("/hello/world"), ".bak"),
697+
PathBuf::from("/hello/world.bak")
698+
);
699+
assert_eq!(
700+
simple_backup_path(&Path::new("/hello/world/"), ".bak"),
701+
PathBuf::from("/hello/world.bak")
702+
);
703+
}
704+
658705
#[test]
659706
fn test_source_is_target_backup() {
660707
let source = Path::new("data.txt.bak");

0 commit comments

Comments
 (0)