Skip to content

Commit 2bfdc02

Browse files
authored
🐛 fix(path): preserve link targets on delete errors (#3427)
1 parent 6196d35 commit 2bfdc02

4 files changed

Lines changed: 149 additions & 14 deletions

File tree

‎docs/changelog/3427.bugfix.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Preserve file permissions when deletion fails on POSIX. On Windows, clear read-only attributes through a handle that
2+
opens the entry itself and prevents replacement, preserving other attributes and rejecting reparse points.

‎src/virtualenv/util/path/_sync.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import shutil
66
import sys
77
from pathlib import Path
8-
from stat import S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR
8+
from stat import S_IXGRP, S_IXOTH, S_IXUSR
99
from typing import TYPE_CHECKING, Final
1010

11+
if os.name == "nt":
12+
from ._win import clear_readonly
13+
1114
if TYPE_CHECKING:
1215
from collections.abc import Callable
1316
from types import TracebackType
@@ -74,11 +77,10 @@ def onerror(func: Callable[[str], object], path: str, exc_info: _ExcInfo) -> Non
7477
exc = exc_info[1] if isinstance(exc_info, tuple) else exc_info
7578
if isinstance(exc, FileNotFoundError):
7679
return
77-
# rmtree also reports os.open/os.close/os.scandir/os.lstat; retrying one of those with a lone path raises
78-
# TypeError over the real error, or deletes nothing while telling rmtree we handled the failure
79-
if func not in {os.unlink, os.rmdir} or os.access(path, os.W_OK):
80-
raise # ruff:ignore[misplaced-bare-raise]
81-
os.chmod(path, os.lstat(path).st_mode | S_IWUSR)
80+
# POSIX deletion depends on the parent directory, so changing the entry's mode cannot help.
81+
if os.name != "nt" or func not in {os.unlink, os.rmdir} or os.path.islink(path) or os.access(path, os.W_OK):
82+
raise exc
83+
clear_readonly(path)
8284
func(path)
8385

8486
if sys.version_info >= (3, 12):

‎src/virtualenv/util/path/_win.py‎

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from __future__ import annotations
22

3+
import os
4+
from typing import Final
5+
6+
if os.name == "nt":
7+
import ctypes
8+
from ctypes import wintypes
9+
310

411
def get_short_path_name(long_name: str) -> str:
512
"""Gets the short path name of a given long path - http://stackoverflow.com/a/23598461/200291."""
6-
import ctypes # ruff:ignore[import-outside-top-level]
7-
from ctypes import wintypes # ruff:ignore[import-outside-top-level]
8-
913
GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW # ruff:ignore[non-lowercase-variable-in-function] # ty: ignore[unresolved-attribute]
1014
GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
1115
GetShortPathNameW.restype = wintypes.DWORD
@@ -18,6 +22,57 @@ def get_short_path_name(long_name: str) -> str:
1822
output_buf_size = needed
1923

2024

21-
__all__ = [
22-
"get_short_path_name",
23-
]
25+
if os.name == "nt":
26+
27+
def clear_readonly(path: str) -> None:
28+
kernel: Final = ctypes.WinDLL("kernel32", use_last_error=True)
29+
create: Final = kernel.CreateFileW
30+
create.argtypes = [
31+
wintypes.LPCWSTR,
32+
wintypes.DWORD,
33+
wintypes.DWORD,
34+
wintypes.LPVOID,
35+
wintypes.DWORD,
36+
wintypes.DWORD,
37+
wintypes.HANDLE,
38+
]
39+
create.restype = wintypes.HANDLE
40+
get_info: Final = kernel.GetFileInformationByHandleEx
41+
get_info.argtypes = [wintypes.HANDLE, ctypes.c_int, wintypes.LPVOID, wintypes.DWORD]
42+
get_info.restype = wintypes.BOOL
43+
set_info: Final = kernel.SetFileInformationByHandle
44+
set_info.argtypes = get_info.argtypes
45+
set_info.restype = wintypes.BOOL
46+
close: Final = kernel.CloseHandle
47+
close.argtypes = [wintypes.HANDLE]
48+
close.restype = wintypes.BOOL
49+
# Open the entry itself and deny replacement while changing its read-only attribute.
50+
handle: Final = create(path, 0x180, 0x3, None, 3, 0x02200000, None)
51+
if handle == wintypes.HANDLE(-1).value:
52+
raise ctypes.WinError(ctypes.get_last_error())
53+
try:
54+
info: Final = _FileBasicInfo()
55+
if not get_info(handle, 0, ctypes.byref(info), ctypes.sizeof(info)):
56+
raise ctypes.WinError(ctypes.get_last_error())
57+
if info.attributes & 0x400:
58+
msg: Final = f"refusing to change attributes of a reparse point: {path}"
59+
raise OSError(msg)
60+
info.attributes = (info.attributes & ~0x1) or 0x80
61+
if not set_info(handle, 0, ctypes.byref(info), ctypes.sizeof(info)):
62+
raise ctypes.WinError(ctypes.get_last_error())
63+
finally:
64+
close(handle)
65+
66+
class _FileBasicInfo(ctypes.Structure):
67+
_fields_: Final = [
68+
("creation", ctypes.c_longlong),
69+
("access", ctypes.c_longlong),
70+
("write", ctypes.c_longlong),
71+
("change", ctypes.c_longlong),
72+
("attributes", wintypes.DWORD),
73+
]
74+
75+
76+
__all__ = ["get_short_path_name"]
77+
if os.name == "nt":
78+
__all__ += ["clear_readonly"]

‎tests/unit/test_util.py‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from __future__ import annotations
22

33
import concurrent.futures
4+
import ctypes
45
import os
56
import sys
67
import traceback
78
import zipfile
9+
from errno import EACCES
810
from stat import S_IEXEC, S_IREAD, S_IRGRP, S_IRWXU, S_IWUSR
911
from typing import TYPE_CHECKING, Final
1012

1113
import pytest
1214

15+
from virtualenv import __version__
1316
from virtualenv.app_data import (
1417
AppDataDiskFolder,
1518
TempAppData,
@@ -23,6 +26,11 @@
2326
from virtualenv.util.path import copy, safe_delete, symlink
2427
from virtualenv.util.subprocess import run_cmd
2528

29+
if os.name == "nt":
30+
from ctypes import wintypes
31+
32+
from virtualenv.util.path._win import clear_readonly
33+
2634
if TYPE_CHECKING:
2735
from collections.abc import Callable
2836
from pathlib import Path
@@ -157,6 +165,74 @@ def test_copy_replaces_contents(tmp_path: Path, directory: bool) -> None:
157165
assert (destination / "entry" if directory else destination).read_bytes() == b"new"
158166

159167

168+
@pytest.mark.skipif(sys.platform != "linux", reason="minimal Linux builds can omit ctypes")
169+
def test_command_line_import_without_ctypes() -> None:
170+
result: Final = run_cmd([
171+
sys.executable,
172+
"-c",
173+
(
174+
"import runpy, sys; sys.modules['ctypes'] = None; sys.argv = ['virtualenv', '--version']; "
175+
"runpy.run_module('virtualenv', run_name='__main__')"
176+
),
177+
])
178+
assert (result[0], __version__ in result[1], result[2]) == (0, True, "")
179+
180+
181+
@pytest.mark.skipif(os.name != "nt", reason="Windows file attributes")
182+
@pytest.mark.parametrize(
183+
"operation",
184+
[
185+
pytest.param("CreateFileW", id="open"),
186+
pytest.param("GetFileInformationByHandleEx", id="read"),
187+
pytest.param("SetFileInformationByHandle", id="write"),
188+
],
189+
)
190+
def test_safe_delete_reports_attribute_errors(tmp_path: Path, mocker: MockerFixture, operation: str) -> None:
191+
folder: Final = tmp_path / "folder"
192+
folder.mkdir()
193+
source: Final = folder / "read-only"
194+
source.touch()
195+
source.chmod(S_IREAD)
196+
kernel: Final = ctypes.WinDLL("kernel32", use_last_error=True)
197+
mocker.patch.object(
198+
kernel, operation, autospec=True, return_value=wintypes.HANDLE(-1).value if operation == "CreateFileW" else 0
199+
)
200+
mocker.patch("ctypes.WinDLL", autospec=True, return_value=kernel)
201+
mocker.patch("ctypes.get_last_error", autospec=True, return_value=5)
202+
with pytest.raises(PermissionError, match="WinError 5"):
203+
safe_delete(folder)
204+
assert source.stat().st_mode & S_IWUSR == 0
205+
source.chmod(S_IREAD | S_IWUSR)
206+
207+
208+
@pytest.mark.skipif(os.name != "nt" or not fs_supports_symlink(), reason="Windows file links")
209+
def test_clear_readonly_refuses_reparse_points(tmp_path: Path) -> None:
210+
source: Final = tmp_path / "source"
211+
source.touch()
212+
link: Final = tmp_path / "link"
213+
link.symlink_to(source)
214+
with pytest.raises(OSError, match="refusing to change attributes"):
215+
clear_readonly(str(link))
216+
217+
218+
@pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable")
219+
def test_safe_delete_does_not_chmod_link_target(tmp_path: Path, mocker: MockerFixture) -> None:
220+
source: Final = tmp_path / "read-only"
221+
source.write_text("unchanged", encoding="utf-8")
222+
source.chmod(S_IREAD)
223+
mode: Final = source.stat().st_mode
224+
folder: Final = tmp_path / "folder"
225+
folder.mkdir()
226+
(folder / "link").symlink_to(source)
227+
unlink: Final = mocker.patch("os.unlink", autospec=True, side_effect=PermissionError(EACCES, "entry is busy"))
228+
try:
229+
with pytest.raises(PermissionError, match="entry is busy"):
230+
safe_delete(folder)
231+
finally:
232+
mocker.stop(unlink)
233+
assert source.stat().st_mode == mode
234+
235+
160236
def test_run_fail(tmp_path) -> None:
161237
code, out, err = run_cmd([str(tmp_path)])
162238
assert err
@@ -211,7 +287,7 @@ def test_safe_delete_raises_the_original_error_not_a_retry_failure(tmp_path: Pat
211287

212288

213289
@pytest.mark.skipif(sys.platform == "win32", reason="Windows deletion ignores directory permissions")
214-
def test_safe_delete_keeps_the_other_mode_bits_when_clearing_read_only(tmp_path: Path) -> None:
290+
def test_safe_delete_preserves_file_mode_when_parent_denies_deletion(tmp_path: Path) -> None:
215291
if hasattr(os, "geteuid") and os.geteuid() == 0:
216292
pytest.skip("root bypasses the permission bits that make the delete fail")
217293
target = tmp_path / "target"
@@ -224,7 +300,7 @@ def test_safe_delete_keeps_the_other_mode_bits_when_clearing_read_only(tmp_path:
224300
try:
225301
with pytest.raises(PermissionError):
226302
safe_delete(target)
227-
assert blocked.stat().st_mode & 0o777 == S_IREAD | S_IRGRP | S_IWUSR
303+
assert blocked.stat().st_mode & 0o777 == S_IREAD | S_IRGRP
228304
finally:
229305
target.chmod(S_IRWXU)
230306

0 commit comments

Comments
 (0)