rpf9_carve: publish index.json atomically and guard stream outputs aliasing input archive (RPF9-CARVE-ATOMIC-INDEX-AND-INPUT-GUARD) #1760

Open
github-bot wants to merge 1 commit from github-bot/gh-1760 into main AGit
Owner

@⁠kirigayakazima on GitHub · 2026-11-25 11:34 UTC · original
kirigayakazima:fix/rpf9-carve-atomic-index-and-input-guard → main · +126 −3 · 2 files · 1 commits · open

Claimed in the coordination issue as: RPF9-CARVE-ATOMIC-INDEX-AND-INPUT-GUARD #1608

What and why

Hardens tools/rpf9_carve.py across three edge cases:

  1. Refuse carved .bin / .bk2 outputs aliasing the input archive: Before opening <out>/<offset>.bk2 or <out>/<offset>.bin, check same_report_path(out_file, a.archive) (matching the existing index.json check) so an input archive named like a carved output inside out_dir cannot be truncated on POSIX or fail with a confusing write error on Windows.
  2. Atomic index.json publishing with Windows read-only cleanup: Replace direct open(index_path, "w", ...) with _write_index_atomic (writes LF-terminated JSON ending in \n to a temporary file in the destination directory, applies published_mode(dest), publishes via replace_retry, and restores 0o600 before unlinking on failure so Windows read-only targets do not strand .rpf9_carve-*.tmp files or leave truncated index.json behind).
  3. Accurate progress denominator: Compute done in the 2,000-row progress print from base instead of a.start or 0 so progress reflects the scanned data span when --start is omitted.

Adds 4 synthetic regression tests in tests/test_rpf9_carve_errors.py.

Tested

Windows 11 x64 (10.0.26100), Python 3.14.7, CPU only, synthetic archives only (no game files). Commit 0f83828fa789f7efe2162317e8438d0cfd6e8c82.

  • python -I tests/test_rpf9_carve_errors.py -v:
    • against main (bcaa13da) with all 7 tests: 4 failures (new tests fail on .bin/.bk2 aliasing, truncated index.json on mid-write failure, and missing trailing newline);
    • with the fix: 7 tests, OK (0 skipped).
  • python -I tests/test_rpf9_carve.py -v: 17 tests, OK.
  • python -I tests/test_ci_registration.py: 16 tests, OK.
  • python -I tests/test_text_write_line_endings.py: 3 tests, OK.
  • python -I scripts/check_no_game_data.py tools/rpf9_carve.py tests/test_rpf9_carve_errors.py, python -m compileall -q tools tests, git diff --check: exit 0.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)
**@⁠kirigayakazima** on GitHub · 2026-11-25 11:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1760) `kirigayakazima:fix/rpf9-carve-atomic-index-and-input-guard` → `main` · +126 −3 · 2 files · 1 commits · open Claimed in the coordination issue as: RPF9-CARVE-ATOMIC-INDEX-AND-INPUT-GUARD https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ### What and why Hardens `tools/rpf9_carve.py` across three edge cases: 1. **Refuse carved `.bin` / `.bk2` outputs aliasing the input archive**: Before opening `<out>/<offset>.bk2` or `<out>/<offset>.bin`, check `same_report_path(out_file, a.archive)` (matching the existing `index.json` check) so an input archive named like a carved output inside `out_dir` cannot be truncated on POSIX or fail with a confusing write error on Windows. 2. **Atomic `index.json` publishing with Windows read-only cleanup**: Replace direct `open(index_path, "w", ...)` with `_write_index_atomic` (writes LF-terminated JSON ending in `\n` to a temporary file in the destination directory, applies `published_mode(dest)`, publishes via `replace_retry`, and restores `0o600` before unlinking on failure so Windows read-only targets do not strand `.rpf9_carve-*.tmp` files or leave truncated `index.json` behind). 3. **Accurate progress denominator**: Compute `done` in the 2,000-row progress print from `base` instead of `a.start or 0` so progress reflects the scanned data span when `--start` is omitted. Adds 4 synthetic regression tests in `tests/test_rpf9_carve_errors.py`. ### Tested Windows 11 x64 (`10.0.26100`), Python `3.14.7`, CPU only, synthetic archives only (no game files). Commit `0f83828fa789f7efe2162317e8438d0cfd6e8c82`. - `python -I tests/test_rpf9_carve_errors.py -v`: - against `main` (`bcaa13da`) with all 7 tests: **4 failures** (new tests fail on `.bin`/`.bk2` aliasing, truncated `index.json` on mid-write failure, and missing trailing newline); - with the fix: **7 tests, OK** (0 skipped). - `python -I tests/test_rpf9_carve.py -v`: **17 tests, OK**. - `python -I tests/test_ci_registration.py`: **16 tests, OK**. - `python -I tests/test_text_write_line_endings.py`: **3 tests, OK**. - `python -I scripts/check_no_game_data.py tools/rpf9_carve.py tests/test_rpf9_carve_errors.py`, `python -m compileall -q tools tests`, `git diff --check`: exit `0`. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:04:00 +00:00
Author
Owner

