diff --git a/tests/test_rpf9_names.py b/tests/test_rpf9_names.py index c8402aa5e..0cee316b5 100644 --- a/tests/test_rpf9_names.py +++ b/tests/test_rpf9_names.py @@ -388,6 +388,36 @@ def test_refusal_names_the_output_path_as_given(self): self.assertIn(f"output {arc} is the input archive", p.stderr) self.assertNotIn(repr(arc), p.stderr) # a repr would quote the path and double every backslash + def test_failed_replace_of_read_only_report_makes_stage_deletable(self): + """A failed replace over a read-only report still cleans the stage: the staged file has the + read-only destination's bits, so the cleanup makes it deletable before deleting, and one + delete failure is swallowed instead of masking the replace error (Windows behavior).""" + with tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "index.json") + with open(out, "w", encoding="utf-8") as f: + f.write('{"prior": true}') + with open(out, "rb") as f: + prior = f.read() + os.chmod(out, 0o444) + self.addCleanup(lambda: os.path.exists(out) and os.chmod(out, 0o600)) + made_writable = [] + real_chmod = os.chmod + + def chmod(staged_path, mode, *args, **kwargs): + if isinstance(staged_path, str) and ".rpf9_names-" in staged_path and mode == 0o600: + made_writable.append(staged_path) + return real_chmod(staged_path, mode, *args, **kwargs) + + with self.assertRaises(OSError) as raised: + with mock.patch.object(rpf9_names.os, "replace", side_effect=OSError("replace denied")), \ + mock.patch.object(rpf9_names.os, "chmod", chmod): + rpf9_names.publish_report(out, [], {"files": 0}) + self.assertIn("replace denied", str(raised.exception)) + self.assertEqual(len(made_writable), 1) # the stage was made deletable + with open(out, "rb") as f: + self.assertEqual(f.read(), prior) # the prior report is untouched + self.assertEqual([n for n in os.listdir(tmp) if n.startswith(".rpf9_names-")], []) + def test_write_error_names_the_report_path_as_given(self): with tempfile.TemporaryDirectory() as tmp: arc = os.path.join(tmp, "tiny.rpf") diff --git a/tools/rpf9_names.py b/tools/rpf9_names.py index 126ffad29..1ff593f59 100644 --- a/tools/rpf9_names.py +++ b/tools/rpf9_names.py @@ -23,6 +23,7 @@ import tempfile import time import zlib +from contextlib import suppress sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) @@ -199,19 +200,28 @@ def publish_report(path, rows, summary): """Write the report at ``path`` atomically, creating missing parent directories. The full JSON is staged next to the destination so an interrupted run leaves only the old report or no report, - never a truncated one. Raised OSError carries a clear description of the staging/destination problem for - ``main`` to turn into a named nonzero diagnostic. + never a truncated one; the stage is made deletable before the cleanup deletes it, so a read-only report on + Windows cannot strand the stage or mask the error with a cleanup failure. Raised OSError carries a clear + description of the staging/destination problem for ``main`` to turn into a named nonzero diagnostic. """ path = os.path.abspath(path) directory = os.path.dirname(path) os.makedirs(directory, exist_ok=True) - with tempfile.TemporaryDirectory(dir=directory, prefix=".rpf9_names-") as stage_dir: - temp = os.path.join(stage_dir, "report.json") + stage_dir = tempfile.TemporaryDirectory(dir=directory, prefix=".rpf9_names-") + try: + temp = os.path.join(stage_dir.name, "report.json") with open(temp, "w", encoding="utf-8", newline="\n") as f: json.dump({"summary": summary, "files": rows}, f, indent=1) f.write("\n") os.chmod(temp, published_mode(path)) os.replace(temp, path) + finally: + # On a failed replace the staged file carries the destination's bits, which a read-only report + # makes read-only: Windows then refuses the delete and the cleanup error masks the real one. + # Make it deletable first and let one delete failure go, so the caller reports the replace. + with suppress(OSError): + os.chmod(temp, 0o600) + stage_dir.cleanup() def _int_arg(text):