diff --git a/install.ps1 b/install.ps1 index fda15608d..f634e6761 100644 --- a/install.ps1 +++ b/install.ps1 @@ -16,7 +16,7 @@ # and Invoke-WebRequest throws on a cut transfer before the hash is read: # the site's build rewrites this line there (scripts/sitebootstrap.py). $mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that." - $defaultInstallSha256 = "1cd472779f9a83fc33c10a464275c6defa1d7995d12091267c4c815384147909" + $defaultInstallSha256 = "d91de8c232409f6c324a86354f21a62972fbf629c088b68fdcc295233263b409" # The pin above refuses any changed byte: this cap only stops a body that # cannot be the installer (a sign-in portal's page) before it is hashed. $maximumInstallerBytes = 8MB diff --git a/install.py b/install.py index fbaec61db..b027ae2ff 100644 --- a/install.py +++ b/install.py @@ -12712,6 +12712,493 @@ def _read_from_folder(label: str) -> FolderIndex: return index +# --- installer/pack_parts.py ------------------------------------------------- + +MAX_MARKER_BYTES = 64 * 1024 + + +class PackPart: + """What one part's marker says, and the folder it was found in.""" + + def __init__(self, pack: str, index: int, count: int, + parts: tuple[tuple[str, int], ...], folder: Path) -> None: + self.pack = pack + self.index = index + self.count = count + self.parts = parts + self.folder = folder + + +def read_part_marker(path: Path) -> PackPart | None: + """The marker at path, None when it is not one split_pack wrote.""" + match = _PART_MARKER.match(path.name) + if match is None: + return None + try: + with open(path, "rb") as handle: + text = handle.read(MAX_MARKER_BYTES).decode("utf-8") + except (OSError, UnicodeDecodeError): + return None + pack = "" + index = count = 0 + parts: list[tuple[str, int]] = [] + for line in text.splitlines(): + key, _sep, value = line.partition(":") + value = value.strip() + if key == "pack": + pack = value + elif key == "part": + numbers = value.split(" of ") + if len(numbers) == 2 and all(n.isdigit() for n in numbers): + index, count = int(numbers[0]), int(numbers[1]) + elif key == "file": + size, _space, name = value.partition(" ") + if size.isdigit() and _PART_FILE.match(name.strip()): + parts.append((name.strip(), int(size))) + if ( + not pack.endswith("_BIOS_Pack.zip") + or not 1 <= index <= count <= MAX_PARTS + or len(parts) != count + or (index, count) != (int(match["index"]), int(match["count"])) + ): + return None + return PackPart(pack, index, count, tuple(parts), path.parent) + + +def find_part_markers(root: Path) -> list[PackPart]: + """Markers in root and in the folders right below it. + + A part extracted into a folder named after itself (the default of + several archive tools) leaves its marker one level down. + """ + found: list[PackPart] = [] + try: + entries = sorted(root.iterdir()) + except OSError: + return found + for entry in entries: + if entry.is_file(): + marker = read_part_marker(entry) + if marker: + found.append(marker) + elif entry.is_dir() and not entry.is_symlink() and entry.name != BACKUP_DIR: + try: + below = sorted(entry.iterdir()) + except OSError: + continue + for child in below: + marker = read_part_marker(child) if child.is_file() else None + if marker: + found.append(marker) + return found + + +def split_pack_lines(root: Path) -> list[str]: + """What --check says about the parts of a pack extracted into root: one + missing, or one extracted into a folder of its own.""" + by_pack: dict[str, list[PackPart]] = {} + for marker in find_part_markers(root): + by_pack.setdefault(marker.pack, []).append(marker) + lines: list[str] = [] + for pack, markers in sorted(by_pack.items()): + listed = max(markers, key=lambda m: m.count) + here = {m.index for m in markers if m.folder == root and m.count == listed.count} + missing = [n for n in range(1, listed.count + 1) if n not in here] + deep = sorted({m.folder for m in markers if m.folder != root}) + if not missing and not deep: + continue + lines.append(f"\n {pack} comes in {listed.count} parts, all needed.") + for folder in deep: + names = sorted(m.index for m in markers if m.folder == folder) + which = ", ".join(str(n) for n in names) + lines.append(f" {'Parts' if len(names) > 1 else 'Part'} {which} went into " + f"{folder}, a folder of its own: move what it holds up into {root}.") + absent = [n for n in missing + if not any(m.index == n and m.folder != root for m in markers)] + for number in absent: + name, size = listed.parts[number - 1] + lines.append(f" missing: part {number}, {name} ({format_size(size)})") + if absent: + lines.append(f" Download {'it' if len(absent) == 1 else 'them'} and extract " + f"into {root}, beside the other parts.") + return lines + + +PACK_MEMBER_DATE = (1980, 1, 1, 0, 0, 0) +PACK_COMPRESSLEVEL = 9 + + +# The marker each part carries at its root, as scripts/split_pack.py writes +# it: the sizes at a fixed width, so its length does not depend on them. +PART_SIZE_WIDTH = 13 +# A member may grow under deflate (zlib's deflateBound is under one byte in +# a thousand, plus a few), and its headers carry a ZIP64 field past 2 GiB. +# A part is planned on these bounds, never on the compressed size, which is +# known only once written. +_ZIP_LOCAL_BYTES = 30 + 20 +_ZIP_CENTRAL_BYTES = 46 + 28 +_ZIP_END_BYTES = 22 + 56 + 20 + + +class PackError(Exception): + """A pack that cannot be made as asked. left names the files a failure + could not take away, none when nothing was left behind.""" + + def __init__(self, words: str, left: list[Path] | None = None) -> None: + super().__init__(words) + self.left = list(left or []) + + +class PackWritten(NamedTuple): + """What write_pack did: the files of the pack, the earlier files of + its name it removed, and those it could not remove or left as they + are, each with the reason.""" + + files: list[Path] + removed: list[Path] + kept: list[tuple[Path, str]] + + +class PackMember(NamedTuple): + """One member of a pack being made: its name, the bytes it must hold + and the file they are read from.""" + + name: str + sha1: str + size: int + source: Path + + +def _member_bound(name: str, size: int) -> int: + """The most bytes a member can take in a part: both headers and its + data deflated at worst.""" + encoded = len(name.encode("utf-8")) + return _ZIP_LOCAL_BYTES + _ZIP_CENTRAL_BYTES + 2 * encoded + size + size // 1000 + 64 + + +def pack_bound(manifest_size: int, members: list[PackMember]) -> int: + """The most bytes the pack can take as one ZIP.""" + return (_ZIP_END_BYTES + _member_bound("manifest.json", manifest_size) + + sum(_member_bound(member.name, member.size) for member in members)) + + +def part_marker_text(name: str, index: int, sizes: list[int]) -> bytes: + """The marker of part index, in the words scripts/split_pack.py writes + and read_part_marker reads back.""" + count = len(sizes) + lines = [ + "RetroBIOS split pack", + f"This folder holds part {index} of {count} of {name}.", + "Every part is needed: extract them all into this same folder.", + "The installer's --check names a part missing from the folder.", + "", + f"pack: {name}", + f"part: {index} of {count}", + ] + lines += [f"file: {size:>{PART_SIZE_WIDTH}} {part_file_name(name, number, count)}" + for number, size in enumerate(sizes, 1)] + return ("\n".join(lines) + "\n").encode("utf-8") + + +def _marker_bound(name: str, count: int) -> int: + marker = len(part_marker_name(name, count, count).encode("utf-8")) + return 30 + 46 + 2 * marker + len(part_marker_text(name, count, [0] * count)) + + +def plan_pack_parts(name: str, manifest_size: int, members: list[PackMember], + limit: int | None) -> list[list[PackMember]]: + """The members of each part, in pack order, manifest.json in the first. + One part without a limit, or when the whole pack stays under it. Each + part's bound stays under the limit, room kept for its marker, the plan + made again until the count it assumed is the count it gives.""" + if limit is None or pack_bound(manifest_size, members) < limit: + return [list(members)] + count = 2 + while True: + floor = _ZIP_END_BYTES + _marker_bound(name, count) + parts: list[list[PackMember]] = [[]] + size = floor + _member_bound("manifest.json", manifest_size) + if size >= limit: + raise PackError(f"the list alone needs {format_size(size)}, more than a part " + f"of {format_size(limit)} holds") + for member in members: + weight = _member_bound(member.name, member.size) + if floor + weight >= limit: + raise PackError(f"{member.name} ({format_size(member.size)}) does not fit " + f"in a part of {format_size(limit)}") + if size + weight >= limit or len(parts[-1]) >= 0xFFFE: + parts.append([]) + size = floor + parts[-1].append(member) + size += weight + if len(parts) > MAX_PARTS: + raise PackError(f"the pack would take {len(parts)} parts, more than " + f"{MAX_PARTS}: give a larger size") + if len(parts) <= count: + return parts + count = len(parts) + + +# ZipInfo's level is private before Python 3.13 (compress_level there, the +# old name kept as its alias), so typeshed does not declare it. +_ZIPINFO_LEVEL = "_compresslevel" + + +def _pack_info(name: str, compress: int = zipfile.ZIP_DEFLATED) -> zipfile.ZipInfo: + """A member header that depends on its name alone.""" + info = zipfile.ZipInfo(name, date_time=PACK_MEMBER_DATE) + info.compress_type = compress + # A ZipInfo given to writestr or open keeps its own level, the + # archive's left unread: without it every member was deflated at 6. + setattr(info, _ZIPINFO_LEVEL, + PACK_COMPRESSLEVEL if compress == zipfile.ZIP_DEFLATED else None) + info.create_system = 3 + info.external_attr = 0o100644 << 16 + return info + + +def _write_member(archive: zipfile.ZipFile, member: PackMember) -> None: + """Copy one member into the archive, its SHA-1 and size checked on the + bytes written, before the archive is closed.""" + info = _pack_info(member.name) + info.file_size = member.size + digest = hashlib.sha1() + size = 0 + with open(member.source, "rb") as src, archive.open(info, "w") as out: + while True: + chunk = src.read(1024 * 1024) + if not chunk: + break + digest.update(chunk) + size += len(chunk) + out.write(chunk) + if size != member.size or digest.hexdigest() != member.sha1: + raise PackError(f"{member.name} does not match the list: {size} bytes of SHA-1 " + f"{digest.hexdigest()}, the list says {member.size} of " + f"{member.sha1}") + + +def _patch_marker(path: Path, marker: str, body: bytes) -> None: + """Write the real marker over its placeholder of the same length: the + data, and the CRC-32 of the local and the central header.""" + with zipfile.ZipFile(path) as archive: + first = archive.infolist()[0] + central = int(archive.start_dir) + if first.filename != marker or first.file_size != len(body) or first.header_offset: + raise PackError(f"{path.name} does not start with its marker") + crc = zlib.crc32(body).to_bytes(4, "little") + with open(path, "r+b") as handle: + local = handle.read(30) + if local[:4] != b"PK\x03\x04": + raise PackError(f"{path.name} has no local header at its start") + data = (30 + int.from_bytes(local[26:28], "little") + + int.from_bytes(local[28:30], "little")) + handle.seek(14) + handle.write(crc) + handle.seek(data) + handle.write(body) + handle.seek(central) + if handle.read(4) != b"PK\x01\x02": + raise PackError(f"{path.name} has no central directory where it says") + handle.seek(central + 16) + handle.write(crc) + + +def _part_contents(name: str, index: int, count: int, manifest: bytes, + chosen: list[PackMember]) -> list[tuple[str, int]]: + """(name, size) of each member of a part, in the order written.""" + held = ([(part_marker_name(name, index, count), + len(part_marker_text(name, index, [0] * count)))] if count > 1 else []) + held += [("manifest.json", len(manifest))] if index == 1 else [] + return held + [(member.name, member.size) for member in chosen] + + +def _discard(paths: Iterable[Path]) -> list[Path]: + """Delete each file, the ones that could not be deleted returned.""" + left = [] + for path in paths: + try: + path.unlink(missing_ok=True) + except OSError: + left.append(path) + return left + + +def _of_this_pack(path: Path, name: str) -> bool: + """Whether path is a regular file that is a pack of this name: a part + whose marker names it, or a ZIP holding a manifest.json.""" + try: + if not stat.S_ISREG(os.lstat(path).st_mode): + return False + with zipfile.ZipFile(path) as archive: + names = archive.namelist() + markers = [member for member in names if _PART_MARKER.match(member)] + if markers: + text = archive.read(markers[0])[:MAX_MARKER_BYTES].decode("utf-8", "replace") + return f"pack: {name}" in text.splitlines() + return "manifest.json" in names + except (OSError, zipfile.BadZipFile, ValueError): + return False + + +def _aside_name(path: Path) -> Path: + """A name beside path no file holds, for the earlier pack while the + new one takes its place.""" + for number in range(1000): + aside = path.with_name(f".{path.name}.{os.getpid()}.{number}.old") + if not os.path.lexists(aside): + return aside + raise PackError(f"no free name beside {path.name} to set it aside") + + +def write_pack(path: Path, manifest: bytes, members: list[PackMember], + limit: int | None) -> PackWritten: + """Write the pack at path, or in parts beside it when a limit is given + and the pack would pass it. + + Each file is written beside its final name, every member checked + against its SHA-1 and the archive read back with its CRCs. Then the + earlier files of this pack (whole, or in parts) are set aside, the new + ones renamed in, and the earlier ones deleted. A failure before the new + set is whole puts the earlier set back and deletes the new files: the + folder holds the pack before or the pack after, never a mix. A file + deleted at the end that cannot be is reported, the pack being written.""" + name = path.name + plan = plan_pack_parts(name, len(manifest), members, limit) + count = len(plan) + finals = ([path] if count == 1 else + [path.with_name(part_file_name(name, index, count)) + for index in range(1, count + 1)]) + scratches: list[Path] = [] + try: + for index, (final, chosen) in enumerate(zip(finals, plan), 1): + handle, made = tempfile.mkstemp(prefix=f".{final.name}.", suffix=".partial", + dir=str(path.parent)) + os.close(handle) + scratches.append(Path(made)) + with zipfile.ZipFile(made, "w", zipfile.ZIP_DEFLATED, + compresslevel=PACK_COMPRESSLEVEL) as archive: + if count > 1: + archive.writestr( + _pack_info(part_marker_name(name, index, count), zipfile.ZIP_STORED), + part_marker_text(name, index, [0] * count)) + if index == 1: + archive.writestr(_pack_info("manifest.json"), manifest) + for member in chosen: + if _stop.is_set(): + raise KeyboardInterrupt + _write_member(archive, member) + sizes = [scratch.stat().st_size for scratch in scratches] + if count > 1: + for index, scratch in enumerate(scratches, 1): + _patch_marker(scratch, part_marker_name(name, index, count), + part_marker_text(name, index, sizes)) + for index, (scratch, chosen) in enumerate(zip(scratches, plan), 1): + shown = finals[index - 1].name + with zipfile.ZipFile(scratch) as archive: + held = [(info.filename, info.file_size) for info in archive.infolist()] + damaged = archive.testzip() + if damaged is not None: + raise PackError(f"{shown}: {damaged} does not read back") + if count > 1 and archive.read(part_marker_name(name, index, count)) != \ + part_marker_text(name, index, sizes): + raise PackError(f"{shown}: its marker does not read back") + if held != _part_contents(name, index, count, manifest, chosen): + raise PackError(f"{shown} does not read back as written") + if limit is not None and count > 1 and sizes[index - 1] >= limit: + raise PackError(f"{shown} reached the limit of {format_size(limit)}") + except BaseException as exc: + left = _discard(scratches) + if left and isinstance(exc, (OSError, PackError)): + raise PackError(str(exc), left) from exc + raise + return _swap_in(path, scratches, finals) + + +def _swap_named(path: Path, finals: list[Path]) -> list[Path]: + """The files beside path that the new pack may replace.""" + stem = path.name[: -len(".zip")] + named: list[Path] = [] + for old in _listed_paths(path.parent): + part = _PART_FILE.match(old.name) + if old == path or old in finals or (part is not None and part["stem"] == stem): + named.append(old) + return named + + +def _swap_restore(exc: OSError | PackError, name: str, scratches: list[Path], + finals: list[Path], aside: list[tuple[Path, Path]], + placed: list[Path]) -> PackError: + """Undo a swap that failed: the new files discarded, the earlier pack put + back where it can be.""" + left = _discard([*placed, *(scratch for scratch, final in zip(scratches, finals) + if final not in placed)]) + unrestored = [] + for old, moved in reversed(aside): + try: + os.replace(moved, old) + except OSError: + unrestored.append((old, moved)) + words = f"{name} could not be put in place: {exc.strerror or exc}" \ + if isinstance(exc, OSError) else str(exc) + if unrestored: + words += ". The earlier pack is set aside as " + ", ".join( + f"{moved.name} (was {old.name})" for old, moved in unrestored) + return PackError(words, [*left, *(moved for _old, moved in unrestored)]) + + +def _swap_in(path: Path, scratches: list[Path], finals: list[Path]) -> PackWritten: + """Put the new files in place of the earlier pack of this name.""" + name = path.name + named = _swap_named(path, finals) + kept: list[tuple[Path, str]] = [] + earlier: list[Path] = [] + aside: list[tuple[Path, Path]] = [] + placed: list[Path] = [] + try: + for old in named: + if old in finals: + if not stat.S_ISREG(os.lstat(old).st_mode): + raise PackError(f"{old.name} is in the way and is not a file") + earlier.append(old) + elif _of_this_pack(old, name): + earlier.append(old) + else: + kept.append((old, "it is not a part of this pack, left as it is")) + for old in earlier: + moved = _aside_name(old) + os.replace(old, moved) + aside.append((old, moved)) + for scratch, final in zip(scratches, finals): + os.replace(scratch, final) + placed.append(final) + except (OSError, PackError) as exc: + raise _swap_restore(exc, name, scratches, finals, aside, placed) from exc + removed = [] + for old, moved in aside: + if old in finals: + if _discard([moved]): + kept.append((moved, "the earlier copy could not be deleted")) + continue + if _discard([moved]): + kept.append((moved, f"the earlier {old.name}, set aside, could not be deleted")) + else: + removed.append(old) + return PackWritten(finals, removed, kept) + + +def _listed_paths(folder: Path) -> list[Path]: + """The files of a folder, [] when it cannot be listed.""" + return [folder / name for name in sorted(_listed(folder))] + + +def _listed(folder: Path) -> list[str]: + try: + return os.listdir(folder) + except OSError: + return [] + + # --- installer/removal_plan.py ----------------------------------------------- # Removal deletes for good, or moves into a backup with --backup, only what @@ -21340,164 +21827,12 @@ def _choose_narrowing(files: list[dict], omitted: list[dict], manifest: dict | N # --- installer/pack.py ------------------------------------------------------- -MAX_MARKER_BYTES = 64 * 1024 - - -class PackPart: - """What one part's marker says, and the folder it was found in.""" - - def __init__(self, pack: str, index: int, count: int, - parts: tuple[tuple[str, int], ...], folder: Path) -> None: - self.pack = pack - self.index = index - self.count = count - self.parts = parts - self.folder = folder - - -def read_part_marker(path: Path) -> PackPart | None: - """The marker at path, None when it is not one split_pack wrote.""" - match = _PART_MARKER.match(path.name) - if match is None: - return None - try: - with open(path, "rb") as handle: - text = handle.read(MAX_MARKER_BYTES).decode("utf-8") - except (OSError, UnicodeDecodeError): - return None - pack = "" - index = count = 0 - parts: list[tuple[str, int]] = [] - for line in text.splitlines(): - key, _sep, value = line.partition(":") - value = value.strip() - if key == "pack": - pack = value - elif key == "part": - numbers = value.split(" of ") - if len(numbers) == 2 and all(n.isdigit() for n in numbers): - index, count = int(numbers[0]), int(numbers[1]) - elif key == "file": - size, _space, name = value.partition(" ") - if size.isdigit() and _PART_FILE.match(name.strip()): - parts.append((name.strip(), int(size))) - if ( - not pack.endswith("_BIOS_Pack.zip") - or not 1 <= index <= count <= MAX_PARTS - or len(parts) != count - or (index, count) != (int(match["index"]), int(match["count"])) - ): - return None - return PackPart(pack, index, count, tuple(parts), path.parent) - - -def find_part_markers(root: Path) -> list[PackPart]: - """Markers in root and in the folders right below it. - - A part extracted into a folder named after itself (the default of - several archive tools) leaves its marker one level down. - """ - found: list[PackPart] = [] - try: - entries = sorted(root.iterdir()) - except OSError: - return found - for entry in entries: - if entry.is_file(): - marker = read_part_marker(entry) - if marker: - found.append(marker) - elif entry.is_dir() and not entry.is_symlink() and entry.name != BACKUP_DIR: - try: - below = sorted(entry.iterdir()) - except OSError: - continue - for child in below: - marker = read_part_marker(child) if child.is_file() else None - if marker: - found.append(marker) - return found - - -def split_pack_lines(root: Path) -> list[str]: - """What --check says about the parts of a pack extracted into root: one - missing, or one extracted into a folder of its own.""" - by_pack: dict[str, list[PackPart]] = {} - for marker in find_part_markers(root): - by_pack.setdefault(marker.pack, []).append(marker) - lines: list[str] = [] - for pack, markers in sorted(by_pack.items()): - listed = max(markers, key=lambda m: m.count) - here = {m.index for m in markers if m.folder == root and m.count == listed.count} - missing = [n for n in range(1, listed.count + 1) if n not in here] - deep = sorted({m.folder for m in markers if m.folder != root}) - if not missing and not deep: - continue - lines.append(f"\n {pack} comes in {listed.count} parts, all needed.") - for folder in deep: - names = sorted(m.index for m in markers if m.folder == folder) - which = ", ".join(str(n) for n in names) - lines.append(f" {'Parts' if len(names) > 1 else 'Part'} {which} went into " - f"{folder}, a folder of its own: move what it holds up into {root}.") - absent = [n for n in missing - if not any(m.index == n and m.folder != root for m in markers)] - for number in absent: - name, size = listed.parts[number - 1] - lines.append(f" missing: part {number}, {name} ({format_size(size)})") - if absent: - lines.append(f" Download {'it' if len(absent) == 1 else 'them'} and extract " - f"into {root}, beside the other parts.") - return lines - - -PACK_MEMBER_DATE = (1980, 1, 1, 0, 0, 0) -PACK_COMPRESSLEVEL = 9 # FAT32 holds no file of 4 GiB or more (its size field is 32 bits). A pack # that would pass that on such a drive is written in parts of at most this # many bytes, the margin keeping each well under. FAT_FILE_LIMIT = 4 * 1024 ** 3 - 1 FAT_PART_BYTES = 4 * 1024 ** 3 - 64 * 1024 ** 2 -# The marker each part carries at its root, as scripts/split_pack.py writes -# it: the sizes at a fixed width, so its length does not depend on them. -PART_SIZE_WIDTH = 13 -# A member may grow under deflate (zlib's deflateBound is under one byte in -# a thousand, plus a few), and its headers carry a ZIP64 field past 2 GiB. -# A part is planned on these bounds, never on the compressed size, which is -# known only once written. -_ZIP_LOCAL_BYTES = 30 + 20 -_ZIP_CENTRAL_BYTES = 46 + 28 -_ZIP_END_BYTES = 22 + 56 + 20 - - -class PackError(Exception): - """A pack that cannot be made as asked. left names the files a failure - could not take away, none when nothing was left behind.""" - - def __init__(self, words: str, left: list[Path] | None = None) -> None: - super().__init__(words) - self.left = list(left or []) - - -class PackWritten(NamedTuple): - """What write_pack did: the files of the pack, the earlier files of - its name it removed, and those it could not remove or left as they - are, each with the reason.""" - - files: list[Path] - removed: list[Path] - kept: list[tuple[Path, str]] - - -class PackMember(NamedTuple): - """One member of a pack being made: its name, the bytes it must hold - and the file they are read from.""" - - name: str - sha1: str - size: int - source: Path - def pack_default_name(plat: str, narrowed: bool) -> str: """_BIOS_Pack.zip, with _Custom when the choice narrows the @@ -21597,329 +21932,6 @@ def pack_manifest(plat: str, install_list: dict, rows: list[dict], return json.dumps(data, ensure_ascii=False, separators=(",", ":")).encode("utf-8") -def _member_bound(name: str, size: int) -> int: - """The most bytes a member can take in a part: both headers and its - data deflated at worst.""" - encoded = len(name.encode("utf-8")) - return _ZIP_LOCAL_BYTES + _ZIP_CENTRAL_BYTES + 2 * encoded + size + size // 1000 + 64 - - -def pack_bound(manifest_size: int, members: list[PackMember]) -> int: - """The most bytes the pack can take as one ZIP.""" - return (_ZIP_END_BYTES + _member_bound("manifest.json", manifest_size) - + sum(_member_bound(member.name, member.size) for member in members)) - - -def part_marker_text(name: str, index: int, sizes: list[int]) -> bytes: - """The marker of part index, in the words scripts/split_pack.py writes - and read_part_marker reads back.""" - count = len(sizes) - lines = [ - "RetroBIOS split pack", - f"This folder holds part {index} of {count} of {name}.", - "Every part is needed: extract them all into this same folder.", - "The installer's --check names a part missing from the folder.", - "", - f"pack: {name}", - f"part: {index} of {count}", - ] - lines += [f"file: {size:>{PART_SIZE_WIDTH}} {part_file_name(name, number, count)}" - for number, size in enumerate(sizes, 1)] - return ("\n".join(lines) + "\n").encode("utf-8") - - -def _marker_bound(name: str, count: int) -> int: - marker = len(part_marker_name(name, count, count).encode("utf-8")) - return 30 + 46 + 2 * marker + len(part_marker_text(name, count, [0] * count)) - - -def plan_pack_parts(name: str, manifest_size: int, members: list[PackMember], - limit: int | None) -> list[list[PackMember]]: - """The members of each part, in pack order, manifest.json in the first. - One part without a limit, or when the whole pack stays under it. Each - part's bound stays under the limit, room kept for its marker, the plan - made again until the count it assumed is the count it gives.""" - if limit is None or pack_bound(manifest_size, members) < limit: - return [list(members)] - count = 2 - while True: - floor = _ZIP_END_BYTES + _marker_bound(name, count) - parts: list[list[PackMember]] = [[]] - size = floor + _member_bound("manifest.json", manifest_size) - if size >= limit: - raise PackError(f"the list alone needs {format_size(size)}, more than a part " - f"of {format_size(limit)} holds") - for member in members: - weight = _member_bound(member.name, member.size) - if floor + weight >= limit: - raise PackError(f"{member.name} ({format_size(member.size)}) does not fit " - f"in a part of {format_size(limit)}") - if size + weight >= limit or len(parts[-1]) >= 0xFFFE: - parts.append([]) - size = floor - parts[-1].append(member) - size += weight - if len(parts) > MAX_PARTS: - raise PackError(f"the pack would take {len(parts)} parts, more than " - f"{MAX_PARTS}: give a larger size") - if len(parts) <= count: - return parts - count = len(parts) - - -# ZipInfo's level is private before Python 3.13 (compress_level there, the -# old name kept as its alias), so typeshed does not declare it. -_ZIPINFO_LEVEL = "_compresslevel" - - -def _pack_info(name: str, compress: int = zipfile.ZIP_DEFLATED) -> zipfile.ZipInfo: - """A member header that depends on its name alone.""" - info = zipfile.ZipInfo(name, date_time=PACK_MEMBER_DATE) - info.compress_type = compress - # A ZipInfo given to writestr or open keeps its own level, the - # archive's left unread: without it every member was deflated at 6. - setattr(info, _ZIPINFO_LEVEL, - PACK_COMPRESSLEVEL if compress == zipfile.ZIP_DEFLATED else None) - info.create_system = 3 - info.external_attr = 0o100644 << 16 - return info - - -def _write_member(archive: zipfile.ZipFile, member: PackMember) -> None: - """Copy one member into the archive, its SHA-1 and size checked on the - bytes written, before the archive is closed.""" - info = _pack_info(member.name) - info.file_size = member.size - digest = hashlib.sha1() - size = 0 - with open(member.source, "rb") as src, archive.open(info, "w") as out: - while True: - chunk = src.read(1024 * 1024) - if not chunk: - break - digest.update(chunk) - size += len(chunk) - out.write(chunk) - if size != member.size or digest.hexdigest() != member.sha1: - raise PackError(f"{member.name} does not match the list: {size} bytes of SHA-1 " - f"{digest.hexdigest()}, the list says {member.size} of " - f"{member.sha1}") - - -def _patch_marker(path: Path, marker: str, body: bytes) -> None: - """Write the real marker over its placeholder of the same length: the - data, and the CRC-32 of the local and the central header.""" - with zipfile.ZipFile(path) as archive: - first = archive.infolist()[0] - central = int(archive.start_dir) - if first.filename != marker or first.file_size != len(body) or first.header_offset: - raise PackError(f"{path.name} does not start with its marker") - crc = zlib.crc32(body).to_bytes(4, "little") - with open(path, "r+b") as handle: - local = handle.read(30) - if local[:4] != b"PK\x03\x04": - raise PackError(f"{path.name} has no local header at its start") - data = (30 + int.from_bytes(local[26:28], "little") - + int.from_bytes(local[28:30], "little")) - handle.seek(14) - handle.write(crc) - handle.seek(data) - handle.write(body) - handle.seek(central) - if handle.read(4) != b"PK\x01\x02": - raise PackError(f"{path.name} has no central directory where it says") - handle.seek(central + 16) - handle.write(crc) - - -def _part_contents(name: str, index: int, count: int, manifest: bytes, - chosen: list[PackMember]) -> list[tuple[str, int]]: - """(name, size) of each member of a part, in the order written.""" - held = ([(part_marker_name(name, index, count), - len(part_marker_text(name, index, [0] * count)))] if count > 1 else []) - held += [("manifest.json", len(manifest))] if index == 1 else [] - return held + [(member.name, member.size) for member in chosen] - - -def _discard(paths: Iterable[Path]) -> list[Path]: - """Delete each file, the ones that could not be deleted returned.""" - left = [] - for path in paths: - try: - path.unlink(missing_ok=True) - except OSError: - left.append(path) - return left - - -def _of_this_pack(path: Path, name: str) -> bool: - """Whether path is a regular file that is a pack of this name: a part - whose marker names it, or a ZIP holding a manifest.json.""" - try: - if not stat.S_ISREG(os.lstat(path).st_mode): - return False - with zipfile.ZipFile(path) as archive: - names = archive.namelist() - markers = [member for member in names if _PART_MARKER.match(member)] - if markers: - text = archive.read(markers[0])[:MAX_MARKER_BYTES].decode("utf-8", "replace") - return f"pack: {name}" in text.splitlines() - return "manifest.json" in names - except (OSError, zipfile.BadZipFile, ValueError): - return False - - -def _aside_name(path: Path) -> Path: - """A name beside path no file holds, for the earlier pack while the - new one takes its place.""" - for number in range(1000): - aside = path.with_name(f".{path.name}.{os.getpid()}.{number}.old") - if not os.path.lexists(aside): - return aside - raise PackError(f"no free name beside {path.name} to set it aside") - - -def write_pack(path: Path, manifest: bytes, members: list[PackMember], - limit: int | None) -> PackWritten: - """Write the pack at path, or in parts beside it when a limit is given - and the pack would pass it. - - Each file is written beside its final name, every member checked - against its SHA-1 and the archive read back with its CRCs. Then the - earlier files of this pack (whole, or in parts) are set aside, the new - ones renamed in, and the earlier ones deleted. A failure before the new - set is whole puts the earlier set back and deletes the new files: the - folder holds the pack before or the pack after, never a mix. A file - deleted at the end that cannot be is reported, the pack being written.""" - name = path.name - plan = plan_pack_parts(name, len(manifest), members, limit) - count = len(plan) - finals = ([path] if count == 1 else - [path.with_name(part_file_name(name, index, count)) - for index in range(1, count + 1)]) - scratches: list[Path] = [] - try: - for index, (final, chosen) in enumerate(zip(finals, plan), 1): - handle, made = tempfile.mkstemp(prefix=f".{final.name}.", suffix=".partial", - dir=str(path.parent)) - os.close(handle) - scratches.append(Path(made)) - with zipfile.ZipFile(made, "w", zipfile.ZIP_DEFLATED, - compresslevel=PACK_COMPRESSLEVEL) as archive: - if count > 1: - archive.writestr( - _pack_info(part_marker_name(name, index, count), zipfile.ZIP_STORED), - part_marker_text(name, index, [0] * count)) - if index == 1: - archive.writestr(_pack_info("manifest.json"), manifest) - for member in chosen: - if _stop.is_set(): - raise KeyboardInterrupt - _write_member(archive, member) - sizes = [scratch.stat().st_size for scratch in scratches] - if count > 1: - for index, scratch in enumerate(scratches, 1): - _patch_marker(scratch, part_marker_name(name, index, count), - part_marker_text(name, index, sizes)) - for index, (scratch, chosen) in enumerate(zip(scratches, plan), 1): - shown = finals[index - 1].name - with zipfile.ZipFile(scratch) as archive: - held = [(info.filename, info.file_size) for info in archive.infolist()] - damaged = archive.testzip() - if damaged is not None: - raise PackError(f"{shown}: {damaged} does not read back") - if count > 1 and archive.read(part_marker_name(name, index, count)) != \ - part_marker_text(name, index, sizes): - raise PackError(f"{shown}: its marker does not read back") - if held != _part_contents(name, index, count, manifest, chosen): - raise PackError(f"{shown} does not read back as written") - if limit is not None and count > 1 and sizes[index - 1] >= limit: - raise PackError(f"{shown} reached the limit of {format_size(limit)}") - except BaseException as exc: - left = _discard(scratches) - if left and isinstance(exc, (OSError, PackError)): - raise PackError(str(exc), left) from exc - raise - return _swap_in(path, scratches, finals) - - -def _swap_named(path: Path, finals: list[Path]) -> list[Path]: - """The files beside path that the new pack may replace.""" - stem = path.name[: -len(".zip")] - named: list[Path] = [] - for old in _listed_paths(path.parent): - part = _PART_FILE.match(old.name) - if old == path or old in finals or (part is not None and part["stem"] == stem): - named.append(old) - return named - - -def _swap_restore(exc: OSError | PackError, name: str, scratches: list[Path], - finals: list[Path], aside: list[tuple[Path, Path]], - placed: list[Path]) -> PackError: - """Undo a swap that failed: the new files discarded, the earlier pack put - back where it can be.""" - left = _discard([*placed, *(scratch for scratch, final in zip(scratches, finals) - if final not in placed)]) - unrestored = [] - for old, moved in reversed(aside): - try: - os.replace(moved, old) - except OSError: - unrestored.append((old, moved)) - words = f"{name} could not be put in place: {exc.strerror or exc}" \ - if isinstance(exc, OSError) else str(exc) - if unrestored: - words += ". The earlier pack is set aside as " + ", ".join( - f"{moved.name} (was {old.name})" for old, moved in unrestored) - return PackError(words, [*left, *(moved for _old, moved in unrestored)]) - - -def _swap_in(path: Path, scratches: list[Path], finals: list[Path]) -> PackWritten: - """Put the new files in place of the earlier pack of this name.""" - name = path.name - named = _swap_named(path, finals) - kept: list[tuple[Path, str]] = [] - earlier: list[Path] = [] - aside: list[tuple[Path, Path]] = [] - placed: list[Path] = [] - try: - for old in named: - if old in finals: - if not stat.S_ISREG(os.lstat(old).st_mode): - raise PackError(f"{old.name} is in the way and is not a file") - earlier.append(old) - elif _of_this_pack(old, name): - earlier.append(old) - else: - kept.append((old, "it is not a part of this pack, left as it is")) - for old in earlier: - moved = _aside_name(old) - os.replace(old, moved) - aside.append((old, moved)) - for scratch, final in zip(scratches, finals): - os.replace(scratch, final) - placed.append(final) - except (OSError, PackError) as exc: - raise _swap_restore(exc, name, scratches, finals, aside, placed) from exc - removed = [] - for old, moved in aside: - if old in finals: - if _discard([moved]): - kept.append((moved, "the earlier copy could not be deleted")) - continue - if _discard([moved]): - kept.append((moved, f"the earlier {old.name}, set aside, could not be deleted")) - else: - removed.append(old) - return PackWritten(finals, removed, kept) - - -def _listed_paths(folder: Path) -> list[Path]: - """The files of a folder, [] when it cannot be listed.""" - return [folder / name for name in sorted(_listed(folder))] - - def _pack_targets(plat: str) -> dict | None: """The cores per board the pack carries, as a pack built by the repository does: the list at hand offline, else the one fetched. None @@ -22159,13 +22171,6 @@ def _fill_pack(args: argparse.Namespace, pack: _PackFill, staging: Path, return 0 -def _listed(folder: Path) -> list[str]: - try: - return os.listdir(folder) - except OSError: - return [] - - # --- installer/plan.py ------------------------------------------------------- # Below this a download is small enough that narrowing it is not worth a line. diff --git a/install.sh b/install.sh index d2025533b..cee8a19ea 100755 --- a/install.sh +++ b/install.sh @@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins # curl or wget stop on a cut transfer before the hash is read: the site's # build rewrites this line there (scripts/sitebootstrap.py). MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that." -DEFAULT_INSTALL_SHA256="1cd472779f9a83fc33c10a464275c6defa1d7995d12091267c4c815384147909" +DEFAULT_INSTALL_SHA256="d91de8c232409f6c324a86354f21a62972fbf629c088b68fdcc295233263b409" # The pin above refuses any changed byte: this cap only stops a body that # cannot be the installer (a sign-in portal's page) before it is hashed. MAX_INSTALLER_BYTES=8388608 diff --git a/install/launchers/index.json b/install/launchers/index.json index 75a611ff6..c4a4aab83 100644 --- a/install/launchers/index.json +++ b/install/launchers/index.json @@ -27,8 +27,8 @@ { "dest": "roms/ports/retrobios/install.py", "repo_path": "install.py", - "size": 1503183, - "sha256": "1cd472779f9a83fc33c10a464275c6defa1d7995d12091267c4c815384147909", + "size": 1503267, + "sha256": "d91de8c232409f6c324a86354f21a62972fbf629c088b68fdcc295233263b409", "executable": false }, { @@ -71,8 +71,8 @@ "muos": { "archive": { "dest": "ARCHIVE/RetroBIOS.muxapp", - "size": 1556808, - "sha256": "9a781fdd3c8023580e7e37a789e64fe81d10149543a65419bd8213b6c4faf094" + "size": 1556892, + "sha256": "2ecdcf3267356eb35390ef379a242ea33365ba18041df8161716ba8a24357190" }, "members": [ { @@ -92,8 +92,8 @@ { "name": "RetroBIOS/install.py", "repo_path": "install.py", - "size": 1503183, - "sha256": "1cd472779f9a83fc33c10a464275c6defa1d7995d12091267c4c815384147909", + "size": 1503267, + "sha256": "d91de8c232409f6c324a86354f21a62972fbf629c088b68fdcc295233263b409", "executable": false }, { @@ -166,8 +166,8 @@ { "dest": ".config/modules/retrobios/install.py", "repo_path": "install.py", - "size": 1503183, - "sha256": "1cd472779f9a83fc33c10a464275c6defa1d7995d12091267c4c815384147909", + "size": 1503267, + "sha256": "d91de8c232409f6c324a86354f21a62972fbf629c088b68fdcc295233263b409", "executable": false }, { diff --git a/installer/pack.py b/installer/pack.py index c4b3a9798..8b920c9b1 100644 --- a/installer/pack.py +++ b/installer/pack.py @@ -1,17 +1,12 @@ -"""--pack: a pack written for a card a computer fills.""" +"""--pack: a pack written for a card a computer fills, from the choice to the parts on disk.""" from __future__ import annotations import argparse -import hashlib import json import os import shutil -import stat import sys import tempfile -import zipfile -import zlib -from collections.abc import Iterable from pathlib import Path, PurePosixPath from typing import TYPE_CHECKING, NamedTuple @@ -27,181 +22,26 @@ from installer.host_fs import DRIVE_FULL, _drive_full, _is_dir, _stop from installer.lists import ListFailure, held_targets, load_targets, targets_file_words from installer.offline import LOCAL from installer.pack_source import ( - _PART_FILE, - _PART_MARKER, - MAX_PARTS, pack_name_platforms, part_file_name, - part_marker_name, ) from installer.paths import shown_path from installer.plan_words import space_remedy from installer.platforms import PLATFORM_NAMES, TARGET_PLATFORMS, display_name -from installer.safe_replace import BACKUP_DIR from installer.space import space_shortfall from installer.text import _plural, format_size, screen_text if TYPE_CHECKING: from installer.terminal import Terminal - -MAX_MARKER_BYTES = 64 * 1024 +from installer.pack_parts import PackError, PackMember, pack_bound, plan_pack_parts, write_pack -class PackPart: - """What one part's marker says, and the folder it was found in.""" - - def __init__(self, pack: str, index: int, count: int, - parts: tuple[tuple[str, int], ...], folder: Path) -> None: - self.pack = pack - self.index = index - self.count = count - self.parts = parts - self.folder = folder - - -def read_part_marker(path: Path) -> PackPart | None: - """The marker at path, None when it is not one split_pack wrote.""" - match = _PART_MARKER.match(path.name) - if match is None: - return None - try: - with open(path, "rb") as handle: - text = handle.read(MAX_MARKER_BYTES).decode("utf-8") - except (OSError, UnicodeDecodeError): - return None - pack = "" - index = count = 0 - parts: list[tuple[str, int]] = [] - for line in text.splitlines(): - key, _sep, value = line.partition(":") - value = value.strip() - if key == "pack": - pack = value - elif key == "part": - numbers = value.split(" of ") - if len(numbers) == 2 and all(n.isdigit() for n in numbers): - index, count = int(numbers[0]), int(numbers[1]) - elif key == "file": - size, _space, name = value.partition(" ") - if size.isdigit() and _PART_FILE.match(name.strip()): - parts.append((name.strip(), int(size))) - if ( - not pack.endswith("_BIOS_Pack.zip") - or not 1 <= index <= count <= MAX_PARTS - or len(parts) != count - or (index, count) != (int(match["index"]), int(match["count"])) - ): - return None - return PackPart(pack, index, count, tuple(parts), path.parent) - - -def find_part_markers(root: Path) -> list[PackPart]: - """Markers in root and in the folders right below it. - - A part extracted into a folder named after itself (the default of - several archive tools) leaves its marker one level down. - """ - found: list[PackPart] = [] - try: - entries = sorted(root.iterdir()) - except OSError: - return found - for entry in entries: - if entry.is_file(): - marker = read_part_marker(entry) - if marker: - found.append(marker) - elif entry.is_dir() and not entry.is_symlink() and entry.name != BACKUP_DIR: - try: - below = sorted(entry.iterdir()) - except OSError: - continue - for child in below: - marker = read_part_marker(child) if child.is_file() else None - if marker: - found.append(marker) - return found - - -def split_pack_lines(root: Path) -> list[str]: - """What --check says about the parts of a pack extracted into root: one - missing, or one extracted into a folder of its own.""" - by_pack: dict[str, list[PackPart]] = {} - for marker in find_part_markers(root): - by_pack.setdefault(marker.pack, []).append(marker) - lines: list[str] = [] - for pack, markers in sorted(by_pack.items()): - listed = max(markers, key=lambda m: m.count) - here = {m.index for m in markers if m.folder == root and m.count == listed.count} - missing = [n for n in range(1, listed.count + 1) if n not in here] - deep = sorted({m.folder for m in markers if m.folder != root}) - if not missing and not deep: - continue - lines.append(f"\n {pack} comes in {listed.count} parts, all needed.") - for folder in deep: - names = sorted(m.index for m in markers if m.folder == folder) - which = ", ".join(str(n) for n in names) - lines.append(f" {'Parts' if len(names) > 1 else 'Part'} {which} went into " - f"{folder}, a folder of its own: move what it holds up into {root}.") - absent = [n for n in missing - if not any(m.index == n and m.folder != root for m in markers)] - for number in absent: - name, size = listed.parts[number - 1] - lines.append(f" missing: part {number}, {name} ({format_size(size)})") - if absent: - lines.append(f" Download {'it' if len(absent) == 1 else 'them'} and extract " - f"into {root}, beside the other parts.") - return lines - - -PACK_MEMBER_DATE = (1980, 1, 1, 0, 0, 0) -PACK_COMPRESSLEVEL = 9 # FAT32 holds no file of 4 GiB or more (its size field is 32 bits). A pack # that would pass that on such a drive is written in parts of at most this # many bytes, the margin keeping each well under. FAT_FILE_LIMIT = 4 * 1024 ** 3 - 1 FAT_PART_BYTES = 4 * 1024 ** 3 - 64 * 1024 ** 2 -# The marker each part carries at its root, as scripts/split_pack.py writes -# it: the sizes at a fixed width, so its length does not depend on them. -PART_SIZE_WIDTH = 13 -# A member may grow under deflate (zlib's deflateBound is under one byte in -# a thousand, plus a few), and its headers carry a ZIP64 field past 2 GiB. -# A part is planned on these bounds, never on the compressed size, which is -# known only once written. -_ZIP_LOCAL_BYTES = 30 + 20 -_ZIP_CENTRAL_BYTES = 46 + 28 -_ZIP_END_BYTES = 22 + 56 + 20 - - -class PackError(Exception): - """A pack that cannot be made as asked. left names the files a failure - could not take away, none when nothing was left behind.""" - - def __init__(self, words: str, left: list[Path] | None = None) -> None: - super().__init__(words) - self.left = list(left or []) - - -class PackWritten(NamedTuple): - """What write_pack did: the files of the pack, the earlier files of - its name it removed, and those it could not remove or left as they - are, each with the reason.""" - - files: list[Path] - removed: list[Path] - kept: list[tuple[Path, str]] - - -class PackMember(NamedTuple): - """One member of a pack being made: its name, the bytes it must hold - and the file they are read from.""" - - name: str - sha1: str - size: int - source: Path - def pack_default_name(plat: str, narrowed: bool) -> str: """_BIOS_Pack.zip, with _Custom when the choice narrows the @@ -301,329 +141,6 @@ def pack_manifest(plat: str, install_list: dict, rows: list[dict], return json.dumps(data, ensure_ascii=False, separators=(",", ":")).encode("utf-8") -def _member_bound(name: str, size: int) -> int: - """The most bytes a member can take in a part: both headers and its - data deflated at worst.""" - encoded = len(name.encode("utf-8")) - return _ZIP_LOCAL_BYTES + _ZIP_CENTRAL_BYTES + 2 * encoded + size + size // 1000 + 64 - - -def pack_bound(manifest_size: int, members: list[PackMember]) -> int: - """The most bytes the pack can take as one ZIP.""" - return (_ZIP_END_BYTES + _member_bound("manifest.json", manifest_size) - + sum(_member_bound(member.name, member.size) for member in members)) - - -def part_marker_text(name: str, index: int, sizes: list[int]) -> bytes: - """The marker of part index, in the words scripts/split_pack.py writes - and read_part_marker reads back.""" - count = len(sizes) - lines = [ - "RetroBIOS split pack", - f"This folder holds part {index} of {count} of {name}.", - "Every part is needed: extract them all into this same folder.", - "The installer's --check names a part missing from the folder.", - "", - f"pack: {name}", - f"part: {index} of {count}", - ] - lines += [f"file: {size:>{PART_SIZE_WIDTH}} {part_file_name(name, number, count)}" - for number, size in enumerate(sizes, 1)] - return ("\n".join(lines) + "\n").encode("utf-8") - - -def _marker_bound(name: str, count: int) -> int: - marker = len(part_marker_name(name, count, count).encode("utf-8")) - return 30 + 46 + 2 * marker + len(part_marker_text(name, count, [0] * count)) - - -def plan_pack_parts(name: str, manifest_size: int, members: list[PackMember], - limit: int | None) -> list[list[PackMember]]: - """The members of each part, in pack order, manifest.json in the first. - One part without a limit, or when the whole pack stays under it. Each - part's bound stays under the limit, room kept for its marker, the plan - made again until the count it assumed is the count it gives.""" - if limit is None or pack_bound(manifest_size, members) < limit: - return [list(members)] - count = 2 - while True: - floor = _ZIP_END_BYTES + _marker_bound(name, count) - parts: list[list[PackMember]] = [[]] - size = floor + _member_bound("manifest.json", manifest_size) - if size >= limit: - raise PackError(f"the list alone needs {format_size(size)}, more than a part " - f"of {format_size(limit)} holds") - for member in members: - weight = _member_bound(member.name, member.size) - if floor + weight >= limit: - raise PackError(f"{member.name} ({format_size(member.size)}) does not fit " - f"in a part of {format_size(limit)}") - if size + weight >= limit or len(parts[-1]) >= 0xFFFE: - parts.append([]) - size = floor - parts[-1].append(member) - size += weight - if len(parts) > MAX_PARTS: - raise PackError(f"the pack would take {len(parts)} parts, more than " - f"{MAX_PARTS}: give a larger size") - if len(parts) <= count: - return parts - count = len(parts) - - -# ZipInfo's level is private before Python 3.13 (compress_level there, the -# old name kept as its alias), so typeshed does not declare it. -_ZIPINFO_LEVEL = "_compresslevel" - - -def _pack_info(name: str, compress: int = zipfile.ZIP_DEFLATED) -> zipfile.ZipInfo: - """A member header that depends on its name alone.""" - info = zipfile.ZipInfo(name, date_time=PACK_MEMBER_DATE) - info.compress_type = compress - # A ZipInfo given to writestr or open keeps its own level, the - # archive's left unread: without it every member was deflated at 6. - setattr(info, _ZIPINFO_LEVEL, - PACK_COMPRESSLEVEL if compress == zipfile.ZIP_DEFLATED else None) - info.create_system = 3 - info.external_attr = 0o100644 << 16 - return info - - -def _write_member(archive: zipfile.ZipFile, member: PackMember) -> None: - """Copy one member into the archive, its SHA-1 and size checked on the - bytes written, before the archive is closed.""" - info = _pack_info(member.name) - info.file_size = member.size - digest = hashlib.sha1() - size = 0 - with open(member.source, "rb") as src, archive.open(info, "w") as out: - while True: - chunk = src.read(1024 * 1024) - if not chunk: - break - digest.update(chunk) - size += len(chunk) - out.write(chunk) - if size != member.size or digest.hexdigest() != member.sha1: - raise PackError(f"{member.name} does not match the list: {size} bytes of SHA-1 " - f"{digest.hexdigest()}, the list says {member.size} of " - f"{member.sha1}") - - -def _patch_marker(path: Path, marker: str, body: bytes) -> None: - """Write the real marker over its placeholder of the same length: the - data, and the CRC-32 of the local and the central header.""" - with zipfile.ZipFile(path) as archive: - first = archive.infolist()[0] - central = int(archive.start_dir) - if first.filename != marker or first.file_size != len(body) or first.header_offset: - raise PackError(f"{path.name} does not start with its marker") - crc = zlib.crc32(body).to_bytes(4, "little") - with open(path, "r+b") as handle: - local = handle.read(30) - if local[:4] != b"PK\x03\x04": - raise PackError(f"{path.name} has no local header at its start") - data = (30 + int.from_bytes(local[26:28], "little") - + int.from_bytes(local[28:30], "little")) - handle.seek(14) - handle.write(crc) - handle.seek(data) - handle.write(body) - handle.seek(central) - if handle.read(4) != b"PK\x01\x02": - raise PackError(f"{path.name} has no central directory where it says") - handle.seek(central + 16) - handle.write(crc) - - -def _part_contents(name: str, index: int, count: int, manifest: bytes, - chosen: list[PackMember]) -> list[tuple[str, int]]: - """(name, size) of each member of a part, in the order written.""" - held = ([(part_marker_name(name, index, count), - len(part_marker_text(name, index, [0] * count)))] if count > 1 else []) - held += [("manifest.json", len(manifest))] if index == 1 else [] - return held + [(member.name, member.size) for member in chosen] - - -def _discard(paths: Iterable[Path]) -> list[Path]: - """Delete each file, the ones that could not be deleted returned.""" - left = [] - for path in paths: - try: - path.unlink(missing_ok=True) - except OSError: - left.append(path) - return left - - -def _of_this_pack(path: Path, name: str) -> bool: - """Whether path is a regular file that is a pack of this name: a part - whose marker names it, or a ZIP holding a manifest.json.""" - try: - if not stat.S_ISREG(os.lstat(path).st_mode): - return False - with zipfile.ZipFile(path) as archive: - names = archive.namelist() - markers = [member for member in names if _PART_MARKER.match(member)] - if markers: - text = archive.read(markers[0])[:MAX_MARKER_BYTES].decode("utf-8", "replace") - return f"pack: {name}" in text.splitlines() - return "manifest.json" in names - except (OSError, zipfile.BadZipFile, ValueError): - return False - - -def _aside_name(path: Path) -> Path: - """A name beside path no file holds, for the earlier pack while the - new one takes its place.""" - for number in range(1000): - aside = path.with_name(f".{path.name}.{os.getpid()}.{number}.old") - if not os.path.lexists(aside): - return aside - raise PackError(f"no free name beside {path.name} to set it aside") - - -def write_pack(path: Path, manifest: bytes, members: list[PackMember], - limit: int | None) -> PackWritten: - """Write the pack at path, or in parts beside it when a limit is given - and the pack would pass it. - - Each file is written beside its final name, every member checked - against its SHA-1 and the archive read back with its CRCs. Then the - earlier files of this pack (whole, or in parts) are set aside, the new - ones renamed in, and the earlier ones deleted. A failure before the new - set is whole puts the earlier set back and deletes the new files: the - folder holds the pack before or the pack after, never a mix. A file - deleted at the end that cannot be is reported, the pack being written.""" - name = path.name - plan = plan_pack_parts(name, len(manifest), members, limit) - count = len(plan) - finals = ([path] if count == 1 else - [path.with_name(part_file_name(name, index, count)) - for index in range(1, count + 1)]) - scratches: list[Path] = [] - try: - for index, (final, chosen) in enumerate(zip(finals, plan), 1): - handle, made = tempfile.mkstemp(prefix=f".{final.name}.", suffix=".partial", - dir=str(path.parent)) - os.close(handle) - scratches.append(Path(made)) - with zipfile.ZipFile(made, "w", zipfile.ZIP_DEFLATED, - compresslevel=PACK_COMPRESSLEVEL) as archive: - if count > 1: - archive.writestr( - _pack_info(part_marker_name(name, index, count), zipfile.ZIP_STORED), - part_marker_text(name, index, [0] * count)) - if index == 1: - archive.writestr(_pack_info("manifest.json"), manifest) - for member in chosen: - if _stop.is_set(): - raise KeyboardInterrupt - _write_member(archive, member) - sizes = [scratch.stat().st_size for scratch in scratches] - if count > 1: - for index, scratch in enumerate(scratches, 1): - _patch_marker(scratch, part_marker_name(name, index, count), - part_marker_text(name, index, sizes)) - for index, (scratch, chosen) in enumerate(zip(scratches, plan), 1): - shown = finals[index - 1].name - with zipfile.ZipFile(scratch) as archive: - held = [(info.filename, info.file_size) for info in archive.infolist()] - damaged = archive.testzip() - if damaged is not None: - raise PackError(f"{shown}: {damaged} does not read back") - if count > 1 and archive.read(part_marker_name(name, index, count)) != \ - part_marker_text(name, index, sizes): - raise PackError(f"{shown}: its marker does not read back") - if held != _part_contents(name, index, count, manifest, chosen): - raise PackError(f"{shown} does not read back as written") - if limit is not None and count > 1 and sizes[index - 1] >= limit: - raise PackError(f"{shown} reached the limit of {format_size(limit)}") - except BaseException as exc: - left = _discard(scratches) - if left and isinstance(exc, (OSError, PackError)): - raise PackError(str(exc), left) from exc - raise - return _swap_in(path, scratches, finals) - - -def _swap_named(path: Path, finals: list[Path]) -> list[Path]: - """The files beside path that the new pack may replace.""" - stem = path.name[: -len(".zip")] - named: list[Path] = [] - for old in _listed_paths(path.parent): - part = _PART_FILE.match(old.name) - if old == path or old in finals or (part is not None and part["stem"] == stem): - named.append(old) - return named - - -def _swap_restore(exc: OSError | PackError, name: str, scratches: list[Path], - finals: list[Path], aside: list[tuple[Path, Path]], - placed: list[Path]) -> PackError: - """Undo a swap that failed: the new files discarded, the earlier pack put - back where it can be.""" - left = _discard([*placed, *(scratch for scratch, final in zip(scratches, finals) - if final not in placed)]) - unrestored = [] - for old, moved in reversed(aside): - try: - os.replace(moved, old) - except OSError: - unrestored.append((old, moved)) - words = f"{name} could not be put in place: {exc.strerror or exc}" \ - if isinstance(exc, OSError) else str(exc) - if unrestored: - words += ". The earlier pack is set aside as " + ", ".join( - f"{moved.name} (was {old.name})" for old, moved in unrestored) - return PackError(words, [*left, *(moved for _old, moved in unrestored)]) - - -def _swap_in(path: Path, scratches: list[Path], finals: list[Path]) -> PackWritten: - """Put the new files in place of the earlier pack of this name.""" - name = path.name - named = _swap_named(path, finals) - kept: list[tuple[Path, str]] = [] - earlier: list[Path] = [] - aside: list[tuple[Path, Path]] = [] - placed: list[Path] = [] - try: - for old in named: - if old in finals: - if not stat.S_ISREG(os.lstat(old).st_mode): - raise PackError(f"{old.name} is in the way and is not a file") - earlier.append(old) - elif _of_this_pack(old, name): - earlier.append(old) - else: - kept.append((old, "it is not a part of this pack, left as it is")) - for old in earlier: - moved = _aside_name(old) - os.replace(old, moved) - aside.append((old, moved)) - for scratch, final in zip(scratches, finals): - os.replace(scratch, final) - placed.append(final) - except (OSError, PackError) as exc: - raise _swap_restore(exc, name, scratches, finals, aside, placed) from exc - removed = [] - for old, moved in aside: - if old in finals: - if _discard([moved]): - kept.append((moved, "the earlier copy could not be deleted")) - continue - if _discard([moved]): - kept.append((moved, f"the earlier {old.name}, set aside, could not be deleted")) - else: - removed.append(old) - return PackWritten(finals, removed, kept) - - -def _listed_paths(folder: Path) -> list[Path]: - """The files of a folder, [] when it cannot be listed.""" - return [folder / name for name in sorted(_listed(folder))] - - def _pack_targets(plat: str) -> dict | None: """The cores per board the pack carries, as a pack built by the repository does: the list at hand offline, else the one fetched. None @@ -861,10 +378,3 @@ def _fill_pack(args: argparse.Namespace, pack: _PackFill, staging: Path, _pack_event(plat, path, members, written, "written", f"{path.name}: {_plural(len(members), 'file')}, every one checked.", size) return 0 - - -def _listed(folder: Path) -> list[str]: - try: - return os.listdir(folder) - except OSError: - return [] diff --git a/installer/pack_parts.py b/installer/pack_parts.py new file mode 100644 index 000000000..abfef1ba6 --- /dev/null +++ b/installer/pack_parts.py @@ -0,0 +1,509 @@ +"""The ZIP parts of a pack: planned, written, marked, swapped into place, and their markers read back.""" + +from __future__ import annotations + +import hashlib +import os +import stat +import tempfile +import zipfile +import zlib +from collections.abc import Iterable +from pathlib import Path +from typing import NamedTuple + +from installer.host_fs import _stop +from installer.pack_source import ( + MAX_PARTS, + _PART_FILE, + _PART_MARKER, + part_file_name, + part_marker_name, +) +from installer.safe_replace import BACKUP_DIR +from installer.text import format_size + + +MAX_MARKER_BYTES = 64 * 1024 + + +class PackPart: + """What one part's marker says, and the folder it was found in.""" + + def __init__(self, pack: str, index: int, count: int, + parts: tuple[tuple[str, int], ...], folder: Path) -> None: + self.pack = pack + self.index = index + self.count = count + self.parts = parts + self.folder = folder + + +def read_part_marker(path: Path) -> PackPart | None: + """The marker at path, None when it is not one split_pack wrote.""" + match = _PART_MARKER.match(path.name) + if match is None: + return None + try: + with open(path, "rb") as handle: + text = handle.read(MAX_MARKER_BYTES).decode("utf-8") + except (OSError, UnicodeDecodeError): + return None + pack = "" + index = count = 0 + parts: list[tuple[str, int]] = [] + for line in text.splitlines(): + key, _sep, value = line.partition(":") + value = value.strip() + if key == "pack": + pack = value + elif key == "part": + numbers = value.split(" of ") + if len(numbers) == 2 and all(n.isdigit() for n in numbers): + index, count = int(numbers[0]), int(numbers[1]) + elif key == "file": + size, _space, name = value.partition(" ") + if size.isdigit() and _PART_FILE.match(name.strip()): + parts.append((name.strip(), int(size))) + if ( + not pack.endswith("_BIOS_Pack.zip") + or not 1 <= index <= count <= MAX_PARTS + or len(parts) != count + or (index, count) != (int(match["index"]), int(match["count"])) + ): + return None + return PackPart(pack, index, count, tuple(parts), path.parent) + + +def find_part_markers(root: Path) -> list[PackPart]: + """Markers in root and in the folders right below it. + + A part extracted into a folder named after itself (the default of + several archive tools) leaves its marker one level down. + """ + found: list[PackPart] = [] + try: + entries = sorted(root.iterdir()) + except OSError: + return found + for entry in entries: + if entry.is_file(): + marker = read_part_marker(entry) + if marker: + found.append(marker) + elif entry.is_dir() and not entry.is_symlink() and entry.name != BACKUP_DIR: + try: + below = sorted(entry.iterdir()) + except OSError: + continue + for child in below: + marker = read_part_marker(child) if child.is_file() else None + if marker: + found.append(marker) + return found + + +def split_pack_lines(root: Path) -> list[str]: + """What --check says about the parts of a pack extracted into root: one + missing, or one extracted into a folder of its own.""" + by_pack: dict[str, list[PackPart]] = {} + for marker in find_part_markers(root): + by_pack.setdefault(marker.pack, []).append(marker) + lines: list[str] = [] + for pack, markers in sorted(by_pack.items()): + listed = max(markers, key=lambda m: m.count) + here = {m.index for m in markers if m.folder == root and m.count == listed.count} + missing = [n for n in range(1, listed.count + 1) if n not in here] + deep = sorted({m.folder for m in markers if m.folder != root}) + if not missing and not deep: + continue + lines.append(f"\n {pack} comes in {listed.count} parts, all needed.") + for folder in deep: + names = sorted(m.index for m in markers if m.folder == folder) + which = ", ".join(str(n) for n in names) + lines.append(f" {'Parts' if len(names) > 1 else 'Part'} {which} went into " + f"{folder}, a folder of its own: move what it holds up into {root}.") + absent = [n for n in missing + if not any(m.index == n and m.folder != root for m in markers)] + for number in absent: + name, size = listed.parts[number - 1] + lines.append(f" missing: part {number}, {name} ({format_size(size)})") + if absent: + lines.append(f" Download {'it' if len(absent) == 1 else 'them'} and extract " + f"into {root}, beside the other parts.") + return lines + + +PACK_MEMBER_DATE = (1980, 1, 1, 0, 0, 0) +PACK_COMPRESSLEVEL = 9 + + +# The marker each part carries at its root, as scripts/split_pack.py writes +# it: the sizes at a fixed width, so its length does not depend on them. +PART_SIZE_WIDTH = 13 +# A member may grow under deflate (zlib's deflateBound is under one byte in +# a thousand, plus a few), and its headers carry a ZIP64 field past 2 GiB. +# A part is planned on these bounds, never on the compressed size, which is +# known only once written. +_ZIP_LOCAL_BYTES = 30 + 20 +_ZIP_CENTRAL_BYTES = 46 + 28 +_ZIP_END_BYTES = 22 + 56 + 20 + + +class PackError(Exception): + """A pack that cannot be made as asked. left names the files a failure + could not take away, none when nothing was left behind.""" + + def __init__(self, words: str, left: list[Path] | None = None) -> None: + super().__init__(words) + self.left = list(left or []) + + +class PackWritten(NamedTuple): + """What write_pack did: the files of the pack, the earlier files of + its name it removed, and those it could not remove or left as they + are, each with the reason.""" + + files: list[Path] + removed: list[Path] + kept: list[tuple[Path, str]] + + +class PackMember(NamedTuple): + """One member of a pack being made: its name, the bytes it must hold + and the file they are read from.""" + + name: str + sha1: str + size: int + source: Path + + +def _member_bound(name: str, size: int) -> int: + """The most bytes a member can take in a part: both headers and its + data deflated at worst.""" + encoded = len(name.encode("utf-8")) + return _ZIP_LOCAL_BYTES + _ZIP_CENTRAL_BYTES + 2 * encoded + size + size // 1000 + 64 + + +def pack_bound(manifest_size: int, members: list[PackMember]) -> int: + """The most bytes the pack can take as one ZIP.""" + return (_ZIP_END_BYTES + _member_bound("manifest.json", manifest_size) + + sum(_member_bound(member.name, member.size) for member in members)) + + +def part_marker_text(name: str, index: int, sizes: list[int]) -> bytes: + """The marker of part index, in the words scripts/split_pack.py writes + and read_part_marker reads back.""" + count = len(sizes) + lines = [ + "RetroBIOS split pack", + f"This folder holds part {index} of {count} of {name}.", + "Every part is needed: extract them all into this same folder.", + "The installer's --check names a part missing from the folder.", + "", + f"pack: {name}", + f"part: {index} of {count}", + ] + lines += [f"file: {size:>{PART_SIZE_WIDTH}} {part_file_name(name, number, count)}" + for number, size in enumerate(sizes, 1)] + return ("\n".join(lines) + "\n").encode("utf-8") + + +def _marker_bound(name: str, count: int) -> int: + marker = len(part_marker_name(name, count, count).encode("utf-8")) + return 30 + 46 + 2 * marker + len(part_marker_text(name, count, [0] * count)) + + +def plan_pack_parts(name: str, manifest_size: int, members: list[PackMember], + limit: int | None) -> list[list[PackMember]]: + """The members of each part, in pack order, manifest.json in the first. + One part without a limit, or when the whole pack stays under it. Each + part's bound stays under the limit, room kept for its marker, the plan + made again until the count it assumed is the count it gives.""" + if limit is None or pack_bound(manifest_size, members) < limit: + return [list(members)] + count = 2 + while True: + floor = _ZIP_END_BYTES + _marker_bound(name, count) + parts: list[list[PackMember]] = [[]] + size = floor + _member_bound("manifest.json", manifest_size) + if size >= limit: + raise PackError(f"the list alone needs {format_size(size)}, more than a part " + f"of {format_size(limit)} holds") + for member in members: + weight = _member_bound(member.name, member.size) + if floor + weight >= limit: + raise PackError(f"{member.name} ({format_size(member.size)}) does not fit " + f"in a part of {format_size(limit)}") + if size + weight >= limit or len(parts[-1]) >= 0xFFFE: + parts.append([]) + size = floor + parts[-1].append(member) + size += weight + if len(parts) > MAX_PARTS: + raise PackError(f"the pack would take {len(parts)} parts, more than " + f"{MAX_PARTS}: give a larger size") + if len(parts) <= count: + return parts + count = len(parts) + + +# ZipInfo's level is private before Python 3.13 (compress_level there, the +# old name kept as its alias), so typeshed does not declare it. +_ZIPINFO_LEVEL = "_compresslevel" + + +def _pack_info(name: str, compress: int = zipfile.ZIP_DEFLATED) -> zipfile.ZipInfo: + """A member header that depends on its name alone.""" + info = zipfile.ZipInfo(name, date_time=PACK_MEMBER_DATE) + info.compress_type = compress + # A ZipInfo given to writestr or open keeps its own level, the + # archive's left unread: without it every member was deflated at 6. + setattr(info, _ZIPINFO_LEVEL, + PACK_COMPRESSLEVEL if compress == zipfile.ZIP_DEFLATED else None) + info.create_system = 3 + info.external_attr = 0o100644 << 16 + return info + + +def _write_member(archive: zipfile.ZipFile, member: PackMember) -> None: + """Copy one member into the archive, its SHA-1 and size checked on the + bytes written, before the archive is closed.""" + info = _pack_info(member.name) + info.file_size = member.size + digest = hashlib.sha1() + size = 0 + with open(member.source, "rb") as src, archive.open(info, "w") as out: + while True: + chunk = src.read(1024 * 1024) + if not chunk: + break + digest.update(chunk) + size += len(chunk) + out.write(chunk) + if size != member.size or digest.hexdigest() != member.sha1: + raise PackError(f"{member.name} does not match the list: {size} bytes of SHA-1 " + f"{digest.hexdigest()}, the list says {member.size} of " + f"{member.sha1}") + + +def _patch_marker(path: Path, marker: str, body: bytes) -> None: + """Write the real marker over its placeholder of the same length: the + data, and the CRC-32 of the local and the central header.""" + with zipfile.ZipFile(path) as archive: + first = archive.infolist()[0] + central = int(archive.start_dir) + if first.filename != marker or first.file_size != len(body) or first.header_offset: + raise PackError(f"{path.name} does not start with its marker") + crc = zlib.crc32(body).to_bytes(4, "little") + with open(path, "r+b") as handle: + local = handle.read(30) + if local[:4] != b"PK\x03\x04": + raise PackError(f"{path.name} has no local header at its start") + data = (30 + int.from_bytes(local[26:28], "little") + + int.from_bytes(local[28:30], "little")) + handle.seek(14) + handle.write(crc) + handle.seek(data) + handle.write(body) + handle.seek(central) + if handle.read(4) != b"PK\x01\x02": + raise PackError(f"{path.name} has no central directory where it says") + handle.seek(central + 16) + handle.write(crc) + + +def _part_contents(name: str, index: int, count: int, manifest: bytes, + chosen: list[PackMember]) -> list[tuple[str, int]]: + """(name, size) of each member of a part, in the order written.""" + held = ([(part_marker_name(name, index, count), + len(part_marker_text(name, index, [0] * count)))] if count > 1 else []) + held += [("manifest.json", len(manifest))] if index == 1 else [] + return held + [(member.name, member.size) for member in chosen] + + +def _discard(paths: Iterable[Path]) -> list[Path]: + """Delete each file, the ones that could not be deleted returned.""" + left = [] + for path in paths: + try: + path.unlink(missing_ok=True) + except OSError: + left.append(path) + return left + + +def _of_this_pack(path: Path, name: str) -> bool: + """Whether path is a regular file that is a pack of this name: a part + whose marker names it, or a ZIP holding a manifest.json.""" + try: + if not stat.S_ISREG(os.lstat(path).st_mode): + return False + with zipfile.ZipFile(path) as archive: + names = archive.namelist() + markers = [member for member in names if _PART_MARKER.match(member)] + if markers: + text = archive.read(markers[0])[:MAX_MARKER_BYTES].decode("utf-8", "replace") + return f"pack: {name}" in text.splitlines() + return "manifest.json" in names + except (OSError, zipfile.BadZipFile, ValueError): + return False + + +def _aside_name(path: Path) -> Path: + """A name beside path no file holds, for the earlier pack while the + new one takes its place.""" + for number in range(1000): + aside = path.with_name(f".{path.name}.{os.getpid()}.{number}.old") + if not os.path.lexists(aside): + return aside + raise PackError(f"no free name beside {path.name} to set it aside") + + +def write_pack(path: Path, manifest: bytes, members: list[PackMember], + limit: int | None) -> PackWritten: + """Write the pack at path, or in parts beside it when a limit is given + and the pack would pass it. + + Each file is written beside its final name, every member checked + against its SHA-1 and the archive read back with its CRCs. Then the + earlier files of this pack (whole, or in parts) are set aside, the new + ones renamed in, and the earlier ones deleted. A failure before the new + set is whole puts the earlier set back and deletes the new files: the + folder holds the pack before or the pack after, never a mix. A file + deleted at the end that cannot be is reported, the pack being written.""" + name = path.name + plan = plan_pack_parts(name, len(manifest), members, limit) + count = len(plan) + finals = ([path] if count == 1 else + [path.with_name(part_file_name(name, index, count)) + for index in range(1, count + 1)]) + scratches: list[Path] = [] + try: + for index, (final, chosen) in enumerate(zip(finals, plan), 1): + handle, made = tempfile.mkstemp(prefix=f".{final.name}.", suffix=".partial", + dir=str(path.parent)) + os.close(handle) + scratches.append(Path(made)) + with zipfile.ZipFile(made, "w", zipfile.ZIP_DEFLATED, + compresslevel=PACK_COMPRESSLEVEL) as archive: + if count > 1: + archive.writestr( + _pack_info(part_marker_name(name, index, count), zipfile.ZIP_STORED), + part_marker_text(name, index, [0] * count)) + if index == 1: + archive.writestr(_pack_info("manifest.json"), manifest) + for member in chosen: + if _stop.is_set(): + raise KeyboardInterrupt + _write_member(archive, member) + sizes = [scratch.stat().st_size for scratch in scratches] + if count > 1: + for index, scratch in enumerate(scratches, 1): + _patch_marker(scratch, part_marker_name(name, index, count), + part_marker_text(name, index, sizes)) + for index, (scratch, chosen) in enumerate(zip(scratches, plan), 1): + shown = finals[index - 1].name + with zipfile.ZipFile(scratch) as archive: + held = [(info.filename, info.file_size) for info in archive.infolist()] + damaged = archive.testzip() + if damaged is not None: + raise PackError(f"{shown}: {damaged} does not read back") + if count > 1 and archive.read(part_marker_name(name, index, count)) != \ + part_marker_text(name, index, sizes): + raise PackError(f"{shown}: its marker does not read back") + if held != _part_contents(name, index, count, manifest, chosen): + raise PackError(f"{shown} does not read back as written") + if limit is not None and count > 1 and sizes[index - 1] >= limit: + raise PackError(f"{shown} reached the limit of {format_size(limit)}") + except BaseException as exc: + left = _discard(scratches) + if left and isinstance(exc, (OSError, PackError)): + raise PackError(str(exc), left) from exc + raise + return _swap_in(path, scratches, finals) + + +def _swap_named(path: Path, finals: list[Path]) -> list[Path]: + """The files beside path that the new pack may replace.""" + stem = path.name[: -len(".zip")] + named: list[Path] = [] + for old in _listed_paths(path.parent): + part = _PART_FILE.match(old.name) + if old == path or old in finals or (part is not None and part["stem"] == stem): + named.append(old) + return named + + +def _swap_restore(exc: OSError | PackError, name: str, scratches: list[Path], + finals: list[Path], aside: list[tuple[Path, Path]], + placed: list[Path]) -> PackError: + """Undo a swap that failed: the new files discarded, the earlier pack put + back where it can be.""" + left = _discard([*placed, *(scratch for scratch, final in zip(scratches, finals) + if final not in placed)]) + unrestored = [] + for old, moved in reversed(aside): + try: + os.replace(moved, old) + except OSError: + unrestored.append((old, moved)) + words = f"{name} could not be put in place: {exc.strerror or exc}" \ + if isinstance(exc, OSError) else str(exc) + if unrestored: + words += ". The earlier pack is set aside as " + ", ".join( + f"{moved.name} (was {old.name})" for old, moved in unrestored) + return PackError(words, [*left, *(moved for _old, moved in unrestored)]) + + +def _swap_in(path: Path, scratches: list[Path], finals: list[Path]) -> PackWritten: + """Put the new files in place of the earlier pack of this name.""" + name = path.name + named = _swap_named(path, finals) + kept: list[tuple[Path, str]] = [] + earlier: list[Path] = [] + aside: list[tuple[Path, Path]] = [] + placed: list[Path] = [] + try: + for old in named: + if old in finals: + if not stat.S_ISREG(os.lstat(old).st_mode): + raise PackError(f"{old.name} is in the way and is not a file") + earlier.append(old) + elif _of_this_pack(old, name): + earlier.append(old) + else: + kept.append((old, "it is not a part of this pack, left as it is")) + for old in earlier: + moved = _aside_name(old) + os.replace(old, moved) + aside.append((old, moved)) + for scratch, final in zip(scratches, finals): + os.replace(scratch, final) + placed.append(final) + except (OSError, PackError) as exc: + raise _swap_restore(exc, name, scratches, finals, aside, placed) from exc + removed = [] + for old, moved in aside: + if old in finals: + if _discard([moved]): + kept.append((moved, "the earlier copy could not be deleted")) + continue + if _discard([moved]): + kept.append((moved, f"the earlier {old.name}, set aside, could not be deleted")) + else: + removed.append(old) + return PackWritten(finals, removed, kept) + + +def _listed_paths(folder: Path) -> list[Path]: + """The files of a folder, [] when it cannot be listed.""" + return [folder / name for name in sorted(_listed(folder))] + + +def _listed(folder: Path) -> list[str]: + try: + return os.listdir(folder) + except OSError: + return [] diff --git a/installer/run.py b/installer/run.py index 825bacea1..3ea8f15ee 100644 --- a/installer/run.py +++ b/installer/run.py @@ -53,7 +53,8 @@ from installer.outcome import ( _report_outcome, _write_advice, ) -from installer.pack import run_pack, split_pack_lines +from installer.pack import run_pack +from installer.pack_parts import split_pack_lines from installer.pack_source import NOT_IN_PACK, pack_lacks_words from installer.paths import _entry_path, shown_path from installer.plan import CONFIRM_FROM_BYTES, _check_free_space, _print_plan, bulk_hint diff --git a/installer/tui_actions.py b/installer/tui_actions.py index 85a788d04..4d16e6e21 100644 --- a/installer/tui_actions.py +++ b/installer/tui_actions.py @@ -11,7 +11,7 @@ from installer.destination import ( from installer.games import games_probe from installer.host_fs import _is_dir, _nearest_folder from installer.offline import LOCAL -from installer.pack import _listed +from installer.pack_parts import _listed from installer.pack_source import PackSource from installer.platforms import display_name from installer.tui_choice import ( diff --git a/wiki/architecture.md b/wiki/architecture.md index 4ffc00c02..ce1b3c7b3 100644 --- a/wiki/architecture.md +++ b/wiki/architecture.md @@ -505,7 +505,7 @@ those it imports: | Sources of files | `offline`, `network`, `lists`, `pack_source`, `folder_index`, `local_copies`, `from_folders`, `diagnosis`, `download`, `datadirs`, `standalone` | | What a run keeps | `narrowing`, `choices`, `romm`, `install_record`, `game_files`, `games_index`, `game_regions`, `game_folders`, `games`, `tally`, `space`, `fetch_plan` | | Line mode | `destination`, `line_folder`, `line_menu`, `plan_words`, `plan`, `failure_causes`, `outcome` | -| Runs and actions | `arguments`, `run_setups`, `run_files`, `run`, `pack`, `removal_plan`, `removal`, `backups`, `handheld_app`, `cli` | +| Runs and actions | `arguments`, `run_setups`, `run_files`, `run`, `pack_parts`, `pack`, `removal_plan`, `removal`, `backups`, `handheld_app`, `cli` | | Full screen | `tui_state`, `tui_routes`, `tui_commands`, `tui_keys`, `tui_paint`, `tui_choice`, `tui_folder_scan`, `tui_rows`, `tui_frame`, `tui_plan_view`, `tui_outcome`, `tui_where_views`, `tui_screens`, `tui_help`, `tui_steps`, `tui_folder_actions`, `tui_actions`, `tui_input`, `tui_receive`, `tui_list_words`, `tui_engine`, `tui_first_screen`, `tui_terminal`, `tui_session` | ## Tests