mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-23 10:07:48 -06:00
chore: merge the pack builder quality work
This commit is contained in:
commit
0809dfd6bf
8 files changed
+4497
-3335
No files matched your search
+2973
-2344
File diff suppressed because it is too large.
Load diff
+664
-416
File diff suppressed because it is too large.
Load diff
@@ -161,9 +161,7 @@ def _has_path_conflict(dest: str, seen_files: set[str], seen_parents: set[str])
|
||||
for parent in _path_parents(dest):
|
||||
if parent in seen_files:
|
||||
return True
|
||||
if dest in seen_parents:
|
||||
return True
|
||||
return False
|
||||
return dest in seen_parents
|
||||
|
||||
def placement_block(
|
||||
dest: str,
|
||||
|
||||
+179
-123
@@ -5,9 +5,16 @@ goes, and which ones the user has to rename."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from packextras import reject_unknown
|
||||
from region import label as region_label
|
||||
from siteguides import INSTALL_PS1, INSTALL_SH, pack_manual
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import Unpack
|
||||
|
||||
|
||||
def _detect_systems(detect: list[dict]) -> list[str]:
|
||||
"""The operating systems the registry's detection rules cover, in order."""
|
||||
order = ("linux", "darwin", "windows")
|
||||
@@ -187,27 +194,33 @@ def _save_section(
|
||||
return "\n".join(lines) + "\n\n"
|
||||
|
||||
|
||||
class ReadmeOptions(TypedDict, total=False):
|
||||
"""What a pack's README states besides its platform and its counts."""
|
||||
|
||||
source: str
|
||||
contributors: list[dict] | None
|
||||
regions: list[str] | None
|
||||
fallback_systems: list[str] | None
|
||||
one_per_slot: bool
|
||||
undecidable_slots: list[str] | None
|
||||
narrowings: list[tuple[str, str]] | None
|
||||
system_filter: list[str] | None
|
||||
detect: list[dict] | None
|
||||
standalone_copies: list[dict] | None
|
||||
launch_folders: int
|
||||
save_folder: str
|
||||
save_where: str
|
||||
save_by_system: bool
|
||||
case_twins: list[tuple[str, str]] | None
|
||||
|
||||
|
||||
def _build_readme(
|
||||
platform_name: str,
|
||||
platform_display: str,
|
||||
base_dest: str,
|
||||
total_files: int,
|
||||
num_systems: int,
|
||||
source: str = "full",
|
||||
contributors: list[dict] | None = None,
|
||||
regions: list[str] | None = None,
|
||||
fallback_systems: list[str] | None = None,
|
||||
one_per_slot: bool = False,
|
||||
undecidable_slots: list[str] | None = None,
|
||||
narrowings: list[tuple[str, str]] | None = None,
|
||||
system_filter: list[str] | None = None,
|
||||
detect: list[dict] | None = None,
|
||||
standalone_copies: list[dict] | None = None,
|
||||
launch_folders: int = 0,
|
||||
save_folder: str = "",
|
||||
save_where: str = "",
|
||||
save_by_system: bool = False,
|
||||
case_twins: list[tuple[str, str]] | None = None,
|
||||
**options: Unpack[ReadmeOptions],
|
||||
) -> str:
|
||||
"""Build a personalized step-by-step README for each platform pack.
|
||||
|
||||
@@ -222,7 +235,9 @@ def _build_readme(
|
||||
where names keep their case (EmuDeck and RetroArch on Linux), where the
|
||||
core asking for the other spelling finds nothing.
|
||||
"""
|
||||
narrowings = narrowings or []
|
||||
reject_unknown("_build_readme", options, ReadmeOptions.__optional_keys__)
|
||||
regions = options.get("regions")
|
||||
launch_folders = options.get("launch_folders", 0)
|
||||
sep = "=" * 50
|
||||
scope = (
|
||||
f"{launch_folders} launch folders" if launch_folders and not num_systems
|
||||
@@ -235,40 +250,7 @@ def _build_readme(
|
||||
f"{sep}\n\n"
|
||||
)
|
||||
|
||||
detect = list(detect or [])
|
||||
bios_paths = [str(rule.get("bios_path", "")) for rule in detect]
|
||||
if any(rule.get("method") == "card_marker" for rule in detect):
|
||||
guide = (
|
||||
"INSTALLATION GUIDE\n\n"
|
||||
+ _card_section(platform_name, platform_display)
|
||||
+ _card_manual(platform_display)
|
||||
)
|
||||
else:
|
||||
guide = (
|
||||
"INSTALLATION GUIDE\n\n"
|
||||
+ _automatic_section(platform_name, platform_display, detect, standalone_copies)
|
||||
+ (pack_manual(platform_name) or _generic_manual(bios_paths))
|
||||
)
|
||||
|
||||
if case_twins:
|
||||
listed = "".join(
|
||||
f" {missing} (the pack has {held})\n" for missing, held in case_twins
|
||||
)
|
||||
guide += (
|
||||
"NAMES THAT DIFFER ONLY BY CASE\n\n"
|
||||
" Some cores open the same file under another spelling. This\n"
|
||||
" pack holds one spelling of each, all Windows and macOS can\n"
|
||||
" store in one folder. Linux, SteamOS and Android keep both:\n"
|
||||
" there, run the installer, which adds these spellings, or\n"
|
||||
" copy each file under the name below.\n\n"
|
||||
f"{listed}\n"
|
||||
)
|
||||
|
||||
if save_folder:
|
||||
guide += _save_section(
|
||||
platform_display, save_folder, save_where, save_by_system,
|
||||
in_frontend_settings=not save_by_system, in_place=not base_dest,
|
||||
)
|
||||
guide = _install_guide(platform_name, platform_display, base_dest, options)
|
||||
|
||||
if regions:
|
||||
region_help = (
|
||||
@@ -292,83 +274,158 @@ def _build_readme(
|
||||
f"{sep}\n"
|
||||
)
|
||||
|
||||
source_info = ""
|
||||
if source == "platform":
|
||||
source_info = (
|
||||
"PACK TYPE: Platform Only\n\n"
|
||||
f" This pack contains only files declared by {platform_display}.\n"
|
||||
" Core extras from emulator profiles are not included.\n"
|
||||
" Use the Full pack for maximum coverage.\n\n"
|
||||
)
|
||||
elif source == "truth":
|
||||
source_info = (
|
||||
"PACK TYPE: Ground Truth\n\n"
|
||||
" This pack contains files that emulators actually load,\n"
|
||||
" based on source code analysis of emulator profiles.\n"
|
||||
" Independent of platform scraper accuracy.\n\n"
|
||||
)
|
||||
|
||||
region_info = ""
|
||||
if regions:
|
||||
pretty = ", ".join(region_label(slug) for slug in regions)
|
||||
region_info = (
|
||||
"PACK TYPE: Region Filtered\n\n"
|
||||
f" Region priority: {pretty}\n\n"
|
||||
" Only the best-matching BIOS was kept for each system.\n"
|
||||
)
|
||||
if fallback_systems:
|
||||
listed = "\n".join(f" {_group_label(s)}" for s in fallback_systems)
|
||||
region_info += (
|
||||
"\n These systems have no BIOS in those regions, so every\n"
|
||||
" BIOS file for them was kept:\n"
|
||||
f"{listed}\n"
|
||||
)
|
||||
region_info += (
|
||||
"\n This shrinks the pack. It does not change how cores pick\n"
|
||||
" a BIOS: most already select per region from fixed filename\n"
|
||||
" lists driven by the game's region. Loading imports from\n"
|
||||
" another region may need the unfiltered pack.\n\n"
|
||||
)
|
||||
|
||||
slot_info = ""
|
||||
if one_per_slot:
|
||||
slot_info = (
|
||||
" Where a core declares which BIOS it prefers, only that one was\n"
|
||||
" kept for each system and region. Systems whose cores declare no\n"
|
||||
" order keep every BIOS file.\n"
|
||||
)
|
||||
if undecidable_slots:
|
||||
slot_info += (
|
||||
f" {len(undecidable_slots)} slot(s) had no declared order.\n"
|
||||
)
|
||||
slot_info += "\n"
|
||||
|
||||
narrowed = ""
|
||||
labels = [label for _tag, label in narrowings]
|
||||
if system_filter:
|
||||
labels.append(f"systems {', '.join(system_filter)}")
|
||||
if labels:
|
||||
listed = "".join(f" {label}\n" for label in labels)
|
||||
narrowed = (
|
||||
"PACK TYPE: Narrowed\n\n"
|
||||
" This pack holds fewer files than the full one:\n"
|
||||
f"{listed}"
|
||||
"\n The unfiltered pack is the one to use when in doubt.\n\n"
|
||||
)
|
||||
|
||||
credits = ""
|
||||
if contributors:
|
||||
credits = "\nCONTRIBUTORS\n\n"
|
||||
for cb in contributors:
|
||||
username = cb.get("username", "")
|
||||
credits += f" @{username}\n"
|
||||
credits += "\n"
|
||||
source_info = _source_info(options.get("source", "full"), platform_display)
|
||||
region_info = _region_info(regions, options.get("fallback_systems"))
|
||||
slot_info = _slot_info(
|
||||
options.get("one_per_slot", False), options.get("undecidable_slots")
|
||||
)
|
||||
narrowed = _narrowed_info(
|
||||
options.get("narrowings") or [], options.get("system_filter")
|
||||
)
|
||||
credits = _credits(options.get("contributors"))
|
||||
|
||||
return (
|
||||
header + narrowed + source_info + region_info + slot_info
|
||||
+ guide + credits + footer
|
||||
)
|
||||
|
||||
|
||||
def _install_guide(
|
||||
platform_name: str, platform_display: str, base_dest: str, options: ReadmeOptions
|
||||
) -> str:
|
||||
"""The installation steps, the case twins and the save folder."""
|
||||
detect = list(options.get("detect") or [])
|
||||
bios_paths = [str(rule.get("bios_path", "")) for rule in detect]
|
||||
if any(rule.get("method") == "card_marker" for rule in detect):
|
||||
guide = (
|
||||
"INSTALLATION GUIDE\n\n"
|
||||
+ _card_section(platform_name, platform_display)
|
||||
+ _card_manual(platform_display)
|
||||
)
|
||||
else:
|
||||
guide = (
|
||||
"INSTALLATION GUIDE\n\n"
|
||||
+ _automatic_section(
|
||||
platform_name, platform_display, detect, options.get("standalone_copies")
|
||||
)
|
||||
+ (pack_manual(platform_name) or _generic_manual(bios_paths))
|
||||
)
|
||||
|
||||
case_twins = options.get("case_twins")
|
||||
if case_twins:
|
||||
listed = "".join(
|
||||
f" {missing} (the pack has {held})\n" for missing, held in case_twins
|
||||
)
|
||||
guide += (
|
||||
"NAMES THAT DIFFER ONLY BY CASE\n\n"
|
||||
" Some cores open the same file under another spelling. This\n"
|
||||
" pack holds one spelling of each, all Windows and macOS can\n"
|
||||
" store in one folder. Linux, SteamOS and Android keep both:\n"
|
||||
" there, run the installer, which adds these spellings, or\n"
|
||||
" copy each file under the name below.\n\n"
|
||||
f"{listed}\n"
|
||||
)
|
||||
|
||||
save_folder = options.get("save_folder", "")
|
||||
if save_folder:
|
||||
save_by_system = options.get("save_by_system", False)
|
||||
guide += _save_section(
|
||||
platform_display, save_folder, options.get("save_where", ""), save_by_system,
|
||||
in_frontend_settings=not save_by_system, in_place=not base_dest,
|
||||
)
|
||||
return guide
|
||||
|
||||
|
||||
def _source_info(source: str, platform_display: str) -> str:
|
||||
"""The PACK TYPE block of a platform-only or ground-truth pack."""
|
||||
if source == "platform":
|
||||
return (
|
||||
"PACK TYPE: Platform Only\n\n"
|
||||
f" This pack contains only files declared by {platform_display}.\n"
|
||||
" Core extras from emulator profiles are not included.\n"
|
||||
" Use the Full pack for maximum coverage.\n\n"
|
||||
)
|
||||
if source == "truth":
|
||||
return (
|
||||
"PACK TYPE: Ground Truth\n\n"
|
||||
" This pack contains files that emulators actually load,\n"
|
||||
" based on source code analysis of emulator profiles.\n"
|
||||
" Independent of platform scraper accuracy.\n\n"
|
||||
)
|
||||
return ""
|
||||
|
||||
|
||||
def _region_info(regions: list[str] | None, fallback_systems: list[str] | None) -> str:
|
||||
"""The PACK TYPE block of a region-filtered pack."""
|
||||
if not regions:
|
||||
return ""
|
||||
pretty = ", ".join(region_label(slug) for slug in regions)
|
||||
region_info = (
|
||||
"PACK TYPE: Region Filtered\n\n"
|
||||
f" Region priority: {pretty}\n\n"
|
||||
" Only the best-matching BIOS was kept for each system.\n"
|
||||
)
|
||||
if fallback_systems:
|
||||
listed = "\n".join(f" {_group_label(s)}" for s in fallback_systems)
|
||||
region_info += (
|
||||
"\n These systems have no BIOS in those regions, so every\n"
|
||||
" BIOS file for them was kept:\n"
|
||||
f"{listed}\n"
|
||||
)
|
||||
region_info += (
|
||||
"\n This shrinks the pack. It does not change how cores pick\n"
|
||||
" a BIOS: most already select per region from fixed filename\n"
|
||||
" lists driven by the game's region. Loading imports from\n"
|
||||
" another region may need the unfiltered pack.\n\n"
|
||||
)
|
||||
return region_info
|
||||
|
||||
|
||||
def _slot_info(one_per_slot: bool, undecidable_slots: list[str] | None) -> str:
|
||||
"""What --one-per-slot kept."""
|
||||
if not one_per_slot:
|
||||
return ""
|
||||
slot_info = (
|
||||
" Where a core declares which BIOS it prefers, only that one was\n"
|
||||
" kept for each system and region. Systems whose cores declare no\n"
|
||||
" order keep every BIOS file.\n"
|
||||
)
|
||||
if undecidable_slots:
|
||||
slot_info += (
|
||||
f" {len(undecidable_slots)} slot(s) had no declared order.\n"
|
||||
)
|
||||
return slot_info + "\n"
|
||||
|
||||
|
||||
def _narrowed_info(
|
||||
narrowings: list[tuple[str, str]], system_filter: list[str] | None
|
||||
) -> str:
|
||||
"""The PACK TYPE block of a pack holding fewer files than the full one."""
|
||||
labels = [label for _tag, label in narrowings]
|
||||
if system_filter:
|
||||
labels.append(f"systems {', '.join(system_filter)}")
|
||||
if not labels:
|
||||
return ""
|
||||
listed = "".join(f" {label}\n" for label in labels)
|
||||
return (
|
||||
"PACK TYPE: Narrowed\n\n"
|
||||
" This pack holds fewer files than the full one:\n"
|
||||
f"{listed}"
|
||||
"\n The unfiltered pack is the one to use when in doubt.\n\n"
|
||||
)
|
||||
|
||||
|
||||
def _credits(contributors: list[dict] | None) -> str:
|
||||
"""The CONTRIBUTORS block."""
|
||||
if not contributors:
|
||||
return ""
|
||||
credits = "\nCONTRIBUTORS\n\n"
|
||||
for cb in contributors:
|
||||
username = cb.get("username", "")
|
||||
credits += f" @{username}\n"
|
||||
return credits + "\n"
|
||||
|
||||
|
||||
def _build_agnostic_rename_readme(
|
||||
destination: str,
|
||||
original: str,
|
||||
@@ -382,8 +439,7 @@ def _build_agnostic_rename_readme(
|
||||
]
|
||||
if alternatives:
|
||||
lines.append("All variants included in this pack:")
|
||||
for alt in sorted(alternatives):
|
||||
lines.append(f" {alt}")
|
||||
lines.extend(f" {alt}" for alt in sorted(alternatives))
|
||||
lines.append("")
|
||||
lines.append(f"To use a different variant, rename it to: {destination}")
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
+53
-31
@@ -283,6 +283,7 @@ def sha1_for_hash(hash_type: str, hash_val: str, db: dict) -> str | None:
|
||||
"""The database key a looked-up hash names, or None."""
|
||||
files_db = db.get("files", {})
|
||||
indexes = db.get("indexes", {})
|
||||
sha1: str | None
|
||||
if hash_type == "sha1":
|
||||
sha1 = hash_val
|
||||
elif hash_type == "crc32":
|
||||
@@ -450,6 +451,53 @@ def resolve_declaration(
|
||||
))
|
||||
|
||||
|
||||
def _declarations_by_destination(
|
||||
pack_systems: dict, base_dest: str, required_only: bool,
|
||||
) -> dict[str, list[dict]]:
|
||||
"""The declarations of each full destination, in system order."""
|
||||
dest_entries: dict[str, list[dict]] = {}
|
||||
for _sys_id, system in sorted(pack_systems.items()):
|
||||
for file_entry in system.get("files", []):
|
||||
if required_only and file_entry.get("required") is False:
|
||||
continue
|
||||
dest = sanitize_pack_path(
|
||||
file_entry.get("destination", file_entry.get("name", ""))
|
||||
)
|
||||
if not dest:
|
||||
continue
|
||||
full = f"{base_dest}/{dest}" if base_dest else dest
|
||||
dest_entries.setdefault(full, []).append(file_entry)
|
||||
return dest_entries
|
||||
|
||||
|
||||
def _archive_choice(
|
||||
resolved: list[tuple[dict, str | None, str]], members: list[dict],
|
||||
) -> dict | None:
|
||||
"""The declaration whose archive the most per-ROM declarations accept."""
|
||||
# One archive declared once per ROM it must hold: the archive
|
||||
# to ship is the one the most declarations accept, not the first
|
||||
# that answers one of them (adam_fdc.zip: a one-member MAME set
|
||||
# answered first, the eight-member set sat beside it).
|
||||
def accepted(path: str | None) -> int:
|
||||
if not path or not path.endswith(".zip"):
|
||||
return -1
|
||||
return sum(inner_rom_check(fe, path) == "ok" for fe in members)
|
||||
|
||||
fe, path, _st = max(resolved, key=lambda item: accepted(item[1]))
|
||||
return fe if accepted(path) > 0 else None
|
||||
|
||||
|
||||
def _hash_choice(resolved: list[tuple[dict, str | None, str]]) -> dict | None:
|
||||
"""The first declaration resolved by md5, else the first by any hash."""
|
||||
best = None
|
||||
for fe, _lp, _st in resolved:
|
||||
if _lp and _st == "md5_exact":
|
||||
return fe
|
||||
if best is None and _lp and resolution_is_hash_exact(_st):
|
||||
best = fe
|
||||
return best
|
||||
|
||||
|
||||
def preferred_entries(
|
||||
pack_systems: dict,
|
||||
db: dict,
|
||||
@@ -470,18 +518,7 @@ def preferred_entries(
|
||||
and could crown a declaration only a data/ cache satisfies.
|
||||
"""
|
||||
preferred_entries: dict[str, int] = {}
|
||||
dest_entries: dict[str, list[dict]] = {}
|
||||
for sys_id, system in sorted(pack_systems.items()):
|
||||
for file_entry in system.get("files", []):
|
||||
if required_only and file_entry.get("required") is False:
|
||||
continue
|
||||
dest = sanitize_pack_path(
|
||||
file_entry.get("destination", file_entry.get("name", ""))
|
||||
)
|
||||
if not dest:
|
||||
continue
|
||||
full = f"{base_dest}/{dest}" if base_dest else dest
|
||||
dest_entries.setdefault(full, []).append(file_entry)
|
||||
dest_entries = _declarations_by_destination(pack_systems, base_dest, required_only)
|
||||
for full, entries in dest_entries.items():
|
||||
if len(entries) < 2:
|
||||
continue
|
||||
@@ -504,26 +541,11 @@ def preferred_entries(
|
||||
]
|
||||
members = [fe for fe in entries if fe.get("zipped_file")]
|
||||
if members:
|
||||
# One archive declared once per ROM it must hold: the archive
|
||||
# to ship is the one the most declarations accept, not the first
|
||||
# that answers one of them (adam_fdc.zip: a one-member MAME set
|
||||
# answered first, the eight-member set sat beside it).
|
||||
def accepted(path: str | None) -> int:
|
||||
if not path or not path.endswith(".zip"):
|
||||
return -1
|
||||
return sum(inner_rom_check(fe, path) == "ok" for fe in members)
|
||||
|
||||
fe, path, _st = max(resolved, key=lambda item: accepted(item[1]))
|
||||
if accepted(path) > 0:
|
||||
preferred_entries[full] = id(fe)
|
||||
chosen = _archive_choice(resolved, members)
|
||||
if chosen is not None:
|
||||
preferred_entries[full] = id(chosen)
|
||||
continue
|
||||
best = None
|
||||
for fe, _lp, _st in resolved:
|
||||
if _lp and _st == "md5_exact":
|
||||
best = fe
|
||||
break
|
||||
if best is None and _lp and resolution_is_hash_exact(_st):
|
||||
best = fe
|
||||
best = _hash_choice(resolved)
|
||||
if best is not None:
|
||||
preferred_entries[full] = id(best)
|
||||
|
||||
|
||||
+509
-338
@@ -7,6 +7,8 @@ coverage report that disagree describe different collections."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import Counter
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from auto_fetch import DEFAULT_BIOS_DIR
|
||||
from artifacts import _build_timestamp
|
||||
@@ -37,7 +39,14 @@ from gamedata import without_game_data
|
||||
from common import resolve_platform_cores
|
||||
from common import sanitize_pack_path
|
||||
import zipfile
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Callable, Mapping
|
||||
|
||||
# Imported at run time where typing has it: a test reads the signature of
|
||||
# verify_pack_against_platform with its annotations evaluated.
|
||||
if sys.version_info >= (3, 11):
|
||||
from typing import Unpack
|
||||
elif TYPE_CHECKING:
|
||||
from typing_extensions import Unpack
|
||||
|
||||
|
||||
@functools.cache
|
||||
@@ -121,6 +130,100 @@ def _same_members(pack: zipfile.ZipFile, name: str, source: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _data_indexes(data_registry: dict | None) -> tuple[dict[str, list[str]], dict[str, str]]:
|
||||
"""The cached data directory files by name and by relative path."""
|
||||
_data_index: dict[str, list[str]] = {}
|
||||
_data_path_index: dict[str, str] = {}
|
||||
if data_registry:
|
||||
for _de in data_registry.values():
|
||||
cache = _de.get("local_cache", "")
|
||||
if not cache or not os.path.isdir(cache):
|
||||
continue
|
||||
for _r, _d, _fns in os.walk(cache):
|
||||
for _fn in _fns:
|
||||
_fp = os.path.join(_r, _fn)
|
||||
_rel = os.path.relpath(_fp, cache)
|
||||
_data_path_index[_rel] = _fp
|
||||
_data_index.setdefault(_fn, []).append(_fp)
|
||||
return _data_index, _data_path_index
|
||||
|
||||
|
||||
def _is_pack_note(name: str) -> bool:
|
||||
"""Whether a member is a note the builder composes, not a collected file."""
|
||||
# The builder writes its notes beside the slot they explain
|
||||
# (bios/INSTRUCTIONS_x.txt, games/keen6/INSTRUCTIONS_y.txt), so
|
||||
# they are told by their own name, not by a root prefix.
|
||||
base = name.rsplit("/", 1)[-1]
|
||||
return base.startswith(("INSTRUCTIONS_", "RENAMED_")) or base == MEDIA_MARKER or name in (
|
||||
"manifest.json",
|
||||
"README.txt",
|
||||
)
|
||||
|
||||
|
||||
def _member_digests(zf: zipfile.ZipFile, info: zipfile.ZipInfo) -> tuple[str, str, int]:
|
||||
"""(sha1, md5, size) of a pack member."""
|
||||
with zf.open(info) as f:
|
||||
sha1_h = hashlib.sha1()
|
||||
md5_h = hashlib.md5()
|
||||
size = 0
|
||||
for chunk in iter(lambda: f.read(65536), b""):
|
||||
sha1_h.update(chunk)
|
||||
md5_h.update(chunk)
|
||||
size += len(chunk)
|
||||
return sha1_h.hexdigest(), md5_h.hexdigest(), size
|
||||
|
||||
|
||||
def _rebuilt_from(zf: zipfile.ZipFile, name: str, by_name: dict, files_db: dict) -> str | None:
|
||||
"""The name of a held archive a rebuilt ZIP carries the members of."""
|
||||
_bn = os.path.basename(name)
|
||||
for _src_sha1 in by_name.get(_bn, []):
|
||||
if _src_sha1 not in files_db:
|
||||
continue
|
||||
_src_path = files_db[_src_sha1]["path"]
|
||||
if not os.path.exists(_src_path):
|
||||
continue
|
||||
if _same_members(zf, name, _src_path):
|
||||
return _bn
|
||||
return None
|
||||
|
||||
|
||||
def _cached_as(
|
||||
zf: zipfile.ZipFile,
|
||||
name: str,
|
||||
size: int,
|
||||
sha1: str,
|
||||
data_indexes: tuple[dict[str, list[str]], dict[str, str]],
|
||||
) -> str | None:
|
||||
"""The name of the data directory file a member is, or None."""
|
||||
_data_index, _data_path_index = data_indexes
|
||||
_bn = os.path.basename(name)
|
||||
_pr = name
|
||||
for _known_prefix in ("system/", "bios/", "BIOS/", "Firmware/"):
|
||||
if name.startswith(_known_prefix):
|
||||
_pr = name[len(_known_prefix):]
|
||||
break
|
||||
_cands = []
|
||||
if _pr in _data_path_index:
|
||||
_cands.append(_data_path_index[_pr])
|
||||
for _dp in _data_index.get(_bn, []):
|
||||
if _dp not in _cands:
|
||||
_cands.append(_dp)
|
||||
for _dp in _cands:
|
||||
if not os.path.exists(_dp):
|
||||
continue
|
||||
# The content, not the size: a member written corrupt
|
||||
# keeps its length, and a same-sized homonym from another
|
||||
# cache is another file.
|
||||
if os.path.getsize(_dp) == size and _file_sha1(_dp) == sha1:
|
||||
return _bn
|
||||
if (
|
||||
name.endswith(".zip") and _dp.endswith(".zip")
|
||||
and _same_members(zf, name, _dp)
|
||||
):
|
||||
return _bn
|
||||
return None
|
||||
|
||||
|
||||
def verify_pack(
|
||||
zip_path: str, db: dict, data_registry: dict | None = None
|
||||
) -> tuple[bool, dict]:
|
||||
@@ -138,53 +241,23 @@ def verify_pack(
|
||||
by_name = db.get("indexes", {}).get("by_name", {}) # name -> [SHA1]
|
||||
|
||||
# Data directory file index
|
||||
_data_index: dict[str, list[str]] = {}
|
||||
_data_path_index: dict[str, str] = {}
|
||||
if data_registry:
|
||||
for _dk, _de in data_registry.items():
|
||||
cache = _de.get("local_cache", "")
|
||||
if not cache or not os.path.isdir(cache):
|
||||
continue
|
||||
for _r, _d, _fns in os.walk(cache):
|
||||
for _fn in _fns:
|
||||
_fp = os.path.join(_r, _fn)
|
||||
_rel = os.path.relpath(_fp, cache)
|
||||
_data_path_index[_rel] = _fp
|
||||
_data_index.setdefault(_fn, []).append(_fp)
|
||||
data_indexes = _data_indexes(data_registry)
|
||||
|
||||
manifest = {
|
||||
manifest: dict = {
|
||||
"schema_version": 1,
|
||||
"version": 1,
|
||||
"generator": "retrobios generate_pack.py",
|
||||
"generated": _build_timestamp(db),
|
||||
"files": [],
|
||||
}
|
||||
errors = []
|
||||
errors: list[str] = []
|
||||
|
||||
with zipfile.ZipFile(zip_path, "r") as zf:
|
||||
for info in zf.infolist():
|
||||
if info.is_dir():
|
||||
if info.is_dir() or _is_pack_note(info.filename):
|
||||
continue
|
||||
name = info.filename
|
||||
# The builder writes its notes beside the slot they explain
|
||||
# (bios/INSTRUCTIONS_x.txt, games/keen6/INSTRUCTIONS_y.txt), so
|
||||
# they are told by their own name, not by a root prefix.
|
||||
base = name.rsplit("/", 1)[-1]
|
||||
if base.startswith(("INSTRUCTIONS_", "RENAMED_")) or base == MEDIA_MARKER or name in (
|
||||
"manifest.json",
|
||||
"README.txt",
|
||||
):
|
||||
continue
|
||||
with zf.open(info) as f:
|
||||
sha1_h = hashlib.sha1()
|
||||
md5_h = hashlib.md5()
|
||||
size = 0
|
||||
for chunk in iter(lambda: f.read(65536), b""):
|
||||
sha1_h.update(chunk)
|
||||
md5_h.update(chunk)
|
||||
size += len(chunk)
|
||||
sha1 = sha1_h.hexdigest()
|
||||
md5 = md5_h.hexdigest()
|
||||
sha1, md5, size = _member_digests(zf, info)
|
||||
|
||||
# Look up in database: files_db keyed by SHA1
|
||||
db_entry = files_db.get(sha1)
|
||||
@@ -195,61 +268,26 @@ def verify_pack(
|
||||
else:
|
||||
# Try MD5 -> SHA1 lookup
|
||||
ref_sha1 = by_md5.get(md5)
|
||||
if ref_sha1:
|
||||
db_entry = files_db.get(ref_sha1)
|
||||
if db_entry:
|
||||
file_name = db_entry.get("name", "")
|
||||
status = "verified_md5"
|
||||
else:
|
||||
status = "untracked"
|
||||
db_entry = files_db.get(ref_sha1) if ref_sha1 else None
|
||||
if db_entry:
|
||||
file_name = db_entry.get("name", "")
|
||||
status = "verified_md5"
|
||||
else:
|
||||
status = "untracked"
|
||||
|
||||
# Rebuilt ZIP: verify inner ROM CRC32s match source
|
||||
if status == "untracked" and name.endswith(".zip"):
|
||||
_bn = os.path.basename(name)
|
||||
for _src_sha1 in by_name.get(_bn, []):
|
||||
if _src_sha1 not in files_db:
|
||||
continue
|
||||
_src_path = files_db[_src_sha1]["path"]
|
||||
if not os.path.exists(_src_path):
|
||||
continue
|
||||
if _same_members(zf, name, _src_path):
|
||||
status = "verified_rebuild"
|
||||
file_name = _bn
|
||||
break
|
||||
rebuilt = _rebuilt_from(zf, name, by_name, files_db)
|
||||
if rebuilt:
|
||||
status = "verified_rebuild"
|
||||
file_name = rebuilt
|
||||
|
||||
# Data directory: check against cached files
|
||||
if status == "untracked" and _data_index:
|
||||
_bn = os.path.basename(name)
|
||||
_pr = name
|
||||
for _known_prefix in ("system/", "bios/", "BIOS/", "Firmware/"):
|
||||
if name.startswith(_known_prefix):
|
||||
_pr = name[len(_known_prefix):]
|
||||
break
|
||||
_cands = []
|
||||
if _pr in _data_path_index:
|
||||
_cands.append(_data_path_index[_pr])
|
||||
for _dp in _data_index.get(_bn, []):
|
||||
if _dp not in _cands:
|
||||
_cands.append(_dp)
|
||||
for _dp in _cands:
|
||||
if not os.path.exists(_dp):
|
||||
continue
|
||||
# The content, not the size: a member written corrupt
|
||||
# keeps its length, and a same-sized homonym from another
|
||||
# cache is another file.
|
||||
if os.path.getsize(_dp) == size and _file_sha1(_dp) == sha1:
|
||||
status = "verified_data"
|
||||
file_name = _bn
|
||||
break
|
||||
if (
|
||||
name.endswith(".zip") and _dp.endswith(".zip")
|
||||
and _same_members(zf, name, _dp)
|
||||
):
|
||||
status = "verified_data"
|
||||
file_name = _bn
|
||||
break
|
||||
if status == "untracked" and data_indexes[0]:
|
||||
cached = _cached_as(zf, name, size, sha1, data_indexes)
|
||||
if cached:
|
||||
status = "verified_data"
|
||||
file_name = cached
|
||||
|
||||
status, file_name = _settle_untracked(
|
||||
status, name, zf, by_md5, held_inside, errors, file_name
|
||||
@@ -374,8 +412,8 @@ def _check_member_hash(
|
||||
for h in digests.values():
|
||||
h.update(chunk)
|
||||
crc = zlib.crc32(chunk, crc)
|
||||
actual = {name: h.hexdigest() for name, h in digests.items()}
|
||||
actual["crc32"] = format(crc & 0xFFFFFFFF, "08x")
|
||||
computed = {name: h.hexdigest() for name, h in digests.items()}
|
||||
computed["crc32"] = format(crc & 0xFFFFFFFF, "08x")
|
||||
|
||||
def composite() -> str | None:
|
||||
# Recalbox's Md5Composite, over the sorted members of a ZIP.
|
||||
@@ -390,10 +428,10 @@ def _check_member_hash(
|
||||
return None
|
||||
|
||||
if digest_verdict(
|
||||
file_entry, zf.getinfo(member).file_size, lambda: actual, mode, rule, composite
|
||||
file_entry, zf.getinfo(member).file_size, lambda: computed, mode, rule, composite
|
||||
):
|
||||
return None
|
||||
return f"{member}: {mode} {actual[mode]} != declared {declared}"
|
||||
return f"{member}: {mode} {computed[mode]} != declared {declared}"
|
||||
|
||||
def _repo_satisfies_declaration(
|
||||
entries: list[dict], db: dict, mode: str
|
||||
@@ -534,13 +572,14 @@ def _structural_errors(zf: zipfile.ZipFile, zip_set: set) -> list[str]:
|
||||
errors.append(f"path traversal: {n}")
|
||||
|
||||
# Zero-byte check (exclude Dolphin GraphicMods markers)
|
||||
for info in zf.infolist():
|
||||
if info.file_size == 0 and not info.is_dir():
|
||||
if "GraphicMods" not in info.filename and info.filename not in (
|
||||
"manifest.json",
|
||||
"README.txt",
|
||||
) and info.filename.rsplit("/", 1)[-1] != MEDIA_MARKER:
|
||||
errors.append(f"zero-byte: {info.filename}")
|
||||
errors.extend(
|
||||
f"zero-byte: {info.filename}"
|
||||
for info in zf.infolist()
|
||||
if info.file_size == 0 and not info.is_dir()
|
||||
and "GraphicMods" not in info.filename
|
||||
and info.filename not in ("manifest.json", "README.txt")
|
||||
and info.filename.rsplit("/", 1)[-1] != MEDIA_MARKER
|
||||
)
|
||||
|
||||
return errors
|
||||
|
||||
@@ -561,20 +600,11 @@ def _shipped_copy(
|
||||
|
||||
|
||||
def _builder_extras(
|
||||
config: dict,
|
||||
emulators_dir: str,
|
||||
expect: _Expectation,
|
||||
db: dict,
|
||||
emu_profiles: dict,
|
||||
baseline_groups: dict[str, list[dict]],
|
||||
base_dest: str,
|
||||
is_flat: bool,
|
||||
zip_set: set[str],
|
||||
zip_lower: dict[str, str],
|
||||
target_cores: set[str] | None,
|
||||
region_drops: set[str],
|
||||
data_registry: dict | None,
|
||||
view: _PackView,
|
||||
include_all: bool = False,
|
||||
required_only: bool = False,
|
||||
) -> dict[str, str]:
|
||||
"""{full destination: local copy} of each core extra the builder writes.
|
||||
|
||||
@@ -588,15 +618,18 @@ def _builder_extras(
|
||||
from packextras import ExtraResolver, hidden_extras
|
||||
from validation import _build_validation_index, destination_owners
|
||||
|
||||
config, emu_profiles = expect.config, expect.emu_profiles
|
||||
data_registry = expect.opts.data_registry
|
||||
base_dest = expect.base_dest
|
||||
mode = config.get("verification_mode", "existence")
|
||||
hash_rule = config.get("hash_rule", "")
|
||||
shipped: dict[str, str | None] = {}
|
||||
for expected, declarations in baseline_groups.items():
|
||||
if expected not in zip_set and expected.lower() not in zip_lower:
|
||||
if not view.holds(expected):
|
||||
continue
|
||||
unflat = (
|
||||
f"{base_dest}/{expected}"
|
||||
if is_flat and base_dest and not expected.startswith(f"{base_dest}/")
|
||||
if view.is_flat and base_dest and not expected.startswith(f"{base_dest}/")
|
||||
else expected
|
||||
)
|
||||
if any(fe.get("storage") in ("user_provided", "external") for fe in declarations):
|
||||
@@ -616,8 +649,8 @@ def _builder_extras(
|
||||
}
|
||||
placed_local: dict[str, str] = {}
|
||||
hidden_extras(
|
||||
config, emulators_dir, db, emu_profiles, shipped, target_cores,
|
||||
region_drops or None,
|
||||
config, expect.opts.emulators_dir, db, emu_profiles, shipped, expect.target_cores,
|
||||
expect.region_drops or None,
|
||||
resolver=ExtraResolver(
|
||||
db, DEFAULT_BIOS_DIR, build_zip_contents_index(db), data_registry,
|
||||
_build_validation_index(platform_profiles),
|
||||
@@ -625,7 +658,7 @@ def _builder_extras(
|
||||
),
|
||||
placed_local=placed_local,
|
||||
include_all=include_all,
|
||||
required_only=required_only,
|
||||
required_only=expect.opts.required_only,
|
||||
)
|
||||
return placed_local
|
||||
|
||||
@@ -639,19 +672,351 @@ def _same_content(zf: zipfile.ZipFile, member: str, local: str) -> bool:
|
||||
return local.lower().endswith(".zip") and _same_members(zf, member, local)
|
||||
|
||||
|
||||
class PlatformCheckOptions(TypedDict, total=False):
|
||||
"""The keyword options of verify_pack_against_platform."""
|
||||
|
||||
emulators_dir: str
|
||||
emu_profiles: dict | None
|
||||
regions: list[str] | None
|
||||
data_registry: dict | None
|
||||
source: str
|
||||
required_only: bool
|
||||
no_game_data: bool
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _PlatformCheckSettings:
|
||||
"""PlatformCheckOptions with their defaults."""
|
||||
|
||||
emulators_dir: str = "emulators"
|
||||
emu_profiles: dict | None = None
|
||||
regions: list[str] | None = None
|
||||
data_registry: dict | None = None
|
||||
source: str = "full"
|
||||
required_only: bool = False
|
||||
no_game_data: bool = False
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _Expectation:
|
||||
"""What the builder would have packed for a platform, before the pack is read."""
|
||||
|
||||
config: dict
|
||||
emu_profiles: dict
|
||||
expected_systems: dict
|
||||
region_drops: set[str]
|
||||
db: dict | None
|
||||
target_cores: set[str] | None
|
||||
opts: _PlatformCheckSettings
|
||||
|
||||
@property
|
||||
def base_dest(self) -> str:
|
||||
return self.config.get("base_destination", "")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _PackView:
|
||||
"""A pack's member names as the checks read them."""
|
||||
|
||||
zf: zipfile.ZipFile
|
||||
zip_set: set[str]
|
||||
zip_lower: dict[str, str]
|
||||
zip_parents: set[str]
|
||||
is_flat: bool
|
||||
|
||||
def holds(self, expected: str) -> bool:
|
||||
return expected in self.zip_set or expected.lower() in self.zip_lower
|
||||
|
||||
|
||||
@dataclass
|
||||
class _BaselineCount:
|
||||
"""The baseline destinations checked, and what each came to."""
|
||||
|
||||
checked: int = 0
|
||||
present: int = 0
|
||||
excluded: int = 0
|
||||
absent: int = 0
|
||||
decl_by_member: dict[str, list[dict]] = field(default_factory=dict)
|
||||
|
||||
|
||||
def _reject_unknown(function: str, options: Mapping[str, object], known: frozenset[str]) -> None:
|
||||
"""Refuse a keyword the options do not declare, as a named parameter would."""
|
||||
for key in options:
|
||||
if key not in known:
|
||||
raise TypeError(f"{function}() got an unexpected keyword argument '{key}'")
|
||||
|
||||
|
||||
def _cut_config(
|
||||
platform_name: str,
|
||||
platforms_dir: str,
|
||||
target_cores: set[str] | None,
|
||||
opts: _PlatformCheckSettings,
|
||||
) -> tuple[dict, dict]:
|
||||
"""The platform's config and the profiles, without game data when asked."""
|
||||
config = load_platform_config(platform_name, platforms_dir)
|
||||
emu_profiles = opts.emu_profiles
|
||||
if emu_profiles is None:
|
||||
emu_profiles = load_emulator_profiles(opts.emulators_dir)
|
||||
if opts.no_game_data:
|
||||
cut = without_game_data(config, emu_profiles, opts.data_registry, target_cores)
|
||||
config, emu_profiles = cut.config, cut.profiles
|
||||
return config, emu_profiles
|
||||
|
||||
|
||||
def _region_drops(
|
||||
config: dict,
|
||||
emu_profiles: dict,
|
||||
expected_systems: dict,
|
||||
db: dict | None,
|
||||
*,
|
||||
target_cores: set[str] | None,
|
||||
opts: _PlatformCheckSettings,
|
||||
) -> set[str]:
|
||||
"""The regional variants the builder leaves out, by the same selection."""
|
||||
region_drops: set[str] = set()
|
||||
if not opts.regions:
|
||||
return region_drops
|
||||
region_index = region_mod.platform_region_index(
|
||||
config, emu_profiles, target_cores
|
||||
)
|
||||
held: set[str] = set()
|
||||
held_zip_contents = build_zip_contents_index(db) if db is not None else {}
|
||||
region_groups, _extra_dests = platform_region_groups(
|
||||
config,
|
||||
expected_systems,
|
||||
opts.emulators_dir,
|
||||
db,
|
||||
config.get("base_destination", ""),
|
||||
emu_profiles,
|
||||
target_cores=target_cores,
|
||||
include_extras=(opts.source != "platform"),
|
||||
include_all=(opts.source == "truth"),
|
||||
required_only=opts.required_only,
|
||||
held_out=held,
|
||||
zip_contents=held_zip_contents,
|
||||
data_registry=opts.data_registry,
|
||||
slot_overrides=slots.pack_overrides(
|
||||
config, emu_profiles, db, held_zip_contents, opts.data_registry,
|
||||
target_cores=target_cores, source=opts.source,
|
||||
) if db is not None else None,
|
||||
)
|
||||
return region_mod.resolve_region_drops(
|
||||
region_groups, region_index, opts.regions, held
|
||||
)
|
||||
|
||||
|
||||
def _pack_view(zf: zipfile.ZipFile, config: dict, base_dest: str) -> _PackView:
|
||||
zip_set = set(zf.namelist())
|
||||
# A case twin stands in for the file only where the platform's
|
||||
# filesystem folds case, as the builder decides: on Lakka bios.col
|
||||
# and BIOS.col are two files two cores open, and either one missing
|
||||
# was counted present through the other.
|
||||
zip_lower = (
|
||||
{n.lower(): n for n in zip_set}
|
||||
if config.get("case_insensitive_fs", False) else {}
|
||||
)
|
||||
|
||||
# Auto-detect flat vs nested ZIP
|
||||
is_flat = bool(base_dest) and not any(
|
||||
n.startswith(base_dest + "/")
|
||||
for n in zip_set
|
||||
if n not in ("README.txt", "manifest.json", MEDIA_MARKER) and not n.endswith("/")
|
||||
)
|
||||
# Mirror the builder's path-conflict logic: a declaration whose path
|
||||
# collides file-vs-directory with a packed member was skipped by the
|
||||
# builder (upstream declares e.g. both SGB1.sfc and SGB1.sfc/program.rom)
|
||||
zip_parents: set[str] = set()
|
||||
for n in zip_set:
|
||||
parts = n.split("/")
|
||||
for i in range(1, len(parts)):
|
||||
zip_parents.add("/".join(parts[:i]))
|
||||
return _PackView(zf, zip_set, zip_lower, zip_parents, is_flat)
|
||||
|
||||
|
||||
def _baseline_groups(
|
||||
expect: _Expectation, is_flat: bool,
|
||||
) -> tuple[dict[str, list[dict]], dict[int, str]]:
|
||||
"""The platform's declarations by the path they expect in the pack, and
|
||||
the system of each declaration."""
|
||||
opts = expect.opts
|
||||
base_dest = expect.base_dest
|
||||
baseline_groups: dict[str, list[dict]] = {}
|
||||
system_of: dict[int, str] = {}
|
||||
for sys_id, system in (expect.expected_systems if opts.source != "truth" else {}).items():
|
||||
for fe in system.get("files", []):
|
||||
system_of[id(fe)] = sys_id
|
||||
if opts.required_only and fe.get("required") is False:
|
||||
continue
|
||||
dest = sanitize_pack_path(fe.get("destination", fe.get("name", "")))
|
||||
if not dest:
|
||||
continue
|
||||
if expect.region_drops and dest in expect.region_drops:
|
||||
continue
|
||||
expected = f"{base_dest}/{dest}" if base_dest and not is_flat else dest
|
||||
baseline_groups.setdefault(expected, []).append(fe)
|
||||
return baseline_groups, system_of
|
||||
|
||||
|
||||
def _count_baseline(
|
||||
expect: _Expectation,
|
||||
view: _PackView,
|
||||
baseline_groups: dict[str, list[dict]],
|
||||
system_of: dict[int, str],
|
||||
errors: list[str],
|
||||
) -> _BaselineCount:
|
||||
"""Each baseline destination present, accounted for, or missing."""
|
||||
db = expect.db
|
||||
data_registry = expect.opts.data_registry
|
||||
count = _BaselineCount(checked=len(baseline_groups))
|
||||
# RetroBat's group: one packed file of it is enough, as verify counts it.
|
||||
packed_groups = {
|
||||
(system_of[id(fe)], fe["group"])
|
||||
for expected, declarations in baseline_groups.items()
|
||||
if view.holds(expected)
|
||||
for fe in declarations
|
||||
if fe.get("group")
|
||||
}
|
||||
exclusion_index = build_zip_contents_index(db) if db is not None else {}
|
||||
categories = EntryCategories(expect.config, expect.emu_profiles, data_registry)
|
||||
for expected, declarations in baseline_groups.items():
|
||||
if expected in view.zip_set:
|
||||
member = expected
|
||||
elif expected.lower() in view.zip_lower:
|
||||
member = view.zip_lower[expected.lower()]
|
||||
elif _has_path_conflict(expected, view.zip_set, view.zip_parents):
|
||||
# Skipped by the builder for the same reason: not an error
|
||||
count.present += 1
|
||||
continue
|
||||
elif db is not None and _intentional_hash_exclusion(
|
||||
declarations,
|
||||
db,
|
||||
zip_contents=exclusion_index,
|
||||
data_dir_registry=data_registry,
|
||||
verification_mode=expect.config.get("verification_mode", "existence"),
|
||||
hash_rule=expect.config.get("hash_rule", ""),
|
||||
):
|
||||
count.excluded += 1
|
||||
continue
|
||||
elif all(
|
||||
fe.get("group") and (system_of[id(fe)], fe["group"]) in packed_groups
|
||||
for fe in declarations
|
||||
):
|
||||
count.absent += 1
|
||||
continue
|
||||
elif db is not None and all(
|
||||
categories.category(fe, fe.get("destination", fe.get("name", "")),
|
||||
system_of[id(fe)]) != BIOS
|
||||
for fe in declarations
|
||||
) and _held_nowhere(declarations, db, exclusion_index, data_registry):
|
||||
# A game's data or an entry no core opens that the collection
|
||||
# lacks: verify counts it apart. Missing firmware stays an error.
|
||||
count.absent += 1
|
||||
continue
|
||||
else:
|
||||
errors.append(f"baseline missing: {expected}")
|
||||
continue
|
||||
count.present += 1
|
||||
count.decl_by_member.setdefault(member, []).extend(declarations)
|
||||
return count
|
||||
|
||||
|
||||
def _native_hash_errors(
|
||||
zf: zipfile.ZipFile, decl_by_member: dict[str, list[dict]], config: dict, db: dict | None,
|
||||
) -> list[str]:
|
||||
"""The platform's native hash check replayed on the pack's bytes."""
|
||||
# Reproduce the platform's native hash check on pack bytes.
|
||||
# A destination declared by several entries passes when the packed
|
||||
# member satisfies any of them. A failure only counts as a pack
|
||||
# error when the repo holds a file matching a declaration: without
|
||||
# one, the pack ships its best effort and the gap is a data issue
|
||||
# reported by verify.py, not a generation bug.
|
||||
verification_mode = config.get("verification_mode", "existence")
|
||||
digest = digest_algorithm(verification_mode)
|
||||
hash_rule = config.get("hash_rule", "")
|
||||
errors: list[str] = []
|
||||
if not (reads_file_contents(verification_mode) and digest):
|
||||
return errors
|
||||
for member, decl_entries in decl_by_member.items():
|
||||
checkable = [
|
||||
fe
|
||||
for fe in decl_entries
|
||||
if str(fe.get(digest) or "").strip()
|
||||
]
|
||||
if not checkable:
|
||||
continue
|
||||
# An archive declared once per ROM it must hold is checked
|
||||
# once per ROM by the frontend: each such declaration has to
|
||||
# pass on its own, where the repository holds an archive
|
||||
# that would.
|
||||
for fe in checkable:
|
||||
if not fe.get("zipped_file"):
|
||||
continue
|
||||
err = _check_member_hash(zf, member, fe, digest, hash_rule)
|
||||
if err is not None and (db is None or _repo_holds_member(fe, db)):
|
||||
errors.append(err)
|
||||
member_errors = []
|
||||
satisfied = False
|
||||
for fe in checkable:
|
||||
err = _check_member_hash(zf, member, fe, digest, hash_rule)
|
||||
if err is None:
|
||||
satisfied = True
|
||||
break
|
||||
member_errors.append(err)
|
||||
if satisfied:
|
||||
continue
|
||||
if db is None or _repo_satisfies_declaration(
|
||||
checkable, db, verification_mode
|
||||
):
|
||||
errors.append(member_errors[0])
|
||||
return errors
|
||||
|
||||
|
||||
def _check_core_extras(
|
||||
expect: _Expectation,
|
||||
view: _PackView,
|
||||
baseline_groups: dict[str, list[dict]],
|
||||
errors: list[str],
|
||||
) -> tuple[int, int]:
|
||||
"""(checked, present) of the core extras the builder places."""
|
||||
# 2. Core extras: the builder's own placement replayed
|
||||
# (packextras.hidden_extras, which verify reads too), after the
|
||||
# platform files it wrote. Each placed extra must sit at its path
|
||||
# with the bytes the builder resolved: an empty seen set and a
|
||||
# presence test credited a core file at a path where the pack
|
||||
# holds the platform's other dump.
|
||||
core_checked = 0
|
||||
core_present = 0
|
||||
opts = expect.opts
|
||||
db = expect.db
|
||||
if db is None or opts.source == "platform":
|
||||
return core_checked, core_present
|
||||
placed_local = _builder_extras(
|
||||
expect, db, baseline_groups, view, include_all=(opts.source == "truth"),
|
||||
)
|
||||
for full, local in sorted(placed_local.items()):
|
||||
core_checked += 1
|
||||
flat = _flat(full, expect.base_dest, view.is_flat)
|
||||
member = flat if flat in view.zip_set else view.zip_lower.get(flat.lower())
|
||||
if member is None:
|
||||
errors.append(f"core missing: {full}")
|
||||
elif not _same_content(view.zf, member, local):
|
||||
errors.append(
|
||||
f"core file differs: {member} is not the {os.path.basename(local)}"
|
||||
" the builder places there"
|
||||
)
|
||||
else:
|
||||
core_present += 1
|
||||
return core_checked, core_present
|
||||
|
||||
|
||||
def verify_pack_against_platform(
|
||||
zip_path: str,
|
||||
platform_name: str,
|
||||
platforms_dir: str,
|
||||
db: dict | None = None,
|
||||
emulators_dir: str = "emulators",
|
||||
emu_profiles: dict | None = None,
|
||||
regions: list[str] | None = None,
|
||||
data_registry: dict | None = None,
|
||||
*,
|
||||
target_cores: set[str] | None = None,
|
||||
source: str = "full",
|
||||
required_only: bool = False,
|
||||
no_game_data: bool = False,
|
||||
**options: Unpack[PlatformCheckOptions],
|
||||
) -> tuple[bool, int, int, list[str], int, int, int, int, int, int]:
|
||||
"""Verify a pack ZIP against its platform config and core requirements.
|
||||
|
||||
@@ -679,19 +1044,11 @@ def verify_pack_against_platform(
|
||||
holds no file for that are a game's data or that no core the platform
|
||||
runs opens. verify.py counts them apart from firmware.
|
||||
"""
|
||||
|
||||
config = load_platform_config(platform_name, platforms_dir)
|
||||
base_dest = config.get("base_destination", "")
|
||||
errors: list[str] = []
|
||||
checked = 0
|
||||
present = 0
|
||||
|
||||
if emu_profiles is None:
|
||||
emu_profiles = load_emulator_profiles(emulators_dir)
|
||||
if no_game_data:
|
||||
cut = without_game_data(config, emu_profiles, data_registry, target_cores)
|
||||
config, emu_profiles = cut.config, cut.profiles
|
||||
|
||||
_reject_unknown(
|
||||
"verify_pack_against_platform", options, PlatformCheckOptions.__optional_keys__
|
||||
)
|
||||
opts = _PlatformCheckSettings(**options)
|
||||
config, emu_profiles = _cut_config(platform_name, platforms_dir, target_cores, opts)
|
||||
expected_systems = config.get("systems", {})
|
||||
if target_cores is not None:
|
||||
expected_systems = filter_systems_by_target(
|
||||
@@ -700,221 +1057,35 @@ def verify_pack_against_platform(
|
||||
target_cores,
|
||||
resolve_platform_cores(config, emu_profiles),
|
||||
)
|
||||
|
||||
region_drops: set[str] = set()
|
||||
if regions:
|
||||
region_index = region_mod.platform_region_index(
|
||||
config, emu_profiles, target_cores
|
||||
)
|
||||
held: set[str] = set()
|
||||
held_zip_contents = build_zip_contents_index(db) if db is not None else {}
|
||||
region_groups, _extra_dests = platform_region_groups(
|
||||
config,
|
||||
expected_systems,
|
||||
emulators_dir,
|
||||
db,
|
||||
base_dest,
|
||||
emu_profiles,
|
||||
target_cores=target_cores,
|
||||
include_extras=(source != "platform"),
|
||||
include_all=(source == "truth"),
|
||||
required_only=required_only,
|
||||
held_out=held,
|
||||
zip_contents=held_zip_contents,
|
||||
data_registry=data_registry,
|
||||
slot_overrides=slots.pack_overrides(
|
||||
config, emu_profiles, db, held_zip_contents, data_registry,
|
||||
target_cores=target_cores, source=source,
|
||||
) if db is not None else None,
|
||||
)
|
||||
region_drops = region_mod.resolve_region_drops(
|
||||
region_groups, region_index, regions, held
|
||||
)
|
||||
expect = _Expectation(
|
||||
config, emu_profiles, expected_systems,
|
||||
_region_drops(
|
||||
config, emu_profiles, expected_systems, db,
|
||||
target_cores=target_cores, opts=opts,
|
||||
),
|
||||
db, target_cores, opts,
|
||||
)
|
||||
errors: list[str] = []
|
||||
|
||||
with zipfile.ZipFile(zip_path, "r") as zf:
|
||||
zip_set = set(zf.namelist())
|
||||
# A case twin stands in for the file only where the platform's
|
||||
# filesystem folds case, as the builder decides: on Lakka bios.col
|
||||
# and BIOS.col are two files two cores open, and either one missing
|
||||
# was counted present through the other.
|
||||
zip_lower = (
|
||||
{n.lower(): n for n in zip_set}
|
||||
if config.get("case_insensitive_fs", False) else {}
|
||||
)
|
||||
|
||||
# Auto-detect flat vs nested ZIP
|
||||
is_flat = bool(base_dest) and not any(
|
||||
n.startswith(base_dest + "/")
|
||||
for n in zip_set
|
||||
if n not in ("README.txt", "manifest.json", MEDIA_MARKER) and not n.endswith("/")
|
||||
)
|
||||
|
||||
errors.extend(_structural_errors(zf, zip_set))
|
||||
view = _pack_view(zf, expect.config, expect.base_dest)
|
||||
errors.extend(_structural_errors(zf, view.zip_set))
|
||||
|
||||
# 1. Baseline file presence + native hash check
|
||||
verification_mode = config.get("verification_mode", "existence")
|
||||
baseline_checked = 0
|
||||
baseline_present = 0
|
||||
baseline_excluded = 0
|
||||
baseline_absent = 0
|
||||
decl_by_member: dict[str, list[dict]] = {}
|
||||
# Mirror the builder's path-conflict logic: a declaration whose path
|
||||
# collides file-vs-directory with a packed member was skipped by the
|
||||
# builder (upstream declares e.g. both SGB1.sfc and SGB1.sfc/program.rom)
|
||||
zip_parents: set[str] = set()
|
||||
for n in zip_set:
|
||||
parts = n.split("/")
|
||||
for i in range(1, len(parts)):
|
||||
zip_parents.add("/".join(parts[:i]))
|
||||
baseline_groups: dict[str, list[dict]] = {}
|
||||
system_of: dict[int, str] = {}
|
||||
for sys_id, system in (expected_systems if source != "truth" else {}).items():
|
||||
for fe in system.get("files", []):
|
||||
system_of[id(fe)] = sys_id
|
||||
if required_only and fe.get("required") is False:
|
||||
continue
|
||||
dest = sanitize_pack_path(fe.get("destination", fe.get("name", "")))
|
||||
if not dest:
|
||||
continue
|
||||
if region_drops and dest in region_drops:
|
||||
continue
|
||||
expected = f"{base_dest}/{dest}" if base_dest and not is_flat else dest
|
||||
baseline_groups.setdefault(expected, []).append(fe)
|
||||
|
||||
baseline_checked = len(baseline_groups)
|
||||
# RetroBat's group: one packed file of it is enough, as verify counts it.
|
||||
packed_groups = {
|
||||
(system_of[id(fe)], fe["group"])
|
||||
for expected, declarations in baseline_groups.items()
|
||||
if expected in zip_set or expected.lower() in zip_lower
|
||||
for fe in declarations
|
||||
if fe.get("group")
|
||||
}
|
||||
exclusion_index = build_zip_contents_index(db) if db is not None else {}
|
||||
categories = EntryCategories(config, emu_profiles, data_registry)
|
||||
for expected, declarations in baseline_groups.items():
|
||||
if expected in zip_set:
|
||||
member = expected
|
||||
elif expected.lower() in zip_lower:
|
||||
member = zip_lower[expected.lower()]
|
||||
elif _has_path_conflict(expected, zip_set, zip_parents):
|
||||
# Skipped by the builder for the same reason: not an error
|
||||
baseline_present += 1
|
||||
continue
|
||||
elif db is not None and _intentional_hash_exclusion(
|
||||
declarations,
|
||||
db,
|
||||
zip_contents=exclusion_index,
|
||||
data_dir_registry=data_registry,
|
||||
verification_mode=verification_mode,
|
||||
hash_rule=config.get("hash_rule", ""),
|
||||
):
|
||||
baseline_excluded += 1
|
||||
continue
|
||||
elif all(
|
||||
fe.get("group") and (system_of[id(fe)], fe["group"]) in packed_groups
|
||||
for fe in declarations
|
||||
):
|
||||
baseline_absent += 1
|
||||
continue
|
||||
elif db is not None and all(
|
||||
categories.category(fe, fe.get("destination", fe.get("name", "")),
|
||||
system_of[id(fe)]) != BIOS
|
||||
for fe in declarations
|
||||
) and _held_nowhere(declarations, db, exclusion_index, data_registry):
|
||||
# A game's data or an entry no core opens that the collection
|
||||
# lacks: verify counts it apart. Missing firmware stays an error.
|
||||
baseline_absent += 1
|
||||
continue
|
||||
else:
|
||||
errors.append(f"baseline missing: {expected}")
|
||||
continue
|
||||
baseline_present += 1
|
||||
decl_by_member.setdefault(member, []).extend(declarations)
|
||||
|
||||
# Reproduce the platform's native hash check on pack bytes.
|
||||
# A destination declared by several entries passes when the packed
|
||||
# member satisfies any of them. A failure only counts as a pack
|
||||
# error when the repo holds a file matching a declaration: without
|
||||
# one, the pack ships its best effort and the gap is a data issue
|
||||
# reported by verify.py, not a generation bug.
|
||||
digest = digest_algorithm(verification_mode)
|
||||
hash_rule = config.get("hash_rule", "")
|
||||
if reads_file_contents(verification_mode) and digest:
|
||||
for member, decl_entries in decl_by_member.items():
|
||||
checkable = [
|
||||
fe
|
||||
for fe in decl_entries
|
||||
if str(fe.get(digest) or "").strip()
|
||||
]
|
||||
if not checkable:
|
||||
continue
|
||||
# An archive declared once per ROM it must hold is checked
|
||||
# once per ROM by the frontend: each such declaration has to
|
||||
# pass on its own, where the repository holds an archive
|
||||
# that would.
|
||||
for fe in checkable:
|
||||
if not fe.get("zipped_file"):
|
||||
continue
|
||||
err = _check_member_hash(zf, member, fe, digest, hash_rule)
|
||||
if err is not None and (db is None or _repo_holds_member(fe, db)):
|
||||
errors.append(err)
|
||||
member_errors = []
|
||||
satisfied = False
|
||||
for fe in checkable:
|
||||
err = _check_member_hash(zf, member, fe, digest, hash_rule)
|
||||
if err is None:
|
||||
satisfied = True
|
||||
break
|
||||
member_errors.append(err)
|
||||
if satisfied:
|
||||
continue
|
||||
if db is None or _repo_satisfies_declaration(
|
||||
checkable, db, verification_mode
|
||||
):
|
||||
errors.append(member_errors[0])
|
||||
|
||||
# 2. Core extras: the builder's own placement replayed
|
||||
# (packextras.hidden_extras, which verify reads too), after the
|
||||
# platform files it wrote. Each placed extra must sit at its path
|
||||
# with the bytes the builder resolved: an empty seen set and a
|
||||
# presence test credited a core file at a path where the pack
|
||||
# holds the platform's other dump.
|
||||
core_checked = 0
|
||||
core_present = 0
|
||||
if db is not None and source != "platform":
|
||||
placed_local = _builder_extras(
|
||||
config, emulators_dir, db, emu_profiles, baseline_groups,
|
||||
base_dest, is_flat, zip_set, zip_lower, target_cores,
|
||||
region_drops, data_registry,
|
||||
include_all=(source == "truth"), required_only=required_only,
|
||||
)
|
||||
for full, local in sorted(placed_local.items()):
|
||||
core_checked += 1
|
||||
member = _flat(full, base_dest, is_flat)
|
||||
member = member if member in zip_set else zip_lower.get(member.lower())
|
||||
if member is None:
|
||||
errors.append(f"core missing: {full}")
|
||||
elif not _same_content(zf, member, local):
|
||||
errors.append(
|
||||
f"core file differs: {member} is not the {os.path.basename(local)}"
|
||||
" the builder places there"
|
||||
)
|
||||
else:
|
||||
core_present += 1
|
||||
|
||||
checked = baseline_checked + core_checked
|
||||
present = baseline_present + core_present
|
||||
baseline_groups, system_of = _baseline_groups(expect, view.is_flat)
|
||||
baseline = _count_baseline(expect, view, baseline_groups, system_of, errors)
|
||||
errors.extend(_native_hash_errors(zf, baseline.decl_by_member, expect.config, db))
|
||||
core_checked, core_present = _check_core_extras(expect, view, baseline_groups, errors)
|
||||
|
||||
return (
|
||||
len(errors) == 0,
|
||||
checked,
|
||||
present,
|
||||
baseline.checked + core_checked,
|
||||
baseline.present + core_present,
|
||||
errors,
|
||||
baseline_checked,
|
||||
baseline_present,
|
||||
baseline.checked,
|
||||
baseline.present,
|
||||
core_checked,
|
||||
core_present,
|
||||
baseline_excluded,
|
||||
baseline_absent,
|
||||
baseline.excluded,
|
||||
baseline.absent,
|
||||
)
|
||||
@@ -217,6 +217,8 @@ def main() -> int:
|
||||
parser.add_argument("--rebuild", metavar="ZIP", help="normalize an archive")
|
||||
parser.add_argument("--output", "-o", help="output path for --rebuild")
|
||||
args = parser.parse_args()
|
||||
if not (args.check or args.rebuild):
|
||||
parser.error("nothing to do: pass --check or --rebuild")
|
||||
|
||||
if args.check:
|
||||
data = Path(args.check).read_bytes()
|
||||
@@ -228,14 +230,11 @@ def main() -> int:
|
||||
print(f" conform: {data == rebuilt}")
|
||||
return 0 if data == rebuilt else 1
|
||||
|
||||
if args.rebuild:
|
||||
data = rebuild_torrentzip(args.rebuild)
|
||||
dest = args.output or args.rebuild
|
||||
Path(dest).write_bytes(data)
|
||||
print(f"{dest}: {len(data)} bytes, md5 {hashlib.md5(data).hexdigest()}")
|
||||
return 0
|
||||
|
||||
parser.error("nothing to do: pass --check or --rebuild")
|
||||
data = rebuild_torrentzip(args.rebuild)
|
||||
dest = args.output or args.rebuild
|
||||
Path(dest).write_bytes(data)
|
||||
print(f"{dest}: {len(data)} bytes, md5 {hashlib.md5(data).hexdigest()}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+111
-72
@@ -5,6 +5,8 @@ capping a real romset: the largest pack is already several gigabytes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import functools
|
||||
import hashlib
|
||||
import os
|
||||
@@ -235,6 +237,109 @@ MAX_ZIP_COMPRESSION_RATIO = 1_100
|
||||
_BOUNDED_RATIO_METHODS = (zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _ZipLimits:
|
||||
"""The resource bounds safe_extract_zip enforces."""
|
||||
|
||||
members: int
|
||||
member_size: int
|
||||
total_size: int
|
||||
compression_ratio: int
|
||||
|
||||
|
||||
def _member_path(member: zipfile.ZipInfo, seen: set[str]) -> tuple[str, list[str], int]:
|
||||
"""(name, path components, mode) of a member whose path and type are safe."""
|
||||
# Archives written on Windows store a backslash separator. It is a
|
||||
# separator, not a filename character, so it is normalized before
|
||||
# the component checks rather than rejected.
|
||||
name = member.filename.replace("\\", "/")
|
||||
if not name or "\x00" in name:
|
||||
raise ValueError(f"Unsafe ZIP member name: {member.filename!r}")
|
||||
if name.startswith("/") or re.match(r"^[A-Za-z]:", name):
|
||||
raise ValueError(f"Absolute ZIP member path: {name}")
|
||||
parts = [part for part in name.split("/") if part]
|
||||
if any(part in (".", "..") for part in parts):
|
||||
raise ValueError(f"ZIP traversal detected: {name}")
|
||||
normalized = "/".join(parts)
|
||||
if normalized in seen:
|
||||
raise ValueError(f"Duplicate ZIP member path: {name}")
|
||||
seen.add(normalized)
|
||||
|
||||
mode = (member.external_attr >> 16) & 0xFFFF
|
||||
file_type = stat.S_IFMT(mode)
|
||||
if file_type not in (0, stat.S_IFREG, stat.S_IFDIR):
|
||||
raise ValueError(f"ZIP link or special file rejected: {name}")
|
||||
if member.flag_bits & 0x1:
|
||||
raise ValueError(f"Encrypted ZIP member rejected: {name}")
|
||||
return name, parts, mode
|
||||
|
||||
|
||||
def _check_member_size(
|
||||
member: zipfile.ZipInfo, name: str, declared_total: int, limits: _ZipLimits,
|
||||
) -> int:
|
||||
"""The declared total with this member, after its size limits are checked."""
|
||||
if member.file_size > limits.member_size:
|
||||
raise ValueError(
|
||||
f"ZIP member {name} is {member.file_size} bytes; "
|
||||
f"limit is {limits.member_size}"
|
||||
)
|
||||
declared_total += member.file_size
|
||||
if declared_total > limits.total_size:
|
||||
raise ValueError(
|
||||
f"ZIP expands to {declared_total} bytes, limit is {limits.total_size}"
|
||||
)
|
||||
if member.file_size and member.compress_type in _BOUNDED_RATIO_METHODS:
|
||||
if member.compress_size == 0:
|
||||
raise ValueError(f"Invalid compression size for ZIP member: {name}")
|
||||
if member.file_size / member.compress_size > limits.compression_ratio:
|
||||
raise ValueError(f"Suspicious compression ratio for ZIP member: {name}")
|
||||
return declared_total
|
||||
|
||||
|
||||
def _install_member(
|
||||
zf: zipfile.ZipFile,
|
||||
member: zipfile.ZipInfo,
|
||||
name: str,
|
||||
target: str,
|
||||
mode: int,
|
||||
umask: int,
|
||||
max_member_size: int,
|
||||
) -> None:
|
||||
"""Stream a member to a temporary sibling, check it, then move it in place."""
|
||||
os.makedirs(os.path.dirname(target), exist_ok=True)
|
||||
tmp_path = ""
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="wb", dir=os.path.dirname(target), delete=False
|
||||
) as tmp_file:
|
||||
tmp_path = tmp_file.name
|
||||
actual_size = 0
|
||||
with zf.open(member, "r") as source:
|
||||
while True:
|
||||
chunk = source.read(1024 * 1024)
|
||||
if not chunk:
|
||||
break
|
||||
actual_size += len(chunk)
|
||||
if actual_size > member.file_size or actual_size > max_member_size:
|
||||
raise ValueError(
|
||||
f"ZIP member exceeded declared or configured size: {name}"
|
||||
)
|
||||
tmp_file.write(chunk)
|
||||
if actual_size != member.file_size:
|
||||
raise ValueError(
|
||||
f"ZIP member size mismatch for {name}: "
|
||||
f"{actual_size} != {member.file_size}"
|
||||
)
|
||||
# The member's own mode, else what a plain open() gives. The
|
||||
# temporary file is private and would keep 0600 otherwise.
|
||||
os.chmod(tmp_path, ((mode & 0o777) or 0o666) & ~umask)
|
||||
os.replace(tmp_path, target)
|
||||
tmp_path = ""
|
||||
finally:
|
||||
if tmp_path and os.path.exists(tmp_path):
|
||||
os.unlink(tmp_path)
|
||||
|
||||
|
||||
def safe_extract_zip(
|
||||
zip_path: str,
|
||||
dest_dir: str,
|
||||
@@ -249,56 +354,22 @@ def safe_extract_zip(
|
||||
Files are streamed to a temporary sibling and atomically installed only
|
||||
after their declared length and CRC have been checked by ``zipfile``.
|
||||
"""
|
||||
limits = _ZipLimits(max_members, max_member_size, max_total_size, max_compression_ratio)
|
||||
dest = os.path.realpath(dest_dir)
|
||||
os.makedirs(dest, exist_ok=True)
|
||||
umask = process_umask()
|
||||
with zipfile.ZipFile(zip_path, "r") as zf:
|
||||
members = zf.infolist()
|
||||
if len(members) > max_members:
|
||||
if len(members) > limits.members:
|
||||
raise ValueError(
|
||||
f"ZIP has {len(members)} members, limit is {max_members}"
|
||||
f"ZIP has {len(members)} members, limit is {limits.members}"
|
||||
)
|
||||
|
||||
declared_total = 0
|
||||
seen: set[str] = set()
|
||||
for member in members:
|
||||
# Archives written on Windows store a backslash separator. It is a
|
||||
# separator, not a filename character, so it is normalized before
|
||||
# the component checks rather than rejected.
|
||||
name = member.filename.replace("\\", "/")
|
||||
if not name or "\x00" in name:
|
||||
raise ValueError(f"Unsafe ZIP member name: {member.filename!r}")
|
||||
if name.startswith("/") or re.match(r"^[A-Za-z]:", name):
|
||||
raise ValueError(f"Absolute ZIP member path: {name}")
|
||||
parts = [part for part in name.split("/") if part]
|
||||
if any(part in (".", "..") for part in parts):
|
||||
raise ValueError(f"ZIP traversal detected: {name}")
|
||||
normalized = "/".join(parts)
|
||||
if normalized in seen:
|
||||
raise ValueError(f"Duplicate ZIP member path: {name}")
|
||||
seen.add(normalized)
|
||||
|
||||
mode = (member.external_attr >> 16) & 0xFFFF
|
||||
file_type = stat.S_IFMT(mode)
|
||||
if file_type not in (0, stat.S_IFREG, stat.S_IFDIR):
|
||||
raise ValueError(f"ZIP link or special file rejected: {name}")
|
||||
if member.flag_bits & 0x1:
|
||||
raise ValueError(f"Encrypted ZIP member rejected: {name}")
|
||||
if member.file_size > max_member_size:
|
||||
raise ValueError(
|
||||
f"ZIP member {name} is {member.file_size} bytes; "
|
||||
f"limit is {max_member_size}"
|
||||
)
|
||||
declared_total += member.file_size
|
||||
if declared_total > max_total_size:
|
||||
raise ValueError(
|
||||
f"ZIP expands to {declared_total} bytes, limit is {max_total_size}"
|
||||
)
|
||||
if member.file_size and member.compress_type in _BOUNDED_RATIO_METHODS:
|
||||
if member.compress_size == 0:
|
||||
raise ValueError(f"Invalid compression size for ZIP member: {name}")
|
||||
if member.file_size / member.compress_size > max_compression_ratio:
|
||||
raise ValueError(f"Suspicious compression ratio for ZIP member: {name}")
|
||||
name, parts, mode = _member_path(member, seen)
|
||||
declared_total = _check_member_size(member, name, declared_total, limits)
|
||||
|
||||
target = os.path.realpath(os.path.join(dest, *parts))
|
||||
if not target.startswith(dest + os.sep) and target != dest:
|
||||
@@ -306,36 +377,4 @@ def safe_extract_zip(
|
||||
if member.is_dir() or name.endswith("/"):
|
||||
os.makedirs(target, exist_ok=True)
|
||||
continue
|
||||
|
||||
os.makedirs(os.path.dirname(target), exist_ok=True)
|
||||
tmp_path = ""
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="wb", dir=os.path.dirname(target), delete=False
|
||||
) as tmp_file:
|
||||
tmp_path = tmp_file.name
|
||||
actual_size = 0
|
||||
with zf.open(member, "r") as source:
|
||||
while True:
|
||||
chunk = source.read(1024 * 1024)
|
||||
if not chunk:
|
||||
break
|
||||
actual_size += len(chunk)
|
||||
if actual_size > member.file_size or actual_size > max_member_size:
|
||||
raise ValueError(
|
||||
f"ZIP member exceeded declared or configured size: {name}"
|
||||
)
|
||||
tmp_file.write(chunk)
|
||||
if actual_size != member.file_size:
|
||||
raise ValueError(
|
||||
f"ZIP member size mismatch for {name}: "
|
||||
f"{actual_size} != {member.file_size}"
|
||||
)
|
||||
# The member's own mode, else what a plain open() gives. The
|
||||
# temporary file is private and would keep 0600 otherwise.
|
||||
os.chmod(tmp_path, ((mode & 0o777) or 0o666) & ~umask)
|
||||
os.replace(tmp_path, target)
|
||||
tmp_path = ""
|
||||
finally:
|
||||
if tmp_path and os.path.exists(tmp_path):
|
||||
os.unlink(tmp_path)
|
||||
_install_member(zf, member, name, target, mode, umask, limits.member_size)
|
||||
Reference in new issue
Block a user