From 312c1194dbc8737322f9b9fb4eb6712c0c741505 Mon Sep 17 00:00:00 2001 From: tphan025 Date: Sat, 20 Dec 2025 22:27:16 +0100 Subject: [PATCH 1/3] cp: set exit code when encountering circular symbolic links error when copying directory --- src/uu/cp/src/copydir.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/uu/cp/src/copydir.rs b/src/uu/cp/src/copydir.rs index bbd3aba6297..6ac1ae09072 100644 --- a/src/uu/cp/src/copydir.rs +++ b/src/uu/cp/src/copydir.rs @@ -22,7 +22,6 @@ use uucore::fs::{ FileInformation, MissingHandling, ResolveMode, canonicalize, path_ends_with_terminator, }; use uucore::show; -use uucore::show_error; use uucore::translate; use uucore::uio_error; use walkdir::{DirEntry, WalkDir}; @@ -513,7 +512,7 @@ pub(crate) fn copy_directory( } // Print an error message, but continue traversing the directory. - Err(e) => show_error!("{e}"), + Err(e) => show!(CpError::WalkDirErr(e)), } } From 01571b5da011a9dd7c0d9c8c87b8582fd5c3b23a Mon Sep 17 00:00:00 2001 From: tphan025 Date: Sat, 20 Dec 2025 22:28:02 +0100 Subject: [PATCH 2/3] cp: add test to ensure that cp sets the status code when encountering circular symbolic links during directory copy --- tests/by-util/test_cp.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index c5d1f93903f..c008cc18202 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -7383,3 +7383,25 @@ fn test_cp_recurse_verbose_output_with_symlink_already_exists() { .no_stderr() .stdout_is(output); } + +#[test] +fn test_cp_circular_symbolic_links_in_directory() { + let source_dir = "source_dir"; + let target_dir = "target_dir"; + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir(source_dir); + at.symlink_file( + format!("{source_dir}/a").as_str(), + format!("{source_dir}/b").as_str(), + ); + at.symlink_file( + format!("{source_dir}/b").as_str(), + format!("{source_dir}/a").as_str(), + ); + + ucmd.arg(source_dir) + .arg(target_dir) + .arg("-rL") + .fails_with_code(1); +} From 29332fca4482f20bd33960443fdb448f97f63a8b Mon Sep 17 00:00:00 2001 From: tphan025 Date: Mon, 22 Dec 2025 01:31:00 +0100 Subject: [PATCH 3/3] cp: check that the output of circular symbolic link test has the correct message --- tests/by-util/test_cp.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index c008cc18202..a9d32115193 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -7403,5 +7403,6 @@ fn test_cp_circular_symbolic_links_in_directory() { ucmd.arg(source_dir) .arg(target_dir) .arg("-rL") - .fails_with_code(1); + .fails_with_code(1) + .stderr_contains("Too many levels of symbolic links"); }