Skip to content

Commit 88fc065

Browse files
authored
GH-73991: Support preserving metadata in pathlib.Path.copy() (#120806)
Add *preserve_metadata* keyword-only argument to `pathlib.Path.copy()`, defaulting to false. When set to true, we copy timestamps, permissions, extended attributes and flags where available, like `shutil.copystat()`. The argument has no effect on Windows, where metadata is always copied. Internally (in the pathlib ABCs), path types gain `_readable_metadata` and `_writable_metadata` attributes. These sets of strings describe what kinds of metadata can be retrieved and stored. We take an intersection of `source._readable_metadata` and `target._writable_metadata` to minimise reads/writes. A new `_read_metadata()` method accepts a set of metadata keys and returns a dict with those keys, and a new `_write_metadata()` method accepts a dict of metadata. We *might* make these public in future, but it's hard to justify while the ABCs are still private.
1 parent 6239d41 commit 88fc065

File tree

5 files changed

+187
-11
lines changed

5 files changed

+187
-11
lines changed

Doc/library/pathlib.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ Creating files and directories
15391539
Copying, renaming and deleting
15401540
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15411541

1542-
.. method:: Path.copy(target, *, follow_symlinks=True)
1542+
.. method:: Path.copy(target, *, follow_symlinks=True, preserve_metadata=False)
15431543

15441544
Copy the contents of this file to the *target* file. If *target* specifies
15451545
a file that already exists, it will be replaced.
@@ -1548,11 +1548,11 @@ Copying, renaming and deleting
15481548
will be created as a symbolic link. If *follow_symlinks* is true and this
15491549
file is a symbolic link, *target* will be a copy of the symlink target.
15501550

1551-
.. note::
1552-
This method uses operating system functionality to copy file content
1553-
efficiently. The OS might also copy some metadata, such as file
1554-
permissions. After the copy is complete, users may wish to call
1555-
:meth:`Path.chmod` to set the permissions of the target file.
1551+
If *preserve_metadata* is false (the default), only the file data is
1552+
guaranteed to be copied. Set *preserve_metadata* to true to ensure that the
1553+
file mode (permissions), flags, last access and modification times, and
1554+
extended attributes are copied where supported. This argument has no effect
1555+
on Windows, where metadata is always preserved when copying.
15561556

15571557
.. versionadded:: 3.14
15581558

Lib/pathlib/_abc.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,32 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
781781
"""
782782
raise UnsupportedOperation(self._unsupported_msg('mkdir()'))
783783

784-
def copy(self, target, follow_symlinks=True):
784+
# Metadata keys supported by this path type.
785+
_readable_metadata = _writable_metadata = frozenset()
786+
787+
def _read_metadata(self, keys=None, *, follow_symlinks=True):
788+
"""
789+
Returns path metadata as a dict with string keys.
790+
"""
791+
raise UnsupportedOperation(self._unsupported_msg('_read_metadata()'))
792+
793+
def _write_metadata(self, metadata, *, follow_symlinks=True):
794+
"""
795+
Sets path metadata from the given dict with string keys.
796+
"""
797+
raise UnsupportedOperation(self._unsupported_msg('_write_metadata()'))
798+
799+
def _copy_metadata(self, target, *, follow_symlinks=True):
800+
"""
801+
Copies metadata (permissions, timestamps, etc) from this path to target.
802+
"""
803+
# Metadata types supported by both source and target.
804+
keys = self._readable_metadata & target._writable_metadata
805+
if keys:
806+
metadata = self._read_metadata(keys, follow_symlinks=follow_symlinks)
807+
target._write_metadata(metadata, follow_symlinks=follow_symlinks)
808+
809+
def copy(self, target, *, follow_symlinks=True, preserve_metadata=False):
785810
"""
786811
Copy the contents of this file to the given target. If this file is a
787812
symlink and follow_symlinks is false, a symlink will be created at the
@@ -793,6 +818,8 @@ def copy(self, target, follow_symlinks=True):
793818
raise OSError(f"{self!r} and {target!r} are the same file")
794819
if not follow_symlinks and self.is_symlink():
795820
target.symlink_to(self.readlink())
821+
if preserve_metadata:
822+
self._copy_metadata(target, follow_symlinks=False)
796823
return
797824
with self.open('rb') as source_f:
798825
try:
@@ -805,6 +832,8 @@ def copy(self, target, follow_symlinks=True):
805832
f'Directory does not exist: {target}') from e
806833
else:
807834
raise
835+
if preserve_metadata:
836+
self._copy_metadata(target)
808837

