Skip to content

Commit 63fe4fb

Browse files
committed
cp: disabled verbose output if file has been skipped
1 parent b3f7aca commit 63fe4fb

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

src/uu/cp/src/cp.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ pub struct Options {
287287
pub progress_bar: bool,
288288
}
289289

290+
/// Enum representing if a file has been skipped.
291+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
292+
enum PerformedAction {
293+
Copied,
294+
Skipped,
295+
}
296+
290297
/// Enum representing various debug states of the offload and reflink actions.
291298
#[derive(Debug)]
292299
#[allow(dead_code)] // All of them are used on Linux
@@ -1974,7 +1981,7 @@ fn handle_copy_mode(
19741981
source_in_command_line: bool,
19751982
source_is_fifo: bool,
19761983
#[cfg(unix)] source_is_stream: bool,
1977-
) -> CopyResult<()> {
1984+
) -> CopyResult<PerformedAction> {
19781985
let source_is_symlink = source_metadata.is_symlink();
19791986

19801987
match options.copy_mode {
@@ -2043,7 +2050,7 @@ fn handle_copy_mode(
20432050
println!("skipped {}", dest.quote());
20442051
}
20452052

2046-
return Ok(());
2053+
return Ok(PerformedAction::Skipped);
20472054
}
20482055
update_control::UpdateMode::ReplaceNoneFail => {
20492056
return Err(Error::Error(format!("not replacing '{}'", dest.display())));
@@ -2054,7 +2061,7 @@ fn handle_copy_mode(
20542061
let src_time = source_metadata.modified()?;
20552062
let dest_time = dest_metadata.modified()?;
20562063
if src_time <= dest_time {
2057-
return Ok(());
2064+
return Ok(PerformedAction::Skipped);
20582065
} else {
20592066
options.overwrite.verify(dest, options.debug)?;
20602067

@@ -2096,7 +2103,7 @@ fn handle_copy_mode(
20962103
}
20972104
};
20982105

2099-
Ok(())
2106+
Ok(PerformedAction::Copied)
21002107
}
21012108

21022109
/// Calculates the permissions for the destination file in a copy operation.
@@ -2322,7 +2329,7 @@ fn copy_file(
23222329
#[cfg(not(unix))]
23232330
let source_is_stream = false;
23242331

2325-
handle_copy_mode(
2332+
let performed_action = handle_copy_mode(
23262333
source,
23272334
dest,
23282335
options,
@@ -2335,7 +2342,7 @@ fn copy_file(
23352342
source_is_stream,
23362343
)?;
23372344

2338-
if options.verbose {
2345+
if options.verbose && performed_action != PerformedAction::Skipped {
23392346
print_verbose_output(options.parents, progress_bar, source, dest);
23402347
}
23412348

tests/by-util/test_cp.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,29 @@ fn test_cp_arg_update_older_dest_not_older_than_src() {
439439
assert_eq!(at.read(new), "new content\n");
440440
}
441441

442+
#[test]
443+
fn test_cp_arg_update_older_dest_not_older_than_src_no_verbose_output() {
444+
let (at, mut ucmd) = at_and_ucmd!();
445+
446+
let old = "test_cp_arg_update_dest_not_older_file1";
447+
let new = "test_cp_arg_update_dest_not_older_file2";
448+
let old_content = "old content\n";
449+
let new_content = "new content\n";
450+
451+
at.write(old, old_content);
452+
at.write(new, new_content);
453+
454+
ucmd.arg(old)
455+
.arg(new)
456+
.arg("--verbose")
457+
.arg("--update=older")
458+
.succeeds()
459+
.no_stderr()
460+
.no_stdout();
461+
462+
assert_eq!(at.read(new), "new content\n");
463+
}
464+
442465
#[test]
443466
fn test_cp_arg_update_older_dest_older_than_src() {
444467
let (at, mut ucmd) = at_and_ucmd!();
@@ -464,6 +487,32 @@ fn test_cp_arg_update_older_dest_older_than_src() {
464487
assert_eq!(at.read(old), "new content\n");
465488
}
466489

490+
#[test]
491+
fn test_cp_arg_update_older_dest_older_than_src_with_verbose_output() {
492+
let (at, mut ucmd) = at_and_ucmd!();
493+
494+
let old = "test_cp_arg_update_dest_older_file1";
495+
let new = "test_cp_arg_update_dest_older_file2";
496+
let old_content = "old content\n";
497+
let new_content = "new content\n";
498+
499+
let mut f = at.make_file(old);
500+
f.write_all(old_content.as_bytes()).unwrap();
501+
f.set_modified(std::time::UNIX_EPOCH).unwrap();
502+
503+
at.write(new, new_content);
504+
505+
ucmd.arg(new)
506+
.arg(old)
507+
.arg("--verbose")
508+
.arg("--update=older")
509+
.succeeds()
510+
.no_stderr()
511+
.stdout_is(format!("'{new}' -> '{old}'\n"));
512+
513+
assert_eq!(at.read(old), "new content\n");
514+
}
515+
467516
#[test]
468517
fn test_cp_arg_update_short_no_overwrite() {
469518
// same as --update=older

0 commit comments

Comments
 (0)