Files
romm/backend/handler/sync/retroarch/sync_handler.py
Zach Clendenen 38dfb208f0 fix(streaming): file RetroArch streaming exits as raw saves (#5174)
* fix(streaming): keep foreign saves offered when the import-spec check fails

A broker that misses the 5 s import-spec deadline used to read as "imports
nothing": the Saves tab vanished and foreign picks were refused with
"Save is not a restorable archive". Keep the last good answer in Redis so
every worker can fall back on it, cache 404s for five minutes instead of
forever, answer a pick that can't be checked with a retryable 503, and
refetch the streaming config whenever the Stream page mounts.

* fix(streaming): code-review fixes for the import-spec fallback

Hold a fallback answer for 10 s so one claim's checks don't each wait out
the broker timeout, read the stored answer back through the broker
response parser, and warn about a pre-import broker once per worker
instead of at every cache expiry.

* refactor(streaming): simplify pass on the import-spec fallback

Make ImportSpecUnavailable the 503 itself so the save and state pickers
no longer convert it by hand, and store the broker's own response body
as the last answer instead of rebuilding its shape.

* chore(streaming): review-polish on the import-spec fallback

Trim the cache comments and the exception docstring, fit the 503 detail
within the line limit, and annotate the fresh-worker test dict for mypy.

* docs(streaming): humanizer pass on import-spec comments and test docstrings

* fix(streaming): tell lasting import-spec failures from passing ones

Only timeouts, connection errors, 5xx and 429 fall back to the broker's
last answer. A refused request (401, other 4xx) or a reply RomM can't read
caches no imports for 30 s and logs an error, so a pick gets a refusal
instead of an endless retry. A 404 from a broker that answered before
uses that answer, since it is likelier a restart behind a proxy than a
downgrade.

* fix(streaming): ask for a retry when a picked state can't be checked

An older save with the newest state resumes the pick only through an
import, so an unanswered import-spec check now returns the 503 instead of
launching from the older archive's own state.

* fix(streaming): share an in-flight config request

App load and the Stream page both fetch the config; two requests could land
out of order and leave the older answer in place. (Stability item 5.)

* fix(streaming): hold the resume picker until the fresh config lands

What the broker imports decides which saves and states are on offer, and the
app-load config can predate its answer. The panel shows a spinner until the
Stream page's own refetch settles, so a pick can't vanish under the player.
(Stability item 4.)

* fix(streaming): ask every platform's broker at once on /config

The checks ran on the default thread pool (cpu+4 threads), so a broker
serving more platforms than that cost one timeout per wave: 10.7 s for 14
platforms on a 6-CPU dev box with a silent broker. The Stream page's picker
now waits on /config, so the checks get a pool of their own.

* fix(streaming): address Greptile review on the import-spec fallback

- Play waits on the fresh config with the resume picker, so it can't send a
  preselected state the player can't see yet.
- The streaming store gets a reset() for logout, and a config reply asked for
  as the previous account is dropped instead of landing in the next one.
- The configFresh write after the refetch checks useIsAlive().
- Trimmed the require_import_spec docstring, the configFresh comment and the
  /config pool comment to the house length.
- Declined: a worker-wide pool for the /config checks. Each request's threads
  end within the broker timeout, and a shared bounded pool would bring back
  timeout waves under concurrent loads.

* fix(streaming): share /config broker checks across requests

Addresses Greptile's re-raised finding on #5174 that each /config request
opened its own pool of up to 64 threads, so overlapping page loads during
a silent broker multiplied the worker's threads.

- The checks run in one worker-wide pool, and a check already in flight
  for the same container and platform is joined rather than asked again,
  so concurrent page loads hold no more threads than one does, with no
  queued-timeout waves.
- Callers await the shared run through asyncio.shield, so a request that
  disconnects doesn't cancel the check others are waiting on.

* fix(streaming): file a RetroArch exit's save raw and boot the newest save

A RetroArch exit holding one .srm and its states is no longer stored as a
.saves.zip: the .srm is filed bare under the core's emulator name with a
timestamped name, and each state goes through the state store. Exits with
more than one save, a memory card, or unlabelled members still keep the zip.

A launch with no pick now boots the newest save, raw or zip. A newer bare
.srm goes through the broker's save import when the broker accepts one,
and the launch screen defaults to the same save.

* fix(streaming): keep the exit zip when a raw save can't be filed safely

pr-ready step 2 (code review).

- Keep the zip for a core that isn't one plain folder name, a malformed
  manifest kind, a member that fails to unpack, or a broker that doesn't
  import saves.
- Fall back to the newest archive when the default raw save fails to import.
- File exit states oldest first; one unreadable stored state no longer
  drops the rest.
- Rename locals that shadowed the archive module; fix a test return type
  and a stale comment.

* refactor(streaming): simplify the default save comparison

pr-ready step 3 (simplify): default_save reuses _newest's ordering, and newestSave takes the first match in the newest-first list.

* docs(streaming): trim the raw-save docstrings

pr-ready step 4 (review-polish).

* fix(streaming): rank saves by their last write and read .ZIP as an archive

The web player writes into its existing save row, which only moves
updated_at, so ranking by created_at let a stream with no pick boot an
older streaming zip over newer web-player progress. The backend default
and the launch screen now rank by updated_at, and the picker dates rows
by it. Upper-case .ZIP saves are now read as archives on both sides, to
match the web player's case-insensitive guard.

* feat(states): hash states so duplicate checks skip reading files

Adds states.content_hash (migration 0149), filled on every state write:
streaming capture, the states upload and update endpoints, the scan and
the RetroArch sync push. The streaming duplicate check compares hashes
and falls back to reading the file for rows stored before this, which
are not backfilled.

* fix(streaming): leave an untouched default off the claim and skip missing saves

Review items 1 and 2: a default sent as a pick bypassed the zip fallback, and a save marked missing could become the newest.

* fix(streaming): code-review fixes for raw exit filing and state hashing

pr-ready step 2: keep the zip when a state can't be filed, don't treat a missing state file as a duplicate, fall back to the native archive when a defaulted save fails, hash off the event loop, count filed states as new.

* docs(streaming): review-polish, trim the newest-save comment

* fix(streaming): bind the background exit pull's states to the swapped disc

Decision 1: the exit save job filed states without the session's disc id, so on a multi-disc game its copy could lose the disc binding. The second copy is still dropped by the hash dedupe.

* fix(streaming): keep the exit zip when one of its states fails to store

Decision 2: a state that threw on store was logged and lost, since the zip holding it was dropped. The zip holds the same .srm, so booting it as the default loses nothing.

* fix(sync): keep updated_at when RetroArch sync flags a vanished save or state

* fix(streaming): record a legacy state's hash when dedupe reads its bytes

* fix(streaming): file a RetroArch exit's auto state raw instead of keeping the zip

The auto state now goes into the state history under its own capture
stamp, so an exit carrying one files its .srm raw like any other. It is
never hydrated, and a resume pick of it is refused, since the broker
would file it into its numbered working slot.

* fix(streaming): hydrate the newest state still on disk

A newer row sync flagged as gone from disk made hydrate push nothing.
It now skips such rows the way the save picks already do.

* docs(streaming): note the auto-state refusal in resolve_resume_state

* fix(streaming): resume a RetroArch auto state like any other state

The broker files any pushed state into its working slot, so .state.auto
reads as slot 0 like a bare .state. Hydrate and the resume pick now push
it instead of skipping or refusing it.

* fix(streaming): address Greptile review on raw RetroArch exits

- A default .srm the broker refuses at activate now boots the newest
  native archive instead of failing the launch. Only a default save
  that rode the import alone falls back; a hand pick still fails.
- Exit states the history hasn't seen are filed just under the next
  state it already holds, so an older slot no longer heads the history
  over the exit capture teardown filed first.
- A raw exit save is skipped only when it matches the newest usable
  save, so returning to an older save records the play, and a match on
  a row whose file is gone no longer deletes the new copy.
- Stream.test.ts clicks the rendered play button instead of casting
  the view model to call onPlay.
- Trim the raw-exit docstrings to one sentence.

* fix(streaming): reserve a free version name for a raw RetroArch exit save

Addresses Greptile: the exit's .srm took a second-resolution tag without
checking whether another save held that name, so it could overwrite one.
Also compares updated_at naively in the mark-missing test, which failed on
PostgreSQL's UTC-aware column.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(streaming): file each RetroArch exit state on the disc it was saved on

Addresses Greptile: every state in the exit archive took the session's
final disc, so a hotkey slot saved before a swap resumed on the wrong one.
The session now keeps each swap with its time, and a session that ran more
than one disc files each state by its archive timestamp.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 20:44:14 -04:00

705 lines
24 KiB
Python

"""Maps RetroArch Cloud Sync's ``<root>/<core>/<file>`` namespace onto RomM's
per-ROM asset storage, and back again for the ``manifest.server`` it diffs."""
from __future__ import annotations
import asyncio
import os
import re
from collections import defaultdict
from collections.abc import Awaitable, Callable, Collection, Iterable, Sequence
from dataclasses import dataclass
from datetime import datetime
from functools import partial
from typing import Literal, NamedTuple
from fastapi import UploadFile
from config import MAX_SAVES_PER_SLOT
from handler.asset_store import (
prune_save_slot,
reserve_version_name,
strip_datetime_tag,
)
from handler.database import (
db_rom_handler,
db_save_handler,
db_screenshot_handler,
db_state_handler,
)
from handler.filesystem import fs_asset_handler, fs_retroarch_sync_handler
from handler.filesystem.assets_handler import hash_save_content
from handler.redis_handler import async_cache
from handler.scan_handler import scan_save
from handler.sync.retroarch import psp
from handler.sync.retroarch.emulator_names import (
to_retroarch_dir_name,
to_romm_emulator,
)
from logger.logger import log
from models.assets import (
AUTOSAVE_SLOT,
EMULATOR_MAX_LENGTH,
Save,
SaveLineage,
Screenshot,
State,
)
from models.rom import Rom
from models.user import User
from utils.filesystem import check_filename_length
AssetKind = Literal["saves", "states"]
MANIFEST_FILE_NAME = "manifest.server"
ASSET_ROOTS: tuple[AssetKind, ...] = ("saves", "states")
# RetroArch's config/thumbnails/system sync categories belong to no ROM, so
# they're kept as opaque per-user blobs.
BLOB_CATEGORIES = ("config", "thumbnails", "system")
# `<game>.state`, `<game>.state3`, `<game>.state.auto`: the auto suffix makes
# this a two-segment extension, which splitext alone gets wrong.
STATE_SUFFIX_PATTERN = re.compile(r"\.state\d*(?:\.auto)?$", re.IGNORECASE)
# Rehashing every asset on each manifest fetch would read gigabytes. Keys carry
# size and mtime, so a changed file misses the cache instead of going stale.
HASH_CACHE_TTL_SECONDS = 60 * 60 * 24
HASH_CONCURRENCY = 8
@dataclass(frozen=True)
class RetroArchSyncPath:
"""A parsed client-side path, e.g. ``saves/Snes9x/Super Mario World.srm``."""
kind: AssetKind
emulator: str | None
file_name: str
@property
def is_state_screenshot(self) -> bool:
return self.kind == "states" and is_state_screenshot_path(self.file_name)
def split_segments(path: str) -> list[str] | None:
"""A client path's non-empty segments, or None if any is `.`, `..` or holds a NUL."""
segments = [segment for segment in path.strip("/").split("/") if segment]
if any(
segment in (os.curdir, os.pardir) or "\x00" in segment for segment in segments
):
return None
return segments
def emulator_from_dir_name(dir_name: str) -> str | None:
"""RomM's `emulator` for a core folder name, or None if too long to store."""
emulator = to_romm_emulator(dir_name)
return emulator if len(emulator) <= EMULATOR_MAX_LENGTH else None
def parse_retroarch_sync_path(path: str) -> RetroArchSyncPath | None:
"""Parse a ``<root>/[<core>/]<file>`` client path, or None if unsupported."""
segments = split_segments(path)
if segments is None or not 2 <= len(segments) <= 3:
return None
if segments[0] not in ASSET_ROOTS:
return None
kind = segments[0]
emulator = None
if len(segments) == 3:
emulator = emulator_from_dir_name(segments[1])
if emulator is None:
return None
return RetroArchSyncPath(kind=kind, emulator=emulator, file_name=segments[-1])
def is_state_screenshot_path(file_name: str) -> bool:
"""Whether a `states/...` file is the `<state file name>.png` RetroArch syncs."""
return file_name.lower().endswith(".png")
def state_name_of_screenshot(file_name: str) -> str:
"""The state file name a `<state file name>.png` screenshot belongs to."""
return file_name[: -len(".png")]
def game_name_from_file_name(kind: AssetKind, file_name: str) -> str:
"""The ROM file name (minus extension) an asset file belongs to."""
if kind == "states":
base = (
state_name_of_screenshot(file_name)
if is_state_screenshot_path(file_name)
else file_name
)
stripped = STATE_SUFFIX_PATTERN.sub("", base)
if stripped != base:
return stripped
return os.path.splitext(base)[0]
return os.path.splitext(file_name)[0]
def state_slot_suffix(file_name: str) -> str:
"""The RetroArch load slot (``state``, ``state1``, ``state.auto``) a state name ends in."""
match = STATE_SUFFIX_PATTERN.search(file_name)
if match:
return match.group(0)[1:].lower()
return os.path.splitext(file_name)[1][1:].lower()
def recency_key(asset: Save | State) -> tuple[datetime, int]:
"""Recency order, with `id` breaking ties on a shared timestamp."""
return (asset.updated_at, asset.id)
def group_states_by_slot(
states: Iterable[State],
) -> dict[tuple[int, str | None, str], State]:
"""The newest state in each (rom, emulator, slot) bucket."""
latest: dict[tuple[int, str | None, str], State] = {}
for state in states:
key = (state.rom_id, state.emulator, state_slot_suffix(state.file_name))
current = latest.get(key)
if current is None or recency_key(state) > recency_key(current):
latest[key] = state
return latest
def canonical_state_file_name(rom: Rom, slot_suffix: str) -> str:
"""The name RetroArch gives this slot, which the manifest advertises."""
return f"{rom.fs_name_no_ext}.{slot_suffix}"
def resolve_state_by_slot(
user: User, rom: Rom, emulator: str | None, requested_file_name: str
) -> State | None:
"""The newest state in the slot a canonical name points at, whatever its name."""
states = db_state_handler.get_states(user_id=user.id, rom_ids=[rom.id])
key = (rom.id, emulator, state_slot_suffix(requested_file_name))
return group_states_by_slot(states).get(key)
# RetroArch's save RAM, the one save file that goes through slots. Companions
# such as `.rtc` stay single unslotted files, since a slot holds one save.
SLOTTED_SAVE_EXTENSION = "srm"
def save_lineage(emulator: str | None, file_name: str) -> SaveLineage | None:
"""The `autosave` lineage a RetroArch save path maps to, or None for an unslotted file."""
lineage = SaveLineage.of(emulator, file_name)
return lineage if lineage.file_extension == SLOTTED_SAVE_EXTENSION else None
def advertised_save_name(save: Save) -> str:
"""A version's name without its tag when that still names its ROM, else RetroArch's name for it."""
name = strip_datetime_tag(save.file_name)
if (
game_name_from_file_name("saves", name).lower()
== save.rom.fs_name_no_ext.lower()
):
return name
return f"{save.rom.fs_name_no_ext}{os.path.splitext(name)[1]}"
def _unslotted_save_at(
user: User, rom: Rom, emulator: str | None, file_name: str
) -> Save | None:
save = db_save_handler.get_save_by_path(
user_id=user.id,
rom_id=rom.id,
file_path=build_asset_file_path(user, rom, "saves", emulator),
file_name=file_name,
)
return save if save and save.slot is None else None
def resolve_save(
user: User, rom: Rom, emulator: str | None, requested_file_name: str
) -> Save | None:
"""The save a RetroArch path serves: the newest `autosave` version, else the unslotted save there."""
lineage = save_lineage(emulator, requested_file_name)
head = (
db_save_handler.get_lineage_head(
user_id=user.id, rom_id=rom.id, slot=AUTOSAVE_SLOT, lineage=lineage
)
if lineage
else None
)
return head or _unslotted_save_at(user, rom, emulator, requested_file_name)
def saves_at_path(
user: User, rom: Rom, emulator: str | None, requested_file_name: str
) -> list[Save]:
"""Every save a RetroArch path covers, so deleting it leaves nothing to resurface."""
lineage = save_lineage(emulator, requested_file_name)
if not lineage:
unslotted = _unslotted_save_at(user, rom, emulator, requested_file_name)
return [unslotted] if unslotted else []
versions = db_save_handler.get_saves(
user_id=user.id, rom_ids=[rom.id], slot=AUTOSAVE_SLOT, lineage=lineage
)
# The manifest hides these behind the head, so whatever their case they
# would resurface once the versions go.
folded = requested_file_name.casefold()
hidden = [
save
for save in db_save_handler.get_saves(
user_id=user.id, rom_ids=[rom.id], slot_is_null=True, lineage=lineage
)
if save.file_name.casefold() == folded
]
return [*versions, *hidden]
SaveUpload = Literal["created", "updated", "unchanged"]
async def store_save(
user: User,
rom: Rom,
emulator: str | None,
requested_file_name: str,
body: UploadFile,
) -> SaveUpload:
"""File an upload as a new `autosave` version, or over the unslotted file it names.
Raises:
ValueError: The timestamped version name is too long.
"""
lineage = save_lineage(emulator, requested_file_name)
if not lineage:
return await _store_unslotted_save(
user, rom, emulator, requested_file_name, body
)
head = await asyncio.to_thread(
db_save_handler.get_lineage_head,
user_id=user.id,
rom_id=rom.id,
slot=AUTOSAVE_SLOT,
lineage=lineage,
)
# A re-sent file adds no version, so it can't push real history past the cap.
content_hash = hash_save_content(body.file)
if head and content_hash and content_hash == head.content_hash:
return "unchanged"
asset_path = build_asset_file_path(user, rom, "saves", emulator)
version_name = await reserve_version_name(
user.id, rom.id, asset_path, requested_file_name
)
check_filename_length(version_name)
await fs_asset_handler.write_file(file=body, path=asset_path, filename=version_name)
scanned = await scan_save(
file_name=version_name,
user=user,
platform_fs_slug=rom.platform.fs_slug,
rom_id=rom.id,
emulator=emulator,
)
scanned.rom_id = rom.id
scanned.user_id = user.id
scanned.emulator = emulator
scanned.slot = AUTOSAVE_SLOT
await asyncio.to_thread(db_save_handler.add_save, save=scanned)
if MAX_SAVES_PER_SLOT:
await prune_save_slot(
user.id, rom.id, AUTOSAVE_SLOT, MAX_SAVES_PER_SLOT, lineage=lineage
)
return "created"
async def _store_unslotted_save(
user: User,
rom: Rom,
emulator: str | None,
file_name: str,
body: UploadFile,
) -> SaveUpload:
existing = await asyncio.to_thread(
_unslotted_save_at, user, rom, emulator, file_name
)
asset_path = build_asset_file_path(user, rom, "saves", emulator)
await fs_asset_handler.write_file(file=body, path=asset_path, filename=file_name)
scanned = await scan_save(
file_name=file_name,
user=user,
platform_fs_slug=rom.platform.fs_slug,
rom_id=rom.id,
emulator=emulator,
)
if existing:
await asyncio.to_thread(
db_save_handler.update_save,
existing.id,
{
"file_size_bytes": scanned.file_size_bytes,
"content_hash": scanned.content_hash,
"missing_from_fs": False,
},
)
return "updated"
scanned.rom_id = rom.id
scanned.user_id = user.id
scanned.emulator = emulator
await asyncio.to_thread(db_save_handler.add_save, save=scanned)
return "created"
def state_screenshot_dir(user: User, rom: Rom, emulator: str | None) -> str:
"""Per core, so same-named states under two cores keep separate screenshots."""
return fs_asset_handler.build_screenshots_file_path(
user=user,
platform_fs_slug=rom.platform.fs_slug,
rom_id=rom.id,
emulator=emulator,
)
def _match_state_screenshot(
user: User, state: State, screenshots: Iterable[Screenshot]
) -> Screenshot | None:
own_dir = state_screenshot_dir(user, state.rom, state.emulator)
rom_dir = state_screenshot_dir(user, state.rom, None)
candidates = [
s
for s in screenshots
if s.file_path == own_dir or not s.file_path.startswith(f"{rom_dir}/")
]
exact_name = f"{state.file_name}.png"
exact = [s for s in candidates if s.file_name == exact_name]
if exact:
return min(exact, key=lambda s: (s.file_path != own_dir, -s.id))
# `State.screenshot` matches on the name stem, which a RetroArch slot name
# (`<rom>.state1`) shares with every other slot's and gallery shot.
if state.file_name_no_ext == state.rom.fs_name_no_ext:
return None
names = {state.file_name, state.file_name_no_ext}
stem_matches = [
s for s in candidates if s.file_name in names or s.file_name_no_ext in names
]
if not stem_matches:
return None
best = min(
stem_matches, key=lambda s: (s.file_name_no_ext != state.file_name, -s.id)
)
return None if best.is_gallery else best
def state_screenshots(user: User, states: Collection[State]) -> dict[int, Screenshot]:
"""The screenshot synced alongside each of these states, by state id."""
if not states:
return {}
by_rom: defaultdict[int, list[Screenshot]] = defaultdict(list)
for screenshot in db_screenshot_handler.get_screenshots(
user_id=user.id, rom_ids={state.rom_id for state in states}
):
by_rom[screenshot.rom_id].append(screenshot)
matches = {
state.id: _match_state_screenshot(user, state, by_rom[state.rom_id])
for state in states
}
return {state_id: shot for state_id, shot in matches.items() if shot}
def resolve_state_screenshot_by_slot(
user: User, rom: Rom, emulator: str | None, requested_file_name: str
) -> Screenshot | None:
"""The screenshot of the state a ``<slot>.png`` name resolves to."""
state = resolve_state_by_slot(
user, rom, emulator, state_name_of_screenshot(requested_file_name)
)
return state_screenshots(user, [state]).get(state.id) if state else None
def build_retroarch_sync_path(
kind: AssetKind, emulator: str | None, file_name: str
) -> str:
if emulator:
return f"{kind}/{to_retroarch_dir_name(emulator)}/{file_name}"
return f"{kind}/{file_name}"
def build_asset_file_path(
user: User, rom: Rom, kind: AssetKind, emulator: str | None
) -> str:
build_path = (
fs_asset_handler.build_saves_file_path
if kind == "saves"
else fs_asset_handler.build_states_file_path
)
return build_path(
user=user,
platform_fs_slug=rom.platform.fs_slug,
rom_id=rom.id,
emulator=emulator,
)
def parse_retroarch_sync_blob_path(path: str) -> str | None:
"""A blob-category client path as ``category/...``, or None if it isn't one."""
segments = split_segments(path)
if segments is None or len(segments) < 2:
return None
if segments[0] not in BLOB_CATEGORIES:
return None
return "/".join(segments)
def user_blob_path(user: User, blob_path: str) -> str:
"""Where a parsed blob path lives under the assets root, namespaced by user."""
return f"{fs_asset_handler.user_folder_path(user)}/retroarch/{blob_path}"
def _decoded(value: str | bytes | None) -> str | None:
if isinstance(value, bytes):
return value.decode()
return value or None
async def cached_hashes(
jobs: Sequence[tuple[str, Callable[[], Awaitable[str | None]]]],
) -> list[str | None]:
"""Each `(cache key, compute)` job's hash, read in one MGET with misses written back in one pipeline."""
if not jobs:
return []
digests = [
_decoded(value) for value in await async_cache.mget([k for k, _ in jobs])
]
misses = [i for i, digest in enumerate(digests) if digest is None]
semaphore = asyncio.Semaphore(HASH_CONCURRENCY)
async def compute(index: int) -> str | None:
async with semaphore:
return await jobs[index][1]()
computed = await asyncio.gather(*(compute(i) for i in misses))
fresh = {i: digest for i, digest in zip(misses, computed, strict=True) if digest}
if fresh:
async with async_cache.pipeline() as pipe:
for i, digest in fresh.items():
await pipe.set(jobs[i][0], digest, ex=HASH_CACHE_TTL_SECONDS)
await pipe.execute()
for i, digest in fresh.items():
digests[i] = digest
return digests
async def _blob_md5(disk_path: str) -> str | None:
try:
return await fs_retroarch_sync_handler.compute_file_md5(disk_path)
except (ValueError, OSError):
return None
async def build_blob_manifest_entries(user: User) -> list[dict[str, str]]:
blob_paths: list[str] = []
jobs: list[tuple[str, Callable[[], Awaitable[str | None]]]] = []
for category in BLOB_CATEGORIES:
prefix = user_blob_path(user, category)
for blob in await fs_retroarch_sync_handler.list_blob_files(prefix):
blob_path = f"{category}/{blob.relative_path}"
blob_paths.append(blob_path)
jobs.append(
(
f"romm:retroarch_sync:blob_md5:{user.id}:{blob_path}:{blob.size}:{blob.mtime}",
partial(_blob_md5, f"{prefix}/{blob.relative_path}"),
)
)
digests = await cached_hashes(jobs)
return [
{"path": blob_path, "hash": digest}
for blob_path, digest in zip(blob_paths, digests, strict=True)
if digest
]
def resolve_roms(
game_names: Iterable[str], can_see: Callable[[Rom], bool]
) -> dict[str, Rom]:
"""The ROM each game name belongs to, the first visible one by id when ambiguous."""
names = set(game_names)
exact: dict[str, Rom] = {}
# The lookup ignores case, so the client's exact spelling wins a tie.
folded: dict[str, Rom] = {}
for rom in db_rom_handler.get_roms_by_fs_names_no_ext(names):
if can_see(rom):
exact.setdefault(rom.fs_name_no_ext, rom)
folded.setdefault(rom.fs_name_no_ext.casefold(), rom)
resolved = {name: exact.get(name) or folded.get(name.casefold()) for name in names}
return {name: rom for name, rom in resolved.items() if rom}
def resolve_rom(game_name: str, can_see: Callable[[Rom], bool]) -> Rom | None:
return resolve_roms([game_name], can_see).get(game_name)
def _mark_missing_from_fs(asset: Save | State | Screenshot) -> None:
update = {"missing_from_fs": True}
# Not a write, so updated_at keeps ranking the asset.
if isinstance(asset, Save):
db_save_handler.update_save(asset.id, update, touch=False)
elif isinstance(asset, State):
db_state_handler.update_state(asset.id, update, touch=False)
else:
db_screenshot_handler.update_screenshot(asset.id, update)
async def _asset_md5(asset: Save | State | Screenshot) -> str | None:
try:
return await fs_asset_handler.compute_file_md5(asset.full_path)
except FileNotFoundError:
_mark_missing_from_fs(asset)
except OSError as exc:
log.debug(f"Failed to compute MD5 for {asset.full_path}: {exc}")
return None
async def asset_md5s(
assets: Sequence[Save | State | Screenshot],
) -> list[str | None]:
"""Each asset's MD5, or None when unreadable; a vanished file flags its row."""
return await cached_hashes(
[
(
f"romm:retroarch_sync:md5:{asset.full_path}"
f":{asset.file_size_bytes}:{asset.updated_at.timestamp()}",
partial(_asset_md5, asset),
)
for asset in assets
]
)
class ManifestAsset(NamedTuple):
path: str
asset: Save | State | Screenshot
def _manifest_assets(
user: User, can_see: Callable[[Rom], bool], tree: AssetKind | None = None
) -> tuple[list[ManifestAsset], Sequence[Save]]:
"""The manifest's saves and states (only `tree`'s when given), plus the unslotted saves its PSP bundles come from."""
saves: Sequence[Save] = []
heads: Sequence[Save] = []
if tree != "states":
saves = db_save_handler.get_saves(user_id=user.id, slot_is_null=True)
heads = db_save_handler.get_lineage_heads(
user_id=user.id, slot=AUTOSAVE_SLOT, file_extension=SLOTTED_SAVE_EXTENSION
)
served_by_head = {(head.rom_id, head.emulator) for head in heads}
listed_saves = [
(advertised_save_name(head), head) for head in heads if can_see(head.rom)
] + [
(save.file_name, save)
for save in saves
if not save.missing_from_fs
and can_see(save.rom)
and not psp.is_psp_bundle_file_name(save.file_name)
and not (
save_lineage(save.emulator, save.file_name)
and (save.rom_id, save.emulator) in served_by_head
)
]
listed_states = (
[
(emulator, canonical_state_file_name(state.rom, slot_suffix), state)
for (_rom_id, emulator, slot_suffix), state in group_states_by_slot(
db_state_handler.get_states(user_id=user.id)
).items()
if not state.missing_from_fs and can_see(state.rom)
]
if tree != "saves"
else []
)
# A path carries no platform, so only the ROM that GET/PUT/DELETE would
# resolve it to may claim it; a same-named ROM elsewhere would shadow it.
owners = resolve_roms(
[game_name_from_file_name("saves", name) for name, _ in listed_saves]
+ [game_name_from_file_name("states", name) for _, name, _ in listed_states],
can_see,
)
def is_addressable(rom: Rom, kind: AssetKind, file_name: str) -> bool:
owner = owners.get(game_name_from_file_name(kind, file_name))
return owner is not None and owner.id == rom.id
assets = [
ManifestAsset(build_retroarch_sync_path("saves", save.emulator, name), save)
for name, save in listed_saves
if is_addressable(save.rom, "saves", name)
]
addressable_states = [
(emulator, file_name, state)
for emulator, file_name, state in listed_states
if is_addressable(state.rom, "states", file_name)
]
screenshots = state_screenshots(user, [state for _, _, state in addressable_states])
for emulator, file_name, state in addressable_states:
state_path = build_retroarch_sync_path("states", emulator, file_name)
assets.append(ManifestAsset(state_path, state))
screenshot = screenshots.get(state.id)
if screenshot and not screenshot.missing_from_fs:
assets.append(ManifestAsset(f"{state_path}.png", screenshot))
return assets, saves
async def list_manifest_paths(
user: User, can_see: Callable[[Rom], bool], tree: AssetKind
) -> list[str]:
"""`tree`'s manifest paths, sorted, without hashing or listing blobs."""
assets, saves = await asyncio.to_thread(_manifest_assets, user, can_see, tree)
paths = [entry.path for entry in assets]
paths += await psp.list_psp_member_paths(saves, can_see)
return sorted(paths)
async def build_manifest(
user: User, can_see: Callable[[Rom], bool]
) -> list[dict[str, str]]:
"""The server manifest RetroArch diffs against, sorted by path."""
# Off the event loop: on a large library the queries run for seconds and
# would stall every other request the worker is serving.
assets, saves = await asyncio.to_thread(_manifest_assets, user, can_see)
digests = await asset_md5s([entry.asset for entry in assets])
hashed = [
(entry, digest) for entry, digest in zip(assets, digests, strict=True) if digest
]
hashed_paths = {entry.path for entry, _ in hashed}
entries = [
{"path": entry.path, "hash": digest}
for entry, digest in hashed
# A state screenshot is listed only alongside its state.
if not isinstance(entry.asset, Screenshot)
or entry.path.removesuffix(".png") in hashed_paths
]
entries += await build_blob_manifest_entries(user)
entries += await psp.build_psp_manifest_entries(saves, can_see)
entries.sort(key=lambda entry: entry["path"])
return entries