coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

cp should raise error when directory specified with trailing / does not exist

Open ahcm opened this issue 3 years ago • 2 comments

If only one source is specified, cp should raise an error when target directory specified with trailing / does not exist. Instead it currently creates a file, as if specified without /. When multiple sources are specified an error is raised.

It's better to always assume that a trailing slash indicates an incorrect directory. It's more in line with other implementations. It's more consistent / less surprising.

$ ls d
ls: cannot access 'd': No such file or directory
$ touch t
$ cp t d/
$ ls d
d
$ touch f
$ cp t f d2/
cp: target: 'd2/' is not a directory

ahcm avatar Jun 18 '22 20:06 ahcm

The similar issue is when d is a symlink to a regular file.

$ echo "sample" > a
$ echo "hello" > t
$ cat a
sample
$ cat t
hello
$ ln -s a d
$ cp t d/
$ cat a
hello

niyaznigmatullin avatar Jun 21 '22 20:06 niyaznigmatullin

Seems that other utilities that use canonicalize also handle trailing slashes not in the way GNU does handle them. For instance, the following usage of realpath fails when running GNU version, and succeeds for uutils. I chose an example with the behavior that is explicitly stated in the GNU documentation that it should fail.

$ touch a
$ ln -s a d
$ realpath -e d/

I want to work on the trailing slashes issue.

niyaznigmatullin avatar Jun 24 '22 14:06 niyaznigmatullin

Closing this ticket as it seems like the issues have been fixed in the meantime and errors are shown when running the examples.

cakebaker avatar Apr 30 '23 14:04 cakebaker