From 21de7e21f816a96bc1e2694e2dc1a138be2ec5a9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 2 May 2026 16:50:30 +0000 Subject: [PATCH 2/8] Fix file exclusion for multi-dot filenames (e.g. game.nds.hash.txt) Agent-Logs-Url: https://github.com/rommapp/romm/sessions/2f711770-100b-4e9e-a66e-ab1a74f025f8 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com> --- backend/handler/filesystem/base_handler.py | 14 ++++++++--- backend/handler/filesystem/roms_handler.py | 9 +++++-- .../handler/filesystem/test_base_handler.py | 25 +++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index cea4a20e1..61a1fe13f 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -215,14 +215,20 @@ class FSHandler: excluded_files: list[str] = [] for file_name in files: - # Split the file name to get the extension. + # Get the compound extension (e.g. "nds.hash.txt" for "game.nds.hash.txt") + # and the last single extension (e.g. "txt") for multi-dot filenames. ext = self.parse_file_extension(file_name) + suffix = Path(file_name).suffix.lstrip(".") - # Exclude the file if it has no extension or the extension is in the excluded list. - if ext and ext.lower() in excluded_extensions: + # Exclude the file if the compound extension or the last single extension + # is in the excluded list. Checking both handles files with multiple dots + # (e.g. "game.nds.hash.txt" should be excluded when "txt" is excluded). + if (ext and ext.lower() in excluded_extensions) or ( + suffix and suffix.lower() in excluded_extensions + ): excluded_files.append(file_name) - # Additionally, check if the file name mathes a pattern in the excluded list. + # Additionally, check if the file name matches a pattern in the excluded list. for name in excluded_names: if file_name == name or fnmatch.fnmatch(file_name, name): excluded_files.append(file_name) diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 8145a1f1e..6b6d36c74 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -455,9 +455,14 @@ class FSRomsHandler(FSHandler): for f_path, file_name in iter_files( f"{abs_fs_path}/{rom.fs_name}", recursive=True ): - # Check if file is excluded + # Check if file is excluded by extension. + # Also check the last single extension for multi-dot filenames + # (e.g. "game.nds.hash.txt" should be excluded when "txt" is excluded). ext = self.parse_file_extension(file_name) - if ext in excluded_file_exts: + suffix = Path(file_name).suffix.lstrip(".") + if (ext and ext.lower() in excluded_file_exts) or ( + suffix and suffix.lower() in excluded_file_exts + ): continue if any( diff --git a/backend/tests/handler/filesystem/test_base_handler.py b/backend/tests/handler/filesystem/test_base_handler.py index 0df61ccfb..3dd59dd96 100644 --- a/backend/tests/handler/filesystem/test_base_handler.py +++ b/backend/tests/handler/filesystem/test_base_handler.py @@ -161,6 +161,31 @@ class TestFSHandler: assert "game.rom" in result assert "data.json" in result + def test_exclude_single_files_multi_dot(self, handler: FSHandler): + """Test that files with multiple dots are excluded based on their last extension""" + files = [ + "game.nds", + "game.nds.hash.txt", + "game.nds.enc.hash.txt", + "readme.txt", + "game.rom", + ] + + with patch("handler.filesystem.base_handler.cm.get_config") as mock_config: + mock_config.return_value.EXCLUDED_SINGLE_EXT = ["txt"] + mock_config.return_value.EXCLUDED_SINGLE_FILES = [] + + result = handler.exclude_single_files(files) + + # Files with .txt as the last extension should be excluded regardless of + # how many dots are in the filename + assert "game.nds.hash.txt" not in result + assert "game.nds.enc.hash.txt" not in result + assert "readme.txt" not in result + # Non-txt files should not be excluded + assert "game.nds" in result + assert "game.rom" in result + async def test_make_directory(self, handler: FSHandler): """Test directory creation""" await handler.make_directory("test_dir") From 55cd0cfc4fb32889be97927163328443b8e035e6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 3 May 2026 01:29:04 +0000 Subject: [PATCH 3/8] Support compound suffix exclusions like "hash.txt" for multi-dot filenames Agent-Logs-Url: https://github.com/rommapp/romm/sessions/d1c69638-bfa0-480e-8050-d565b234ea44 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com> --- backend/handler/filesystem/base_handler.py | 28 +++++++++------ backend/handler/filesystem/roms_handler.py | 11 +++--- .../handler/filesystem/test_base_handler.py | 36 ++++++++++++++++--- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index 61a1fe13f..61b1c122c 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -205,6 +205,19 @@ class FSHandler: match = EXTENSION_REGEX.search(file_name) return match.group(1) if match else "" + def iter_file_extensions(self, file_name: str) -> list[str]: + """Return all right-anchored sub-extensions for a filename. + + For "game.nds.enc.hash.txt" this yields: + ["nds.enc.hash.txt", "enc.hash.txt", "hash.txt", "txt"] + This allows exclusion rules like "hash.txt" to match multi-dot filenames. + """ + ext = self.parse_file_extension(file_name) + if not ext: + return [] + parts = ext.split(".") + return [".".join(parts[i:]) for i in range(len(parts))] + def extract_uuid_v4_from_filename(self, file_name: str) -> str: match = UUID_V4_REGEX.search(file_name) return match.group(0) if match else "" @@ -215,16 +228,11 @@ class FSHandler: excluded_files: list[str] = [] for file_name in files: - # Get the compound extension (e.g. "nds.hash.txt" for "game.nds.hash.txt") - # and the last single extension (e.g. "txt") for multi-dot filenames. - ext = self.parse_file_extension(file_name) - suffix = Path(file_name).suffix.lstrip(".") - - # Exclude the file if the compound extension or the last single extension - # is in the excluded list. Checking both handles files with multiple dots - # (e.g. "game.nds.hash.txt" should be excluded when "txt" is excluded). - if (ext and ext.lower() in excluded_extensions) or ( - suffix and suffix.lower() in excluded_extensions + # Check all right-anchored sub-extensions so that rules like "hash.txt" + # match multi-dot filenames such as "game.nds.enc.hash.txt". + if any( + e.lower() in excluded_extensions + for e in self.iter_file_extensions(file_name) ): excluded_files.append(file_name) diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 6b6d36c74..90f0aa0c8 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -456,12 +456,11 @@ class FSRomsHandler(FSHandler): f"{abs_fs_path}/{rom.fs_name}", recursive=True ): # Check if file is excluded by extension. - # Also check the last single extension for multi-dot filenames - # (e.g. "game.nds.hash.txt" should be excluded when "txt" is excluded). - ext = self.parse_file_extension(file_name) - suffix = Path(file_name).suffix.lstrip(".") - if (ext and ext.lower() in excluded_file_exts) or ( - suffix and suffix.lower() in excluded_file_exts + # Check all right-anchored sub-extensions so that rules like "hash.txt" + # match multi-dot filenames such as "game.nds.enc.hash.txt". + if any( + e.lower() in excluded_file_exts + for e in self.iter_file_extensions(file_name) ): continue diff --git a/backend/tests/handler/filesystem/test_base_handler.py b/backend/tests/handler/filesystem/test_base_handler.py index 3dd59dd96..5d9e329d4 100644 --- a/backend/tests/handler/filesystem/test_base_handler.py +++ b/backend/tests/handler/filesystem/test_base_handler.py @@ -145,6 +145,22 @@ class TestFSHandler: assert handler.parse_file_extension("no_extension") == "" assert handler.parse_file_extension("file.with.dots.txt") == "with.dots.txt" + def test_iter_file_extensions(self, handler: FSHandler): + """Test that all right-anchored sub-extensions are returned""" + assert handler.iter_file_extensions("game.nds") == ["nds"] + assert handler.iter_file_extensions("game.nds.hash.txt") == [ + "nds.hash.txt", + "hash.txt", + "txt", + ] + assert handler.iter_file_extensions("game.nds.enc.hash.txt") == [ + "nds.enc.hash.txt", + "enc.hash.txt", + "hash.txt", + "txt", + ] + assert handler.iter_file_extensions("no_extension") == [] + def test_exclude_single_files(self, handler: FSHandler): """Test file exclusion functionality""" files = ["test.txt", "game.rom", "excluded.tmp", "data.json"] @@ -162,7 +178,7 @@ class TestFSHandler: assert "data.json" in result def test_exclude_single_files_multi_dot(self, handler: FSHandler): - """Test that files with multiple dots are excluded based on their last extension""" + """Test that files with multiple dots are excluded by last or compound extension""" files = [ "game.nds", "game.nds.hash.txt", @@ -172,17 +188,29 @@ class TestFSHandler: ] with patch("handler.filesystem.base_handler.cm.get_config") as mock_config: + # Exclude by last single extension mock_config.return_value.EXCLUDED_SINGLE_EXT = ["txt"] mock_config.return_value.EXCLUDED_SINGLE_FILES = [] result = handler.exclude_single_files(files) - # Files with .txt as the last extension should be excluded regardless of - # how many dots are in the filename assert "game.nds.hash.txt" not in result assert "game.nds.enc.hash.txt" not in result assert "readme.txt" not in result - # Non-txt files should not be excluded + assert "game.nds" in result + assert "game.rom" in result + + with patch("handler.filesystem.base_handler.cm.get_config") as mock_config: + # Exclude by compound sub-extension "hash.txt" + mock_config.return_value.EXCLUDED_SINGLE_EXT = ["hash.txt"] + mock_config.return_value.EXCLUDED_SINGLE_FILES = [] + + result = handler.exclude_single_files(files) + + assert "game.nds.hash.txt" not in result + assert "game.nds.enc.hash.txt" not in result + # "readme.txt" does NOT end in "hash.txt" — should remain + assert "readme.txt" in result assert "game.nds" in result assert "game.rom" in result From 101629628e0a62a72bbcb242dab75c31f8d557df Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 3 May 2026 22:46:21 +0000 Subject: [PATCH 4/8] Simplify extension exclusion to use ends-with check instead of sub-extension iteration Agent-Logs-Url: https://github.com/rommapp/romm/sessions/a81b2023-a243-4721-bc5e-c6fa1a473a79 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com> --- backend/handler/filesystem/base_handler.py | 22 +++++-------------- backend/handler/filesystem/roms_handler.py | 8 +++---- .../handler/filesystem/test_base_handler.py | 16 -------------- 3 files changed, 9 insertions(+), 37 deletions(-) diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index 61b1c122c..bfcb48057 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -205,19 +205,6 @@ class FSHandler: match = EXTENSION_REGEX.search(file_name) return match.group(1) if match else "" - def iter_file_extensions(self, file_name: str) -> list[str]: - """Return all right-anchored sub-extensions for a filename. - - For "game.nds.enc.hash.txt" this yields: - ["nds.enc.hash.txt", "enc.hash.txt", "hash.txt", "txt"] - This allows exclusion rules like "hash.txt" to match multi-dot filenames. - """ - ext = self.parse_file_extension(file_name) - if not ext: - return [] - parts = ext.split(".") - return [".".join(parts[i:]) for i in range(len(parts))] - def extract_uuid_v4_from_filename(self, file_name: str) -> str: match = UUID_V4_REGEX.search(file_name) return match.group(0) if match else "" @@ -228,11 +215,12 @@ class FSHandler: excluded_files: list[str] = [] for file_name in files: - # Check all right-anchored sub-extensions so that rules like "hash.txt" - # match multi-dot filenames such as "game.nds.enc.hash.txt". + # Check whether the filename ends with any excluded extension entry. + # Using ends-with handles both simple rules ("txt") and compound rules + # ("hash.txt") against multi-dot filenames like "game.nds.enc.hash.txt". if any( - e.lower() in excluded_extensions - for e in self.iter_file_extensions(file_name) + file_name.lower().endswith("." + ext.lower()) + for ext in excluded_extensions ): excluded_files.append(file_name) diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 90f0aa0c8..29c021c33 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -456,11 +456,11 @@ class FSRomsHandler(FSHandler): f"{abs_fs_path}/{rom.fs_name}", recursive=True ): # Check if file is excluded by extension. - # Check all right-anchored sub-extensions so that rules like "hash.txt" - # match multi-dot filenames such as "game.nds.enc.hash.txt". + # Using ends-with handles both simple rules ("txt") and compound + # rules ("hash.txt") for multi-dot filenames like "game.nds.enc.hash.txt". if any( - e.lower() in excluded_file_exts - for e in self.iter_file_extensions(file_name) + file_name.lower().endswith("." + ext.lower()) + for ext in excluded_file_exts ): continue diff --git a/backend/tests/handler/filesystem/test_base_handler.py b/backend/tests/handler/filesystem/test_base_handler.py index 5d9e329d4..3fe7efbd7 100644 --- a/backend/tests/handler/filesystem/test_base_handler.py +++ b/backend/tests/handler/filesystem/test_base_handler.py @@ -145,22 +145,6 @@ class TestFSHandler: assert handler.parse_file_extension("no_extension") == "" assert handler.parse_file_extension("file.with.dots.txt") == "with.dots.txt" - def test_iter_file_extensions(self, handler: FSHandler): - """Test that all right-anchored sub-extensions are returned""" - assert handler.iter_file_extensions("game.nds") == ["nds"] - assert handler.iter_file_extensions("game.nds.hash.txt") == [ - "nds.hash.txt", - "hash.txt", - "txt", - ] - assert handler.iter_file_extensions("game.nds.enc.hash.txt") == [ - "nds.enc.hash.txt", - "enc.hash.txt", - "hash.txt", - "txt", - ] - assert handler.iter_file_extensions("no_extension") == [] - def test_exclude_single_files(self, handler: FSHandler): """Test file exclusion functionality""" files = ["test.txt", "game.rom", "excluded.tmp", "data.json"] From 9593c302926ea7973da13e52a2810bb1cfb6cde7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 3 May 2026 23:34:30 +0000 Subject: [PATCH 5/8] Address PR review: normalize exclusion sets, avoid duplicates, add multi-dot test for get_rom_files Agent-Logs-Url: https://github.com/rommapp/romm/sessions/8cbbc2ca-a3e3-4c61-9e47-f8544d59231a Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com> --- backend/handler/filesystem/base_handler.py | 22 ++++--- backend/handler/filesystem/roms_handler.py | 6 +- .../handler/filesystem/test_roms_handler.py | 61 ++++++++++++++++++- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index bfcb48057..689d3ef42 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -210,24 +210,30 @@ class FSHandler: return match.group(0) if match else "" def exclude_single_files(self, files: list[str]) -> list[str]: - excluded_extensions = cm.get_config().EXCLUDED_SINGLE_EXT - excluded_names = cm.get_config().EXCLUDED_SINGLE_FILES + config = cm.get_config() + # Config already stores extensions lowercased; use a set for O(1) lookups. + excluded_extensions = set(config.EXCLUDED_SINGLE_EXT) + excluded_names = config.EXCLUDED_SINGLE_FILES excluded_files: list[str] = [] for file_name in files: + file_name_lower = file_name.lower() + # Check whether the filename ends with any excluded extension entry. # Using ends-with handles both simple rules ("txt") and compound rules # ("hash.txt") against multi-dot filenames like "game.nds.enc.hash.txt". if any( - file_name.lower().endswith("." + ext.lower()) - for ext in excluded_extensions + file_name_lower.endswith("." + ext) for ext in excluded_extensions ): excluded_files.append(file_name) + continue - # Additionally, check if the file name matches a pattern in the excluded list. - for name in excluded_names: - if file_name == name or fnmatch.fnmatch(file_name, name): - excluded_files.append(file_name) + # Check if the file name matches a pattern in the excluded list. + if any( + file_name == name or fnmatch.fnmatch(file_name, name) + for name in excluded_names + ): + excluded_files.append(file_name) # Return files that are not in the filtered list. return [f for f in files if f not in excluded_files] diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 29c021c33..8c7fb256e 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -434,7 +434,8 @@ class FSRomsHandler(FSHandler): ) excluded_file_names = cm.get_config().EXCLUDED_MULTI_PARTS_FILES - excluded_file_exts = cm.get_config().EXCLUDED_MULTI_PARTS_EXT + # Config already stores extensions lowercased; use a set for O(1) lookups. + excluded_file_exts = set(cm.get_config().EXCLUDED_MULTI_PARTS_EXT) rom_crc_c = 0 rom_md5_h = hashlib.md5(usedforsecurity=False) if calculate_hashes else None @@ -458,8 +459,9 @@ class FSRomsHandler(FSHandler): # Check if file is excluded by extension. # Using ends-with handles both simple rules ("txt") and compound # rules ("hash.txt") for multi-dot filenames like "game.nds.enc.hash.txt". + file_name_lower = file_name.lower() if any( - file_name.lower().endswith("." + ext.lower()) + file_name_lower.endswith("." + ext) for ext in excluded_file_exts ): continue diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index e6c5bdc74..9e1dbce39 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -378,7 +378,66 @@ class TestFSRomsHandler: assert rom_file.file_size_bytes > 0 assert rom_file.last_modified is not None - async def test_rename_fs_rom_same_name(self, handler: FSRomsHandler): + @pytest.mark.asyncio + async def test_get_rom_files_multi_rom_multi_dot_exclusion( + self, handler: FSRomsHandler, rom_multi + ): + """Multi-dot filenames in a multi-part dir are excluded by simple or compound ext rules.""" + multi_dot_file = ( + handler.base_path + / "n64/roms/Super Mario 64 (J) (Rev A)/game.nds.hash.txt" + ) + multi_dot_file.write_text("hash data") + + try: + # Exclude by the last single extension "txt" + config_txt = Config( + EXCLUDED_PLATFORMS=[], + EXCLUDED_SINGLE_EXT=[], + EXCLUDED_SINGLE_FILES=[], + EXCLUDED_MULTI_FILES=[], + EXCLUDED_MULTI_PARTS_EXT=["txt"], + EXCLUDED_MULTI_PARTS_FILES=[], + PLATFORMS_BINDING={}, + PLATFORMS_VERSIONS={}, + ROMS_FOLDER_NAME="roms", + FIRMWARE_FOLDER_NAME="bios", + ) + with pytest.MonkeyPatch.context() as m: + m.setattr( + "handler.filesystem.roms_handler.cm.get_config", lambda: config_txt + ) + parsed = await handler.get_rom_files(rom_multi) + file_names = [rf.file_name for rf in parsed.rom_files] + assert "game.nds.hash.txt" not in file_names + assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names + + # Exclude by the compound extension "hash.txt" + config_compound = Config( + EXCLUDED_PLATFORMS=[], + EXCLUDED_SINGLE_EXT=[], + EXCLUDED_SINGLE_FILES=[], + EXCLUDED_MULTI_FILES=[], + EXCLUDED_MULTI_PARTS_EXT=["hash.txt"], + EXCLUDED_MULTI_PARTS_FILES=[], + PLATFORMS_BINDING={}, + PLATFORMS_VERSIONS={}, + ROMS_FOLDER_NAME="roms", + FIRMWARE_FOLDER_NAME="bios", + ) + with pytest.MonkeyPatch.context() as m: + m.setattr( + "handler.filesystem.roms_handler.cm.get_config", + lambda: config_compound, + ) + parsed = await handler.get_rom_files(rom_multi) + file_names = [rf.file_name for rf in parsed.rom_files] + assert "game.nds.hash.txt" not in file_names + assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names + finally: + multi_dot_file.unlink(missing_ok=True) + + """Test rename_fs_rom when old and new names are the same""" old_name = "test_rom.n64" new_name = "test_rom.n64" From 78c0b5b8948d1615e1919fba1ae548050313c63a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 3 May 2026 23:35:19 +0000 Subject: [PATCH 6/8] Fix broken test_rename_fs_rom_same_name function definition after new test insertion Agent-Logs-Url: https://github.com/rommapp/romm/sessions/8cbbc2ca-a3e3-4c61-9e47-f8544d59231a Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com> --- backend/tests/handler/filesystem/test_roms_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index 9e1dbce39..2a373cff6 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -437,7 +437,7 @@ class TestFSRomsHandler: finally: multi_dot_file.unlink(missing_ok=True) - + async def test_rename_fs_rom_same_name(self, handler: FSRomsHandler): """Test rename_fs_rom when old and new names are the same""" old_name = "test_rom.n64" new_name = "test_rom.n64" From da005cf81a3d8f29b69c1f958e607e670f6af8db Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 3 May 2026 23:36:23 +0000 Subject: [PATCH 7/8] Optimize fnmatch check and use consistent n64 filename in test Agent-Logs-Url: https://github.com/rommapp/romm/sessions/8cbbc2ca-a3e3-4c61-9e47-f8544d59231a Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com> --- backend/handler/filesystem/base_handler.py | 6 +++--- backend/tests/handler/filesystem/test_roms_handler.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index 689d3ef42..268b4e3b5 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -229,9 +229,9 @@ class FSHandler: continue # Check if the file name matches a pattern in the excluded list. - if any( - file_name == name or fnmatch.fnmatch(file_name, name) - for name in excluded_names + # Check exact match first (O(1) via set), then fall back to glob patterns. + if file_name in excluded_names or any( + fnmatch.fnmatch(file_name, name) for name in excluded_names ): excluded_files.append(file_name) diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index 2a373cff6..fa4f3f2d2 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -385,7 +385,7 @@ class TestFSRomsHandler: """Multi-dot filenames in a multi-part dir are excluded by simple or compound ext rules.""" multi_dot_file = ( handler.base_path - / "n64/roms/Super Mario 64 (J) (Rev A)/game.nds.hash.txt" + / "n64/roms/Super Mario 64 (J) (Rev A)/game.n64.hash.txt" ) multi_dot_file.write_text("hash data") @@ -409,7 +409,7 @@ class TestFSRomsHandler: ) parsed = await handler.get_rom_files(rom_multi) file_names = [rf.file_name for rf in parsed.rom_files] - assert "game.nds.hash.txt" not in file_names + assert "game.n64.hash.txt" not in file_names assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names # Exclude by the compound extension "hash.txt" @@ -432,7 +432,7 @@ class TestFSRomsHandler: ) parsed = await handler.get_rom_files(rom_multi) file_names = [rf.file_name for rf in parsed.rom_files] - assert "game.nds.hash.txt" not in file_names + assert "game.n64.hash.txt" not in file_names assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names finally: multi_dot_file.unlink(missing_ok=True) From 5e3a2707b0134fc0c9b4aeba9663a278a71237e8 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sun, 3 May 2026 19:39:19 -0400 Subject: [PATCH 8/8] cleanup --- backend/handler/filesystem/base_handler.py | 14 ++++---------- backend/handler/filesystem/roms_handler.py | 12 +++++------- .../tests/handler/filesystem/test_roms_handler.py | 3 +-- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index 268b4e3b5..b93042cf4 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -210,26 +210,20 @@ class FSHandler: return match.group(0) if match else "" def exclude_single_files(self, files: list[str]) -> list[str]: - config = cm.get_config() - # Config already stores extensions lowercased; use a set for O(1) lookups. - excluded_extensions = set(config.EXCLUDED_SINGLE_EXT) - excluded_names = config.EXCLUDED_SINGLE_FILES + cnfg = cm.get_config() + excluded_extensions = cnfg.EXCLUDED_SINGLE_EXT + excluded_names = cnfg.EXCLUDED_SINGLE_FILES excluded_files: list[str] = [] for file_name in files: file_name_lower = file_name.lower() # Check whether the filename ends with any excluded extension entry. - # Using ends-with handles both simple rules ("txt") and compound rules - # ("hash.txt") against multi-dot filenames like "game.nds.enc.hash.txt". - if any( - file_name_lower.endswith("." + ext) for ext in excluded_extensions - ): + if any(file_name_lower.endswith("." + ext) for ext in excluded_extensions): excluded_files.append(file_name) continue # Check if the file name matches a pattern in the excluded list. - # Check exact match first (O(1) via set), then fall back to glob patterns. if file_name in excluded_names or any( fnmatch.fnmatch(file_name, name) for name in excluded_names ): diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 8c7fb256e..411f24f6c 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -433,9 +433,9 @@ class FSRomsHandler(FSHandler): rom.platform_slug not in NON_HASHABLE_PLATFORMS and calculate_hashes ) - excluded_file_names = cm.get_config().EXCLUDED_MULTI_PARTS_FILES - # Config already stores extensions lowercased; use a set for O(1) lookups. - excluded_file_exts = set(cm.get_config().EXCLUDED_MULTI_PARTS_EXT) + cnfg = cm.get_config() + excluded_file_names = cnfg.EXCLUDED_MULTI_PARTS_FILES + excluded_file_exts = cnfg.EXCLUDED_MULTI_PARTS_EXT rom_crc_c = 0 rom_md5_h = hashlib.md5(usedforsecurity=False) if calculate_hashes else None @@ -457,15 +457,13 @@ class FSRomsHandler(FSHandler): f"{abs_fs_path}/{rom.fs_name}", recursive=True ): # Check if file is excluded by extension. - # Using ends-with handles both simple rules ("txt") and compound - # rules ("hash.txt") for multi-dot filenames like "game.nds.enc.hash.txt". file_name_lower = file_name.lower() if any( - file_name_lower.endswith("." + ext) - for ext in excluded_file_exts + file_name_lower.endswith("." + ext) for ext in excluded_file_exts ): continue + # Check if the file name matches a pattern in the excluded list. if any( file_name == exc_name or fnmatch.fnmatch(file_name, exc_name) for exc_name in excluded_file_names diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index fa4f3f2d2..ed7787931 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -384,8 +384,7 @@ class TestFSRomsHandler: ): """Multi-dot filenames in a multi-part dir are excluded by simple or compound ext rules.""" multi_dot_file = ( - handler.base_path - / "n64/roms/Super Mario 64 (J) (Rev A)/game.n64.hash.txt" + handler.base_path / "n64/roms/Super Mario 64 (J) (Rev A)/game.n64.hash.txt" ) multi_dot_file.write_text("hash data")