ffx: extract main(argv), atomic output writes, and error guards in ffx_dispatch_profile with CLI suite #2375

Merged
Ammmae0x merged 2 commits from upstream-pr/2383 into main 2026-11-25 14:51:09 +00:00
Ammmae0x commented 2026-11-25 14:34:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T14:34:58Z.


Change

Harden tools/ffx_dispatch_profile.py for programmatic and CLI use without changing its stdout summary format or .bin payload outputs:

  • Extract the inline __main__ block into main(argv=None) -> int forwarding argv to parser.parse_args(argv).
  • Reject output_directory colliding with image via _same_path() (Path.resolve() equality and Path.samefile() for hardlinks/aliases) and reject an existing non-directory path before creating directories or writing files.
  • Stage each <name>.bin file inside tempfile.TemporaryDirectory(dir=args.output_directory) and commit it with os.replace() so write/replace failures leave no partial .bin files or leftover staging directories.
  • Catch (OSError, ValueError, struct.error, pefile.PEFormatError) cleanly via parser.exit(1, ...) with no raw Python tracebacks.
  • Add tests/ffx_dispatch_profile_cli_check.py and register ffx_dispatch_profile_cli_check in CMakeLists.txt.

Verification

Verified on commit 1e18e8b2 (rebased on upstream/main a31f70a1):

  • out/pe-env/Scripts/python.exe -B tests/ffx_dispatch_profile_cli_check.py -v -> Ran 9 tests in 0.909s - OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/ffx_dispatch_profile_check.py -v -> Ran 10 tests in 0.217s - OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py -> form coverage checks passed (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py -> Self-test passed. Checked 19 Markdown files and 462 links. (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> Self-test passed. Checked 1675 tracked source, script and build/CI files: no bidirectional controls. (exit 0)

Spot mutation checks against tools/ffx_dispatch_profile.py (8/8 killed by tests/ffx_dispatch_profile_cli_check.py, restored cleanly):

  • Drop argv forwarding, bypass resolve() collision check, bypass samefile() collision check, remove non-directory check, bypass TemporaryDirectory + os.replace() staging, and individually drop struct.error, pefile.PEFormatError, and OSError handlers (all returned exit 1).

Evidence and follow-up

  • Positive and negative controls build a synthetic PE64 image in memory using tests/ffx_dispatch_profile_check.build_synthetic_pe() (MIT, repo-authored; no game files, dumps, or binaries).
  • Negative controls cover missing image, mismatching UPSCALER hash, invalid PE (pefile.PEFormatError), truncated data (struct.error), path collision via .. alias and hardlink, existing regular file as output_directory, and os.replace() OSError leaving zero partial .bin files or staging directories.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6606911041 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2383). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T14:34:58Z. --- ## Change Harden `tools/ffx_dispatch_profile.py` for programmatic and CLI use without changing its stdout summary format or `.bin` payload outputs: - Extract the inline `__main__` block into `main(argv=None) -> int` forwarding `argv` to `parser.parse_args(argv)`. - Reject `output_directory` colliding with `image` via `_same_path()` (`Path.resolve()` equality and `Path.samefile()` for hardlinks/aliases) and reject an existing non-directory path before creating directories or writing files. - Stage each `<name>.bin` file inside `tempfile.TemporaryDirectory(dir=args.output_directory)` and commit it with `os.replace()` so write/replace failures leave no partial `.bin` files or leftover staging directories. - Catch `(OSError, ValueError, struct.error, pefile.PEFormatError)` cleanly via `parser.exit(1, ...)` with no raw Python tracebacks. - Add `tests/ffx_dispatch_profile_cli_check.py` and register `ffx_dispatch_profile_cli_check` in `CMakeLists.txt`. ## Verification Verified on commit `1e18e8b2` (rebased on `upstream/main` `a31f70a1`): - `out/pe-env/Scripts/python.exe -B tests/ffx_dispatch_profile_cli_check.py -v` -> `Ran 9 tests in 0.909s - OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/ffx_dispatch_profile_check.py -v` -> `Ran 10 tests in 0.217s - OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` -> `form coverage checks passed` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` -> `Self-test passed. Checked 19 Markdown files and 462 links.` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> `Self-test passed. Checked 1675 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) Spot mutation checks against `tools/ffx_dispatch_profile.py` (8/8 killed by `tests/ffx_dispatch_profile_cli_check.py`, restored cleanly): - Drop `argv` forwarding, bypass `resolve()` collision check, bypass `samefile()` collision check, remove non-directory check, bypass `TemporaryDirectory` + `os.replace()` staging, and individually drop `struct.error`, `pefile.PEFormatError`, and `OSError` handlers (all returned exit 1). ## Evidence and follow-up - Positive and negative controls build a synthetic PE64 image in memory using `tests/ffx_dispatch_profile_check.build_synthetic_pe()` (MIT, repo-authored; no game files, dumps, or binaries). - Negative controls cover missing image, mismatching `UPSCALER` hash, invalid PE (`pefile.PEFormatError`), truncated data (`struct.error`), path collision via `..` alias and hardlink, existing regular file as `output_directory`, and `os.replace()` `OSError` leaving zero partial `.bin` files or staging directories. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-25 15:01:54 +00:00
R0Y00 commented 2026-11-25 15:02:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T14:46:48Z.


I found and fixed one output collision on this branch. A valid input can itself be named initializer.bin, so the generated file path can alias the input even when the output directory does not. The tool now checks all generated destinations before staging any output. The new synthetic test keeps the input bytes intact and verifies that no earlier program outputs are written. Both Windows suites passed, 20 cases total.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7165801967 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2383#issuecomment-7165801967). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T14:46:48Z. --- I found and fixed one output collision on this branch. A valid input can itself be named initializer.bin, so the generated file path can alias the input even when the output directory does not. The tool now checks all generated destinations before staging any output. The new synthetic test keeps the input bytes intact and verifies that no earlier program outputs are written. Both Windows suites passed, 20 cases total.
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
R0Y00/Xbox2Windows!2375
No description provided.