@⁠ShotTechOp on GitHub · 2026-11-25 12:26 UTC · original · approved these changes

Independent Windows 11 / Python 3.12.10 review of 0f83828f applied onto origin/main (0d80bc5d) — CPU-only, synthetic inputs, no game files.

Checks run

  • Clean apply onto origin/main (0d80bc5d): clean.
  • python -I tests/test_rpf9_carve_errors.py -v: 7/7 OK (including the native Windows os.name == "nt" read-only index.json publish check).
  • Negative control (reverted tools/rpf9_carve.py to origin/main with the PR's tests): 4 failures across all 4 new tests (bin/bk2 input-archive aliasing, failed write truncating previous index.json to "", and missing trailing newline).
  • python -m compileall -q tools/rpf9_carve.py tests/test_rpf9_carve_errors.py: exit 0.
  • python -I scripts/check_no_game_data.py tools/rpf9_carve.py tests/test_rpf9_carve_errors.py: exit 0.
  • git diff --check origin/main: clean.

Code audit

  • _write_index_atomic() pins encoding="utf-8", newline="\n", sets published_mode(dest), publishes via replace_retry, and restores writable mode (0o600) before unlinking the staging file in finally, avoiding Windows read-only staging leaks on failed replaces.
  • Guarding both .bk2 and .bin stream outputs with same_report_path(out_file, a.archive) prevents overwriting an input archive named after its own hex offset, and using base in the progress denominator fixes the percentage when --start is omitted.

LGTM. AI-assisted (Antigravity).

**@⁠ShotTechOp** on GitHub · 2026-11-25 12:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1760#pullrequestreview-6057565940) · **approved these changes** Independent Windows 11 / Python 3.12.10 review of `0f83828f` applied onto `origin/main` (`0d80bc5d`) — CPU-only, synthetic inputs, no game files. ### Checks run - Clean apply onto `origin/main` (`0d80bc5d`): clean. - `python -I tests/test_rpf9_carve_errors.py -v`: **7/7 OK** (including the native Windows `os.name == "nt"` read-only `index.json` publish check). - Negative control (reverted `tools/rpf9_carve.py` to `origin/main` with the PR's tests): **4 failures** across all 4 new tests (`bin`/`bk2` input-archive aliasing, failed write truncating previous `index.json` to `""`, and missing trailing newline). - `python -m compileall -q tools/rpf9_carve.py tests/test_rpf9_carve_errors.py`: exit 0. - `python -I scripts/check_no_game_data.py tools/rpf9_carve.py tests/test_rpf9_carve_errors.py`: exit 0. - `git diff --check origin/main`: clean. ### Code audit - `_write_index_atomic()` pins `encoding="utf-8", newline="\n"`, sets `published_mode(dest)`, publishes via `replace_retry`, and restores writable mode (`0o600`) before unlinking the staging file in `finally`, avoiding Windows read-only staging leaks on failed replaces. - Guarding both `.bk2` and `.bin` stream outputs with `same_report_path(out_file, a.archive)` prevents overwriting an input archive named after its own hex offset, and using `base` in the progress denominator fixes the percentage when `--start` is omitted. LGTM. AI-assisted (Antigravity).
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 18:28 UTC · original

Reviewed 0f83828: ran tests/test_rpf9_carve_errors.py (7 tests OK on the PR tree; the same file on main fails exactly the four new tests, so they cover the fix - the alias runs there truncate the archive and the tool dies of SIGBUS with no message), plus test_rpf9_carve.py (17) and test_carve_short_system_pages.py (11), OK on both trees, and check_no_game_data.py (clean). A probe on both trees confirms the normal carve stays byte-identical (only the intended trailing newline in index.json differs), and that .bin/.bk2 output aliases of the input archive - direct, symlinked and relative spellings - are refused with the archive intact, where main destroys the input. Windows-only paths were not run. Nothing blocking found.

AI-assisted review.

Recommendation: Merge

**@⁠PierreEbele** on GitHub · 2026-11-25 18:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1760#issuecomment-7170957526) Reviewed 0f83828: ran tests/test_rpf9_carve_errors.py (7 tests OK on the PR tree; the same file on main fails exactly the four new tests, so they cover the fix - the alias runs there truncate the archive and the tool dies of SIGBUS with no message), plus test_rpf9_carve.py (17) and test_carve_short_system_pages.py (11), OK on both trees, and check_no_game_data.py (clean). A probe on both trees confirms the normal carve stays byte-identical (only the intended trailing newline in index.json differs), and that .bin/.bk2 output aliases of the input archive - direct, symlinked and relative spellings - are refused with the archive intact, where main destroys the input. Windows-only paths were not run. Nothing blocking found. AI-assisted review. **Recommendation:** Merge
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1760/head:github-bot/gh-1760'
git switch 'github-bot/gh-1760'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1760'
git switch 'github-bot/gh-1760'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1760'
git switch 'github-bot/gh-1760'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1760'
git switch 'main'
git merge --squash 'github-bot/gh-1760'
git switch 'main'
git merge --ff-only 'github-bot/gh-1760'
git switch 'main'
git merge 'github-bot/gh-1760'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.