From: Paul Eggert Date: Fri, 8 Sep 2023 16:14:06 +0000 (-0700) Subject: cp: avoid needless unlinkat after fstatat ELOOP X-Git-Tag: v9.5~165 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3cff27ddc1acf57e5da8b9e8b8c887bfd070b2c0;p=thirdparty%2Fcoreutils.git cp: avoid needless unlinkat after fstatat ELOOP * 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. --- diff --git a/src/copy.c b/src/copy.c index 75cd7f081b..0c2004c7b0 100644 --- a/src/copy.c +++ b/src/copy.c @@ -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));