refactor: read zip member crcs in one place

core.ziptools.readable_zip_member_crcs answers None for a file that is
no readable archive, and zip_member_crcs reads it as empty. The covering
archive choice of resolve.packfiles uses it in place of its own reader,
so it still gives up on an unreadable candidate and now shares the cache.
No behavior change.
This commit is contained in:
Abdessamad Derraz committed 2026-11-20 20:03:26 +01:00
1 parent 187d9ea5e4
commit cc85999e22
2 files changed
+14 -17

No files matched your search

+11 -5
View File
@@ -69,14 +69,14 @@ def _member_md5(
@functools.lru_cache(maxsize=4096)
def _member_crcs(path: str, mtime: float) -> frozenset[str]:
def _member_crcs(path: str, mtime: float) -> frozenset[str] | None:
try:
with zipfile.ZipFile(path) as archive:
return frozenset(
format(info.CRC, "08x") for info in archive.infolist() if not info.is_dir()
)
except (zipfile.BadZipFile, OSError):
return frozenset()
return None
@functools.lru_cache(maxsize=4096)
@@ -100,15 +100,21 @@ def zip_member_names(path: str) -> frozenset[str]:
return _member_names(path, mtime)
def zip_member_crcs(path: str) -> frozenset[str]:
"""CRC-32 of each member, read from the central directory."""
def readable_zip_member_crcs(path: str) -> frozenset[str] | None:
"""CRC-32 of each member, read from the central directory, None when
the file is no readable archive."""
try:
mtime = os.path.getmtime(path)
except OSError:
return frozenset()
return None
return _member_crcs(path, mtime)
def zip_member_crcs(path: str) -> frozenset[str]:
"""CRC-32 of each member, empty when the file is no readable archive."""
return readable_zip_member_crcs(path) or frozenset()
_zip_contents_cache: tuple[tuple, dict] | None = None
# Member md5s per archive, keyed by the archive's sha1: content addressed, so
+3 -12
View File
@@ -11,7 +11,6 @@ import os
import re
import urllib.error
import urllib.request
import zipfile
from pathlib import Path
from model.largefiles import fetch_large_file
@@ -19,6 +18,7 @@ from core.hashing import 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
from core.ziptools import readable_zip_member_crcs
from resolve.validation import inner_rom_check
# CLI-wide network policy. Public helpers can override it explicitly, while
# every CLI mode inherits --offline (including large-file release fallback).
@@ -472,15 +472,6 @@ def _archive_choice(
return fe if accepted(path) > 0 else None
def _member_crcs(path: str) -> frozenset[int] | None:
"""The crc32 of each member of an archive, None when it is no archive."""
try:
with zipfile.ZipFile(path) as zf:
return frozenset(info.CRC for info in zf.infolist() if not info.is_dir())
except (OSError, zipfile.BadZipFile):
return None
def _covering_archive(candidates: list[tuple[dict, str]]) -> dict | None:
"""The archive holding every member each other candidate holds, and more.
@@ -489,9 +480,9 @@ def _covering_archive(candidates: list[tuple[dict, str]]) -> dict | None:
members, never the container, and its cdi driver needs all three, so
the larger set serves both readers where the first one serves one.
"""
sets: list[tuple[dict, frozenset[int]]] = []
sets: list[tuple[dict, frozenset[str]]] = []
for fe, path in candidates:
members = _member_crcs(path)
members = readable_zip_member_crcs(path)
if members is None:
return None
sets.append((fe, members))