cp: don’t remove nonempty cloned dest

This follows up on comments by Pádraig Brady (bug#56391).
* src/copy.c (copy_reg): When --reflink=always removes a file
due to an FICLONE failure, do not remove a nonempty file.
This commit is contained in:
Paul Eggert committed 2022-07-06 14:31:32 -05:00
1 parent 1edda5009b
commit 8da1edffff
2 files changed
+13 -2

No files matched your search

+3
View File
@@ -18,6 +18,9 @@ GNU coreutils NEWS -*- outline -*-
** Changes in behavior
'cp --reflink=always A B' no longer leaves behind a newly created
empty file B merely because copy-on-write clones are not supported.
'ls -v' and 'sort -V' go back to sorting ".0" before ".A",
reverting to the behavior in coreutils-9.0 and earlier.
This behavior is now documented.
+10 -2
View File
@@ -1279,9 +1279,17 @@ 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)
/* Remove the destination if cp --reflink=always created it
but cloned no data. If clone_file failed with
EOPNOTSUPP, EXDEV or EINVAL no data were copied so do not
go to the expense of lseeking. */
if (*new_dst
&& (is_ENOTSUP (errno) || errno == EXDEV || errno == EINVAL
|| lseek (dest_desc, 0, SEEK_END) == 0)
&& 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;
}