mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-24 02:27:52 -06:00
refactor: find held records by the resolver rule
The gap report counted a file held on any crc32 hit, without the declared size and even against a contradicting stronger hash. The pack check split md5 lists by hand and took a loose ROM for the member a zipped_file md5 names. Both now ask held_record_for, the resolver's hash steps. The pack check still compares whole digests, as Batocera does, so a cut md5 names a file without satisfying it. records_for_hash returns every held record an index value names. The variant search reads it instead of its own kind-to-index table. Any one accepted value still nominates a candidate there, because the validation index merges what several emulators accept and their own checks judge each file.
This commit is contained in:
1 parent
4acfd8f763
commit
331f244aac
8 files changed
+178
-55
No files matched your search
@@ -97,26 +97,33 @@ def sha1_at(db: dict, path: str | None) -> str | None:
|
||||
_HASH_INDEXES = {"md5": "by_md5", "crc32": "by_crc32", "sha256": "by_sha256"}
|
||||
|
||||
|
||||
def record_for_hash(db: dict, kind: str, value: object) -> str | None:
|
||||
"""The sha1 the database keys the file one hash value names under.
|
||||
def records_for_hash(db: dict, kind: str, value: object) -> list[str]:
|
||||
"""The sha1 of every held record one hash value names, index order kept.
|
||||
|
||||
A cut md5 (Batocera writes 29 characters) names the one held md5 it
|
||||
starts, as cut_md5_owner reads it.
|
||||
starts, as cut_md5_owner reads it. An index value that is a list names
|
||||
several records, all returned.
|
||||
"""
|
||||
if not (value and isinstance(value, str)):
|
||||
return None
|
||||
return []
|
||||
files = db.get("files", {})
|
||||
value = value.strip().lower()
|
||||
if kind == "sha1":
|
||||
return value if value in files else None
|
||||
return [value] if value in files else []
|
||||
index = db.get("indexes", {}).get(_HASH_INDEXES[kind], {})
|
||||
found = index.get(value)
|
||||
if found is None and kind == "md5":
|
||||
owner = cut_md5_owner(value, index)
|
||||
found = index.get(owner) if owner else None
|
||||
if isinstance(found, list):
|
||||
found = found[0] if found else None
|
||||
return found if found in files else None
|
||||
if found is None:
|
||||
return []
|
||||
named = [found] if isinstance(found, str) else list(found)
|
||||
return [sha1 for sha1 in named if sha1 in files]
|
||||
|
||||
|
||||
def record_for_hash(db: dict, kind: str, value: object) -> str | None:
|
||||
"""The sha1 of the first held record one hash value names."""
|
||||
return next(iter(records_for_hash(db, kind, value)), None)
|
||||
|
||||
|
||||
_mame_clone_map_cache: dict[str, dict[str, str]] = {}
|
||||
|
||||
@@ -38,7 +38,7 @@ from resolve.launchers import (
|
||||
suffixed_stem,
|
||||
)
|
||||
from resolve.layout import system_dir_path
|
||||
from resolve.localfile import resolve_local_file
|
||||
from resolve.localfile import held_record_for, resolve_local_file
|
||||
from resolve.ownership import foreign_tree, owner_tokens
|
||||
from resolve.standalone import platform_layouts
|
||||
from resolve.validation import (
|
||||
@@ -624,19 +624,14 @@ def entry_source(f: dict, index: dict) -> str | None:
|
||||
return source
|
||||
found = found or source
|
||||
|
||||
def values(field: str) -> list[str]:
|
||||
return parse_hash_list(f.get(field))
|
||||
|
||||
# The declared content anywhere in bios/ comes before a data/ copy, as in
|
||||
# the resolver: the hash steps run before the data directories.
|
||||
held = (
|
||||
any(index["by_md5"].get(v) for v in parse_hash_list(f.get("md5")))
|
||||
or any(v in db_files for v in values("sha1"))
|
||||
# rust_dos names its SC-55 ROMs by sha256 alone
|
||||
or any(v in index["by_sha256"] for v in values("sha256"))
|
||||
or any(index["by_crc32"].get(v) for v in values("crc32"))
|
||||
)
|
||||
return "bios" if held else found
|
||||
# the resolver: the hash steps run before the data directories, and the
|
||||
# held record is the one every declared hash agrees with.
|
||||
held = held_record_for(f, {
|
||||
"files": db_files,
|
||||
"indexes": {key: index[key] for key in ("by_md5", "by_sha256", "by_crc32")},
|
||||
})
|
||||
return "bios" if held is not None else found
|
||||
|
||||
|
||||
def _resolve_archive_source(
|
||||
|
||||
+21
-15
@@ -12,7 +12,7 @@ from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.artifacts import build_timestamp
|
||||
from packs.paths import MEDIA_MARKER, has_path_conflict
|
||||
from core.ziptools import build_zip_contents_index
|
||||
from core.ziptools import build_zip_contents_index, is_archive_path
|
||||
from core.ziptools import safe_member_parts
|
||||
from resolve.nativemode import digest_algorithm, platform_mode
|
||||
from resolve.validation import (
|
||||
@@ -22,7 +22,8 @@ from resolve.nativemode import hash_mismatch_excludes_file
|
||||
import sys
|
||||
from resolve.cores import filter_systems_by_target
|
||||
from core.hashing import (
|
||||
compute_hashes, file_digest, md5_composite_of, md5sum, parse_hash_list, stream_hashes,
|
||||
compute_hashes, file_digest, md5_composite, md5_composite_of, md5sum, parse_hash_list,
|
||||
stream_hashes,
|
||||
)
|
||||
from core.options import reject_unknown
|
||||
import io
|
||||
@@ -34,6 +35,7 @@ from packs.paths import flat_arcname
|
||||
from resolve.nativemode import reads_file_contents
|
||||
from resolve import regions as resolve_regions
|
||||
from packs import slots
|
||||
from resolve.localfile import held_record_for
|
||||
from resolve.packfiles import resolve_declaration, resolve_file
|
||||
from resolve.categories import BIOS, EntryCategories
|
||||
from resolve.gamedata import without_game_data
|
||||
@@ -386,28 +388,32 @@ def _check_member_hash(
|
||||
def _repo_satisfies_declaration(
|
||||
entries: list[dict], db: dict, mode: str
|
||||
) -> bool:
|
||||
"""Check whether any repo file matches one of the declared hashes.
|
||||
"""Check whether any repo file matches one of the declarations.
|
||||
|
||||
Used to separate pack divergence (a matching file exists but was not
|
||||
packed) from data coverage gaps (no repo file matches the upstream
|
||||
declaration): only the former is a pack generation error.
|
||||
declaration): only the former is a pack generation error. A held file
|
||||
is the resolver's: every declared hash agrees with it, and a zipped_file
|
||||
md5 names a member of a held archive, not a loose file. The verdict
|
||||
compares whole digests as the frontend does, so a cut md5 names a held
|
||||
file without being satisfied by it.
|
||||
"""
|
||||
from core.hashing import md5_composite
|
||||
|
||||
files_db = db.get("files", {})
|
||||
by_md5 = db.get("indexes", {}).get("by_md5", {})
|
||||
by_name = db.get("indexes", {}).get("by_name", {})
|
||||
|
||||
for fe in entries:
|
||||
if fe.get("zipped_file"):
|
||||
if _repo_holds_member(fe, db):
|
||||
return True
|
||||
continue
|
||||
hashes = parse_hash_list(fe.get(mode))
|
||||
if not hashes:
|
||||
continue
|
||||
for h in hashes:
|
||||
if mode == "sha1":
|
||||
entry = files_db.get(h)
|
||||
else:
|
||||
entry = files_db.get(by_md5.get(h, ""))
|
||||
if entry and os.path.exists(entry.get("path", "")):
|
||||
held_sha1 = held_record_for(fe, db)
|
||||
if held_sha1 is not None:
|
||||
held = files_db[held_sha1]
|
||||
digest = held_sha1 if mode == "sha1" else str(held.get(mode, ""))
|
||||
if digest.lower() in hashes and os.path.exists(held.get("path", "")):
|
||||
return True
|
||||
if mode == "md5":
|
||||
hash_set = set(hashes)
|
||||
@@ -416,7 +422,7 @@ def _repo_satisfies_declaration(
|
||||
if not entry:
|
||||
continue
|
||||
path = entry.get("path", "")
|
||||
if not path.endswith(".zip") or not os.path.exists(path):
|
||||
if not is_archive_path(path) or not os.path.exists(path):
|
||||
continue
|
||||
try:
|
||||
if md5_composite(path).lower() in hash_set:
|
||||
@@ -430,7 +436,7 @@ def _repo_holds_member(file_entry: dict, db: dict) -> bool:
|
||||
files_db = db.get("files", {})
|
||||
for sha1 in db.get("indexes", {}).get("by_name", {}).get(file_entry.get("name", ""), []):
|
||||
path = (files_db.get(sha1) or {}).get("path", "")
|
||||
if not path.endswith(".zip") or not os.path.exists(path):
|
||||
if not is_archive_path(path) or not os.path.exists(path):
|
||||
continue
|
||||
if inner_rom_check(file_entry, path) == "ok":
|
||||
return True
|
||||
|
||||
@@ -23,6 +23,7 @@ from resolve.content import (
|
||||
size_refusal,
|
||||
)
|
||||
from resolve.archives import archive_members
|
||||
from model.database import records_for_hash
|
||||
from model.profiles import CONTENT_CHECKS, entry_system_ids, lists_files
|
||||
from core.hashing import compute_hashes, parse_hash_list
|
||||
from core.paths import BIOS_DIR, sanitize_pack_path
|
||||
@@ -799,23 +800,18 @@ def _platform_accepted(file_entry: dict, platform_digest: str) -> set[str]:
|
||||
return set()
|
||||
|
||||
|
||||
def _variant_candidates(expected: dict, indexes: dict, fname: str) -> Iterator[str]:
|
||||
"""The sha1 of each held file that may stand for a name, best evidence first."""
|
||||
def _variant_candidates(expected: dict, db: dict, fname: str) -> Iterator[str]:
|
||||
"""The sha1 of each held file that may stand for a name, best evidence first.
|
||||
|
||||
Any one accepted value names a candidate: the index merges what several
|
||||
emulators accept, and their own checks then judge each file.
|
||||
"""
|
||||
# Sorted: the index holds sets, whose order follows the per-process
|
||||
# string hash, and the first passing candidate is the one shipped.
|
||||
for hash_type, index_key in (
|
||||
("sha1", None), ("md5", "by_md5"), ("crc32", "by_crc32"), ("sha256", "by_sha256"),
|
||||
):
|
||||
for hash_type in ("sha1", "md5", "crc32", "sha256"):
|
||||
for value in sorted(expected.get(hash_type) or []):
|
||||
if index_key is None:
|
||||
yield value
|
||||
continue
|
||||
found = indexes.get(index_key, {}).get(value)
|
||||
if isinstance(found, str):
|
||||
yield found
|
||||
elif isinstance(found, list):
|
||||
yield from found
|
||||
yield from indexes.get("by_name", {}).get(fname, [])
|
||||
yield from records_for_hash(db, hash_type, value)
|
||||
yield from db.get("indexes", {}).get("by_name", {}).get(fname, [])
|
||||
|
||||
|
||||
def find_validated_variant(
|
||||
@@ -848,11 +844,10 @@ def find_validated_variant(
|
||||
accepted = _platform_accepted(file_entry, platform_digest)
|
||||
|
||||
files_db = db.get("files", {})
|
||||
indexes = db.get("indexes", {})
|
||||
current_real = os.path.realpath(current_path)
|
||||
seen: set[str] = set()
|
||||
|
||||
for sha1 in _variant_candidates(validation_index[fname], indexes, fname):
|
||||
for sha1 in _variant_candidates(validation_index[fname], db, fname):
|
||||
entry = files_db.get(sha1) or {}
|
||||
path = entry.get("path", "")
|
||||
if not path or not os.path.exists(path):
|
||||
|
||||
Reference in new issue
Block a user