Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ def _fastcopy_sendfile(fsrc, fdst):
_USE_CP_SENDFILE = False
raise _GiveupOnFastCopy(err)

if err.errno == errno.ENODATA:
# In rare cases, sendfile() on Linux Lustre returns
# ENODATA.
_USE_CP_SENDFILE = False

# 'infd' and 'outfd' are assumed to be seekable, as
# they are checked to be "regular" files.
dstpos = os.lseek(outfd, 0, os.SEEK_CUR)
if dstpos > 0:
# Some data has already been written but we use
# sendfile in a mode that does not update the
# input fd position when reading. Hence seek the
# input fd to the correct position before falling
# back on POSIX read/write method.
os.lseek(infd, dstpos, os.SEEK_SET)

raise _GiveupOnFastCopy(err)

if err.errno == errno.ENOSPC: # filesystem is full
raise err from None

Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,42 @@ def test_file2file_not_supported(self):
finally:
shutil._USE_CP_SENDFILE = True

def test_exception_on_enodata_call(self):
# Test logic when sendfile(2) call returns ENODATA error on
# the not-first call on the file and we need to fall back to
# traditional POSIX while preserving the position of where we
# got to in writing
def syscall(*args, **kwargs):
if flag:
raise OSError(errno.ENODATA, "yo")
flag.append(None)
return eval(self.PATCHPOINT)(*args, **kwargs)

flag = []
orig_syscall = eval(self.PATCHPOINT)
# Reduce block size so that multiple syscalls are needed
fstat_mock = unittest.mock.Mock()
fstat_mock.st_size = 65536 + 1
with unittest.mock.patch('os.fstat', return_value=fstat_mock):
with (
unittest.mock.patch(self.PATCHPOINT, create=True,
side_effect=syscall),
self.get_files() as (src, dst)
):
self.assertRaises(_GiveupOnFastCopy,
self.zerocopy_fun, src, dst)

# Reset flag so that second syscall fails again
flag.clear()
with unittest.mock.patch(self.PATCHPOINT, create=True,
side_effect=syscall) as m2:
shutil._USE_CP_SENDFILE = True
shutil.copyfile(TESTFN, TESTFN2)
m2.assert_called()
shutil._USE_CP_SENDFILE = True
self.assertEqual(flag, [None])
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA)


@unittest.skipUnless(shutil._USE_CP_COPY_FILE_RANGE, "os.copy_file_range() not supported")
class TestZeroCopyCopyFileRange(_ZeroCopyFileLinuxTest, unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`shutil.copyfile`: Detect problem seen on Lustre filesystems
giving "Errno 61" due to the sendfile(2) optimised implementation and
fall back to the Posix standard read/write implementation.
Loading