Merge pull request #832 from trigger337/fix/dds-png-stage-cleanup

dds_png: restore write mode before unlink on replace failure in main (DDS-PNG-STAGE-CLEANUP)
This commit is contained in:
MoonEyes
2026-11-29 19:36:37 +01:00
committed by GitHub
2 changed files with 25 additions and 1 deletions

View File

@ -1300,6 +1300,26 @@ class HalfFloatTests(unittest.TestCase):
raw = zlib.decompress(b"".join(p for t, p, _ in chunks if t == b"IDAT"))
self.assertEqual(raw[1:], bytes((255, 0, 128, 255, 0, 255, 64, 128)))
def test_cli_cleanup_restores_write_mode_on_replace_failure(self):
payload = halves(0x3C00, 0x0000, 0x3800, 0x3C00, 0x7E00, 0x7C00, 0x3400, 0x3800)
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, "in.dds")
dst = os.path.join(tmp, "out.png")
with open(src, "wb") as f:
f.write(dds_buffer(2, 1, payload, pitch=16, dxgi=10, **HALF_FLOAT))
chmod_calls = []
real_chmod = os.chmod
def track_chmod(p, mode, **kwargs):
chmod_calls.append((p, mode))
return real_chmod(p, mode, **kwargs)
with mock.patch.object(dds.os, "replace", side_effect=OSError("replace failed")), \
mock.patch.object(dds.os, "chmod", side_effect=track_chmod):
ret = dds.main([src, dst])
self.assertEqual(ret, 1)
self.assertTrue(any(mode == 0o600 for p, mode in chmod_calls))
if __name__ == "__main__":
unittest.main()

View File

@ -1746,7 +1746,11 @@ def main(argv: list[str] | None = None) -> int:
f.write(png)
if os.path.isfile(output):
shutil.copymode(output, pending)
os.replace(pending, output)
try:
os.replace(pending, output)
except BaseException:
os.chmod(pending, 0o600)
raise
except (DdsError, OSError) as exc:
print(f"ERROR: {exc}", file=sys.stderr)
return 1