Skip to content
Closed
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
25 changes: 11 additions & 14 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,13 @@ fn copy_source(
}
}
}
res
// this is just for gnu tests compatibility
Ok(res.map_err(|err| {
if err.to_string().contains("No such file or directory") {
return format!("cannot stat {}: No such file or directory", source.quote());
}
err.to_string()
})?)
}
}

Expand Down Expand Up @@ -2245,19 +2251,10 @@ fn copy_file(
let context = context_for(source, dest);
let context = context.as_str();

let source_metadata = {
let result = if options.dereference(source_in_command_line) {
fs::metadata(source)
} else {
fs::symlink_metadata(source)
};
// this is just for gnu tests compatibility
result.map_err(|err| {
if err.to_string().contains("No such file or directory") {
return format!("cannot stat {}: No such file or directory", source.quote());
}
err.to_string()
})?
let source_metadata = if options.dereference(source_in_command_line) {
fs::metadata(source)?
} else {
fs::symlink_metadata(source)?
};

let dest_permissions = calculate_dest_permissions(dest, &source_metadata, options, context)?;
Expand Down
22 changes: 22 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5831,6 +5831,28 @@ fn test_cp_parents_absolute_path() {
at.file_exists(res);
}

/// Test the behavior of copying a non existent file to a folder
#[test]
#[cfg(unix)]
fn test_cp_existant_and_nonexistent_file() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.mkdir(TEST_COPY_TO_FOLDER_NEW);

scene
.ucmd()
.arg("-a")
.arg(TEST_NONEXISTENT_FILE)
.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_COPY_TO_FOLDER_NEW)
.fails()
.stderr_only("cp: cannot stat 'nonexistent_file.txt': No such file or directory\n");

// Check existing file has been copied
assert!(at.file_exists(TEST_COPY_TO_FOLDER_NEW_FILE));
}

#[test]
fn test_copy_symlink_overwrite() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
Loading