809838
def copytree(self, target, *, follow_symlinks=True, dirs_exist_ok=False,
810839
ignore=None, on_error=None):

Lib/pathlib/_local.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
except ImportError:
1818
grp = None
1919

20-
from ._os import UnsupportedOperation, copyfile
20+
from ._os import (UnsupportedOperation, copyfile, file_metadata_keys,
21+
read_file_metadata, write_file_metadata)
2122
from ._abc import PurePathBase, PathBase
2223

2324

@@ -781,8 +782,12 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
781782
if not exist_ok or not self.is_dir():
782783
raise
783784

785+
_readable_metadata = _writable_metadata = file_metadata_keys
786+
_read_metadata = read_file_metadata
787+
_write_metadata = write_file_metadata
788+
784789
if copyfile:
785-
def copy(self, target, follow_symlinks=True):
790+
def copy(self, target, *, follow_symlinks=True, preserve_metadata=False):
786791
"""
787792
Copy the contents of this file to the given target. If this file is a
788793
symlink and follow_symlinks is false, a symlink will be created at the
@@ -799,7 +804,8 @@ def copy(self, target, follow_symlinks=True):
799804
return
800805
except UnsupportedOperation:
801806
pass # Fall through to generic code.
802-
PathBase.copy(self, target, follow_symlinks=follow_symlinks)
807+
PathBase.copy(self, target, follow_symlinks=follow_symlinks,
808+
preserve_metadata=preserve_metadata)
803809

804810
def chmod(self, mode, *, follow_symlinks=True):
805811
"""

