# ar / deb fixtures (tools/fixtures.py): GNU ar (binutils: deterministic `rcSD`, real times `rcSU`, a library of two # `as`-assembled objects with its "/" symbol table), llvm-ar --format=bsd (#1/ names, __.SYMDEF), bsdtar arbsd, and # Debian packages from dpkg-deb --build (-Zxz / gzip / zstd / none) plus two built here (bzip2 and lzma members, as # older dpkg wrote them). The expected entries are the input files; the .deb data entries come from Python's tarfile. # tests/gen/ar.luau { files = { { name, data } }, gnuD = data (GNU ar rcSD of files), archives = { { name, data, # variant, entries = { { name, size, mtime?, crc } }, symbols? = { { name, member } }, roundtrip? } }, # debs = { { name, data, control = { [field] = value }, payload, entries = { { name, type, size, crc? } } } } } import bz2, gzip, io, lzma, os, shutil, subprocess, tarfile, tempfile, time, zlib MTIME = 1700000000 def sh(pkgs, script, cwd, env=None): argv = ["nix", "shell"] + ["nixpkgs#" + p for p in pkgs] + ["-c", "sh", "-c", script] return subprocess.run(argv, cwd=cwd, capture_output=True, check=True, env=env).stdout def ar_member(name, data, mtime=MTIME): h = b"%-16s%-12d%-6d%-6d%-8s%-10d`\n" % (name, mtime, 0, 0, b"100644", len(data)) return h + data + (b"\n" if len(data) % 2 else b"") def unpack(z, m): if z == "xz": return lzma.decompress(m) if z == "gzip": return gzip.decompress(m) if z == "zstd": return subprocess.run(["nix", "shell", "nixpkgs#zstd", "-c", "zstd", "-d", "-c"], input=m, capture_output=True, check=True).stdout return m def tar_entries(raw): out = [] with tarfile.open(fileobj=io.BytesIO(raw)) as t: for m in t.getmembers(): e = {"name": m.name + ("/" if m.isdir() and not m.name.endswith("/") else ""), "type": m.type.decode() if m.type != b"\0" else "0", "size": m.size if m.isfile() else 0} if m.isfile(): e["crc"] = zlib.crc32(t.extractfile(m).read()) out.append(e) return out def generate(ctx): rng = ctx.rng(17) files = [ ("a.txt", b"hello, ar\n"), ("odd", b"x" * 7), ("empty", b""), ("exactly15chars_", b"fifteen"), ("sixteen_chars_16", b"sixteen"), ("a_very_long_member_name.txt", rng.randbytes(1001)), ("another-long-member-name.bin", b"long name two\n"), ] tmp = tempfile.mkdtemp() try: names = [n for n, _ in files] for n, d in files: with open(os.path.join(tmp, n), "wb") as f: f.write(d) os.utime(os.path.join(tmp, n), (MTIME, MTIME)) q = " ".join(names) archives = [] gnuD = sh(["binutils"], "ar rcSD out.a %s && cat out.a && rm out.a" % q, tmp) ents = [{"name": n, "size": len(d), "crc": zlib.crc32(d)} for n, d in files] timed = [dict(e, mtime=MTIME) for e in ents] archives.append({"name": "gnu-D", "data": gnuD, "variant": "gnu", "entries": ents}) archives.append({"name": "gnu-U", "data": sh(["binutils"], "ar rcSU out.a %s && cat out.a && rm out.a" % q, tmp), "variant": "gnu", "entries": timed}) # a library: two objects, with a symbol table with open(os.path.join(tmp, "s1.s"), "w") as f: f.write(".globl foo\nfoo:\n.data\n.globl bar\nbar: .long 1\n") with open(os.path.join(tmp, "s2.s"), "w") as f: f.write(".globl baz_with_a_long_symbol_name\nbaz_with_a_long_symbol_name:\n") sh(["binutils"], "as -o one.o s1.s && as -o second_object_long_name.o s2.s", tmp) objs = [] for n in ("one.o", "second_object_long_name.o"): with open(os.path.join(tmp, n), "rb") as f: d = f.read() objs.append({"name": n, "size": len(d), "crc": zlib.crc32(d)}) syms = [{"name": "foo", "member": "one.o"}, {"name": "bar", "member": "one.o"}, {"name": "baz_with_a_long_symbol_name", "member": "second_object_long_name.o"}] lib = "one.o second_object_long_name.o a.txt" archives.append({"name": "gnu-lib", "data": sh(["binutils"], "ar rcsD out.a %s && cat out.a && rm out.a" % lib, tmp), "variant": "gnu", "entries": objs + ents[:1], "symbols": syms}) archives.append({"name": "llvm-bsd", "data": sh(["llvm"], "llvm-ar rcSD --format=bsd out.a %s && cat out.a && rm out.a" % q, tmp), "variant": "bsd", "entries": ents}) archives.append({"name": "llvm-bsd-lib", "data": sh(["llvm"], "llvm-ar rcsD --format=bsd out.a %s && cat out.a && rm out.a" % lib, tmp), "variant": "bsd", "entries": objs + ents[:1], "symbols": syms}) archives.append({"name": "bsdtar-arbsd", "data": sh(["libarchive"], "bsdtar --format=arbsd -cf - %s" % q, tmp), "variant": "bsd", "entries": timed, "roundtrip": True}) # Debian packages pkg = os.path.join(tmp, "pkg") os.makedirs(os.path.join(pkg, "DEBIAN")) os.makedirs(os.path.join(pkg, "usr", "share", "doc", "hello")) os.makedirs(os.path.join(pkg, "usr", "bin")) control = ("Package: hello\nVersion: 1.0-1\nArchitecture: all\nMaintainer: Ada \n" "Depends: libc6 (>= 2.31)\nDescription: a greeting\n prints a greeting\n .\n and exits\n") with open(os.path.join(pkg, "DEBIAN", "control"), "w") as f: f.write(control) with open(os.path.join(pkg, "usr", "bin", "hello"), "wb") as f: f.write(b"#!/bin/sh\necho hello\n") os.chmod(os.path.join(pkg, "usr", "bin", "hello"), 0o755) with open(os.path.join(pkg, "usr", "share", "doc", "hello", "README"), "wb") as f: f.write(rng.randbytes(2000)) fields = {"Package": "hello", "Version": "1.0-1", "Architecture": "all", "Maintainer": "Ada ", "Depends": "libc6 (>= 2.31)", "Description": "a greeting\nprints a greeting\n.\nand exits"} env = dict(os.environ, SOURCE_DATE_EPOCH=str(MTIME)) debs = [] for z, ext in (("xz", ".xz"), ("gzip", ".gz"), ("zstd", ".zst"), ("none", "")): data = sh(["dpkg"], "dpkg-deb --root-owner-group -Z%s --build pkg out.deb >/dev/null && cat out.deb && rm out.deb" % z, tmp, env) raw = None p = 8 while p < len(data): n = int(data[p + 48:p + 58]) if data[p:p + 8] == b"data.tar": m = data[p + 60:p + 60 + n] raw = unpack(z, m) p += 60 + n + (n & 1) debs.append({"name": "dpkg-" + z, "data": data, "control": fields, "payload": "data.tar" + ext, "entries": tar_entries(raw)}) # older compressors, assembled here: control.tar.bz2 + data.tar.bz2, control.tar.lzma + data.tar.lzma def tar_of(root, prefix): b = io.BytesIO() with tarfile.open(fileobj=b, mode="w", format=tarfile.GNU_FORMAT) as t: t.add(root, arcname=prefix, filter=lambda ti: setattr(ti, "mtime", MTIME) or ti) return b.getvalue() ctl = tar_of(os.path.join(pkg, "DEBIAN"), ".") dat_dir = os.path.join(tmp, "datatree") shutil.copytree(pkg, dat_dir, ignore=shutil.ignore_patterns("DEBIAN")) dat = tar_of(dat_dir, ".") alone = lambda b: lzma.compress(b, format=lzma.FORMAT_ALONE) for name, comp, ext in (("py-bzip2", bz2.compress, ".bz2"), ("py-lzma", alone, ".lzma")): data = b"!\n" + ar_member(b"debian-binary", b"2.0\n") + ar_member(b"control.tar" + ext.encode(), comp(ctl)) + \ ar_member(b"data.tar" + ext.encode(), comp(dat)) debs.append({"name": name, "data": data, "control": fields, "payload": "data.tar" + ext, "entries": tar_entries(dat)}) finally: shutil.rmtree(tmp) ctx.emit("ar", {"files": [{"name": n, "data": d} for n, d in files], "gnuD": gnuD, "archives": archives, "debs": debs})