Skip to content

Commit

Permalink
pythonGH-73991: Support copying directory symlinks on older Windows (p…
Browse files Browse the repository at this point in the history
…ython#120807)

Check for `ERROR_INVALID_PARAMETER` when calling `_winapi.CopyFile2()` and
raise `UnsupportedOperation`. In `Path.copy()`, handle this exception and
fall back to the `PathBase.copy()` implementation.
  • Loading branch information
barneygale authored and estyxx committed Jul 17, 2024
1 parent 6b681cc commit 09d6efa
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
5 changes: 0 additions & 5 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1554,11 +1554,6 @@ Copying, renaming and deleting
permissions. After the copy is complete, users may wish to call
:meth:`Path.chmod` to set the permissions of the target file.

.. warning::
On old builds of Windows (before Windows 10 build 19041), this method
raises :exc:`OSError` when a symlink to a directory is encountered and
*follow_symlinks* is false.

.. versionadded:: 3.14


Expand Down
4 changes: 2 additions & 2 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
operating systems.
"""

from ._abc import *
from ._os import *
from ._local import *

__all__ = (_abc.__all__ +
__all__ = (_os.__all__ +
_local.__all__)
11 changes: 1 addition & 10 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@
import posixpath
from glob import _GlobberBase, _no_recurse_symlinks
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
from ._os import copyfileobj


__all__ = ["UnsupportedOperation"]
from ._os import UnsupportedOperation, copyfileobj


@functools.cache
def _is_case_sensitive(parser):
return parser.normcase('Aa') == 'Aa'


class UnsupportedOperation(NotImplementedError):
"""An exception that is raised when an unsupported operation is called on
a path object.
"""
pass


class ParserBase:
"""Base class for path parsers, which do low-level path manipulation.
Expand Down
19 changes: 11 additions & 8 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
except ImportError:
grp = None

from ._abc import UnsupportedOperation, PurePathBase, PathBase
from ._os import copyfile
from ._os import UnsupportedOperation, copyfile
from ._abc import PurePathBase, PathBase


__all__ = [
Expand Down Expand Up @@ -791,12 +791,15 @@ def copy(self, target, follow_symlinks=True):
try:
target = os.fspath(target)
except TypeError:
if isinstance(target, PathBase):
# Target is an instance of PathBase but not os.PathLike.
# Use generic implementation from PathBase.
return PathBase.copy(self, target, follow_symlinks=follow_symlinks)
raise
copyfile(os.fspath(self), target, follow_symlinks)
if not isinstance(target, PathBase):
raise
else:
try:
copyfile(os.fspath(self), target, follow_symlinks)
return
except UnsupportedOperation:
pass # Fall through to generic code.
PathBase.copy(self, target, follow_symlinks=follow_symlinks)

def chmod(self, mode, *, follow_symlinks=True):
"""
Expand Down
27 changes: 24 additions & 3 deletions Lib/pathlib/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
_winapi = None


__all__ = ["UnsupportedOperation"]


class UnsupportedOperation(NotImplementedError):
"""An exception that is raised when an unsupported operation is attempted.
"""
pass


def get_copy_blocksize(infd):
"""Determine blocksize for fastcopying on Linux.
Hopefully the whole file will be copied in a single call.
Expand Down Expand Up @@ -106,18 +115,30 @@ def copyfile(source, target, follow_symlinks):
Copy from one file to another using CopyFile2 (Windows only).
"""
if follow_symlinks:
flags = 0
_winapi.CopyFile2(source, target, 0)
else:
# Use COPY_FILE_COPY_SYMLINK to copy a file symlink.
flags = _winapi.COPY_FILE_COPY_SYMLINK
try:
_winapi.CopyFile2(source, target, flags)
return
except OSError as err:
# Check for ERROR_ACCESS_DENIED
if err.winerror != 5 or not _is_dirlink(source):
if err.winerror == 5 and _is_dirlink(source):
pass
else:
raise

# Add COPY_FILE_DIRECTORY to copy a directory symlink.
flags |= _winapi.COPY_FILE_DIRECTORY
_winapi.CopyFile2(source, target, flags)
try:
_winapi.CopyFile2(source, target, flags)
except OSError as err:
# Check for ERROR_INVALID_PARAMETER
if err.winerror == 87:
raise UnsupportedOperation(err) from None
else:
raise
else:
copyfile = None

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import stat
import unittest

from pathlib._abc import UnsupportedOperation, ParserBase, PurePathBase, PathBase
from pathlib._os import UnsupportedOperation
from pathlib._abc import ParserBase, PurePathBase, PathBase
import posixpath

from test.support import is_wasi
Expand Down

0 comments on commit 09d6efa

Please sign in to comment.