mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-22 17:47:48 -06:00
fix: compare declared hashes in one place
This commit is contained in:
1 parent
e3294d08ea
commit
e462b03848
12 files changed
+93
-97
No files matched your search
@@ -187,6 +187,20 @@ def parse_hash_list(raw: str | list | int | None) -> list[str]:
|
||||
return out
|
||||
|
||||
|
||||
HASH_FIELDS = ("sha1", "md5", "sha256", "crc32")
|
||||
|
||||
|
||||
def declared_hashes(entry: dict, fields: Iterable[str] = HASH_FIELDS) -> dict[str, set[str]]:
|
||||
"""Hash field -> the values an entry declares, for each field it declares."""
|
||||
found = {f: set(parse_hash_list(entry.get(f))) for f in fields}
|
||||
return {f: values for f, values in found.items() if values}
|
||||
|
||||
|
||||
def lists_hashes(entry: dict) -> bool:
|
||||
"""Whether a declaration says anything about content at all."""
|
||||
return bool(declared_hashes(entry))
|
||||
|
||||
|
||||
def parse_md5_list(raw: str | list | None) -> list[str]:
|
||||
"""Normalize an md5 field into a lowercase list.
|
||||
|
||||
|
||||
@@ -19,7 +19,8 @@ from resolve.archives import contents_members, holds_required_members
|
||||
from resolve.standalone import platform_layouts, standalone_set
|
||||
from resolve.cores import resolve_platform_cores
|
||||
from resolve.reconcile import HASH_FIELDS, hash_values
|
||||
from resolve.collisions import accepted_by_all, lists_hashes, find_collisions
|
||||
from core.hashing import lists_hashes
|
||||
from resolve.collisions import accepted_by_all, find_collisions
|
||||
from resolve.validation import opens_by_name, outside_gap_scope
|
||||
|
||||
|
||||
|
||||
@@ -6,14 +6,14 @@ import os
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from core.hashing import parse_md5_list
|
||||
from core.hashing import lists_hashes, parse_md5_list
|
||||
from model.declarations import declared_slot
|
||||
from model.profiles import entry_system_ids, lists_files
|
||||
from packs.gaps import gap_destination
|
||||
from resolve import nativemode
|
||||
from resolve.archives import archive_entry, holds_required_members
|
||||
from core.paths import comparable_destination
|
||||
from resolve.collisions import accepted_by_all, content_sha1, lists_hashes
|
||||
from resolve.collisions import accepted_by_all, content_sha1
|
||||
from resolve.cores import resolve_platform_cores
|
||||
from resolve.layout import system_dir_path
|
||||
from resolve.localfile import resolution_is_hash_exact, resolve_local_file
|
||||
|
||||
@@ -18,7 +18,10 @@ from pathlib import Path
|
||||
|
||||
from model.systems import norm_system_id
|
||||
from resolve.cores import resolve_platform_cores, unresolved_platform_cores
|
||||
from resolve.reconcile import HASH_FIELDS, NativeSystem, entry_names, hash_values
|
||||
from core.hashing import declared_hashes
|
||||
from resolve.reconcile import (
|
||||
HASH_FIELDS, NativeSystem, comparable, contradicts, entry_names, hash_values,
|
||||
)
|
||||
from resolve.launchers import record_names as launcher_names
|
||||
|
||||
|
||||
@@ -37,10 +40,16 @@ def _entry_hashes(entry: dict) -> set[str]:
|
||||
|
||||
|
||||
def _declared_hashes(entry: dict) -> dict[str, set[str]]:
|
||||
"""Hash field -> the values a platform entry declares, alt_md5 as md5."""
|
||||
found = {f: set(hash_values(entry, f)) for f in HASH_FIELDS}
|
||||
found["md5"] |= set(hash_values(entry, "alt_md5"))
|
||||
return {f: values for f, values in found.items() if values}
|
||||
"""Hash field -> the values a platform entry declares, alt_md5 as md5.
|
||||
|
||||
A Recalbox composite md5 describes no container a profile declares
|
||||
(comparable), so it neither accepts nor contradicts a profile entry.
|
||||
"""
|
||||
entry = comparable(entry)
|
||||
found = declared_hashes(entry)
|
||||
if alt := set(hash_values(entry, "alt_md5")):
|
||||
found["md5"] = found.get("md5", set()) | alt
|
||||
return found
|
||||
|
||||
|
||||
def _accepts(accepted: set[str], declared: dict[str, set[str]]) -> bool:
|
||||
@@ -49,16 +58,6 @@ def _accepts(accepted: set[str], declared: dict[str, set[str]]) -> bool:
|
||||
return any(values & accepted for values in declared.values())
|
||||
|
||||
|
||||
def _contradicts(profile_entry: dict, declared: dict[str, set[str]]) -> bool:
|
||||
"""True when both sides state the same kind of hash and no value agrees:
|
||||
the profile entry describes another dump under the same name."""
|
||||
for field_name, values in declared.items():
|
||||
own = set(hash_values(profile_entry, field_name))
|
||||
if own and not set(values) & set(own):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@dataclass
|
||||
class ReadEvidence:
|
||||
"""What the examined profiles open, indexed for one lookup per entry."""
|
||||
@@ -427,7 +426,7 @@ class EntryCategories:
|
||||
return [
|
||||
{**cited(named[p]), "by": "name"}
|
||||
for p in sorted(named)
|
||||
if not (declared and _contradicts(named[p], declared))
|
||||
if not (declared and contradicts(declared_hashes(named[p]), declared))
|
||||
]
|
||||
|
||||
def explain(self, entry: dict, destination: str, system: str) -> dict:
|
||||
|
||||
@@ -6,7 +6,7 @@ import os
|
||||
import zlib
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from core.hashing import compute_hashes, parse_hash_list, parse_md5_list
|
||||
from core.hashing import compute_hashes, declared_hashes, parse_md5_list
|
||||
from core.paths import comparable_destination
|
||||
from core.ziptools import check_inside_zip
|
||||
from resolve.localfile import resolve_local_file, variant_base_name
|
||||
@@ -106,19 +106,6 @@ def find_collisions(
|
||||
return collisions
|
||||
|
||||
|
||||
def lists_hashes(entry: dict) -> bool:
|
||||
"""Whether a declaration says anything about content at all."""
|
||||
return any(
|
||||
_declared_hashes(entry, field_name)
|
||||
for field_name in ("md5", "sha1", "sha256", "crc32")
|
||||
)
|
||||
|
||||
|
||||
def _declared_hashes(entry: dict, field_name: str) -> set[str]:
|
||||
"""Hashes an entry accepts for one field, declared as a string or a list."""
|
||||
return set(parse_hash_list(entry.get(field_name)))
|
||||
|
||||
|
||||
def content_sha1(path: str, db: dict) -> str | None:
|
||||
"""The sha1 of the file at path, from the database or read from disk.
|
||||
|
||||
@@ -145,9 +132,8 @@ def accepted_by_all(path: str, entries: list[dict], db: dict) -> bool:
|
||||
):
|
||||
return False
|
||||
continue
|
||||
for field_name in ("md5", "sha1", "sha256", "crc32"):
|
||||
declared = _declared_hashes(entry, field_name)
|
||||
if declared and str(record.get(field_name, "")).lower() not in declared:
|
||||
for field_name, declared in declared_hashes(entry).items():
|
||||
if str(record.get(field_name, "")).lower() not in declared:
|
||||
if field_name == "crc32" and _headerless_crc32(path, record, entry) in declared:
|
||||
continue
|
||||
return False
|
||||
|
||||
@@ -20,7 +20,7 @@ import re
|
||||
from collections.abc import Callable, Iterable, Mapping
|
||||
from dataclasses import dataclass
|
||||
|
||||
from core.hashing import parse_hash_list
|
||||
from core.hashing import HASH_FIELDS, declared_hashes
|
||||
from model.database import record_at
|
||||
from model.declarations import CONFIGS_ROOT, ROMS_ROOT, declared_root, declared_slot, outer_layout
|
||||
from model.profiles import lists_files, profile_names
|
||||
@@ -49,7 +49,6 @@ _ROOTED = ("/", "~/", "$HOME/", "$home/", "${HOME}/")
|
||||
|
||||
_LABELLED = re.compile(r"^([^\s()]+\.[A-Za-z0-9]+) \([^()]*\)$")
|
||||
_PLACEHOLDER = re.compile(r"\{(\w+)\}")
|
||||
_HASH_FIELDS = ("sha1", "md5", "crc32", "sha256")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -1259,13 +1258,8 @@ def declared_destinations(config: dict) -> set[str]:
|
||||
|
||||
|
||||
def _hashes(entry: dict) -> dict[str, set[str]]:
|
||||
found: dict[str, set[str]] = {}
|
||||
for field in _HASH_FIELDS:
|
||||
value = entry.get(field) or (entry.get("expected") or {}).get(field)
|
||||
if not value:
|
||||
continue
|
||||
found[field] = set(parse_hash_list(value))
|
||||
return found
|
||||
expected = entry.get("expected") or {}
|
||||
return declared_hashes({f: entry.get(f) or expected.get(f) for f in HASH_FIELDS})
|
||||
|
||||
|
||||
def accepts(entry: dict, record: dict) -> bool:
|
||||
|
||||
@@ -15,7 +15,7 @@ from pathlib import Path
|
||||
|
||||
from model.database import record_at
|
||||
from model.largefiles import fetch_large_file
|
||||
from core.hashing import cut_md5_owner, parse_hash_list
|
||||
from core.hashing import HASH_FIELDS, cut_md5_owner, parse_hash_list
|
||||
from resolve.content import declared_hash_verdict
|
||||
from resolve.localfile import resolution_is_hash_exact, resolve_local_file
|
||||
from core.paths import REPO_ROOT, declared_destination
|
||||
@@ -88,7 +88,7 @@ def download_external(
|
||||
def _declared_hashes(file_entry: dict) -> dict[str, list[str]]:
|
||||
return {
|
||||
field: parse_hash_list(file_entry.get(field))
|
||||
for field in ("sha1", "md5", "sha256", "crc32")
|
||||
for field in HASH_FIELDS
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,13 +17,11 @@ from collections.abc import Callable
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
from core.hashing import parse_hash_list
|
||||
from core.hashing import HASH_FIELDS, declared_hashes, parse_hash_list
|
||||
from model.profiles import profile_names
|
||||
from model.systems import norm_system_id
|
||||
from core.paths import REPO_ROOT
|
||||
|
||||
HASH_FIELDS = ("sha1", "md5", "sha256", "crc32")
|
||||
|
||||
|
||||
def hash_values(entry: dict, field_name: str) -> list[str]:
|
||||
"""Return a hash field as a list, whatever shape it was written in."""
|
||||
@@ -89,14 +87,19 @@ def contradicted_hashes(truth_entry: dict, platform_entry: dict | None) -> list[
|
||||
value settle all of them, so it stayed silent on an md5 or a sha256
|
||||
the export rewrote.
|
||||
"""
|
||||
checked = checked_hashes(truth_entry) or {}
|
||||
theirs = comparable(platform_entry)
|
||||
return [
|
||||
field_name for field_name in HASH_FIELDS
|
||||
if (ours := set(hash_values(checked, field_name)))
|
||||
and (held := set(hash_values(theirs, field_name)))
|
||||
and not ours & held
|
||||
]
|
||||
ours = declared_hashes(checked_hashes(truth_entry) or {})
|
||||
theirs = declared_hashes(comparable(platform_entry))
|
||||
return [f for f, values in ours.items() if f in theirs and not values & theirs[f]]
|
||||
|
||||
|
||||
def contradicts(ours: dict[str, set[str]], theirs: dict[str, set[str]]) -> bool:
|
||||
"""A hash field both declared maps hold without a value in common."""
|
||||
return any(f in theirs and not values & theirs[f] for f, values in ours.items())
|
||||
|
||||
|
||||
def shares_hash(ours: dict[str, set[str]], theirs: dict[str, set[str]]) -> bool:
|
||||
"""A value in common on any hash field both declared maps hold."""
|
||||
return any(values & theirs.get(f, set()) for f, values in ours.items())
|
||||
|
||||
|
||||
def is_label(entry: dict) -> bool:
|
||||
@@ -422,22 +425,12 @@ def by_name(truth_entry: dict, platform_entry: dict) -> bool:
|
||||
|
||||
def by_hash(truth_entry: dict, platform_entry: dict) -> bool:
|
||||
"""A value on any of the four hash fields in common: a renamed file."""
|
||||
ours = {v for f in HASH_FIELDS for v in hash_values(truth_entry, f)}
|
||||
if not ours:
|
||||
return False
|
||||
theirs = comparable(platform_entry)
|
||||
return bool(ours & {v for f in HASH_FIELDS for v in hash_values(theirs, f)})
|
||||
return shares_hash(declared_hashes(truth_entry), declared_hashes(comparable(platform_entry)))
|
||||
|
||||
|
||||
def _contradicts(truth_entry: dict, platform_entry: dict) -> bool:
|
||||
"""A hash both entries declare, without a value in common."""
|
||||
platform_entry = comparable(platform_entry)
|
||||
return any(
|
||||
(ours := set(hash_values(truth_entry, f)))
|
||||
and (theirs := set(hash_values(platform_entry, f)))
|
||||
and not ours & theirs
|
||||
for f in HASH_FIELDS
|
||||
)
|
||||
return contradicts(declared_hashes(truth_entry), declared_hashes(comparable(platform_entry)))
|
||||
|
||||
|
||||
def _agreeing(test: Callable[[dict, dict], bool]) -> Callable[[dict, dict], bool]:
|
||||
|
||||
@@ -16,8 +16,9 @@ from model.systems import norm_system_id
|
||||
from model.registry import load_data_dir_registry
|
||||
from resolve.standalone import platform_layouts, standalone_set as platform_standalone_set
|
||||
from resolve.cores import resolve_platform_cores
|
||||
from resolve.reconcile import contradicts, shares_hash
|
||||
from model.alternatives import groups_of
|
||||
from core.hashing import compute_hashes, parse_hash_list
|
||||
from core.hashing import HASH_FIELDS, compute_hashes, declared_hashes, lists_hashes, parse_hash_list
|
||||
from resolve.validation import (
|
||||
filter_files_by_mode,
|
||||
installed_with_emulator,
|
||||
@@ -134,8 +135,6 @@ _COPIED_FIELDS = (
|
||||
"priority",
|
||||
"region",
|
||||
)
|
||||
_HASH_FIELDS = ("sha1", "md5", "sha256", "crc32")
|
||||
|
||||
|
||||
def _merge_file_into_system(
|
||||
system: dict,
|
||||
@@ -242,24 +241,12 @@ def _brought(file_entry: dict, emu_name: str) -> set[str]:
|
||||
|
||||
|
||||
def _shares_hash(a: dict, b: dict) -> bool:
|
||||
return any(
|
||||
_declared(a, field) & _declared(b, field)
|
||||
for field in ("sha1", "md5", "sha256", "crc32")
|
||||
)
|
||||
|
||||
|
||||
def _declared(entry: dict, field: str) -> set[str]:
|
||||
"""The values an entry declares for a hash field, one, a list or comma-joined."""
|
||||
return set(parse_hash_list(entry.get(field)))
|
||||
return shares_hash(declared_hashes(a), declared_hashes(b))
|
||||
|
||||
|
||||
def _contents_disagree(a: dict, b: dict) -> bool:
|
||||
"""A hash both declare without a value in common, or no size both admit."""
|
||||
for field in ("sha1", "md5", "sha256", "crc32"):
|
||||
ours, theirs = _declared(a, field), _declared(b, field)
|
||||
if ours and theirs and not ours & theirs:
|
||||
return True
|
||||
return _sizes_clash(a, b)
|
||||
return contradicts(declared_hashes(a), declared_hashes(b)) or _sizes_clash(a, b)
|
||||
|
||||
|
||||
def _fold_into(existing: dict, file_entry: dict, emu_name: str) -> None:
|
||||
@@ -339,7 +326,7 @@ def _merge_hashes(
|
||||
(dosbox's MT-32 control ROMs, raze's GRP versions): ``revision`` keeps
|
||||
the union. Two cores that disagree keep the first value and are reported.
|
||||
"""
|
||||
for h in _HASH_FIELDS:
|
||||
for h in HASH_FIELDS:
|
||||
theirs = file_entry.get(h, "")
|
||||
ours = existing.get(h, "")
|
||||
if not theirs:
|
||||
@@ -405,7 +392,7 @@ def _new_truth_entry(file_entry: dict, emu_name: str, db: dict | None) -> dict:
|
||||
if val is not None:
|
||||
entry[field] = val
|
||||
# Strip empty string hashes (profile says "" when hash is unknown)
|
||||
for h in _HASH_FIELDS:
|
||||
for h in HASH_FIELDS:
|
||||
if entry.get(h) == "":
|
||||
del entry[h]
|
||||
# Normalize CRC32: strip 0x prefix, lowercase
|
||||
@@ -426,7 +413,7 @@ def _new_truth_entry(file_entry: dict, emu_name: str, db: dict | None) -> dict:
|
||||
_enrich_hashes(entry, db)
|
||||
# Which core gave each hash, for the conflict warning. Dropped before
|
||||
# the model is written.
|
||||
entry["_hash_by"] = {h: emu_name for h in _HASH_FIELDS if entry.get(h)}
|
||||
entry["_hash_by"] = {h: emu_name for h in HASH_FIELDS if entry.get(h)}
|
||||
return entry
|
||||
|
||||
|
||||
@@ -437,7 +424,7 @@ def _has_exploitable_data(entry: dict) -> bool:
|
||||
sibling cores before being judged empty.
|
||||
"""
|
||||
return bool(
|
||||
any(entry.get(h) for h in ("sha1", "md5", "sha256", "crc32"))
|
||||
lists_hashes(entry)
|
||||
or entry.get("path")
|
||||
or entry.get("size")
|
||||
or entry.get("min_size")
|
||||
@@ -542,10 +529,7 @@ def _identity(entry: dict, db: dict | None) -> dict:
|
||||
|
||||
def _identity_values(entry: dict) -> set[str]:
|
||||
"""Every hash an identity declares."""
|
||||
values: set[str] = set()
|
||||
for field in ("sha1", "md5", "sha256", "crc32"):
|
||||
values |= _declared(entry, field)
|
||||
return values
|
||||
return {v for values in declared_hashes(entry).values() for v in values}
|
||||
|
||||
|
||||
def _declaration_verdict(entry: dict, declaration: dict, db: dict | None) -> str:
|
||||
|
||||
@@ -25,7 +25,7 @@ from resolve.archives import archive_members
|
||||
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
|
||||
from core.hashing import md5_composite, parse_md5_list
|
||||
from core.hashing import lists_hashes, md5_composite, parse_md5_list
|
||||
from resolve.nativemode import SIZE_AND_ANY_HASH, digest_algorithm, hash_mismatch_excludes_file
|
||||
from core.ziptools import check_inside_zip, check_inside_zip_data
|
||||
|
||||
@@ -919,7 +919,7 @@ def agnostic_substitute(
|
||||
asked, and the pack and verify read this one answer. An entry that
|
||||
declares a content hash names one file: no substitute stands for it.
|
||||
"""
|
||||
if any(file_entry.get(h) for h in ("sha1", "md5", "sha256", "crc32")):
|
||||
if lists_hashes(file_entry):
|
||||
return None
|
||||
by_name = db.get("indexes", {}).get("by_name", {})
|
||||
files_db = db.get("files", {})
|
||||
|
||||
@@ -59,6 +59,21 @@ class EachProfileEntryIsHashedOnce(unittest.TestCase):
|
||||
self.assertFalse(candidates._accepts({"a" * 32}, {"md5": {"a" * 29}}))
|
||||
self.assertFalse(candidates._accepts({"a" * 32}, {"md5": {"b" * 32}}))
|
||||
|
||||
class CompositeMd5(unittest.TestCase):
|
||||
"""Recalbox hashes an archive by its members (zip_digest: composite), a
|
||||
digest no profile declares: it contradicts no container md5, as reconcile
|
||||
reads it."""
|
||||
|
||||
def test_a_composite_md5_leaves_the_same_named_entry(self):
|
||||
profiles = {"core": {"emulator": "Core", "type": "libretro", "cores": ["core"],
|
||||
"systems": ["s"],
|
||||
"files": [{"name": "neogeo.zip", "md5": "a" * 32}]}}
|
||||
cats = EntryCategories(_config(["core"], []), profiles)
|
||||
composite = {"name": "neogeo.zip", "md5": "b" * 32, "zip_digest": "composite"}
|
||||
self.assertEqual(cats._matching("core", BIOS, composite), [{"name": "neogeo.zip", "by": "name"}])
|
||||
self.assertEqual(cats._matching("core", BIOS, {"name": "neogeo.zip", "md5": "b" * 32}), [])
|
||||
|
||||
|
||||
class Category(unittest.TestCase):
|
||||
def test_each_category(self):
|
||||
cats = EntryCategories(_config(["bluemsx", "devilutionx"], []), PROFILES)
|
||||
|
||||
@@ -97,5 +97,15 @@ class TheSiteReadsAHashFieldTheResolversWay(unittest.TestCase):
|
||||
self.assertEqual(lookup.sorted_hash_values(raw), sorted(hashing.parse_hash_list(raw)))
|
||||
|
||||
|
||||
|
||||
class DeclaredHashes(unittest.TestCase):
|
||||
def test_only_fields_with_a_value_count(self):
|
||||
entry = {"md5": "A, b", "sha1": " , ", "crc32": "0x1234ABCD", "sha256": ""}
|
||||
self.assertEqual(hashing.declared_hashes(entry), {"md5": {"a", "b"}, "crc32": {"1234abcd"}})
|
||||
# A field holding only separators declares nothing.
|
||||
self.assertFalse(hashing.lists_hashes({"sha1": " , "}))
|
||||
self.assertTrue(hashing.lists_hashes({"crc32": "1234abcd"}))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user