Skip to content

The agent that grows with you

fix(update): dropping a staged tree never changes a hardlinked live artifact's mode (review F74)

Tekniumacf93ace8f6f
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index 050f352784d..1975138e8a8 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -705,14 +705,22 @@ def _drop_path(path: Path) -> None:
shutil.rmtree(path)
except OSError:
# A staged copy keeps its source's modes: a read-only directory refuses to give up its
- # entries. These trees are the update's own copies, so make them writable and retry.
+ # entries. These trees are the update's own copies, so make their directories writable and
+ # retry. Files keep their modes: a grafted artifact is a hardlink to the LIVE file, and POSIX
+ # deletion needs only the parent's write bit. Windows refuses to delete a read-only file, so
+ # there only a file no other link shares has its read-only bit cleared.
rwx = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
os.chmod(path, os.stat(path).st_mode | rwx)
for dirpath, dirnames, files in os.walk(path):
- for name in (*dirnames, *files):
+ for name in dirnames:
child = os.path.join(dirpath, name)
- if not os.path.islink(child): # about to be deleted: only the owner bits matter
- os.chmod(child, os.stat(child).st_mode | (rwx if name in dirnames else stat.S_IWUSR))
+ if not os.path.islink(child):
+ os.chmod(child, os.stat(child).st_mode | rwx)
+ for name in files if sys.platform == "win32" else ():
+ child = os.path.join(dirpath, name)
+ st = os.lstat(child)
+ if st.st_nlink == 1 and not stat.S_ISLNK(st.st_mode):
+ os.chmod(child, st.st_mode | stat.S_IWUSR)
shutil.rmtree(path)
elif path.exists() or path.is_symlink():
path.unlink()
diff --git a/tests/hermes_cli/test_early_recovery.py b/tests/hermes_cli/test_early_recovery.py
index 22217b466f9..6d9e0502503 100644
--- a/tests/hermes_cli/test_early_recovery.py
+++ b/tests/hermes_cli/test_early_recovery.py
@@ -237,3 +237,18 @@ def test_an_unparsed_zip_journal_keeps_itself_and_every_backup(tmp_path, monkeyp
assert journal.is_file()
assert {p.name: p.read_text(encoding="utf-8") for p in root.iterdir() if p != journal and p.suffix != ".lock"} == {
"a.py": "NEW_A", "a.py.hermes-update-old": "OLD_A", "b.py": "OLD_B", "b.py.hermes-update-staging": "NEW_B"}
+
+
[email protected](sys.platform == "win32" or os.geteuid() == 0, reason="POSIX modes; root ignores them")
+def test_dropping_a_staged_tree_never_changes_a_hardlinked_live_files_mode(tmp_path):
+ live = tmp_path / "release"
+ live.mkdir()
+ (live / "Hermes.exe").write_bytes(b"app")
+ staging = tmp_path / "apps.hermes-update-staging" / "release"
+ staging.mkdir(parents=True)
+ os.link(live / "Hermes.exe", staging / "Hermes.exe")
+ (live / "Hermes.exe").chmod(0o555)
+ staging.chmod(0o555) # a read-only dir refuses rmtree: the retry path runs
+ er._drop_path(staging.parent)
+ assert not staging.parent.exists()
+ assert (live / "Hermes.exe").stat().st_mode & 0o777 == 0o555