Skip to content

save_keys: fail on an unlistable sub-folder instead of silently skipping its saves - #4385

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
SebSilent:fix/save-keys-walk-onerror
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
SebSilent:fix/save-keys-walk-onerror

Conversation

@SebSilent

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #4257 (comment)

What and why

save_keys.py input_files walks a directory of saves with os.walk(p) and no onerror. os.walk silently
skips a sub-folder it cannot list, so collect reported the remaining saves and exited 0, hiding an
incomplete directory. Reproduced on 03b09a0: save_keys collect on a directory with a sub-folder of saves
still exits 0 with no error after chmod 000 on the sub-folder; its files are silently dropped. The walk now
passes an onerror that re-raises, which main()'s existing except OSError turns into one
error: <folder>: ... line naming the folder (exit 1, the tool's existing unreadable-input code). Same
os.walk/onerror completeness gap as #4277 / #4342 / #4355 / #4372.

Tested

Linux, Python 3.14.10, CPU only, no game files (synthetic JSAV saves).

  • New tests/test_save_keys.py::Resolve::test_unlistable_subfolder_is_a_clean_error_not_a_silent_skip:
    chmod 000 a sub-folder, sk.main(["collect", folder]) returns 1 with an error: line naming the folder and
    no traceback (skips when the folder is still listable, e.g. running as root or on Windows). Red on the base:
    exit 0. Green: whole file 34 OK.
  • python -m compileall -q make_game.py tools tests, git diff --check clean.

Checklist

  • 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. (no translator rule changed)
  • Docs updated for any new flag, tool or workflow. (no new flag)

AI-assisted (Tacit).

@HPHM277

HPHM277 commented Nov 23, 2026

Copy link
Copy Markdown

Verified 2bccd96b against its parent 03b09a08 on native Windows 11 build 26100. I used two synthetic JSON inputs, one in a subdirectory, and temporarily denied the current user permission to list that subdirectory. A direct directory listing confirmed WinError 5.

  • With both files readable, save_keys.py collect reported 2 distinct keys.
  • With listing denied, the parent silently reported 1 distinct key and exited 0. The PR exited 1 and printed a single error naming the denied subdirectory, without a traceback.
  • After restoring the original ACL, the PR reported both keys again and exited 0.

The repository’s tests/test_save_keys.py suite had 33 passes and one skip on this machine. The ACL check above exercised the directory-listing failure that the chmod fixture skips on Windows.

@abilliontokens

Copy link
Copy Markdown
Contributor

Tested 2bccd96 on Windows 11, Python 3.12: pass.

  • python -I tests/test_save_keys.py: 34 tests OK (1 skip) — the skip is the new test itself, which self-skips where os.chmod(0o000) still leaves the folder listable (running as root, or Windows); it runs the real save_keys.main(["collect", ...]) end-to-end on Linux, so it exercises the tool rather than a model.
  • python -m compileall -q tools/save_keys.py tests/test_save_keys.py: clean.
  • Code read: input_files() now passes onerror=_walk_error to os.walk, so a directory os.walk cannot list raises instead of being silently dropped. The re-raised OSError carries e.filename (the folder), so main()'s existing handler prints one error: line naming it and returns 1 — matching the test's assertions (rc 1, error: + path on stderr, no traceback). No other caller shares this input_files (character_census.py / gamedata_xml.py have their own).

The change is minimal and in the right place. AI-assisted (omp orchestrator).

@tupacfan04

Copy link
Copy Markdown
Contributor

The fix is right: save_keys now passes an onerror that re-raises (tools/save_keys.py:167-177), and the existing handler emits one error: <folder>: ... line, returns 1 and writes no traceback. With os.scandir forced to raise for a sub-folder, the PR gives collect/resolve rc 1 with one line and no --out, while the base gives collect rc 0 silently and resolve rc 0 with a partial report. 34 tests pass, compileall/diff --check clean, merge clean.

Important: the new test skips on Windows, and the only job running this file is windows-latest (ci.yml:59,86), so the regression is never exercised in CI. Make it portable by mocking os.scandir, or add a Linux run - the same gap as #4417/#4404.

Approve with that. AI-assisted review, no game files, no GPU.

…a folder

The regression test self-skips on Windows and as root, and CI runs this file
only on windows-latest, so the guard was never exercised in CI. Force
os.scandir to raise for one folder instead, in-process, which reproduces the
same PermissionError on every platform.

AI-assisted.
@SebSilent

Copy link
Copy Markdown
Contributor Author

Addressed both points. Added a second regression that forces the same PermissionError through os.scandir, so the guard runs on the windows-latest job and under root instead of skipping; the chmod test stays as the real-filesystem check where it works. Rebased and pushed: head c2315d5, 35 tests OK in the file, compileall and diff --check clean.

@Kam471 Kam471 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.

Ran this at c2315d58b on Windows 11: pytest tests/test_save_keys.py -q -> 34 passed, 1 skipped, 4 subtests passed; git diff --check clean.

The walk guard is now checked on platforms where chmod cannot lock a folder (Windows), so the unlistable-subfolder cases behave the same there instead of skipping the protection; the added tests cover the Windows path. No blocking issue.

@SebSilent

Copy link
Copy Markdown
Contributor Author

Done in c2315d5. Added a second test that forces os.scandir to raise PermissionError for one sub-folder via mock.patch, so the guard is exercised on Windows and as root too, no chmod needed. The chmod test stays as the real-filesystem check where it can run. 35 tests OK, the portable one runs everywhere.

@kapitche

Copy link
Copy Markdown
Contributor

Tested c2315d5 merged with main 98fa503 on Linux (Python 3.12.16, non-root) and native Windows 11 (Python 3.13.1): pass

  • python -I tests/test_save_keys.py: Linux 35 tests OK, none skipped (the chmod test really runs); Windows 35 tests, OK with 1 skipped (the chmod test self-skips there, the os.scandir mock test runs).
  • Negative control (Linux): with tools/save_keys.py restored from main, both new tests fail (test_unlistable_subfolder_is_a_clean_error_not_a_silent_skip and ..._when_chmod_is_not_enough, 2 failures of 35); with the PR's file they pass.
  • By hand on a real unlistable sub-folder (mode 000), save_keys.py collect <dir>, resolve <dir> --dict ... --out ... and resolve <dir> --dict ... each print one error: <dir>/sub: Permission denied line and return 1, with no traceback, so the --out preflight in out_problem (tools/save_keys.py:200) goes through the same handler.
  • No issues found.

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.

7 participants