coreutils
[Top][All Lists]
Advanced

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

Re: [PATCH] cp: Add --keep-directory-symlink option


From: Pádraig Brady
Subject: Re: [PATCH] cp: Add --keep-directory-symlink option
Date: Wed, 24 Jan 2024 14:40:21 +0000
User-agent: Mozilla Thunderbird

On 24/01/2024 12:16, Daan De Meyer wrote:
When recursively copying files into OS trees, it often happens that
some subdirectory of the source directory is a symlink in the target
directory. Currently, cp will fail in that scenario with the error:

"cannot overwrite non-directory %s with directory %s"

However, we'd like cp in this scenario to follow the destination
directory symlink and copy the files into the symlinked directory
instead. Let's support this by adding a new option
--keep-directory-symlink that makes cp follow destination directory
symlinks.

I see this use case comes from https://github.com/systemd/mkosi/issues/2168

We name the option --keep-directory-symlink to keep consistent with
tar which has the same option with the same effect.

The tar docs say "It is mainly intended to provide
compatibility with the Slackware installation scripts"

I also see rsync has the --keep-dirlinks option.
Note rsync mentions the security implications
where you have to trust the destination can't
create symlink to arbitrary dirs, and rsync
has extra options to protect against this like --munge-links etc.
We should at least mention the security implications in the texinfo.

So there does seem to be a use for this functionality.
I'd be 55:45 for adding it.

+"), stdout);
+      fputs (_("\
+      --keep-directory-symlink  preserve existing symlinks to directories\n\
  "), stdout);

s/preserve/follow/ to be more explicit about what happens

diff --git a/tests/cp/keep-directory-symlink.sh 
b/tests/cp/keep-directory-symlink.sh
new file mode 100755
index 000000000..6ae116cd8
--- /dev/null
+++ b/tests/cp/keep-directory-symlink.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+# Test that cp --keep-directory-symlink follows symlinks.
+
+# Copyright (C) 2006-2023 Free Software Foundation, Inc.

A single year suffices here

+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ cp
+
+mkdir -p a/b b/d/e || framework_failure_
+ln -s b a/d || framework_failure_
+

s/framework_failure_/fail=1/ below

+cp -RT --copy-contents b a && framework_failure_
+cp -RT --copy-contents --keep-directory-symlink b a || framework_failure_
+ls a/b/e

.. || fail=1

thanks,
Pádraig



reply via email to

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