bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

cp and "blah/." or "." as a source


From: Zak
Subject: cp and "blah/." or "." as a source
Date: Sun, 3 Dec 2006 15:57:36 +0000

Hi,

What should happen when I copy a directory ending in "/." to another directory? Like:
        cp -a src/. dst

What I see happening (both with BSD cp and GNU cp) is something similar to having used '*' instead of '.':

laptop:~/cptest zak$ rm -rf *
laptop:~/cptest zak$ mkdir src
laptop:~/cptest zak$ touch src/a src/.a
laptop:~/cptest zak$ ~/coreutils/coreutils-6.6/src/cp -a src/. dest
laptop:~/cptest zak$ ls -la dest
total 0
drwxr-xr-x   3 zak  zak  102 Dec  2 19:06 .
drwxr-xr-x   4 zak  zak  136 Dec  2 19:06 ..
-rw-r--r--   1 zak  zak    0 Dec  2 19:06 .a
-rw-r--r--   1 zak  zak    0 Dec  2 19:06 a

It even has a go if the source is just ".":

laptop:~/cptest zak$ cd dest
laptop:~/cptest/dest zak$ mkdir blah
laptop:~/cptest/dest zak$ ~/coreutils/coreutils-6.6/src/cp -a . blah
/Users/zak/coreutils/coreutils-6.6/src/cp: cannot copy a directory, `.', into itself, `blah/.'
laptop:~/cptest/dest zak$ ls -laR
total 0
drwxr-xr-x   5 zak  zak  170 Dec  2 19:14 .
drwxr-xr-x   4 zak  zak  136 Dec  2 19:06 ..
-rw-r--r--   1 zak  zak    0 Dec  2 19:06 .a
-rw-r--r--   1 zak  zak    0 Dec  2 19:06 a
drwxr-xr-x   5 zak  zak  170 Dec  2 19:14 blah

./blah:
total 0
drwxr-xr-x   5 zak  zak  170 Dec  2 19:14 .
drwxr-xr-x   5 zak  zak  170 Dec  2 19:14 ..
-rw-r--r--   1 zak  zak    0 Dec  2 19:06 .a
-rw-r--r--   1 zak  zak    0 Dec  2 19:06 a
drwxr-xr-x   4 zak  zak  136 Dec  2 19:14 blah

./blah/blah:
total 0
drwxr-xr-x   4 zak  zak  136 Dec  2 19:14 .
drwxr-xr-x   5 zak  zak  170 Dec  2 19:14 ..
-rw-r--r--   1 zak  zak    0 Dec  2 19:06 .a
-rw-r--r--   1 zak  zak    0 Dec  2 19:06 a

This means it handles dotfiles as well - a much neater way to copy them than using ugly wildcards, tar, rsync, find or whatever else. The documentation doesn't make it clear (to me anyway) what /should/ happen here, but I'd guess (wildly) that POSIX says cp should fail and do nothing rather than do what it does. So what I'm really looking for here is to have the current behaviour classified one way (correct, and explicitly stated as the expected outcome in the docs) or the other (broken, cp needs fixing, and the expected behaviour should be stated in the docs; shame, because I like it and don't really want it fixed) so I can decide whether to ingrain it in my brain.

The behaviour seems to be down to the following argument munge in cp.c (around line 639; arg is "src/." and target_directory is "dst"):
/* Append the last component of `arg' to `target_directory'.  */

ASSIGN_BASENAME_STRDUPA (arg_base, arg);

(This puts "." into arg_base.)

/* For `cp -R source/.. dest', don't copy into `dest/..'. */
dst_name = (STREQ (arg_base, "..")
                        ? xstrdup (target_directory)
: file_name_concat (target_directory, arg_base, NULL));

This would normally turn "cp -a foo/a bar" into "cp -a foo/a bar/ a" (and non-existent target bar/a would get created), but in this case it turns "cp -a foo/. bar" into "cp -a foo/. bar/.", and there's no need to create "bar/.".

So, is the current behaviour correct or broken?

Cheers,

Zak




reply via email to

[Prev in Thread] Current Thread [Next in Thread]