summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog11
-rw-r--r--NEWS3
-rw-r--r--THANKS1
-rw-r--r--src/copy.c20
-rw-r--r--tests/cp/Makefile.am1
-rwxr-xr-xtests/cp/thru-dangling54
6 files changed, 88 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index cb3fbfef7..6a30666f1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2007-06-10 Jim Meyering <jim@meyering.net>
+
+ bug-fix: cp would fail to write through a dangling symlink
+ * NEWS: Mention the bug fix.
+ * src/copy.c (copy_reg): When open fails with EEXIST, the destination
+ is lstat'able, and a symlink, call open again, but now without O_EXCL.
+ * tests/cp/thru-dangling: New file, to test for the above fix.
+ * tests/cp/Makefile.am (TESTS): Add thru-dangling.
+ * THANKS: Add Michael McLagan.
+ Bug report from Michael McLagan in <http://bugzilla.redhat.com/243588>.
+
2007-06-04 Paul Eggert <eggert@cs.ucla.edu>
* doc/coreutils.texi (Common options): Mention that -h and
diff --git a/NEWS b/NEWS
index 715b0d698..9db7902d0 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,9 @@ GNU coreutils NEWS -*- outline -*-
** Bug fixes
+ cp no longer fails to write through a dangling symlink
+ [bug introduced in coreutils-6.7]
+
cut now diagnoses a range starting with zero (e.g., -f 0-2) as invalid;
before, it would treat it as if it started with 1 (-f 1-2).
diff --git a/THANKS b/THANKS
index 4ffd1a840..7d9c52752 100644
--- a/THANKS
+++ b/THANKS
@@ -351,6 +351,7 @@ Michael Hasselberg mikelh@zonta.ping.de
Michael Hohn hohn@math.utah.edu
Michael J. Croghan mcroghan@usatoday.com
Michael McFarland sidlon@yahoo.com
+Michael McLagan mmclagan@invlogic.com
Michael Piefel piefel@informatik.hu-berlin.de
Michael Steffens michael.steffens@s.netic.de
Michael Stone mstone@debian.org
diff --git a/src/copy.c b/src/copy.c
index f4e3b174b..b46221c01 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -352,8 +352,24 @@ copy_reg (char const *src_name, char const *dst_name,
}
if (*new_dst)
- dest_desc = open (dst_name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
- dst_mode & ~omitted_permissions);
+ {
+ int open_flags = O_WRONLY | O_CREAT | O_BINARY;
+ dest_desc = open (dst_name, open_flags | O_EXCL,
+ dst_mode & ~omitted_permissions);
+
+ /* When trying to copy through a dangling destination symlink,
+ the above open fails with EEXIST. If that happens, and
+ lstat'ing the DST_NAME shows that it is a symlink, repeat
+ the open call, but this time without O_EXCL. */
+ if (dest_desc < 0 && errno == EEXIST)
+ {
+ struct stat dangling_link_sb;
+ if (lstat (dst_name, &dangling_link_sb) == 0
+ && S_ISLNK (dangling_link_sb.st_mode))
+ dest_desc = open (dst_name, open_flags,
+ dst_mode & ~omitted_permissions);
+ }
+ }
else
omitted_permissions = 0;
diff --git a/tests/cp/Makefile.am b/tests/cp/Makefile.am
index 536c0221d..52e876b08 100644
--- a/tests/cp/Makefile.am
+++ b/tests/cp/Makefile.am
@@ -18,6 +18,7 @@
# 02110-1301, USA.
TESTS = \
+ thru-dangling \
cp-a-selinux \
file-perm-race parent-perm-race \
backup-dir \
diff --git a/tests/cp/thru-dangling b/tests/cp/thru-dangling
new file mode 100755
index 000000000..3888cf3d9
--- /dev/null
+++ b/tests/cp/thru-dangling
@@ -0,0 +1,54 @@
+#!/bin/sh
+# Ensure that cp works when the destination is a dangling symlink
+
+# Copyright (C) 2007 Free Software Foundation, Inc.
+
+# 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 2 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, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ cp --version
+fi
+
+. $srcdir/../envvar-check
+
+pwd=`pwd`
+t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
+trap 'status=$?; cd "$pwd" && chmod -R u+rwx $t0 && rm -rf $t0 && exit $status' 0
+trap '(exit $?); exit $?' 1 2 13 15
+
+framework_failure=0
+mkdir -p $tmp || framework_failure=1
+cd $tmp || framework_failure=1
+
+ln -s no-such dangle || framework_failure=1
+echo hi > f || framework_failure=1
+echo hi > exp || framework_failure=1
+
+if test $framework_failure = 1; then
+ echo "$0: failure in testing framework" 1>&2
+ (exit 1); exit 1
+fi
+
+fail=0
+
+cp f dangle > out 2>&1 || fail=1
+cat no-such >> out || fail=1
+
+cmp out exp || fail=1
+test $fail = 1 && diff out exp 2> /dev/null
+
+(exit $fail); exit $fail