diff --git a/scripts/core/crypto_verify.py b/scripts/core/crypto_verify.py index a3af62621..e5cc96c07 100644 --- a/scripts/core/crypto_verify.py +++ b/scripts/core/crypto_verify.py @@ -21,6 +21,8 @@ import struct from collections.abc import Callable from pathlib import Path +from core.hashing import file_key + # Key file parsing (keys.txt / aes_keys.txt format) @@ -489,17 +491,6 @@ _CRYPTO_VERIFIERS: dict[str, Callable] = { } -def _stat_key(path: str | Path) -> tuple | None: - """(path, inode, size, mtime_ns, ctime_ns): the file as compute_hashes - keys it, None when it cannot be read.""" - try: - stat = Path(path).stat() - except OSError: - return None - return (str(Path(path).resolve()), stat.st_ino, stat.st_size, - stat.st_mtime_ns, stat.st_ctime_ns) - - # (verifier, file key, keys file key) -> reason or None. verify --all ran the # curve and RSA checks of the same five files once per platform, 35 runs for # 5 answers: the answer depends on the file's bytes and the keys file alone. @@ -524,8 +515,8 @@ def check_crypto_validation( if not keys_file: return "crypto check skipped: no keys file found" - file_key, keys_key = _stat_key(local_path), _stat_key(keys_file) - key = (filename, file_key, keys_key) if file_key and keys_key else None + held_key, keys_key = file_key(local_path), file_key(keys_file) + key = (filename, held_key, keys_key) if held_key and keys_key else None if key in _crypto_cache: return _crypto_cache[key] keys = parse_keys_file(keys_file) diff --git a/scripts/core/hashing.py b/scripts/core/hashing.py index 66c301a0c..0eac99564 100644 --- a/scripts/core/hashing.py +++ b/scripts/core/hashing.py @@ -16,6 +16,9 @@ from collections.abc import Iterable _ALL_ALGORITHMS = frozenset({"sha1", "md5", "sha256", "crc32", "adler32"}) +# No caller asks for an archive member above this bound. +MAX_HASHED_MEMBER = 512 * 1024 * 1024 + # (path, inode, size, mtime_ns, ctime_ns) -> the digests read so far. # verify --all hashed GB_dmg.bin 13 times, once per platform and entry. The # bytes are still read from disk, once per file and change. @@ -34,14 +37,7 @@ def compute_hashes( algos = frozenset(algorithms) if algorithms else _ALL_ALGORITHMS if not cache: return _compute_hashes(filepath, algos) - try: - stat = os.stat(filepath) - key: tuple | None = ( - os.path.realpath(filepath), stat.st_ino, stat.st_size, - stat.st_mtime_ns, stat.st_ctime_ns, - ) - except OSError: - key = None + key = file_key(filepath) known = _hash_cache.get(key) if key else None if known is not None and algos <= known.keys(): return {name: known[name] for name in algos} @@ -51,6 +47,19 @@ def compute_hashes( return result +def file_key(filepath: str | Path) -> tuple | None: + """(path, inode, size, mtime_ns, ctime_ns): what a digest cache keys a + file by, None when it cannot be read.""" + try: + stat = os.stat(filepath) + except OSError: + return None + return ( + os.path.realpath(filepath), stat.st_ino, stat.st_size, + stat.st_mtime_ns, stat.st_ctime_ns, + ) + + def _compute_hashes(filepath: str | Path, algos: frozenset[str]) -> dict[str, str]: """Read the file once and compute the requested digests.""" with open(filepath, "rb") as f: @@ -139,7 +148,7 @@ def md5_composite_of(source: str | Path | BinaryIO) -> str: h = hashlib.md5() for name in names: info = zf.getinfo(name) - if info.file_size > 512 * 1024 * 1024: + if info.file_size > MAX_HASHED_MEMBER: continue # skip oversized entries h.update(zf.read(name)) return h.hexdigest() diff --git a/scripts/core/ziptools.py b/scripts/core/ziptools.py index 20544cf98..2ffcd4d1c 100644 --- a/scripts/core/ziptools.py +++ b/scripts/core/ziptools.py @@ -8,7 +8,6 @@ from __future__ import annotations from dataclasses import dataclass import functools -import hashlib import os import re import stat @@ -17,7 +16,7 @@ import tempfile import zipfile from core.artifacts import process_umask, set_plain_mode -from core.hashing import md5sum +from core.hashing import MAX_HASHED_MEMBER, md5sum from core.paths import REPO_ROOT @@ -57,7 +56,7 @@ def _member_md5( for fname in archive.namelist(): if fname.casefold() != folded: continue - if archive.getinfo(fname).file_size > 512 * 1024 * 1024: + if archive.getinfo(fname).file_size > MAX_HASHED_MEMBER: return "error" if not hashed: return "present" @@ -125,10 +124,6 @@ MEMBER_CACHE = os.path.join( ) -# No caller asks for a member above this bound, the index's default. -MAX_HASHED_MEMBER = 512 * 1024 * 1024 - - def _read_member_cache(path: str) -> dict[str, list[list]]: import json @@ -170,11 +165,8 @@ def _members_md5(path: str) -> list[list] | None: if info.file_size > MAX_HASHED_MEMBER: members.append([None, info.file_size]) continue - h = hashlib.md5() with zf.open(info.filename) as inner: - for chunk in iter(lambda: inner.read(65536), b""): - h.update(chunk) - members.append([h.hexdigest(), info.file_size]) + members.append([md5sum(inner), info.file_size]) except (zipfile.BadZipFile, OSError) as exc: print(f"warning: unreadable archive {path}: {exc}", file=sys.stderr) return None @@ -183,7 +175,7 @@ def _members_md5(path: str) -> list[list] | None: def build_zip_contents_index( db: dict, - max_entry_size: int = 512 * 1024 * 1024, + max_entry_size: int = MAX_HASHED_MEMBER, cache_path: str | None = MEMBER_CACHE, ) -> dict: """Build {inner_rom_md5: zip_file_sha1} for ROMs inside ZIP files.