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"]