feat: opt-in per-platform subfolder scanning with non-destructive identity

Adds opt-in, per-platform subfolder scanning (closes #2050). When enabled
for a platform, RomM recurses that platform's subfolders and treats each
nested file as its own ROM, instead of collapsing a subfolder into one
multi-file ROM — so libraries organized into Hacks/, Translations/,
Homebrew/, etc. are scanned correctly.

Enable per platform in config.yml (default off, existing setups unaffected):

  scan:
    subfolders:
      nes: true                       # recurse every subfolder
      snes: ["Hacks", "Translations"] # recurse only these; keep others whole

Key difference from a naive path-identity approach: identity is reconciled
by content hash on scan. Moving or renaming a ROM between subfolders is
detected by its hash against a now-missing ROM and relocated in place, so
saves, play history, favorites and collection membership follow it. Falls
back to path identity when hashing is unavailable (skip_hash_calculation or
a non-hashable platform). A cheap size pre-filter avoids hashing every
newly-seen file (e.g. on first enable).

Details:
- Per-platform opt-in via scan.subfolders (fs_slug -> bool | list[str]); no
  DB schema change.
- Recursion skips hidden (dot-prefixed) folders, and keeps a folder whole as
  a single multi-file ROM when it holds a disc/playlist descriptor
  (.m3u/.cue/.gdi/.ccd/.toc) — covers cue+bin / multi-disc games.
- Path-based keying for the steady-state lookup (get_roms_by_fs_name and
  mark_missing_roms key on full path) so identically-named files in different
  subfolders stay distinct; the (platform_id, fs_name) index is non-unique.
- File resolution (download/delete/hash) uses the ROM's stored fs_path.
- Scan log flags entries that became "missing" because a folder is now
  recursed, so stale entries are easy to clean up.
- Frontend: the Files tab shows a ROM's on-disk Location (click-to-copy),
  built as a shared LocationChip mirroring HashChip's RTag-based pattern.
  New i18n keys added to all locales.

Tests: backend unit tests for recursion (collisions, hidden folders,
descriptor dirs kept whole, named-list form), full-path keying in
get_roms_by_fs_name / mark_missing_roms, and hash-based relocation.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sk2dzM3K9qxWBdPAeGb7us
This commit is contained in:
Claude
2026-06-21 20:46:50 +00:00
parent 49f2b1659c
commit 5b494c3bc4
28 changed files with 951 additions and 100 deletions

View File

@@ -1171,7 +1171,11 @@ class DBRomsHandler(DBBaseHandler):
fs_names: Iterable[str],
session: Session = None, # type: ignore
) -> dict[str, Rom]:
"""Retrieve a dictionary of roms by their filesystem names.
"""Retrieve a dictionary of roms keyed by their full path (fs_path/fs_name).
Filters by file name for an indexed lookup, but keys the result on the
full path so identically-named files in different subfolders (subfolder
scanning) remain distinct.
Eager-loads only `platform` (used downstream by the scan loop via
`rom.platform_slug` / `rom.platform.fs_slug`). This deliberately
@@ -1195,7 +1199,7 @@ class DBRomsHandler(DBBaseHandler):
.all()
)
return {rom.fs_name: rom for rom in roms}
return {rom.full_path: rom for rom in roms}
@begin_session
def update_rom(
@@ -1278,19 +1282,23 @@ class DBRomsHandler(DBBaseHandler):
) -> Sequence[Rom]:
"""Sync `missing_from_fs` for a platform against the keep-list.
The keep-list holds rom full paths (fs_path/fs_name) so that
identically-named files in different subfolders are tracked
independently when subfolder scanning is enabled.
Reads the rows once and writes only those whose state actually
changes, so a re-scan of an unchanged platform issues no updates.
"""
keep_set = set(fs_roms_to_keep)
rows = session.execute(
select(Rom.id, Rom.fs_name, Rom.missing_from_fs).where(
select(Rom.id, Rom.fs_path, Rom.fs_name, Rom.missing_from_fs).where(
Rom.platform_id == platform_id
)
).all()
flips: dict[bool, list[int]] = {True: [], False: []}
for rom_id, fs_name, was_missing in rows:
is_missing = fs_name not in keep_set
for rom_id, fs_path, fs_name, was_missing in rows:
is_missing = f"{fs_path}/{fs_name}" not in keep_set
if is_missing != was_missing:
flips[is_missing].append(rom_id)
@@ -1305,8 +1313,12 @@ class DBRomsHandler(DBBaseHandler):
return (
session.scalars(
# `fs_path` is eager-loaded alongside `fs_name` so callers can
# read `rom.full_path` after the session closes (the returned
# instances are detached); without it that lazy-loads and raises
# DetachedInstanceError.
select(Rom)
.options(load_only(Rom.id, Rom.fs_name))
.options(load_only(Rom.id, Rom.fs_name, Rom.fs_path))
.where(
and_(
Rom.platform_id == platform_id,
@@ -1319,6 +1331,40 @@ class DBRomsHandler(DBBaseHandler):
.all()
)
@begin_session
def get_roms_for_relocation(
self,
platform_id: int,
session: Session = None, # type: ignore
) -> Sequence[Rom]:
"""Light-weight load of a platform's roms for relocation matching.
Returns detached rom instances carrying only the columns needed to
detect a moved/renamed file by content (identity hashes + size) and to
resolve its current `full_path`. Used by the scan loop to relocate a
rom whose on-disk path changed instead of re-importing it as new.
"""
return (
session.scalars(
select(Rom)
.options(
load_only(
Rom.id,
Rom.fs_name,
Rom.fs_path,
Rom.fs_size_bytes,
Rom.crc_hash,
Rom.md5_hash,
Rom.sha1_hash,
Rom.ra_hash,
)
)
.where(Rom.platform_id == platform_id)
)
.unique()
.all()
)
@begin_session
def add_rom_user(
self,