From a76224f8a6ce85221af6df21b45281a7eafdfd73 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 00:42:03 +0300 Subject: [PATCH] Fail the install when romfs/ is gone or unreadable after cleanup An install whose romfs/ went empty and unreadable after the extraction had verified it (a folder Windows was still deleting) ended with result ok, and the game stopped at its first file read. Cleanup now checks that romfs.bin and index.json are still there, match the verified size and hash record, and that romfs.bin opens; otherwise it fails with cleanup.romfs_unreadable, which tells the player to restart Windows and run the installer on the same folder (resume extracts the game data again). Reported in #107. tests/installer_test.py CleanupTest: an emptied, removed or resized romfs passed cleanup before, fails now. Co-Authored-By: Claude Opus 5.5 --- installer/steps.py | 16 +++++++++++++ installer/text/en.json | 1 + installer/text/ru.json | 1 + tests/installer_test.py | 52 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/installer/steps.py b/installer/steps.py index 3984f784..9d31fb13 100644 --- a/installer/steps.py +++ b/installer/steps.py @@ -661,8 +661,24 @@ class Cleanup(Step): remove(stale) for name in ('gen', 'build', 'check', 'shader_list.tsv'): remove(ctx.temp / name) + romfs_problem(dest / 'romfs', expected_update(ctx)) return {} +def romfs_problem(romfs, expected): + """Raise cleanup.romfs_unreadable unless romfs/ still holds the verified image. The game opens romfs.bin at start; + an install whose romfs/ went empty and unreadable after the extraction verified it (a folder Windows was still + deleting, #107) reported success and the game stopped at its first file read.""" + try: + ok = extract.romfs_verified(romfs, expected) and (romfs / 'romfs.bin').stat().st_size == expected['romfs']['size'] + if ok: + with open(romfs / 'romfs.bin', 'rb') as f: + f.read(1) + except (OSError, ValueError): + ok = False + if not ok: + raise InstallError('cleanup.romfs_unreadable', path=str(romfs)) + + def all_steps(): return [Check(), Extract(), Lift(), Compile(), Prepare(), Cleanup()] diff --git a/installer/text/en.json b/installer/text/en.json index 5964a0f6..3425f5a5 100644 --- a/installer/text/en.json +++ b/installer/text/en.json @@ -75,6 +75,7 @@ "prepare.shader_list_missing": "The shader list is missing.", "prepare.romfs_patches_failed": "Writing the game data patches failed (exit code {code}). Details: {log}", "prepare.shaders_failed": "Preparing the shaders failed (exit code {code}). Details: {log}", + "cleanup.romfs_unreadable": "The game data in {path} is missing or cannot be read after the install. Restart Windows, then run the installer again on the same folder: it extracts the game data again and keeps the rest.", "cleanup.ini_failed": "totk.ini cannot be written: {detail}", "warn.lang_needs_fonts": "Text in {lang} needs the Switch system fonts, which the installer does not extract; some text may not show.", diff --git a/installer/text/ru.json b/installer/text/ru.json index c3a87db8..b1dc33ae 100644 --- a/installer/text/ru.json +++ b/installer/text/ru.json @@ -75,6 +75,7 @@ "prepare.shader_list_missing": "Нет списка шейдеров.", "prepare.romfs_patches_failed": "Не удалось записать патчи данных игры (код выхода {code}). Подробности: {log}", "prepare.shaders_failed": "Не удалось подготовить шейдеры (код выхода {code}). Подробности: {log}", + "cleanup.romfs_unreadable": "Данные игры в {path} пропали или не читаются после установки. Перезагрузите Windows и запустите установщик снова на ту же папку: он заново распакует данные игры, остальное сохранится.", "cleanup.ini_failed": "Не удаётся записать totk.ini: {detail}", "warn.lang_needs_fonts": "Для языка «{lang}» нужны системные шрифты Switch, которые установка не извлекает; часть текста может не отображаться.", diff --git a/tests/installer_test.py b/tests/installer_test.py index d7404929..036d897a 100644 --- a/tests/installer_test.py +++ b/tests/installer_test.py @@ -14,7 +14,7 @@ from types import SimpleNamespace ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) -from installer import ini, runner, state # noqa: E402 +from installer import ini, runner, state, steps # noqa: E402 from installer.events import Reporter # noqa: E402 from installer.layout import Layout # noqa: E402 @@ -241,6 +241,56 @@ class IniTest(unittest.TestCase): self.assertEqual(ini.get_language(text), 'ja') +class CleanupTest(unittest.TestCase): + """The real Cleanup step on a finished install whose romfs/ is intact, emptied or gone (#107).""" + + def setUp(self): + self.steps = steps + self.tmp = tempfile.TemporaryDirectory() + base = Path(self.tmp.name) + self.dest, temp = base / 'dest', base / 'temp' + (self.dest / 'romfs').mkdir(parents=True) + temp.mkdir() + image = b'romfs image' + (self.dest / 'romfs' / 'romfs.bin').write_bytes(image) + digest = steps.extract.sha256(self.dest / 'romfs' / 'romfs.bin') + (self.dest / 'romfs' / 'index.json').write_text(json.dumps({'romfs_size': len(image), 'romfs_sha256': digest})) + self.expected = {'romfs': {'size': len(image), 'sha256': digest}} + self.ctx = SimpleNamespace(reporter=Reporter(out=io.StringIO(), log_path=base / 'x.log'), dest=self.dest, + temp=temp, lang='en-US', state={}, + layout=SimpleNamespace(ini_example=ROOT / 'totk.ini.example')) + self.saved = steps.expected_update + steps.expected_update = lambda ctx: self.expected + + def tearDown(self): + self.steps.expected_update = self.saved + self.ctx.reporter.close() + self.tmp.cleanup() + + def run_cleanup(self): + try: + self.steps.Cleanup().run(self.ctx) + return 'ok' + except self.steps.InstallError as e: + return e.id + + def test_intact_romfs_passes(self): + self.assertEqual(self.run_cleanup(), 'ok') + + def test_emptied_romfs_fails(self): + for p in (self.dest / 'romfs').iterdir(): + p.unlink() + self.assertEqual(self.run_cleanup(), 'cleanup.romfs_unreadable') + + def test_missing_romfs_fails(self): + self.steps.remove(self.dest / 'romfs') + self.assertEqual(self.run_cleanup(), 'cleanup.romfs_unreadable') + + def test_romfs_of_another_size_fails(self): + (self.dest / 'romfs' / 'romfs.bin').write_bytes(b'short') + self.assertEqual(self.run_cleanup(), 'cleanup.romfs_unreadable') + + class EventsTest(unittest.TestCase): def test_lines_are_json_and_the_log_is_english(self): with tempfile.TemporaryDirectory() as d: