Skip to content
Open
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
2 changes: 1 addition & 1 deletion Lib/pathlib/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def copyfileobj(source_f, target_f):
_ficlone(source_fd, target_fd)
return
except OSError as err:
if err.errno not in (EBADF, EOPNOTSUPP, ETXTBSY, EXDEV):
if err.errno not in (EBADF, EOPNOTSUPP, EPERM, ETXTBSY, EXDEV):
raise err

# Use OS copy where available.
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_pathlib/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,31 @@ class LocalToLocalPathCopyTest(CopyTestBase, unittest.TestCase):
target_ground = LocalPathGround(Path)


class CopyFileobjFallbackTest(unittest.TestCase):
def test_ficlone_eperm_falls_back(self):
# FICLONE can fail with EPERM on filesystems that block reflinks
# (e.g. overlayfs, or an ioctl blocked by SELinux). copyfileobj
# must fall back to another copy method instead of failing.
import errno
import tempfile
import unittest.mock
from pathlib import _os as pathlib_os

def failing_ficlone(source_fd, target_fd):
raise OSError(errno.EPERM, 'Operation not permitted')

with tempfile.TemporaryDirectory() as tmpdir:
source = f'{tmpdir}/source.txt'
target = f'{tmpdir}/target.txt'
with open(source, 'wb') as f:
f.write(b'hello world')
with open(source, 'rb') as source_f, open(target, 'wb') as target_f:
with unittest.mock.patch.object(
pathlib_os, '_ficlone', failing_ficlone):
pathlib_os.copyfileobj(source_f, target_f)
with open(target, 'rb') as f:
self.assertEqual(f.read(), b'hello world')


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix pathlib.Path.copy failing with PermissionError when the FICLONE reflink ioctl returns EPERM; it now falls back to other copy methods.
Loading