coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

cp -r <directory> -T <target> throws a StripPrefixError

Open EnumeratedDev opened this issue 10 months ago • 5 comments

Description:

Recursively copying a directory with the --no-target-directory (-T) option to another location with a different name throws a StripPrefixError

Steps to Reproduce:

  • Create a directory (i.e named temp)

  • Copy the created directory and give it a new name with the following command cp -r temp -T new_temp

Expected Behavior:

A new directory should be created at the target location containing the same files as the original one

Actual Behavior:

The command throws the following error: cp: StripPrefixError(StripPrefixError(()))

Environment:

OS: Void Linux cp version: cp 0.0.30

EnumeratedDev avatar Mar 15 '25 12:03 EnumeratedDev

This is triggered by this last line:

https://github.com/uutils/coreutils/blob/7e26f790520219e173f7be427d5065e0e2a2804a/src/uu/cp/src/copydir.rs#L179-L188

descendant is "" and context.root is "temp"

I need to understand what get_local_to_root_parent, should be doing, but that may help find the error there.

Carreau avatar Mar 15 '25 15:03 Carreau

EDIT: no, this is completely wrong, ignore.

~~Could it be the following ?~~

 impl<'a> Context<'a> {
     fn new(root: &'a Path, target: &'a Path) -> std::io::Result<Self> {
         let current_dir = env::current_dir()?;
         let root_path = current_dir.join(root);
-        let root_parent = if target.exists() && !root.to_str().unwrap().ends_with("/.") {
+        let root_parent = if root.exists() && !root.to_str().unwrap().ends_with("/.") {
             root_path.parent().map(|p| p.to_path_buf())
         } else {
             Some(root_path)
         };
         Ok(Self {
             current_dir,
             root_parent,
             target,
             root,
         })
     }
 }

~~it seems weird to look at whether target exists to compute the root parent, and explain why @EnumuratedDev issue appears only if new_temp does not exists ?~~

Carreau avatar Mar 15 '25 16:03 Carreau

#7457 creates a failing test for this case.

Carreau avatar Mar 15 '25 18:03 Carreau

Could you try #7465 ?

Carreau avatar Mar 16 '25 19:03 Carreau

Could you try #7465 ?

It looks like this has fixed the problem

EnumeratedDev avatar Mar 17 '25 10:03 EnumeratedDev