"""Assemble a release X11 overlay from two audited vpac clients.""" import argparse import filecmp import gzip import hashlib from importlib import import_module import os from pathlib import Path import shutil import stat import subprocess import tarfile import tempfile RELEASE_COMMANDS = ( "usr/bin/Xorg", "usr/bin/Xvfb", "usr/bin/Xvfb-glx", "usr/bin/startx", "usr/bin/vinix-wine-host", "usr/bin/vinix-xinput", ) def tree(root): entries = {} for directory, folders, files in os.walk(root): # os.walk puts symlinks to directories in folders but does not traverse them. for name in folders + files: path = Path(directory, name) entries[path.relative_to(root).as_posix()] = path.lstat().st_mode return entries def overlay_paths(base, full): before, after = tree(base), tree(full) for path, mode in before.items(): updated = after.get(path) if updated is None or mode != updated: raise ValueError(f"X11 install removed or replaced /{path}") if stat.S_ISDIR(mode): continue original, installed = base / path, full / path matches = (os.readlink(original) == os.readlink(installed) if stat.S_ISLNK(mode) else filecmp.cmp(original, installed, shallow=False)) if not matches: raise ValueError(f"X11 install changed /{path} from the desktop root") added = sorted(after.keys() - before.keys()) for path in added: mode = after[path] if not (stat.S_ISREG(mode) or stat.S_ISLNK(mode) or stat.S_ISDIR(mode)): raise ValueError(f"unexpected sidecar entry type for /{path}") return added def packages(client): package_fields = import_module("stage-image").package_fields records = (client / "db/local").glob("*/desc") return {record["NAME"]: record["VERSION"] for record in (package_fields(path.read_text()) for path in records)} def owner_root(entry): entry.uid = entry.gid = 0 entry.uname = entry.gname = "root" return entry def write_payload(output, root, paths): with tarfile.open(fileobj=output, mode="w|", format=tarfile.USTAR_FORMAT) as tar: for path in paths: tar.add(root / path, arcname=f"./{path}", recursive=False, filter=owner_root) class TarComparison: def __init__(self, source): self.source = source def write(self, data): if self.source.read(len(data)) != data: raise ValueError("prepared X11 archive differs from the audited overlay") return len(data) def bundle(args): base, full = args.desktop.resolve(), args.x11.resolve() output = args.output.resolve() if output.is_relative_to(base) or output.is_relative_to(full): raise ValueError("put sidecar output outside both clients") if output.exists(): raise FileExistsError(output) for client in (base, full): subprocess.run([args.auditor, args.arch, client], check=True) common, combined = packages(base), packages(full) if ("vinix-release-desktop" not in common or "vinix-release-x11" in common or "vinix-release-x11" not in combined or not common.items() < combined.items()): raise ValueError("expected desktop and desktop-plus-X11 package roots") base, full = base / "root", full / "root" paths = overlay_paths(base, full) for name in RELEASE_COMMANDS: if name not in paths or not os.access(full / name, os.X_OK): raise ValueError(f"sidecar is missing executable /{name}") suffix = "amd64" if args.arch == "x86_64" else "arm64" name = f"vinix-x11-{suffix}.tar.gz" with tempfile.TemporaryDirectory(dir=output.parent) as work: staging = Path(work, "sidecar") staging.mkdir(mode=0o755) archive = staging / name if args.prepared: shutil.copyfile(args.prepared / name, archive) with gzip.open(archive, "rb") as compressed: write_payload(TarComparison(compressed), full, paths) if compressed.read(1): raise ValueError("prepared X11 archive contains extra data") else: with archive.open("xb") as data: with gzip.GzipFile(filename="", mode="wb", fileobj=data, mtime=0) as compressed: write_payload(compressed, full, paths) with archive.open("rb") as data: digest = hashlib.file_digest(data, "sha256").hexdigest() checksum = f"{digest} {name}\n" if args.prepared and (args.prepared / (name + ".sha256")).read_text() != checksum: raise ValueError("prepared X11 archive checksum differs") (staging / (name + ".sha256")).write_text(checksum) staging.rename(output) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("arch", choices=("x86_64", "aarch64")) for label in ("desktop", "x11", "auditor", "output"): parser.add_argument(label, type=Path) parser.add_argument("--prepared", type=Path, help="reuse and verify the archive in this sidecar directory") bundle(parser.parse_args())