]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cp: avoid needless unlinkat after fstatat ELOOP
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 8 Sep 2023 16:14:06 +0000 (09:14 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 8 Sep 2023 23:25:39 +0000 (16:25 -0700)
* src/copy.c (copy_internal): When cp -f's fstatat fails on the
destination with ELOOP, report an error immediately when fstatat
used AT_SYMLINK_NOFOLLOW, as the later unlinkat would fail too.

src/copy.c

index 75cd7f081bfd3fb751416ff75679c0e131d2f3b6..0c2004c7b0ce88be4b6a3b6d8710c410d737510b 100644 (file)
@@ -2319,8 +2319,12 @@ copy_internal (char const *src_name, char const *dst_name,
             }
           else if (errno == ENOENT)
             new_dst = true;
-          else if (errno == ELOOP && x->unlink_dest_after_failed_open)
-            /* Leave new_dst=false so we unlink later.  */;
+          else if (errno == ELOOP && !use_lstat
+                   && x->unlink_dest_after_failed_open)
+            {
+              /* cp -f's destination might be a symlink loop.
+                 Leave new_dst=false so that we try to unlink later.  */
+            }
           else
             {
               error (0, errno, _("cannot stat %s"), quoteaf (dst_name));