From 3cff27ddc1acf57e5da8b9e8b8c887bfd070b2c0 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 8 Sep 2023 09:14:06 -0700 Subject: [PATCH] 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. --- src/copy.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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)); -- 2.47.2