* 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-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
** 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).
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
}
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;
# 02110-1301, USA.
TESTS = \
+ thru-dangling \
cp-a-selinux \
file-perm-race parent-perm-race \
backup-dir \
--- /dev/null
+#!/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