# tar + gzip fixtures (tools/fixtures.py): reference archives from Python's tarfile (ustar / GNU / pax formats, long # names, non-ASCII names, links, directories, pax mtimes) and from the host / nix GNU tar (--format=ustar / gnu / # posix / v7), gzip members from Python's gzip and the gzip CLI (FNAME, MTIME, levels, concatenated members), and # compressed tars (.tar.gz, .tgz, .tar.zst, .tar.xz, .tar.bz2, .tar.br) for the api's unwrap. # tests/gen/tar.luau { files = { { name, data, ... } }, writes = { { format, entries, data } }, tars = { { name, # data, entries = { { name, size, type, link?, mtime, mode, crc } } } }, gzips = { ... }, # wrapped = { { name, data, inner = "tar" } } } import bz2, gzip, io, json, lzma, os, shutil, subprocess, tarfile, tempfile, zlib def tool(name, pkg, argv, input=None, cwd=None): exe = shutil.which(name) cmd = ([exe] if exe else ["nix", "shell", "nixpkgs#" + pkg, "-c", name]) + argv return subprocess.run(cmd, input=input, capture_output=True, check=True, cwd=cwd).stdout def entries_of(data): out = [] with tarfile.open(fileobj=io.BytesIO(data)) 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, "mtime": m.mtime, "mode": m.mode} if m.issym() or m.islnk(): e["link"] = m.linkname if m.isfile(): e["crc"] = zlib.crc32(t.extractfile(m).read()) out.append(e) return out def generate(ctx): rng = ctx.rng(5) files = [ ("hello.txt", b"hello, tar\n"), ("empty", b""), ("dir/sub/data.bin", rng.randbytes(3000)), ("exact512", bytes(range(256)) * 2), ("d" * 60 + "/" + "e" * 60 + "/long-name-over-100-bytes.txt", b"long " * 50), ("x" * 150, b"no slash to split at"), ("unicodé/naïve-ファイル.txt", "ünïcödé".encode()), ] # the writer test: the same entries through Python's tarfile in three formats (fixed metadata) writes = [] for fmt, pf in (("ustar", tarfile.USTAR_FORMAT), ("gnu", tarfile.GNU_FORMAT), ("pax", tarfile.PAX_FORMAT)): buf = io.BytesIO() with tarfile.open(fileobj=buf, mode="w", format=pf) as t: ents = [] for name, data in files: if fmt == "ustar" and (not name.isascii() or name.startswith("x" * 101)): continue ti = tarfile.TarInfo(name) ti.size, ti.mtime, ti.mode, ti.uname, ti.gname = len(data), 1700000000, 0o644, "user", "group" t.addfile(ti, io.BytesIO(data)) ents.append({"name": name, "data": data}) ti = tarfile.TarInfo("adir") ti.type, ti.mtime, ti.mode, ti.uname, ti.gname = tarfile.DIRTYPE, 1700000000, 0o755, "user", "group" t.addfile(ti) ents.append({"name": "adir", "dir": True}) ti = tarfile.TarInfo("link-to-hello") ti.type, ti.linkname, ti.mtime, ti.mode, ti.uname, ti.gname = tarfile.SYMTYPE, "hello.txt", 1700000000, 0o777, "user", "group" t.addfile(ti) ents.append({"name": "link-to-hello", "type": "2", "link": "hello.txt", "mode": 0o777}) writes.append({"format": fmt, "entries": ents, "data": buf.getvalue()}) tars = [] for w in writes: tars.append({"name": "python-" + w["format"], "data": w["data"], "entries": entries_of(w["data"])}) # pax with a fractional mtime and a huge uid buf = io.BytesIO() with tarfile.open(fileobj=buf, mode="w", format=tarfile.PAX_FORMAT) as t: ti = tarfile.TarInfo("frac.txt") ti.size, ti.mtime, ti.uid = 3, 1600000000.5, 3000000 t.addfile(ti, io.BytesIO(b"abc")) tars.append({"name": "python-pax-extras", "data": buf.getvalue(), "entries": entries_of(buf.getvalue())}) # GNU tar on a real tree tmp = tempfile.mkdtemp() try: root = os.path.join(tmp, "tree") for name, data in files: p = os.path.join(root, name) os.makedirs(os.path.dirname(p), exist_ok=True) with open(p, "wb") as f: f.write(data) os.makedirs(os.path.join(root, "emptydir")) os.symlink("hello.txt", os.path.join(root, "sym")) os.link(os.path.join(root, "hello.txt"), os.path.join(root, "hard")) for dirpath, dirs, fs in os.walk(root): for n in dirs + fs: os.utime(os.path.join(dirpath, n), (1650000000, 1650000000), follow_symlinks=False) for fmt in ("ustar", "gnu", "posix", "v7"): argv = ["--format=" + fmt, "--sort=name", "--owner=0", "--group=0", "--numeric-owner", "-cf", "-", "."] if fmt in ("ustar", "v7"): argv = ["--exclude=./" + "x" * 150, "--exclude=./d" + "d" * 59] + argv if fmt == "v7": argv = ["--exclude=./unicodé"] + argv data = tool("tar", "gnutar", argv, cwd=root) tars.append({"name": "gnutar-" + fmt, "data": data, "entries": entries_of(data)}) finally: shutil.rmtree(tmp) # gzip text = ("The quick brown fox jumps over the lazy dog. " * 300).encode() gzips = [] b = io.BytesIO() with gzip.GzipFile(filename="story.txt", mode="wb", fileobj=b, mtime=1234567890, compresslevel=9) as g: g.write(text) gzips.append({"name": "python-fname", "data": b.getvalue(), "entry": "story.txt", "mtime": 1234567890, "crc": zlib.crc32(text), "size": len(text)}) gzips.append({"name": "python-noname", "data": gzip.compress(text, 1, mtime=0), "entry": "data", "mtime": 0, "crc": zlib.crc32(text), "size": len(text)}) cli = tool("gzip", "gzip", ["-9", "-n", "-c"], input=text) gzips.append({"name": "cli-n", "data": cli, "entry": "data", "mtime": 0, "crc": zlib.crc32(text), "size": len(text)}) two = gzip.compress(text[:1000], mtime=0) + gzip.compress(text[1000:], mtime=0) gzips.append({"name": "members", "data": two, "entry": "data", "mtime": 0, "crc": zlib.crc32(text), "size": len(text)}) # compressed tars inner = writes[2]["data"] wrapped = [ {"name": "a.tar.gz", "data": gzip.compress(inner, mtime=0), "outer": "gzip"}, {"name": "a.tgz", "data": tool("gzip", "gzip", ["-c", "-n"], input=inner), "outer": "gzip"}, {"name": "a.tar.bz2", "data": bz2.compress(inner), "outer": "bz2"}, {"name": "a.tar.xz", "data": lzma.compress(inner), "outer": "xz"}, {"name": "a.tar.zst", "data": tool("zstd", "zstd", ["-c", "-q"], input=inner), "outer": "zst"}, {"name": "a.tar.br", "data": ctx.python(["brotli"], "import brotli, sys; sys.stdout.buffer.write(brotli.compress(sys.stdin.buffer.read()))", input=inner), "outer": "br"}, ] ctx.emit("tar", {"files": [{"name": n, "data": d} for n, d in files], "writes": writes, "tars": tars, "gzips": gzips, "wrapped": wrapped, "innerEntries": entries_of(inner)})