From def2d441cb5fc39f39612f76fbc7087a3deeacd6 Mon Sep 17 00:00:00 2001 From: Ryan McClelland Date: Fri, 14 Aug 2026 12:22:28 -0700 Subject: [PATCH] fix(gamelist): drop partial results when a document is unparseable Streaming means valid entries are consumed before the parser reaches any damage further down the file, so a truncated or corrupt gamelist returned whatever had been read so far. The previous full-document parse rejected such a file outright and imported nothing. Clear the accumulated entries when a parse error escapes both the incremental pass and the fallback, restoring that behavior. Errors raised while building an entry still keep what was read, as before. Also filter the fallback path to game/folder elements so it matches the incremental path, and hoist the lowercase filename and suffix tuple out of the per-extension loop in exclude_single_files. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01TLTDjv3niosCXZWtjKELK2 --- backend/handler/filesystem/base_handler.py | 7 +- backend/handler/metadata/gamelist_handler.py | 8 ++- .../handler/metadata/test_gamelist_handler.py | 68 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index f113bbc5d..1f1e65d51 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -272,11 +272,12 @@ class FSHandler: excluded_extensions = cnfg.EXCLUDED_SINGLE_EXT excluded_names = cnfg.EXCLUDED_SINGLE_FILES + # Built once rather than per file, and endswith takes the whole tuple. + excluded_suffixes = tuple(f".{ext}" for ext in excluded_extensions) + def is_excluded(file_name: str) -> bool: # Check whether the filename ends with any excluded extension entry. - if any( - file_name.lower().endswith("." + ext) for ext in excluded_extensions - ): + if file_name.lower().endswith(excluded_suffixes): return True # Check if the file name matches a pattern in the excluded list. diff --git a/backend/handler/metadata/gamelist_handler.py b/backend/handler/metadata/gamelist_handler.py index 881e21944..e673e730e 100644 --- a/backend/handler/metadata/gamelist_handler.py +++ b/backend/handler/metadata/gamelist_handler.py @@ -377,7 +377,9 @@ class GamelistHandler(MetadataHandler): xml_content = gamelist_path.read_text(encoding="utf-8", errors="replace") xml_content = ALTERNATIVE_EMULATOR_SELF_CLOSING_RE.sub("", xml_content) xml_content = ALTERNATIVE_EMULATOR_PAIRED_RE.sub("", xml_content) - yield from ET.fromstring(xml_content) + for elem in ET.fromstring(xml_content): + if elem.tag in ("game", "folder"): + yield elem def _parse_gamelist_xml( self, gamelist_path: Path, platform: Platform @@ -481,6 +483,10 @@ class GamelistHandler(MetadataHandler): self._gamelist_cache[cache_key] = roms_data except ET.ParseError as e: log.warning(f"Failed to parse gamelist.xml at {gamelist_path}: {e}") + # Entries read before the document turned out to be invalid are + # dropped, so a corrupt file yields nothing rather than a partial + # import that silently looks complete. + roms_data.clear() except Exception as e: log.error(f"Error reading gamelist.xml at {gamelist_path}: {e}") diff --git a/backend/tests/handler/metadata/test_gamelist_handler.py b/backend/tests/handler/metadata/test_gamelist_handler.py index 7159bc4e4..e73a717c5 100644 --- a/backend/tests/handler/metadata/test_gamelist_handler.py +++ b/backend/tests/handler/metadata/test_gamelist_handler.py @@ -297,6 +297,52 @@ class TestGamelistHandler: assert roms_data["Tetris.gb"].get("name") == "Tetris" assert roms_data["Zelda.gb"].get("summary") == "Adventure" + def test_parse_gamelist_truncated_after_valid_entries_returns_nothing( + self, tmp_path + ): + """A corrupt document imports nothing, not the part read before the error. + + Entries are streamed, so valid games are consumed before the parser + reaches the damage. + """ + gamelist_path = tmp_path / "gamelist.xml" + gamelist_path.write_text( + """ + + + ./Tetris.gb + Tetris + + + ./Zelda.gb + Zelda + + + ./Truncated.gb + Trunca""", + encoding="utf-8", + ) + + handler = GamelistHandler() + platform = SimpleNamespace(id=4, fs_slug="gb") + + with ( + patch( + "handler.metadata.gamelist_handler.get_preferred_media_types", + return_value=[], + ), + patch( + "handler.metadata.gamelist_handler.extract_metadata_from_gamelist_rom", + return_value=dict(MOCK_METADATA), + ), + ): + roms_data = handler._parse_gamelist_xml( + gamelist_path, cast(Platform, platform) + ) + + assert roms_data == {} + assert 4 not in handler._gamelist_cache + def test_iter_game_elements_yields_only_game_and_folder(self, tmp_path): gamelist_path = tmp_path / "gamelist.xml" gamelist_path.write_text( @@ -314,3 +360,25 @@ class TestGamelistHandler: tags = [elem.tag for elem in handler._iter_game_elements(gamelist_path)] assert tags == ["game", "folder"] + + def test_iter_game_elements_filters_on_the_fallback_path(self, tmp_path): + """The ES-DE sibling forces the fallback, which must filter identically.""" + gamelist_path = tmp_path / "gamelist.xml" + gamelist_path.write_text( + """ + + + + + gb + ./Tetris.gb + ./Sub + +""", + encoding="utf-8", + ) + + handler = GamelistHandler() + tags = [elem.tag for elem in handler._iter_game_elements(gamelist_path)] + + assert tags == ["game", "folder"]