aboutsummaryrefslogtreecommitdiff
path: root/bin/cp
diff options
context:
space:
mode:
authorKyle Evans <kevans@FreeBSD.org>2022-01-27 18:02:17 +0000
committerKyle Evans <kevans@FreeBSD.org>2022-02-22 22:06:02 +0000
commit33ad990ce974be50abdc25427b0f365699d83a34 (patch)
tree4d73fe6e36a94c235ae92d8ea94b0db12c75f157 /bin/cp
parent07625bc4b30d66be5690d65debe56153aaf648bd (diff)
downloadsrc-33ad990ce974be50abdc25427b0f365699d83a34.tar.gz
src-33ad990ce974be50abdc25427b0f365699d83a34.zip
cp: fix -R with links
The traversal was previously not properly honoring -H/-L/-P. Notably, we should not have been resolving symlinks encountered during traversal when either -H or -P are specified. Sponsored by: Klara, Inc. Reviewed by: bapt Differential Revision: https://reviews.freebsd.org/D34063
Diffstat (limited to 'bin/cp')
-rw-r--r--bin/cp/cp.c23
-rwxr-xr-xbin/cp/tests/cp_test.sh64
2 files changed, 83 insertions, 4 deletions
diff --git a/bin/cp/cp.c b/bin/cp/cp.c
index 4328b89cfd7a..7156c7f10742 100644
--- a/bin/cp/cp.c
+++ b/bin/cp/cp.c
@@ -87,7 +87,7 @@ static char emptystring[] = "";
PATH_T to = { to.p_path, emptystring, "" };
int fflag, iflag, lflag, nflag, pflag, sflag, vflag;
-static int Rflag, rflag;
+static int Hflag, Lflag, Rflag, rflag;
volatile sig_atomic_t info;
enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
@@ -100,11 +100,10 @@ main(int argc, char *argv[])
{
struct stat to_stat, tmp_stat;
enum op type;
- int Hflag, Lflag, ch, fts_options, r, have_trailing_slash;
+ int ch, fts_options, r, have_trailing_slash;
char *target;
fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
- Hflag = Lflag = 0;
while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1)
switch (ch) {
case 'H':
@@ -271,6 +270,22 @@ main(int argc, char *argv[])
&to_stat)));
}
+/* Does the right thing based on -R + -H/-L/-P */
+static int
+copy_stat(const char *path, struct stat *sb)
+{
+
+ /*
+ * For -R -H/-P, we need to lstat() instead; copy() cares about the link
+ * itself rather than the target if we're not following links during the
+ * traversal.
+ */
+ if (!Rflag || Lflag)
+ return (stat(path, sb));
+ return (lstat(path, sb));
+}
+
+
static int
copy(char *argv[], enum op type, int fts_options, struct stat *root_stat)
{
@@ -443,7 +458,7 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat)
}
/* Not an error but need to remember it happened. */
- if (stat(to.p_path, &to_stat) == -1)
+ if (copy_stat(to.p_path, &to_stat) == -1)
dne = 1;
else {
if (to_stat.st_dev == curr->fts_statp->st_dev &&
diff --git a/bin/cp/tests/cp_test.sh b/bin/cp/tests/cp_test.sh
index 3220b45b4438..36d64900f4bb 100755
--- a/bin/cp/tests/cp_test.sh
+++ b/bin/cp/tests/cp_test.sh
@@ -43,6 +43,21 @@ basic_body()
check_size baz 4
}
+atf_test_case basic_symlink
+basic_symlink_body()
+{
+ echo "foo" > bar
+ ln -s bar baz
+
+ atf_check cp baz foo
+ atf_check test '!' -L foo
+
+ atf_check -e inline:"cp: baz and baz are identical (not copied).\n" \
+ -s exit:1 cp baz baz
+ atf_check -e inline:"cp: bar and baz are identical (not copied).\n" \
+ -s exit:1 cp baz bar
+}
+
atf_test_case chrdev
chrdev_body()
{
@@ -130,12 +145,61 @@ matching_srctgt_nonexistent_body()
atf_check -e not-empty -s not-exit:0 stat foo/dne/foo
}
+recursive_link_setup()
+{
+ extra_cpflag=$1
+
+ mkdir -p foo/bar
+ ln -s bar foo/baz
+
+ mkdir foo-mirror
+ eval "cp -R $extra_cpflag foo foo-mirror"
+}
+
+atf_test_case recursive_link_dflt
+recursive_link_dflt_body()
+{
+ recursive_link_setup
+
+ # -P is the default, so this should work and preserve the link.
+ atf_check cp -R foo foo-mirror
+ atf_check test -L foo-mirror/foo/baz
+}
+
+atf_test_case recursive_link_Hflag
+recursive_link_Hflag_body()
+{
+ recursive_link_setup
+
+ # -H will not follow either, so this should also work and preserve the
+ # link.
+ atf_check cp -RH foo foo-mirror
+ atf_check test -L foo-mirror/foo/baz
+}
+
+atf_test_case recursive_link_Lflag
+recursive_link_Lflag_body()
+{
+ recursive_link_setup -L
+
+ # -L will work, but foo/baz ends up expanded to a directory.
+ atf_check test -d foo-mirror/foo/baz -a \
+ '(' ! -L foo-mirror/foo/baz ')'
+ atf_check cp -RL foo foo-mirror
+ atf_check test -d foo-mirror/foo/baz -a \
+ '(' ! -L foo-mirror/foo/baz ')'
+}
+
atf_init_test_cases()
{
atf_add_test_case basic
+ atf_add_test_case basic_symlink
atf_add_test_case chrdev
atf_add_test_case matching_srctgt
atf_add_test_case matching_srctgt_contained
atf_add_test_case matching_srctgt_link
atf_add_test_case matching_srctgt_nonexistent
+ atf_add_test_case recursive_link_dflt
+ atf_add_test_case recursive_link_Hflag
+ atf_add_test_case recursive_link_Lflag
}