Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Jim Meyering <jim <at> meyering.net>
Subject: FYI: bug-fix: cp would fail to write through a dangling symlink
Newsgroups: gmane.comp.gnu.core-utils.bugs
Date: Sunday 10th June 2007 13:12:18 UTC (over 10 years ago)
Michael,

Thank you for reporting that.
Here's how I'm fixing it upstream:

 ChangeLog              |   11 +++++++++
 NEWS                   |    3 ++
 THANKS                 |    1 +
 src/copy.c             |   20 ++++++++++++++++-
 tests/cp/Makefile.am   |    1 +
 tests/cp/thru-dangling |   54
++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 88 insertions(+), 2 deletions(-)
 create mode 100755 tests/cp/thru-dangling

diff --git a/ChangeLog b/ChangeLog
index cb3fbfe..6a30666 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2007-06-10  Jim Meyering  
+
+	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  

 	* doc/coreutils.texi (Common options): Mention that -h and
diff --git a/NEWS b/NEWS
index 715b0d6..9db7902 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 4ffd1a8..7d9c527 100644
--- a/THANKS
+++ b/THANKS
@@ -351,6 +351,7 @@ Michael Hasselberg                 
[email protected]
 Michael Hohn                        [email protected]
 Michael J. Croghan                  [email protected]
 Michael McFarland                   [email protected]
+Michael McLagan                     [email protected]
 Michael Piefel                      [email protected]
 Michael Steffens                    [email protected]
 Michael Stone                       [email protected]
diff --git a/src/copy.c b/src/copy.c
index f4e3b17..b46221c 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 536c022..52e876b 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 0000000..3888cf3
--- /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
 
CD: 3ms