Skip to content

cp: copy dir if source path ends with dot #7874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2025
Merged
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
49 changes: 1 addition & 48 deletions src/uu/cp/src/copydir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,6 @@ impl Entry {
}
}

/// Decide whether the given path ends with `/.`.
///
/// # Examples
///
/// ```rust,ignore
/// assert!(ends_with_slash_dot("/."));
/// assert!(ends_with_slash_dot("./."));
/// assert!(ends_with_slash_dot("a/."));
///
/// assert!(!ends_with_slash_dot("."));
/// assert!(!ends_with_slash_dot("./"));
/// assert!(!ends_with_slash_dot("a/.."));
/// ```
fn ends_with_slash_dot<P>(path: P) -> bool
where
P: AsRef<Path>,
{
// `path.ends_with(".")` does not seem to work
path.as_ref().display().to_string().ends_with("/.")
}

#[allow(clippy::too_many_arguments)]
/// Copy a single entry during a directory traversal.
fn copy_direntry(
Expand All @@ -248,10 +227,7 @@ fn copy_direntry(

// If the source is a directory and the destination does not
// exist, ...
if source_absolute.is_dir()
&& !ends_with_slash_dot(&source_absolute)
&& !local_to_target.exists()
{
if source_absolute.is_dir() && !local_to_target.exists() {
return if target_is_file {
Err("cannot overwrite non-directory with directory".into())
} else {
Expand Down Expand Up @@ -590,26 +566,3 @@ fn build_dir(
builder.create(path)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::ends_with_slash_dot;

#[test]
#[allow(clippy::cognitive_complexity)]
fn test_ends_with_slash_dot() {
assert!(ends_with_slash_dot("/."));
assert!(ends_with_slash_dot("./."));
assert!(ends_with_slash_dot("../."));
assert!(ends_with_slash_dot("a/."));
assert!(ends_with_slash_dot("/a/."));

assert!(!ends_with_slash_dot(""));
assert!(!ends_with_slash_dot("."));
assert!(!ends_with_slash_dot("./"));
assert!(!ends_with_slash_dot(".."));
assert!(!ends_with_slash_dot("/.."));
assert!(!ends_with_slash_dot("a/.."));
assert!(!ends_with_slash_dot("/a/.."));
}
}
18 changes: 18 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,24 @@ fn test_cp_recurse_several() {
assert_eq!(at.read(TEST_COPY_TO_FOLDER_NEW_FILE), "Hello, World!\n");
}

#[test]
fn test_cp_recurse_source_path_ends_with_slash_dot() {
let source_dir = "source_dir";
let target_dir = "target_dir";
let file = "file";
let (at, mut ucmd) = at_and_ucmd!();

at.mkdir(source_dir);
at.touch(format!("{source_dir}/{file}"));

ucmd.arg("-r")
.arg(format!("{source_dir}/."))
.arg(target_dir)
.succeeds()
.no_output();
assert!(at.file_exists(format!("{target_dir}/{file}")));
}

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