Skip to content
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

cp: incorrectly copies directory when path ends in dot #3897

Closed
jfinkels opened this issue Sep 3, 2022 · 4 comments · Fixed by #4184
Closed

cp: incorrectly copies directory when path ends in dot #3897

jfinkels opened this issue Sep 3, 2022 · 4 comments · Fixed by #4184
Labels

Comments

@jfinkels
Copy link
Collaborator

jfinkels commented Sep 3, 2022

cp -R incorrectly copies a directory when the path ends in a dot.

GNU cp v8.30:

mkdir x y
cd y
cp -R ../x/. .  # succeeds and does *not* copy x/ into y/

uutils cp:

mkdir x y
cd y
../target/debug/cp -R ../x/. .  # succeeds and *does* copy x/ into y/

The corresponding GNU test case is tests/cp/src-base-dot.sh. This test is incorrectly marked as passing on the main branch because the GNU test case doesn't actually verify that the directory x/ is not copied to y/.

Originally mentioned by @jfinkels in #3894 (comment)

@stefins
Copy link
Contributor

stefins commented Sep 5, 2022

I'm working on this!

Edit: I'm not working on this anymore

@eds-collabora
Copy link
Contributor

I had a quick look into this. The issue is that rust normalizes out of existence most occurrences of '.' in paths whenever API methods are called (i.e. lazily) which interacts incorrectly with the logic that's in place. In particular,

let root_parent = if target.exists() {
    root_path.parent()
} else {
    Some(root_path.as_path())
};

produces the wrong answer in the case of the code in cp. If we take the example, then

cp -R ../x/. .

leads to root_path = <cwd>/y/../x/. and root_path.parent() = <cwd>/y/.. and this is incorrect; the x has been lost from the root path because parent() normalized the trailing dot out of existence before it did anything. This then leads to it being treated as if it were contained in the target, and everything stems from this.

I don't see an easy fix here; it's probably going to come down to a custom implementation running over the OsStr representation of the path I guess.

@jfinkels
Copy link
Collaborator Author

Maybe this is a duplicate of #3886? If so it may be fixed by pull request #3954.

@cakebaker
Copy link
Contributor

Closing this ticket as it seems to be fixed and the directory is not copied when running the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants