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

Open
opened 2026-11-26 21:18:54 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1760
Author: @kirigayakazima
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rpf9-carve-atomic-index-and-input-guard


📝 Commits (1)

  • 0f83828 rpf9_carve: publish index.json atomically and guard stream outputs aliasing input archive

📊 Changes

2 files changed (+126 additions, -3 deletions)

View changed files

📝 tests/test_rpf9_carve_errors.py (+89 -0)
📝 tools/rpf9_carve.py (+37 -3)

📄 Description

Claimed in the coordination issue as: RPF9-CARVE-ATOMIC-INDEX-AND-INPUT-GUARD https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161163878

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1760 **Author:** [@kirigayakazima](https://github.com/kirigayakazima) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rpf9-carve-atomic-index-and-input-guard` --- ### 📝 Commits (1) - [`0f83828`](https://github.com/ubiquitarianism/leonida/commit/0f83828fa789f7efe2162317e8438d0cfd6e8c82) rpf9_carve: publish index.json atomically and guard stream outputs aliasing input archive ### 📊 Changes **2 files changed** (+126 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_rpf9_carve_errors.py` (+89 -0) 📝 `tools/rpf9_carve.py` (+37 -3) </details> ### 📄 Description Claimed in the coordination issue as: RPF9-CARVE-ATOMIC-INDEX-AND-INPUT-GUARD https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161163878 ### 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1773
No description provided.