Skip to content

navmesh_tile_obj: preserve OBJ permissions via published_mode, retry replace, and preflight --out - #7848

Open
narekshaghoyandev wants to merge 1 commit into
ItsNotRexed:mainfrom
narekshaghoyandev:navmesh-tile-obj-permissions-and-preflight
Open

narekshaghoyandev wants to merge 1 commit into
ItsNotRexed:mainfrom
narekshaghoyandev:navmesh-tile-obj-permissions-and-preflight

Conversation

@narekshaghoyandev

Copy link
Copy Markdown
Contributor

Hardens output publishing and --out validation in tools/navmesh_tile_obj.py (without touching __main__ stdout setup from #7589/#7816):

  1. Preserve/publish file permissions (file_mode.published_mode): tempfile.NamedTemporaryFile(..., delete=False) creates staged files with mode 0o600, so os.replace previously published new .obj files with 0o600 (ignoring umask) and clobbered permissions on existing files (e.g., 0o640 -> 0o600). The staged file is now chmod'd with published_mode(args.out) before replacing.
  2. Windows-safe atomic replace & temp cleanup (replace_retry.replace): Replaces via replace_retry.replace to survive transient Windows sharing/AV locks, and restores 0o600 before temp.unlink(missing_ok=True) in finally so failed publishes of read-only destinations still clean up staged files.
  3. Preflight --out before scanning the archive: Refuses --out naming an existing directory or having a non-directory existing ancestor before opening/scanning the RPF archive.

Verification

  • python3 -I tests/test_navmesh_tile_obj.py -v: 3/3 pass (2/2 new tests fail against main).
  • python3 -I scripts/check_no_game_data.py tools/navmesh_tile_obj.py tests/test_navmesh_tile_obj.py: clean.
  • python3 -m compileall -q tools/navmesh_tile_obj.py tests/test_navmesh_tile_obj.py: clean.

Claim: #7804 (comment)
AI-assisted: yes (Gemini)

@TieruYT

TieruYT commented Nov 23, 2026

Copy link
Copy Markdown

Windows, Python 3.13.7, synthetic inputs. Tested eba034a with the same focused tests against the implementation at bf85ff7.

I exercised publication against a real Windows file lock in addition to the two focused repository tests. I kept tile.obj open for reading throughout one export: the command returned 2, preserved its original bytes, and left only input.rpf and tile.obj in the directory. In a second run I released the reader after 45 ms: the command returned 0, published an OBJ beginning with o tile_0, and again left no temporary file. Both focused tests passed on this revision.

AI-assisted verification · GPT-6.2 Astra Medium

@NiHuShu

NiHuShu commented Nov 23, 2026

Copy link
Copy Markdown

Measured actual Linux file modes on CachyOS x86-64, CPython 3.14.10, with the PR’s synthetic three-vertex fixture. I ran python -I tools/navmesh_tile_obj.py <archive> --tile 0 --out <tile.obj> under umask 0027.

Revision New OBJ Replace existing 0604 OBJ Archive scanner calls for invalid output
Base bf85ff703c34223928a6a64822c19e9925e91793 0600 0600 1
Head eba034aa5195d8a019fbdcabe351c1d561826926 0640 0604 0

All four valid exports exited 0 and wrote tile 0. With the output beneath a regular file, I counted calls through a wrapping mock around the real archive scanner. Both revisions exited 2. Head reported that the output parent was not a directory before scanning. python -I tests/test_navmesh_tile_obj.py -v passed 3/3 on head, zero skips.

CPU only; GPU not used. AI-assisted.

@azterixx

Copy link
Copy Markdown

checked: #7848 | with: python -I -m unittest discover -s tests -p test_navmesh_tile_obj.py | on: macOS 27.2.1, Apple M1 Pro, CPython 3.15.2, head eba034a, CPU only | result: pass (3 tests; main@dde82f27e has 1)

@tupacfan04 tupacfan04 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mode/retry logic is sound, but the --out preflight does not check that the destination can actually be written.

tools/navmesh_tile_obj.py:50-59 checks only args.out.is_dir() and that the first existing ancestor is a directory. A read-only destination parent passes preflight; navmesh_resources(args.archive) still scans the whole archive, and the failure only happens later at args.out.parent.mkdir/NamedTemporaryFile (:69-70). Add a writability preflight before the scan (create/remove a probe file in the output directory; avoid os.access alone because root succeeds), plus a POSIX-only 0o555 parent test with a mocked scanner.

The new test at tests/test_navmesh_tile_obj.py:93-117 also does not exercise the read-only cleanup/persistent-retry case it claims:

  • the mode block is skipped on Windows (:103);
  • the staged file is always writable (new mode or 0o640), never 0o444;
  • it patches navmesh_tile_obj.replace_retry directly (:111), bypassing the real retry loop.

Removing the chmod 0600 before unlink (tools/navmesh_tile_obj.py:82-86) would leave this test green. Follow the existing tests/test_saves.py:306-312 pattern: published_mode → 0o444, replace raises PermissionError, assert the staging file is removed; and verify a persistent PermissionError from the real retry path returns 2 without masking the error or leaving a temp.

External tests cover a transient Windows lock and POSIX modes, not a read-only destination. Synthetic only; no game files. AI-assisted (OpenCode).

@gut0to gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exporter now rejects output directories and non-directory ancestors before scanning, writes to a temporary sibling, applies the same published permission policy used by other tools, and replaces with retry semantics. On replace failure it preserves the old output and removes the temporary file. Ran py -3.12 -I tests/test_navmesh_tile_obj.py on this PR head: 3 tests passed.

@neoisg8

neoisg8 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Confirmed independently on Windows 11 (CPython 3.14.3).

Negative control on main

On main:

  1. Passing an invalid --out (naming an existing directory or a path with a non-directory ancestor) still opened and scanned the archive before failing, reproducing AssertionError: archive should not be scanned in test_preflight_refuses_bad_out_before_scanning_archive.
  2. Staged files were replaced via bare os.replace, clobbering permissions to 0o600 (from NamedTemporaryFile), and did not use retry replacement (AttributeError: ... has no attribute 'replace_retry').

Verification with PR #7848

  • Preflight validation in main() checks args.out.is_dir() and walks ancestor directories before calling navmesh_resources(), exiting 2 on invalid destinations without scanning the archive.
  • file_mode.published_mode chmods the staged file to match destination permissions or new_file_mode(), and replace_retry is used for resilient atomic replacement on Windows.
  • The finally block resets permissions to 0o600 before unlinking, ensuring failed replacements cleanly delete the temporary staging file.
  • All 3 tests in tests/test_navmesh_tile_obj.py pass in 1.42s.
  • python -m compileall and python -I scripts/check_no_game_data.py pass with clean output.

LGTM.

(Review conducted with AI assistance)

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested eba034aa on Linux, CPython 3.14, CPU only, synthetic inputs, merged onto main dde82f27: pass

  • python -I tests/test_navmesh_tile_obj.py: 3 tests OK.
  • With only tools/navmesh_tile_obj.py reverted to main: 2 failures.
  • compileall (where the change has Python files) and scripts/check_no_game_data.py on the changed files: clean.

#7843 fixes the same symptom in the same function; the two conflict with each other, so only one can land.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting; claimed in #8047.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested eba034a on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_navmesh_tile_obj.py - all exit 0.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants