8000 gh-81340: Use `copy_file_range` in `shutil.copyfile` copy functions by illia-v · Pull Request #93152 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-81340: Use copy_file_range in shutil.copyfile copy functions #93152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 3, 2025
Prev Previous commit
Next Next commit
Drop the allow_reflink argument
  • Loading branch information
illia-v committed Jun 5, 2024
commit 41d48d9cdfcee263c980405cf67d542d47c7f6a4
36 changes: 12 additions & 24 deletions Doc/library/shutil.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Directory and files operations
be copied.


.. function:: copyfile(src, dst, *, follow_symlinks=True, allow_reflink=True)
.. function:: copyfile(src, dst, *, follow_symlinks=True)

Copy the contents (no metadata) of the file named *src* to a file named
*dst* and return *dst* in the most efficient way possible.
Expand All @@ -67,10 +67,6 @@ Directory and files operations
a new symbolic link will be created instead of copying the
file *src* points to.

*allow_reflink* enables copy-on-write on supported Linux filesystems (e.g.,
btrfs and XFS). :func:`os.copy_file_range` is used internally when
*allow_reflink* is true.

.. audit-event:: shutil.copyfile src,dst shutil.copyfile

.. versionchanged:: 3.3
Expand All @@ -87,9 +83,9 @@ Directory and files operations
copy the file more efficiently. See
:ref:`shutil-platform-dependent-efficient-copy-operations` section.

.. versionchanged:: 3.12
Added *allow_reflink* argument. Copy-on-write is enabled by default on
supported Linux filesystems.
.. versionchanged:: 3.14
Copy-on-write or server-side copy may be used internally on supported
filesystems via :func:`os.copy_file_range`.

.. exception:: SameFileError

Expand Down Expand Up @@ -163,7 +159,7 @@ Directory and files operations
.. versionchanged:: 3.3
Added *follow_symlinks* argument and support for Linux extended attributes.

.. function:: copy(src, dst, *, follow_symlinks=True, allow_reflink=True)
.. function:: copy(src, dst, *, follow_symlinks=True)

Copies the file *src* to the file or directory *dst*. *src* and *dst*
should be :term:`path-like objects <path-like object>` or strings. If
Expand All @@ -176,10 +172,6 @@ Directory and files operations
is true and *src* is a symbolic link, *dst* will be a copy of
the file *src* refers to.

*allow_reflink* enables copy-on-write on supported Linux filesystems (e.g.,
btrfs and XFS). :func:`os.copy_file_range` is used internally when
*allow_reflink* is true.

:func:`~shutil.copy` copies the file data and the file's permission
mode (see :func:`os.chmod`). Other metadata, like the
file's creation and modification times, is not preserved.
Expand All @@ -199,11 +191,11 @@ Directory and files operations
copy the file more efficiently. See
:ref:`shutil-platform-dependent-efficient-copy-operations` section.

.. versionchanged:: 3.12
Added *allow_reflink* argument. Copy-on-write is enabled by default on
supported Linux filesystems.
.. versionchanged:: 3.14
Copy-on-write or server-side copy may be used internally on supported
filesystems via :func:`os.copy_file_range`.

.. function:: copy2(src, dst, *, follow_symlinks=True, allow_reflink=True)
.. function:: copy2(src, dst, *, follow_symlinks=True)

Identical to :func:`~shutil.copy` except that :func:`copy2`
also attempts to preserve file metadata.
Expand All @@ -217,10 +209,6 @@ Directory and files operations
it can; :func:`copy2` never raises an exception because it
cannot preserve file metadata.

*allow_reflink* enables copy-on-write on supported Linux filesystems (e.g.,
btrfs and XFS). :func:`os.copy_file_range` is used internally when
*allow_reflink* is true.

:func:`copy2` uses :func:`copystat` to copy the file metadata.
Please see :func:`copystat` for more information
about platform support for modifying symbolic link metadata.
Expand All @@ -239,9 +227,9 @@ Directory and files operations
copy the file more efficiently. See
:ref:`shutil-platform-dependent-efficient-copy-operations` section.

.. versionchanged:: 3.12
Added *allow_reflink* argument. Copy-on-write is enabled by default on
supported Linux filesystems.
.. versionchanged:: 3.14
Copy-on-write or server-side copy may be used internally on supported
filesystems via :func:`os.copy_file_range`.

.. function:: ignore_patterns(*patterns)

Expand Down
12 changes: 6 additions & 6 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def _stat(fn):
def _islink(fn):
return fn.is_symlink() if isinstance(fn, os.DirEntry) else os.path.islink(fn)

def copyfile(src, dst, *, follow_symlinks=True, allow_reflink=True):
def copyfile(src, dst, *, follow_symlinks=True):
"""Copy data from src to dst in the most efficient way possible.

If follow_symlinks is not set and src is a symbolic link, a new
Expand Down Expand Up @@ -318,7 +318,7 @@ def copyfile(src, dst, *, follow_symlinks=True, allow_reflink=True):
# Linux
elif _USE_CP_SENDFILE or _USE_CP_COPY_FILE_RANGE:
# reflink may be implicit in copy_file_range.
if _USE_CP_COPY_FILE_RANGE and allow_reflink:
if _USE_CP_COPY_FILE_RANGE:
try:
_fastcopy_copy_file_range(fsrc, fdst)
return dst
Expand Down Expand Up @@ -467,7 +467,7 @@ def lookup(name):
else:
raise

def copy(src, dst, *, follow_symlinks=True, allow_reflink=True):
def copy(src, dst, *, follow_symlinks=True):
"""Copy data and mode bits ("cp src dst"). Return the file's destination.

The destination may be a directory.
Expand All @@ -481,11 +481,11 @@ def copy(src, dst, *, follow_symlinks=True, allow_reflink=True):
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst, follow_symlinks=follow_symlinks, allow_reflink=allow_reflink)
copyfile(src, dst, follow_symlinks=follow_symlinks)
copymode(src, dst, follow_symlinks=follow_symlinks)
return dst

def copy2(src, dst, *, follow_symlinks=True, allow_reflink=True):
def copy2(src, dst, *, follow_symlinks=True):
"""Copy data and metadata. Return the file's destination.

Metadata is copied with copystat(). Please see the copystat function
Expand Down Expand Up @@ -521,7 +521,7 @@ def copy2(src, dst, *, follow_symlinks=True, allow_reflink=True):
else:
raise

copyfile(src, dst, follow_symlinks=follow_symlinks, allow_reflink=allow_reflink)
copyfile(src, dst, follow_symlinks=follow_symlinks)
copystat(src, dst, follow_symlinks=follow_symlinks)
return dst

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Use :func:`os.copy_file_range` in :func:`shutil.copy`, :func:`shutil.copy2`,
and :func:`shutil.copyfile` functions by default. An underlying Linux system
call gives filesystems an opportunity to implement the use of copy-on-write
(in case of btrfs and XFS) or server-side copy (in the case of NFS.) The
functions have a new *allow_reflink* argument to control the functionality.
(in case of btrfs and XFS) or server-side copy (in the case of NFS.)
Patch by Illia Volochii.
0