740027
diff -urNp coreutils-8.22-orig/src/copy.c coreutils-8.22/src/copy.c
740027
--- coreutils-8.22-orig/src/copy.c	2015-07-03 14:42:56.829772551 +0200
740027
+++ coreutils-8.22/src/copy.c	2015-07-03 14:51:05.371383675 +0200
740027
@@ -1292,20 +1292,12 @@ close_src_desc:
740027
    copy a regular file onto a symlink that points to it.
740027
    Try to minimize the cost of this function in the common case.
740027
    Set *RETURN_NOW if we've determined that the caller has no more
740027
-   work to do and should return successfully, right away.
740027
-
740027
-   Set *UNLINK_SRC if we've determined that the caller wants to do
740027
-   'rename (a, b)' where 'a' and 'b' are distinct hard links to the same
740027
-   file. In that case, the caller should try to unlink 'a' and then return
740027
-   successfully.  Ideally, we wouldn't have to do that, and we'd be
740027
-   able to rely on rename to remove the source file.  However, POSIX
740027
-   mistakenly requires that such a rename call do *nothing* and return
740027
-   successfully.  */
740027
+   work to do and should return successfully, right away.  */
740027
 
740027
 static bool
740027
 same_file_ok (char const *src_name, struct stat const *src_sb,
740027
               char const *dst_name, struct stat const *dst_sb,
740027
-              const struct cp_options *x, bool *return_now, bool *unlink_src)
740027
+              const struct cp_options *x, bool *return_now)
740027
 {
740027
   const struct stat *src_sb_link;
740027
   const struct stat *dst_sb_link;
740027
@@ -1316,7 +1308,6 @@ same_file_ok (char const *src_name, stru
740027
   bool same = SAME_INODE (*src_sb, *dst_sb);
740027
 
740027
   *return_now = false;
740027
-  *unlink_src = false;
740027
 
740027
   /* FIXME: this should (at the very least) be moved into the following
740027
      if-block.  More likely, it should be removed, because it inhibits
740027
@@ -1348,14 +1339,11 @@ same_file_ok (char const *src_name, stru
740027
               /* Here we have two symlinks that are hard-linked together,
740027
                  and we're not making backups.  In this unusual case, simply
740027
                  returning true would lead to mv calling "rename(A,B)",
740027
-                 which would do nothing and return 0.  I.e., A would
740027
-                 not be removed.  Hence, the solution is to tell the
740027
-                 caller that all it must do is unlink A and return.  */
740027
+                 which would do nothing and return 0.  */
740027
               if (same_link)
740027
                 {
740027
-                  *unlink_src = true;
740027
                   *return_now = true;
740027
-                  return true;
740027
+                  return ! x->move_mode;
740027
                 }
740027
             }
740027
 
740027
@@ -1443,26 +1431,22 @@ same_file_ok (char const *src_name, stru
740027
     return true;
740027
 #endif
740027
 
740027
-  /* They may refer to the same file if we're in move mode and the
740027
-     target is a symlink.  That is ok, since we remove any existing
740027
-     destination file before opening it -- via 'rename' if they're on
740027
-     the same file system, via 'unlink (DST_NAME)' otherwise.
740027
-     It's also ok if they're distinct hard links to the same file.  */
740027
   if (x->move_mode || x->unlink_dest_before_opening)
740027
     {
740027
+     /* They may refer to the same file if we're in move mode and the
740027
+        target is a symlink.  That is ok, since we remove any existing
740027
+        destination file before opening it -- via 'rename' if they're on
740027
+        the same file system, via 'unlink (DST_NAME)' otherwise.  */
740027
       if (S_ISLNK (dst_sb_link->st_mode))
740027
         return true;
740027
 
740027
+      /* It's not ok if they're distinct hard links to the same file as
740027
+         this causes a race condition and we may lose data in this case.  */
740027
       if (same_link
740027
           && 1 < dst_sb_link->st_nlink
740027
           && ! same_name (src_name, dst_name))
740027
         {
740027
-          if (x->move_mode)
740027
-            {
740027
-              *unlink_src = true;
740027
-              *return_now = true;
740027
-            }
740027
-          return true;
740027
+          return ! x->move_mode;
740027
         }
740027
     }
740027
 
740027
@@ -1820,11 +1804,10 @@ copy_internal (char const *src_name, cha
740027
         { /* Here, we know that dst_name exists, at least to the point
740027
              that it is stat'able or lstat'able.  */
740027
           bool return_now;
740027
-          bool unlink_src;
740027
 
740027
           have_dst_lstat = !use_stat;
740027
           if (! same_file_ok (src_name, &src_sb, dst_name, &dst_sb,
740027
-                              x, &return_now, &unlink_src))
740027
+                              x, &return_now))
740027
             {
740027
               error (0, 0, _("%s and %s are the same file"),
740027
                      quote_n (0, src_name), quote_n (1, dst_name));
740027
@@ -1883,22 +1866,14 @@ copy_internal (char const *src_name, cha
740027
              cp and mv treat -i and -f differently.  */
740027
           if (x->move_mode)
740027
             {
740027
-              if (abandon_move (x, dst_name, &dst_sb)
740027
-                  || (unlink_src && unlink (src_name) == 0))
740027
+              if (abandon_move (x, dst_name, &dst_sb))
740027
                 {
740027
                   /* Pretend the rename succeeded, so the caller (mv)
740027
                      doesn't end up removing the source file.  */
740027
                   if (rename_succeeded)
740027
                     *rename_succeeded = true;
740027
-                  if (unlink_src && x->verbose)
740027
-                    printf (_("removed %s\n"), quote (src_name));
740027
                   return true;
740027
                 }
740027
-              if (unlink_src)
740027
-                {
740027
-                  error (0, errno, _("cannot remove %s"), quote (src_name));
740027
-                  return false;
740027
-                }
740027
             }
740027
           else
740027
             {
740027
diff -urNp coreutils-8.22-orig/tests/cp/same-file.sh coreutils-8.22/tests/cp/same-file.sh
740027
--- coreutils-8.22-orig/tests/cp/same-file.sh	2013-12-04 15:48:30.000000000 +0100
740027
+++ coreutils-8.22/tests/cp/same-file.sh	2015-07-03 14:54:12.539772880 +0200
740027
@@ -36,7 +36,7 @@ ln dangling-slink hard-link > /dev/null 2>&1 \
740027
 rm -f no-such dangling-slink hard-link
740027
 
740027
 test $hard_link_to_symlink_does_the_deref = yes \
740027
-    && remove_these_sed='/^0 -[bf]*l .*sl1 ->/d' \
740027
+    && remove_these_sed='/^0 -[bf]*l .*sl1 ->/d; /hlsl/d' \
740027
     || remove_these_sed='/^ELIDE NO TEST OUTPUT/d'
740027
 
740027
 exec 3>&1 1> actual
740027
@@ -44,7 +44,8 @@ exec 3>&1 1> actual
740027
 # FIXME: This should be bigger: like more than 8k
740027
 contents=XYZ
740027
 
740027
-for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' 'foo hardlink'; do
740027
+for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' \
740027
+  'foo hardlink' 'hlsl sl2'; do
740027
   for options in '' -d -f -df --rem -b -bd -bf -bdf \
740027
                  -l -dl -fl -dfl -bl -bdl -bfl -bdfl; do
740027
     case $args$options in
740027
@@ -76,6 +77,8 @@ for args in 'foo symlink' 'symlink foo'
740027
         continue ;;
740027
       'yes:sl1 sl2:-bfl')
740027
         continue ;;
740027
+      yes:hlsl*)
740027
+        continue ;;
740027
     esac
740027
 
740027
     rm -rf dir
740027
@@ -86,6 +87,7 @@ for args in 'foo symlink' 'symlink foo'
740027
     case "$args" in *hardlink*) ln foo hardlink ;; esac
740027
     case "$args" in *sl1*) ln -s foo sl1;; esac
740027
     case "$args" in *sl2*) ln -s foo sl2;; esac
