Skip to content

The agent that grows with you

fix(pm): confine the read-only retry to Windows unlink/rmdir failures

teknium1eb7e8620324b
diff --git a/pm/filesystem.py b/pm/filesystem.py
index 1c687b6b0c5..f8469d6b5a2 100644
--- a/pm/filesystem.py
+++ b/pm/filesystem.py
@@ -85,14 +85,20 @@ def durable_write_bytes(path: Path, data: bytes) -> None:
def remove_tree(path: Path) -> None:
- """``shutil.rmtree`` that clears a read-only bit and retries the failing unlink.
+ """``shutil.rmtree`` that clears a Windows read-only attribute and retries that one unlink.
Windows refuses to unlink read-only files (PortableGit ships ``etc/hosts`` read-only), and no
- retry clears that bit. Lives here, not in ``hermes_cli``: PM installs run before the rest of
- the tree exists (the Docker toolchain stage copies only ``pm/``).
+ retry clears that bit. Every other failure, and every POSIX failure, raises as plain rmtree
+ would: a POSIX permission error is about the parent directory, and chmodding the path there
+ would follow a symlink out of the tree. Lives here, not in ``hermes_cli``: PM installs run
+ before the rest of the tree exists (the Docker toolchain stage copies only ``pm/``).
"""
- def _retry_writable(func, failed, _exc):
- os.chmod(failed, stat.S_IWRITE)
+ def _retry_writable(func, failed, error):
+ error = error[1] if isinstance(error, tuple) else error
+ if os.name != "nt" or func not in (os.unlink, os.rmdir) or not isinstance(error, PermissionError):
+ raise error
+ # Windows >= 3.13 chmod follows links; the attribute to clear is the link's own.
+ os.chmod(failed, stat.S_IWRITE, **({"follow_symlinks": False} if os.chmod in os.supports_follow_symlinks else {}))
func(failed)
handler = {"onexc": _retry_writable} if sys.version_info >= (3, 12) else {"onerror": _retry_writable}
diff --git a/tests/pm/test_pm_core.py b/tests/pm/test_pm_core.py
index 965bd0c3081..496ed4803d1 100644
--- a/tests/pm/test_pm_core.py
+++ b/tests/pm/test_pm_core.py
@@ -5,7 +5,9 @@ stores."""
from __future__ import annotations
+import os
import shutil
+import stat
import threading
from pathlib import Path
@@ -511,6 +513,36 @@ def test_held_replaced_entry_does_not_fail_reinstall_and_is_reclaimed(pm_env, mo
assert (runtime / entry_name / "bin/faketool").is_file()
[email protected](hasattr(os, "geteuid") and os.geteuid() == 0, reason="root ignores directory modes")
+def test_unremovable_replaced_entry_on_posix_is_set_aside_without_touching_outside(pm_env, tmp_path):
+ """A POSIX permission failure inside the replaced entry (an unreadable dir, an
+ unwritable dir holding a symlink) sets the tree aside as before; cleanup never
+ raises past that and never chmods the symlink's target outside the tree."""
+ from pm.install import ensure
+
+ _, runtime, *_ = pm_env
+ ensure("faketool", base_env={})
+ entry = runtime / Facts(runtime / "facts.json").get("faketool")["entry"]
+ outside = tmp_path / "outside.txt"
+ outside.write_text("keep", encoding="utf-8")
+ outside.chmod(0o644)
+ (entry / "sealed").mkdir()
+ (entry / "pinned").mkdir()
+ (entry / "pinned" / "link").symlink_to(outside)
+ (entry / "sealed").chmod(0)
+ (entry / "pinned").chmod(0o500)
+ try:
+ (entry / "bin/faketool").unlink()
+ ensure("faketool", base_env={})
+ assert (entry / "bin/faketool").read_bytes() == b"#!x"
+ assert stat.S_IMODE(outside.stat().st_mode) == 0o644
+ assert list(runtime.glob(".reclaim-*"))
+ finally:
+ for held in [*runtime.glob(".reclaim-*/*"), *runtime.glob(".previous-*/*")]:
+ held.chmod(0o700)
+
+
def test_held_displacement_does_not_fail_restore_and_gc_spares_interrupted_one(pm_env, monkeypatch):
"""A restore that succeeded leaves the displaced tree as garbage; a held one
must not fail the install. An interrupted restore's displacement is never