Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 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
21 changes: 17 additions & 4 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,24 @@ def _fastcopy_sendfile(fsrc, fdst):
err.filename = fsrc.name
err.filename2 = fdst.name

if err.errno == errno.ENOTSOCK:
# sendfile() on this platform (probably Linux < 2.6.33)
# does not support copies between regular files (only
# sockets).
if err.errno in (errno.ENOTSOCK, errno.ENODATA):
# ENOTSOCK: sendfile() on this platform (probably
# Linux < 2.6.33) does not support copies between
# regular files (only sockets).
#
# ENODATA: In rare cases, sendfile() on Linux Lustre
# returns ENODATA.
_USE_CP_SENDFILE = False

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
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 not flag:
flag.append(None)
return orig_syscall(*args, **kwargs)
else:
raise OSError(errno.ENODATA, "yo")

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with (
    unittest.mock.patch('os.fstat', return_value=mock):
    unittest.mock.patch(self.PATCHPOINT, create=True,
                        side_effect=syscall):
    self.get_files() as (src, dst)
):
    self.assertRaises(_GiveupOnFastCopy, self.zerocopy_func, src, dst)

You'll need to dedent the rest of the code as well. Also, don't bind context managers if they are not used.


# Reset flag so that second syscall fails again
flag = []
with unittest.mock.patch(self.PATCHPOINT, create=True,
side_effect=syscall) as m2:
shutil._USE_CP_SENDFILE = True
shutil.copyfile(TESTFN, TESTFN2)
assert m2.called
shutil._USE_CP_SENDFILE = True
assert flag
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