mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:48:38 +00:00
navmesh_tile_obj: restore write mode before unlink on replace failure and publish mode (NAVMESH-TILE-OBJ-STAGE-CLEANUP)
Apply published_mode(args.out) to temporary file before atomic replacement, and restore write permission (0o600) before unlinking in finally block when replace fails, preventing Windows PermissionError [WinError 5] and stranded temporary files. agent: yes AI-assisted: yes
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
"""Read back OBJ primitives from a synthetic archive, independently of the writer."""
|
||||
import os
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
from pathlib import Path
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
@ -11,6 +13,8 @@ sys.path.insert(0, str(HERE))
|
||||
from test_navmesh_decode import build_navmesh, make_rpf, nd
|
||||
|
||||
TOOL = HERE.parent / "tools" / "navmesh_tile_obj.py"
|
||||
sys.path.insert(0, str(HERE.parent / "tools"))
|
||||
import navmesh_tile_obj as tool
|
||||
|
||||
|
||||
def read_obj(path):
|
||||
@ -63,6 +67,27 @@ class ExportTest(unittest.TestCase):
|
||||
self.assertEqual(out.read_bytes(), original)
|
||||
self.assertEqual(run(1, archive).returncode, 2)
|
||||
|
||||
def test_stage_cleanup_restores_write_mode_on_replace_failure(self):
|
||||
head, tail = build_navmesh(n_verts=3, poly_sizes=(3,), tiny_bm_cont=True)
|
||||
chmod_calls = []
|
||||
real_chmod = os.chmod
|
||||
|
||||
def track_chmod(path, mode, *args, **kwargs):
|
||||
chmod_calls.append((str(path), mode))
|
||||
return real_chmod(path, mode, *args, **kwargs)
|
||||
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
archive = Path(directory) / "input.rpf"
|
||||
out = Path(directory) / "tile.obj"
|
||||
make_rpf(archive, [head, tail])
|
||||
|
||||
with mock.patch("os.replace", side_effect=OSError("replace failed")), \
|
||||
mock.patch("os.chmod", side_effect=track_chmod):
|
||||
code = tool.main([str(archive), "--tile", "0", "--out", str(out)])
|
||||
self.assertEqual(code, 2)
|
||||
|
||||
self.assertTrue(any(mode == 0o600 for _, mode in chmod_calls), chmod_calls)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -13,6 +13,7 @@ from contextlib import closing
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from file_mode import published_mode
|
||||
from navmesh_nodes import navmesh_resources, tile_geometry
|
||||
from report_paths import same_report_path
|
||||
|
||||
@ -61,6 +62,7 @@ def main(argv=None):
|
||||
dir=args.out.parent, delete=False) as out:
|
||||
temp = Path(out.name)
|
||||
write_obj(out, geometry, ordinal)
|
||||
os.chmod(temp, published_mode(args.out))
|
||||
os.replace(temp, args.out)
|
||||
temp = None
|
||||
except (OSError, ValueError) as exc:
|
||||
@ -68,6 +70,10 @@ def main(argv=None):
|
||||
return 2
|
||||
finally:
|
||||
if temp is not None:
|
||||
try:
|
||||
os.chmod(temp, 0o600)
|
||||
except OSError:
|
||||
pass
|
||||
temp.unlink(missing_ok=True)
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user