Lib/pathlib/_os.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Low-level OS functionality wrappers used by pathlib.
33
"""
44

5-
from errno import EBADF, EOPNOTSUPP, ETXTBSY, EXDEV
5+
from errno import *
66
import os
77
import stat
88
import sys
@@ -178,3 +178,100 @@ def copyfileobj(source_f, target_f):
178178
write_target = target_f.write
179179
while buf := read_source(1024 * 1024):
180180
write_target(buf)
181+
182+
183+
# Kinds of metadata supported by the operating system.
184+
file_metadata_keys = {'mode', 'times_ns'}
185+
if hasattr(os.stat_result, 'st_flags'):
186+
file_metadata_keys.add('flags')
187+
if hasattr(os, 'listxattr'):
188+
file_metadata_keys.add('xattrs')
189+
file_metadata_keys = frozenset(file_metadata_keys)
190+
191+
192+
def read_file_metadata(path, keys=None, *, follow_symlinks=True):
193+
"""
194+
Returns local path metadata as a dict with string keys.
195+
"""
196+
if keys is None:
197+
keys = file_metadata_keys
198+
assert keys.issubset(file_metadata_keys)
199+
result = {}
200+
for key in keys:
201+
if key == 'xattrs':
202+
try:
203+
result['xattrs'] = [
204+
(attr, os.getxattr(path, attr, follow_symlinks=follow_symlinks))
205+
for attr in os.listxattr(path, follow_symlinks=follow_symlinks)]
206+
except OSError as err:
207+
if err.errno not in (EPERM, ENOTSUP, ENODATA, EINVAL, EACCES):
208+
raise
209+
continue
210+
st = os.stat(path, follow_symlinks=follow_symlinks)
211+
if key == 'mode':
212+
result['mode'] = stat.S_IMODE(st.st_mode)
213+
elif key == 'times_ns':
214+
result['times_ns'] = st.st_atime_ns, st.st_mtime_ns
215+
elif key == 'flags':
216+
result['flags'] = st.st_flags
217+
return result
218+
219+
220+
def write_file_metadata(path, metadata, *, follow_symlinks=True):
221+
"""
222+
Sets local path metadata from the given dict with string keys.
223+
"""
224+
assert frozenset(metadata.keys()).issubset(file_metadata_keys)
225+
226+
def _nop(*args, ns=None, follow_symlinks=None):
227+
pass
228+
229+
if follow_symlinks:
230+
# use the real function if it exists
231+
def lookup(name):
232+
return getattr(os, name, _nop)
233+
else:
234+
# use the real function only if it exists
235+
# *and* it supports follow_symlinks
236+
def lookup(name):
237+
fn = getattr(os, name, _nop)
238+
if fn in os.supports_follow_symlinks:
239+
return fn
240+
return _nop
241+
242+
times_ns = metadata.get('times_ns')
243+
if times_ns is not None:
244+
lookup("utime")(path, ns=times_ns, follow_symlinks=follow_symlinks)
245+
# We must copy extended attributes before the file is (potentially)
246+
# chmod()'ed read-only, otherwise setxattr() will error with -EACCES.
247+
xattrs = metadata.get('xattrs')
248+
if xattrs is not None:
249+
for attr, value in xattrs:
250+
try:
251+
os.setxattr(path, attr, value, follow_symlinks=follow_symlinks)
252+
except OSError as e:
253+
if e.errno not in (EPERM, ENOTSUP, ENODATA, EINVAL, EACCES):
254+
raise
255+
mode = metadata.get('mode')
256+
if mode is not None:
257+
try:
258+
lookup("chmod")(path, mode, follow_symlinks=follow_symlinks)
259+
except NotImplementedError:
260+
# if we got a NotImplementedError, it's because
261+
# * follow_symlinks=False,
262+
# * lchown() is unavailable, and
263+
# * either
264+
# * fchownat() is unavailable or
265+
# * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW.
266+
# (it returned ENOSUP.)
267+
# therefore we're out of options--we simply cannot chown the
268+
# symlink. give up, suppress the error.
269+
# (which is what shutil always did in this circumstance.)
270+
pass
271+
flags = metadata.get('flags')
272+
if flags is not None:
273+
try:
274+
lookup("chflags")(path, flags, follow_symlinks=follow_symlinks)
275+
except OSError as why:
276+
if why.errno not in (EOPNOTSUPP, ENOTSUP):
277+
raise

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,50 @@ def test_open_unbuffered(self):
653653
self.assertIsInstance(f, io.RawIOBase)
654654
self.assertEqual(f.read().strip(), b"this is file A")
655655

656+
def test_copy_file_preserve_metadata(self):
657+
base = self.cls(self.base)
658+
source = base / 'fileA'
659+
if hasattr(os, 'setxattr'):
660+
os.setxattr(source, b'user.foo', b'42')
661+
if hasattr(os, 'chmod'):
662+
os.chmod(source, stat.S_IRWXU | stat.S_IRWXO)
663+
if hasattr(os, 'chflags') and hasattr(stat, 'UF_NODUMP'):
664+
os.chflags(source, stat.UF_NODUMP)
665+
source_st = source.stat()
666+
target = base / 'copyA'
667+
source.copy(target, preserve_metadata=True)
668+
self.assertTrue(target.exists())
669+
self.assertEqual(source.read_text(), target.read_text())
670+
target_st = target.stat()
671+
self.assertLessEqual(source_st.st_atime, target_st.st_atime)
672+
self.assertLessEqual(source_st.st_mtime, target_st.st_mtime)
673+
if hasattr(os, 'getxattr'):
674+
self.assertEqual(os.getxattr(target, b'user.foo'), b'42')
675+
self.assertEqual(source_st.st_mode, target_st.st_mode)
676+
if hasattr(source_st, 'st_flags'):
677+
self.assertEqual(source_st.st_flags, target_st.st_flags)
678+
679+
@needs_symlinks
680+
def test_copy_link_preserve_metadata(self):
681+
base = self.cls(self.base)
682+
source = base / 'linkA'
683+
if hasattr(os, 'lchmod'):
684+
os.lchmod(source, stat.S_IRWXU | stat.S_IRWXO)
685+
if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'):
686+
os.lchflags(source, stat.UF_NODUMP)
687+
source_st = source.lstat()
688+
target = base / 'copyA'
689+
source.copy(target, follow_symlinks=False, preserve_metadata=True)
690+
self.assertTrue(target.exists())
691+
self.assertTrue(target.is_symlink())
692+
self.assertEqual(source.readlink(), target.readlink())
693+
target_st = target.lstat()
694+
self.assertLessEqual(source_st.st_atime, target_st.st_atime)
695+
self.assertLessEqual(source_st.st_mtime, target_st.st_mtime)
696+
self.assertEqual(source_st.st_mode, target_st.st_mode)
697+
if hasattr(source_st, 'st_flags'):
698+
self.assertEqual(source_st.st_flags, target_st.st_flags)
699+
656700
@unittest.skipIf(sys.platform == "win32" or sys.platform == "wasi", "directories are always readable on Windows and WASI")
657701
@unittest.skipIf(root_in_posix, "test fails with root privilege")
658702
def test_copytree_no_read_permission(self):

0 commit comments

Comments
 (0)