Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,11 +829,10 @@ fn rename(
let to_metadata = fs::symlink_metadata(to)?;
let from_is_dir =
from_metadata.is_dir() || (path_ends_with_terminator(from) && from.is_dir());
// A directory can't replace a non-directory, so don't skip it as up to
// date: let the rename report the error, as GNU mv does.
if (!from_is_dir || to_metadata.is_dir())
&& from_metadata.modified()? <= to_metadata.modified()?
{
// Only a non-directory is skipped as up to date. A directory source is
// always moved, and the rename reports it if it can't replace the
// destination, as GNU mv does.
if !from_is_dir && from_metadata.modified()? <= to_metadata.modified()? {
return Ok(());
}
}
Expand Down
50 changes: 50 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,56 @@ fn test_mv_update_dir_onto_newer_symlink_fails() {
assert!(at.dir_exists("dir"));
}

#[test]
#[cfg(unix)]
fn test_mv_update_dir_onto_newer_empty_dir() {
let (at, mut ucmd) = at_and_ucmd!();

// -u only skips a non-directory source, so an older directory is still moved
at.mkdir("release");
at.write("release/manifest", "package\n");
std::fs::File::open(at.plus("release"))
.unwrap()
.set_modified(std::time::UNIX_EPOCH)
.unwrap();
at.mkdir_all("stage/release");

ucmd.arg("-u")
.arg("release")
.arg("stage")
.succeeds()
.no_output();

assert!(!at.dir_exists("release"));
assert_eq!(at.read("stage/release/manifest"), "package\n");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also add a case where stage/release isn't empty? it should fail now instead of silently skipping

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 2f935ed: test_mv_update_dir_onto_newer_non_empty_dir_fails puts a file in stage/release, and mv -u release stage now fails with "Directory not empty" and leaves both directories as they were.


#[test]
#[cfg(unix)]
fn test_mv_update_dir_onto_newer_non_empty_dir_fails() {
let (at, mut ucmd) = at_and_ucmd!();

// An older directory isn't skipped either: replacing a non-empty one fails
at.mkdir("release");
at.write("release/manifest", "package\n");
std::fs::File::open(at.plus("release"))
.unwrap()
.set_modified(std::time::UNIX_EPOCH)
.unwrap();
at.mkdir_all("stage/release");
at.write("stage/release/old", "old\n");

ucmd.arg("-u")
.arg("release")
.arg("stage")
.fails()
.stderr_contains("Directory not empty");

assert_eq!(at.read("release/manifest"), "package\n");
assert_eq!(at.read("stage/release/old"), "old\n");
assert!(!at.file_exists("stage/release/manifest"));
}

#[test]
#[cfg(all(unix, not(target_os = "android")))]
fn test_mv_file_backs_up_broken_symlink() {
Expand Down
Loading