Files

111 lines
5.2 KiB
Python

# cpio fixtures (tools/fixtures.py): GNU cpio (nixpkgs#cpio, --reproducible) archives of one tree in newc / crc / odc /
# bin, the bin one byte-swapped into the big-endian old binary format, bsdtar (libarchive) newc / odc archives, a crafted
# newc archive with device nodes and a bad crc sum, and a .cpio.gz. The expected entries come from os.lstat of the tree.
# tests/gen/cpio.luau { archives = { { name, data, format, gnu?, entries = { { name, type, mode, size, mtime, nlink,
# link?, crc? } } } }, devices = data, badcrc = data, gz = data }
import gzip, os, shutil, stat, struct, subprocess, tempfile, zlib
TYPES = {stat.S_IFREG: "0", stat.S_IFDIR: "5", stat.S_IFLNK: "2", stat.S_IFIFO: "6", stat.S_IFCHR: "3", stat.S_IFBLK: "4"}
def run(ctx, pkg, script, cwd):
return subprocess.run(["nix", "shell", "nixpkgs#" + pkg, "-c", "sh", "-c", script], cwd=cwd, capture_output=True, check=True).stdout
def swap_bin(data):
"""the little-endian old binary archive -> the big-endian one (every u16 header field swapped)"""
out, p = bytearray(), 0
while True:
f = struct.unpack("<13H", data[p:p + 26])
ns, fs = f[10], f[11] << 16 | f[12]
name = data[p + 26:p + 26 + ns - 1]
out += struct.pack(">13H", *f)
q = p + 26 + ns + (ns & 1)
e = q + fs + (fs & 1)
out += data[p + 26:e]
p = e
if name == b"TRAILER!!!":
return bytes(out + data[p:])
def newc(name, mode, size=0, data=b"", nlink=1, rdev=(0, 0), check=0, ino=1, crc=False):
ns = len(name) + 1
h = b"070701" + b"".join(b"%08X" % v for v in (ino, mode, 0, 0, nlink, 1600000000, size, 0, 0, rdev[0], rdev[1], ns, check))
if crc:
h = b"070702" + h[6:]
b = h + name + b"\0"
b += b"\0" * (-len(b) % 4) + data + b"\0" * (-len(data) % 4)
return b
def generate(ctx):
rng = ctx.rng(11)
files = [
("hello.txt", b"hello, cpio\n"),
("empty", b""),
("dir/sub/data.bin", rng.randbytes(3000)),
("exact4", b"four"),
("odd", b"x" * 7),
("d" * 60 + "/" + "e" * 80 + "/a-name-longer-than-a-hundred-bytes.txt", b"long " * 40),
("unicodé/naïve.txt", "ünïcödé".encode()),
]
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.chmod(os.path.join(root, "odd"), 0o755)
os.makedirs(os.path.join(root, "emptydir"))
os.symlink("hello.txt", os.path.join(root, "sym"))
os.symlink("dir/sub/data.bin", os.path.join(root, "dir/sublink"))
os.link(os.path.join(root, "hello.txt"), os.path.join(root, "hard"))
os.link(os.path.join(root, "hello.txt"), os.path.join(root, "dir/hard2"))
os.mkfifo(os.path.join(root, "fifo"))
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)
os.utime(root, (1650000000, 1650000000))
names = sorted(subprocess.run(["find", "."], cwd=root, capture_output=True, check=True).stdout.decode().split("\n")[:-1])
names = [n[2:] if n.startswith("./") else n for n in names]
def expected():
out = []
for n in names:
st = os.lstat(os.path.join(root, n))
t = TYPES[stat.S_IFMT(st.st_mode)]
e = {"name": n, "type": t, "mode": stat.S_IMODE(st.st_mode), "mtime": int(st.st_mtime), "nlink": st.st_nlink,
"size": st.st_size if t == "0" else 0}
if t == "2":
e["link"] = os.readlink(os.path.join(root, n))
if t == "0":
with open(os.path.join(root, n), "rb") as f:
e["crc"] = zlib.crc32(f.read())
out.append(e)
return out
want = expected()
listing = "find . | LC_ALL=C sort | "
archives = []
for fmt in ("newc", "crc", "odc", "bin"):
data = run(ctx, "cpio", listing + "cpio -o -H %s --reproducible 2>/dev/null" % fmt, root)
archives.append({"name": "gnu-" + fmt, "data": data, "format": fmt, "gnu": True, "entries": want})
archives.append({"name": "gnu-binbe", "data": swap_bin(archives[3]["data"]), "format": "binbe", "entries": want})
for fmt in ("newc", "odc"):
data = run(ctx, "libarchive", listing + "sed 's|^\\./||' | bsdtar --format=%s -n -cf - -T -" % fmt, root)
archives.append({"name": "bsdtar-" + fmt, "data": data, "format": fmt, "entries": want})
gz = gzip.compress(archives[0]["data"], mtime=0)
finally:
shutil.rmtree(tmp)
devices = b"".join([
newc(b"dev", 0o040755, nlink=2, ino=1),
newc(b"dev/null", 0o020666, rdev=(1, 3), ino=2),
newc(b"dev/sda", 0o060660, rdev=(8, 0), ino=3),
newc(b"dev/sock", 0o140755, ino=4),
newc(b"TRAILER!!!", 0, ino=0),
])
badcrc = newc(b"f", 0o100644, 3, b"abc", check=1, crc=True) + newc(b"TRAILER!!!", 0, ino=0, crc=True)
ctx.emit("cpio", {"archives": archives, "devices": devices, "badcrc": badcrc, "gz": gz})