Merge pull request #4129 from sdornan/claude/github-issue-4126-852db9

fix(hashing): extract CHD SHA-1 for folder-based ROMs
This commit is contained in:
Georges-Antoine Assi
2026-08-06 09:38:39 -04:00
committed by GitHub
2 changed files with 60 additions and 11 deletions

View File

@@ -128,6 +128,11 @@ ARCHIVE_READERS = {
}
def _chd_sha1_hash(file_path: Path) -> str:
"""Return the embedded CHD v5 raw+meta SHA-1, or "" for non-CHD files."""
return extract_chd_hash(file_path) if is_chd_file(file_path) else ""
def _make_file_hash(
crc_c: int, md5_h: Any, sha1_h: Any, chd_sha1_hash: str = ""
) -> FileHash:
@@ -379,7 +384,6 @@ class FSRomsHandler(FSHandler):
f"{abs_fs_path}/{rom.fs_name}", recursive=True
):
# Check if file is excluded by extension.
f_rom_dir = Path(f_path, rom.fs_name)
file_name_lower = file_name.lower()
if any(
file_name_lower.endswith("." + ext) for ext in excluded_file_exts
@@ -396,6 +400,8 @@ class FSRomsHandler(FSHandler):
# Check if this is a top-level file (not in a subdirectory)
is_top_level = f_path.samefile(Path(abs_fs_path, rom.fs_name))
abs_file_path = Path(f_path, file_name)
if hashable_platform:
try:
if is_top_level:
@@ -403,7 +409,7 @@ class FSRomsHandler(FSHandler):
crc_c, rom_crc_c, md5_h, rom_md5_h, sha1_h, rom_sha1_h = (
await asyncio.to_thread(
self._calculate_rom_hashes,
Path(f_path, file_name),
abs_file_path,
rom_crc_c,
rom_md5_h,
rom_sha1_h,
@@ -413,7 +419,7 @@ class FSRomsHandler(FSHandler):
# Calculate individual file hash only
crc_c, _, md5_h, _, sha1_h, _ = await asyncio.to_thread(
self._calculate_rom_hashes,
Path(f_path, file_name),
abs_file_path,
)
except zlib.error:
crc_c = 0
@@ -424,11 +430,7 @@ class FSRomsHandler(FSHandler):
crc_c,
md5_h,
sha1_h,
chd_sha1_hash=(
extract_chd_hash(f_rom_dir)
if is_chd_file(f_rom_dir)
else ""
),
chd_sha1_hash=_chd_sha1_hash(abs_file_path),
)
else:
file_hash = FileHash(
@@ -568,9 +570,7 @@ class FSRomsHandler(FSHandler):
crc_c,
md5_h,
sha1_h,
chd_sha1_hash=(
extract_chd_hash(rom_dir) if is_chd_file(rom_dir) else ""
),
chd_sha1_hash=_chd_sha1_hash(rom_dir),
)
rom_files.append(
self._build_rom_file(

View File

@@ -914,6 +914,55 @@ class TestFSRomsHandler:
# Header SHA1 stored separately in chd_sha1_hash
assert parsed_rom_files.rom_files[0].chd_sha1_hash == internal_sha1
@pytest.mark.asyncio
async def test_get_rom_files_folder_extracts_chd_hash_per_file(
self, tmp_path: Path
):
"""Every CHD in a folder-based ROM keeps its own header SHA1."""
disc_platform = Platform(name="PlayStation", slug="psx", fs_slug="psx")
roms_path = tmp_path / disc_platform.fs_slug / "roms"
game_dir = roms_path / "Final Fantasy VII"
game_dir.mkdir(parents=True)
sha1_by_disc = {
"Final Fantasy VII (Disc 1).chd": "0123456789abcdef0123456789abcdef01234567",
"Final Fantasy VII (Disc 2).chd": "89abcdef0123456789abcdef0123456789abcdef",
}
for file_name, internal_sha1 in sha1_by_disc.items():
header = bytearray(124)
header[0:8] = b"MComprHD"
header[12:16] = int(5).to_bytes(4, "big")
header[84:104] = bytes.fromhex(internal_sha1)
(game_dir / file_name).write_bytes(header + file_name.encode())
(game_dir / "Final Fantasy VII.m3u").write_text("\n".join(sha1_by_disc) + "\n")
test_handler = FSRomsHandler()
test_handler.base_path = tmp_path
rom = Rom(
id=1,
fs_name="Final Fantasy VII",
fs_extension="",
fs_path=str(roms_path.relative_to(tmp_path)),
platform=disc_platform,
)
with patch(
"adapters.services.rahasher.RAHasherService.calculate_hash",
return_value="",
):
parsed_rom_files = await test_handler.get_rom_files(rom)
chd_hash_by_file = {
rom_file.file_name: rom_file.chd_sha1_hash
for rom_file in parsed_rom_files.rom_files
}
assert chd_hash_by_file == {
**sha1_by_disc,
"Final Fantasy VII.m3u": "",
}
@staticmethod
def _setup_archive_rom(
tmp_path: Path, platform: Platform, fs_name: str, fs_extension: str, data: bytes