Merge pull request #4078 from sylvestre/update-i

{cp, mv} -i --update source existing should not do anything and exit 0
This commit is contained in:
Sylvestre Ledru 2022-10-25 08:31:39 +02:00 committed by GitHub
commit 42fad7c4e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 0 deletions

View file

@ -1264,6 +1264,12 @@ fn copy_file(
symlinked_files: &mut HashSet<FileInformation>, symlinked_files: &mut HashSet<FileInformation>,
source_in_command_line: bool, source_in_command_line: bool,
) -> CopyResult<()> { ) -> CopyResult<()> {
if options.update && options.overwrite == OverwriteMode::Interactive(ClobberMode::Standard) {
// `cp -i --update old new` when `new` exists doesn't copy anything
// and exit with 0
return Ok(());
}
if file_or_link_exists(dest) { if file_or_link_exists(dest) {
handle_existing_dest(source, dest, options, source_in_command_line)?; handle_existing_dest(source, dest, options, source_in_command_line)?;
} }

View file

@ -371,6 +371,12 @@ fn rename(from: &Path, to: &Path, b: &Behavior) -> io::Result<()> {
let mut backup_path = None; let mut backup_path = None;
if to.exists() { if to.exists() {
if b.update && b.overwrite == OverwriteMode::Interactive {
// `mv -i --update old new` when `new` exists doesn't move anything
// and exit with 0
return Ok(());
}
match b.overwrite { match b.overwrite {
OverwriteMode::NoClobber => return Ok(()), OverwriteMode::NoClobber => return Ok(()),
OverwriteMode::Interactive => { OverwriteMode::Interactive => {

View file

@ -215,6 +215,18 @@ fn test_cp_target_directory_is_file() {
.stderr_contains(format!("'{}' is not a directory", TEST_HOW_ARE_YOU_SOURCE)); .stderr_contains(format!("'{}' is not a directory", TEST_HOW_ARE_YOU_SOURCE));
} }
#[test]
fn test_cp_arg_update_interactive() {
new_ucmd!()
.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HOW_ARE_YOU_SOURCE)
.arg("-i")
.arg("--update")
.succeeds()
.no_stdout()
.no_stderr();
}
#[test] #[test]
fn test_cp_arg_interactive() { fn test_cp_arg_interactive() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();

View file

@ -184,6 +184,25 @@ fn test_mv_interactive() {
assert!(at.file_exists(file_b)); assert!(at.file_exists(file_b));
} }
#[test]
fn test_mv_arg_update_interactive() {
let (at, mut ucmd) = at_and_ucmd!();
let file_a = "test_mv_replace_file_a";
let file_b = "test_mv_replace_file_b";
at.touch(file_a);
at.touch(file_b);
ucmd.arg(file_a)
.arg(file_b)
.arg("-i")
.arg("--update")
.succeeds()
.no_stdout()
.no_stderr();
}
#[test] #[test]
fn test_mv_no_clobber() { fn test_mv_no_clobber() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();