From 7fc84d1c0f6b35231b0b4577b70aaa26bf548a7c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
Date: Sat, 7 Jan 2023 16:10:01 +0000
Subject: [PATCH] copy: copy_file_range: handle ENOENT for CIFS

* src/copy.c (sparse_copy): Fallback to standard copy upon ENOENT,
which was seen intermittently across CIFS file systems.
* NEWS: Mention the bug fix, though qualify it as an "issue"
rather than a bug, as coreutils is likely only highlighting
a CIFS bug in this case.
Fixes https://bugs.gnu.org/60455

Reference:https://github.com/coreutils/coreutils/commit/7fc84d1c0f6b35231b0b4577b70aaa26bf548a7c
Conflict:NEWS context adapation

---
 NEWS       | 4 ++++
 src/copy.c | 5 +++++
 2 files changed, 9 insertions(+)

diff --git a/NEWS b/NEWS
index 9d3f253..b65bc85 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,10 @@ GNU coreutils NEWS                 -*- outline -*-
 * Noteworthy changes in release 9.0 (2021-09-24) [stable]

 ** Bug fixes
+  cp, mv, and install now handle ENOENT failures across CIFS file systems,
+  falling back from copy_file_range to a better supported standard copy.
+  [issue introduced in coreutils-9.0]
+
   stty now wraps output appropriately for the terminal width.
   Previously it may have output 1 character too wide for certain widths.
   [bug introduced in coreutils-5.3]
diff --git a/src/copy.c b/src/copy.c
index 519c43b00..98f2ba45a 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -290,6 +290,11 @@ sparse_copy (int src_fd, int dest_fd, char **abuf, size_t buf_size,
             if (errno == EPERM && *total_n_read == 0)
               break;
 
+            /* ENOENT was seen sometimes across CIFS shares, resulting in
+               no data being copied, but subsequent standard copies succeed.  */
+            if (errno == ENOENT && *total_n_read == 0)
+              break;
+
             if (errno == EINTR)
               n_copied = 0;
             else
-- 
2.27.0