740027
+    case "$args" in *hlsl*) ln sl2 hlsl;;esac
740027
     (
740027
       (
740027
         # echo 1>&2 cp $options $args
740027
@@ -211,6 +213,24 @@ cat <<\EOF | sed "$remove_these_sed" > e
740027
 0 -bfl (foo hardlink)
740027
 0 -bdfl (foo hardlink)
740027
 
740027
+1 [cp: 'hlsl' and 'sl2' are the same file] (foo hlsl -> foo sl2 -> foo)
740027
+0 -d (foo hlsl -> foo sl2 -> foo)
740027
+1 -f [cp: 'hlsl' and 'sl2' are the same file] (foo hlsl -> foo sl2 -> foo)
740027
+0 -df (foo hlsl -> foo sl2 -> foo)
740027
+0 --rem (foo hlsl -> foo sl2)
740027
+0 -b (foo hlsl -> foo sl2 sl2.~1~ -> foo)
740027
+0 -bd (foo hlsl -> foo sl2 -> foo sl2.~1~ -> foo)
740027
+0 -bf (foo hlsl -> foo sl2 sl2.~1~ -> foo)
740027
+0 -bdf (foo hlsl -> foo sl2 -> foo sl2.~1~ -> foo)
740027
+1 -l [cp: cannot create hard link 'sl2' to 'hlsl'] (foo hlsl -> foo sl2 -> foo)
740027
+0 -dl (foo hlsl -> foo sl2 -> foo)
740027
+0 -fl (foo hlsl -> foo sl2)
740027
+0 -dfl (foo hlsl -> foo sl2 -> foo)
740027
+0 -bl (foo hlsl -> foo sl2 sl2.~1~ -> foo)
740027
+0 -bdl (foo hlsl -> foo sl2 -> foo)
740027
+0 -bfl (foo hlsl -> foo sl2 sl2.~1~ -> foo)
740027
+0 -bdfl (foo hlsl -> foo sl2 -> foo)
740027
+
740027
 EOF
740027
 
740027
 exec 1>&3 3>&-
740027
diff -urNp coreutils-8.22-orig/tests/local.mk coreutils-8.22/tests/local.mk
740027
--- coreutils-8.22-orig/tests/local.mk	2015-07-03 14:42:56.820772485 +0200
740027
+++ coreutils-8.22/tests/local.mk	2015-07-03 14:55:07.060176869 +0200
740027
@@ -591,7 +591,6 @@ all_tests =					\
740027
   tests/mv/hard-3.sh				\
740027
   tests/mv/hard-4.sh				\
740027
   tests/mv/hard-link-1.sh			\
740027
-  tests/mv/hard-verbose.sh			\
740027
   tests/mv/i-1.pl				\
740027
   tests/mv/i-2.sh				\
740027
   tests/mv/i-3.sh				\
740027
diff -urNp coreutils-8.22-orig/tests/mv/force.sh coreutils-8.22/tests/mv/force.sh
740027
--- coreutils-8.22-orig/tests/mv/force.sh	2013-12-04 15:48:30.000000000 +0100
740027
+++ coreutils-8.22/tests/mv/force.sh	2015-07-03 14:56:42.840885931 +0200
740027
@@ -25,18 +25,19 @@ ff2=mvforce2
740027
 echo force-contents > $ff || framework_failure_
740027
 ln $ff $ff2 || framework_failure_
740027
 
740027
-# This mv command should exit nonzero.
740027
-mv $ff $ff > out 2>&1 && fail=1
740027
+# mv should fail for the same name, or separate hardlinks as in
740027
+# both cases rename() will do nothing and return success.
740027
+# One could unlink(src) in the hardlink case, but that would
740027
+# introduce races with overlapping mv instances removing both hardlinks.
740027
 
740027
-cat > exp <
740027
-mv: '$ff' and '$ff' are the same file
740027
-EOF
740027
+for dest in $ff $ff2; do
740027
+  # This mv command should exit nonzero.
740027
+  mv $ff $dest > out 2>&1 && fail=1
740027
 
740027
-compare exp out || fail=1
740027
-test $(cat $ff) = force-contents || fail=1
740027
+  printf "mv: '$ff' and '$dest' are the same file\n" > exp
740027
+  compare exp out || fail=1
740027
 
740027
-# This should succeed, even though the source and destination
740027
-# device and inodes are the same.
740027
-mv $ff $ff2 || fail=1
740027
+  test $(cat $ff) = force-contents || fail=1
740027
+done
740027
 
740027
 Exit $fail
740027
diff -urNp coreutils-8.22-orig/tests/mv/hard-4.sh coreutils-8.22/tests/mv/hard-4.sh
740027
--- coreutils-8.22-orig/tests/mv/hard-4.sh	2013-12-04 15:48:30.000000000 +0100
740027
+++ coreutils-8.22/tests/mv/hard-4.sh	2015-07-03 14:58:31.179687188 +0200
740027
@@ -1,5 +1,5 @@
740027
 #!/bin/sh
740027
-# ensure that mv removes a in this case: touch a; ln a b; mv a b
740027
+# ensure that mv maintains a in this case: touch a; ln a b; mv a b
740027
 
740027
 # Copyright (C) 2003-2013 Free Software Foundation, Inc.
740027
 
740027
@@ -21,15 +21,19 @@ print_ver_ mv
740027
 touch a || framework_failure_
740027
 ln a b || framework_failure_
740027
 
740027
+# Between coreutils-5.0 and coreutils-8.24, 'a' would be removed.
740027
+# Before coreutils-5.0.1 the issue would not have been diagnosed.
740027
+# We don't emulate the rename(a,b) with unlink(a) as that would
740027
+# introduce races with overlapping mv instances removing both links.
740027
+mv a b 2>err && fail=1
740027
+printf "mv: 'a' and 'b' are the same file\n" > exp
740027
+compare exp err || fail=1
740027
 
740027
-mv a b || fail=1
740027
 
740027
-# In coreutils-5.0 and earlier, a would not be removed.
740027
-test -r a && fail=1
740027
+test -r a || fail=1
740027
 test -r b || fail=1
740027
 
740027
-# Make sure it works also with --backup.
740027
-ln b a
740027
+# Make sure it works with --backup.
740027
 mv --backup=simple a b || fail=1
740027
 test -r a && fail=1
740027
 test -r b || fail=1
740027
diff -urNp coreutils-8.22-orig/tests/mv/i-4.sh coreutils-8.22/tests/mv/i-4.sh
740027
--- coreutils-8.22-orig/tests/mv/i-4.sh	2013-12-04 15:48:30.000000000 +0100
740027
+++ coreutils-8.22/tests/mv/i-4.sh	2015-07-03 15:00:39.533718254 +0200
740027
@@ -23,6 +23,7 @@ for i in a b; do
740027
   echo $i > $i || framework_failure_
740027
 done
740027
 echo y > y || framework_failure_
740027
+echo n > n || framework_failure_
740027
 
740027
 mv -i a b < y >/dev/null 2>&1 || fail=1
740027
 
740027
@@ -32,18 +33,15 @@ case "$(cat b)" in
740027
   *) fail=1 ;;
740027
 esac
740027
 
740027
-# Ensure that mv -i a b works properly with 'n' and 'y'
740027
-# responses, even when a and b are hard links to the same file.
740027
-# This 'n' test would fail (no prompt) for coreutils-5.0.1 through 5.3.0.
740027
-echo n > n
740027
+# Ensure that mv -i a b works properly with 'n' and 'y' responses,
740027
+# when a and b are hard links to the same file.
740027
 rm -f a b
740027
 echo a > a
740027
 ln a b
740027
-mv -i a b < n >/dev/null 2>&1 || fail=1
740027
+mv -i a b < y 2>err && fail=1
740027
 test -r a || fail=1
740027
 test -r b || fail=1
740027
-mv -i a b < y >/dev/null 2>&1 || fail=1
740027
-test -r a && fail=1
740027
-test -r b || fail=1
740027
+printf "mv: 'a' and 'b' are the same file\n" > exp
740027
+compare exp err || fail=1
740027
 
740027
 Exit $fail
740027
diff -urNp coreutils-8.22-orig/tests/mv/symlink-onto-hardlink-to-self.sh coreutils-8.22/tests/mv/symlink-onto-hardlink-to-self.sh
740027
--- coreutils-8.22-orig/tests/mv/symlink-onto-hardlink-to-self.sh	2013-12-04 15:48:30.000000000 +0100
740027
+++ coreutils-8.22/tests/mv/symlink-onto-hardlink-to-self.sh	2015-07-03 15:01:39.209190741 +0200
740027
@@ -1,10 +1,10 @@
740027
 #!/bin/sh
740027
-# Demonstrate that when moving a symlink onto a hardlink-to-that-symlink, the
740027
-# source symlink is removed.  Depending on your kernel (e.g., Linux, Solaris,
740027
+# Demonstrate that when moving a symlink onto a hardlink-to-that-symlink,
740027
+# an error is presented.  Depending on your kernel (e.g., Linux, Solaris,
740027
 # but not NetBSD), prior to coreutils-8.16, the mv would successfully perform
740027
 # a no-op.  I.e., surprisingly, mv s1 s2 would succeed, yet fail to remove s1.
740027
 
740027
-# Copyright (C) 2012-2013 Free Software Foundation, Inc.
740027
+# Copyright (C) 2012-2014 Free Software Foundation, Inc.
740027
 
740027
 # This program is free software: you can redistribute it and/or modify
740027
 # it under the terms of the GNU General Public License as published by
740027
@@ -26,27 +26,34 @@ print_ver_ mv
740027
 touch f || framework_failure_
740027
 ln -s f s2 || framework_failure_
740027
 
740027
-for opt in '' --backup; do
740027
+# Attempt to create a hard link to that symlink.
740027
+# On some systems, it's not possible: they create a hard link to the referent.
740027
+ln s2 s1 || framework_failure_
740027
+
740027
+# If s1 is not a symlink, skip this test.
740027
+test -h s1 \
740027
+  || skip_ your kernel or file system cannot create a hard link to a symlink
740027
 
740027
-  # Attempt to create a hard link to that symlink.
740027
-  # On some systems, it's not possible: they create a hard link to the referent.
740027
-  ln s2 s1 || framework_failure_
740027
-
740027
-  # If s1 is not a symlink, skip this test.
740027
-  test -h s1 \
740027
-    || skip_ your kernel or file system cannot create a hard link to a symlink
740027
+for opt in '' --backup; do
740027
 
740027
-  mv $opt s1 s2 > out 2>&1 || fail=1
740027
-  compare /dev/null out || fail=1
740027
+  if test "$opt" = --backup; then
740027
+    mv $opt s1 s2 > out 2>&1 || fail=1
740027
+    compare /dev/null out || fail=1
740027
 
740027
-  # Ensure that s1 is gone.
740027
-  test -e s1 && fail=1
740027
+    # Ensure that s1 is gone.
740027
+    test -e s1 && fail=1
740027
 
740027
-  if test "$opt" = --backup; then
740027
     # With --backup, ensure that the backup file was created.
740027
     ref=$(readlink s2~) || fail=1
740027
     test "$ref" = f || fail=1
740027
   else
740027
+    echo "mv: 's1' and 's2' are the same file" > exp
740027
+    mv $opt s1 s2 2>err && fail=1
740027
+    compare exp err || fail=1
740027
+
740027
+    # Ensure that s1 is still present.
740027
+    test -e s1 || fail=1
740027
+
740027
     # Without --backup, ensure there is no backup file.
740027
     test -e s2~ && fail=1
740027
   fi