Subject: Re: bug#56391: `cp --reflink=always` creates empty file on failure

From: Paul Eggert <[email-masked]>
Date: Tue, 5 Jul 2022 10:04:48 -0500

Thanks for reporting that. I installed the attached patch.

From 08f14d9492e35188b7ed85eb59b7e605285d8b09 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Tue, 5 Jul 2022 09:34:17 -0500
Subject: [PATCH] =?UTF-8?q?cp:=20don=E2=80=99t=20create=20empty=20file=20i?=
 =?UTF-8?q?f=20cannot=20clone?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* src/copy.c (copy_reg): With --reflink=always, if FICLONE fails
on a file we just created, clean up by removing the file (Bug#56391).
---
 src/copy.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/copy.c b/src/copy.c
index 0c368d0e4..eaed148b4 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -1279,6 +1279,9 @@ copy_reg (char const *src_name, char const *dst_name,
         {
           error (0, errno, _("failed to clone %s from %s"),
                  quoteaf_n (0, dst_name), quoteaf_n (1, src_name));
+          if (*new_dst && unlinkat (dst_dirfd, dst_relname, 0) != 0
+              && errno != ENOENT)
+            error (0, errno, _("cannot remove %s"), quoteaf (dst_name));
           return_val = false;
           goto close_src_and_dst_desc;
         }
-- 
2.36.1