Skip to content
Closed
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
14 changes: 8 additions & 6 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,13 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
errors = []
use_srcentry = copy_function is copy2 or copy_function is copy

try:
copystat(src, dst)
except OSError as why:
# Copying file access times may fail on Windows
if getattr(why, 'winerror', None) is None:
errors.append((src, dst, str(why)))

for srcentry in entries:
if srcentry.name in ignored_names:
continue
Expand Down Expand Up @@ -598,12 +605,7 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
errors.extend(err.args[0])
except OSError as why:
errors.append((srcname, dstname, str(why)))
try:
copystat(src, dst)
except OSError as why:
# Copying file access times may fail on Windows
if getattr(why, 'winerror', None) is None:
errors.append((src, dst, str(why)))

if errors:
raise Error(errors)
return dst
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,25 @@ def test_copytree_subdirectory(self):
rv = shutil.copytree(src_dir, dst_dir)
self.assertEqual(['pol'], os.listdir(rv))

def test_copytree_xattr(self):
# gh-144220: copytree() must call copystat() to copy extended
# attributes before copying files.

src_dir = self.mkdtemp()
dst_dir = os.path.join(self.mkdtemp(), 'destination')
create_file((src_dir, 'test.txt'), '123')

def copystat(src, dst, *, follow_symlinks=True):
if os.path.isdir(dst) and os.listdir(dst):
raise Exception('Directory not empty')

with unittest.mock.patch('shutil.copystat', side_effect=copystat):
shutil.copytree(src_dir, dst_dir)
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
actual = read_file((dst_dir, 'test.txt'))
self.assertEqual(actual, '123')


class TestCopy(BaseTest, unittest.TestCase):

### shutil.copymode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`shutil.copytree` now copies directory metadata before copying files. It
prevents an error when setting an extended attribute (``bcachefs.casefold``) on
an non-empty directory. Patch by Victor Stinner.
Loading