Files
romm/backend/handler/filesystem/firmware_handler.py
Georges-Antoine Assi a7ecfd96a9 feat!: make filesystem.structure the single library layout
`filesystem.structure` now spells out the whole layout rather than only the part
below a platform folder. A template is relative to the library root, `{platform}`
marks the platform folder, and `{game}` marks where a game begins.

Merge `{gameFile}` and `{gameDir}` into one `{game}` terminal. The information a
terminal carried was the depth, not the kind: at a declared level a file is a
game of its own and a folder is one multi-file game, which a stat answers. That
drops the two-template idiom for a level holding both, and `_discover_default_roms`
with it, since the default scan is now just `roms/{platform}/{game}`.

Retire the auto-detected `{platform}/roms` layout, along with
`filesystem.roms_folder` and `filesystem.firmware_folder`, which named one path
segment each that a template now contains. Both are refused at startup with the
template that reproduces the layout, so an instance that relied on either says so
instead of scanning an empty library and marking every rom missing. Firmware gets
its own `structure.firmware` key rather than following the rom layout implicitly.

`get_roms_fs_structure` keeps meaning the folder a platform's games start in, so
the `.physical` sentinel paths already in the database still resolve. Per-platform
overrides have to agree with `default` on where the platform folder sits;
relocating one platform needs platform discovery and the watcher to union several
roots, which is left out.

The setup wizard reports `library_ready` plus the configured template instead of
a `struct_a`/`struct_b` discriminator, so the banner shows the layout in force
rather than guessing it client-side.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 08:39:21 -04:00

56 lines
2.0 KiB
Python

import binascii
import hashlib
from config import LIBRARY_BASE_PATH
from config.config_manager import config_manager as cm
from exceptions.fs_exceptions import FirmwareNotFoundException
from utils.hashing import crc32_to_hex
from .base_handler import FSHandler
class FSFirmwareHandler(FSHandler):
def __init__(self) -> None:
super().__init__(base_path=LIBRARY_BASE_PATH)
def get_firmware_fs_structure(self, fs_slug: str) -> str:
"""The platform's firmware folder, relative to the library root."""
return cm.get_config().firmware_structure.platform_path(fs_slug)
async def get_firmware(self, platform_fs_slug: str):
"""Gets all filesystem firmware for a platform
Args:
platform: platform where firmware belong
Returns:
list with all the filesystem firmware for a platform
"""
firmware_path = self.get_firmware_fs_structure(platform_fs_slug)
try:
fs_firmware_files = await self.list_files(path=firmware_path)
except FileNotFoundError as e:
raise FirmwareNotFoundException(
f"Firmware not found for platform {platform_fs_slug}"
) from e
return [f for f in self.exclude_single_files(fs_firmware_files)]
async def calculate_file_hashes(self, firmware_path: str, file_name: str) -> dict:
file_path = f"{firmware_path}/{file_name}"
async with await self.stream_file(file_path=file_path) as f:
crc_c = 0
md5_h = hashlib.md5(usedforsecurity=False)
sha1_h = hashlib.sha1(usedforsecurity=False)
# Read in chunks to avoid memory issues
while chunk := await f.read(8192):
md5_h.update(chunk)
sha1_h.update(chunk)
crc_c = binascii.crc32(chunk, crc_c)
return {
"crc_hash": crc32_to_hex(crc_c),
"md5_hash": md5_h.hexdigest(),
"sha1_hash": sha1_h.hexdigest(),
}