Files

76 lines
4.5 KiB
Python

# FAT disk image fixtures (tools/fixtures.py): FAT12 / FAT16 / FAT32 images made by dosfstools' `mkfs.fat` and filled
# by mtools' `mcopy -s` (long names, lower-case 8.3 names, non-ASCII names, nested directories, an empty file, files
# over many clusters), a hard-disk image with an MBR and a FAT16 partition (mkfs.fat --offset); the reference listing
# is the tree mtools extracts again (`mcopy -s`), dates under TZ=UTC. Our writer's floppies (tools/fixtures/
# _arch2_ours.luau) are checked by `fsck.fat -n` and extracted by mtools. Images are emitted gzip-compressed.
# tests/gen/fat.luau { cases = { { name, gz, bits, label?, entries = { { name, size?, crc?, dir?, mtime? } } } },
# ours = { { size, ok, fsck } } }
import gzip, importlib.util, os, shutil, struct, zlib
_s = importlib.util.spec_from_file_location("_arch2", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_arch2.py"))
A = importlib.util.module_from_spec(_s)
_s.loader.exec_module(A)
INPUTS = ["tools/fixtures/_arch2.py", "tools/fixtures/_arch2_ours.luau", "src/Rosetta/containers/Fat.luau", "src/Rosetta/archive/fat"]
PKGS = ["dosfstools", "mtools"]
def extract(img, out, tmp):
os.makedirs(out, exist_ok=True)
A.tool(PKGS, ["mcopy", "-s", "-n", "-m", "-i", img, "::/*", out + "/"], cwd=tmp)
return A.read_tree(out)
def generate(ctx):
os.environ["TZ"] = "UTC"
os.environ["MTOOLS_SKIP_CHECK"] = "1"
rng = ctx.rng(12)
tree = [("README.TXT", b"upper 8.3\n"), ("lower.txt", b"lower case 8.3\n"), ("Mixed Case Long Name.text", b"long\n"),
("docs/a very long file name that spans several entries.md", b"x" * 3000), ("docs/sub/deep/leaf.bin", rng.randbytes(20000)),
("empty.dat", b""), ("naïve café 日本.txt", "unicode".encode()), ("big.bin", rng.randbytes(150000))]
tmp = A.tmpdir()
try:
src = os.path.join(tmp, "src")
A.tree(tree, src)
dirs = {"docs/", "docs/sub/", "docs/sub/deep/"}
cases = []
for bits, kb, label in ((12, 1440, "FLOPPY"), (12, 720, None), (16, 16384, "SIXTEEN"), (32, 40960, "THIRTYTWO")):
img = os.path.join(tmp, "t%d-%d.img" % (bits, kb))
argv = ["mkfs.fat", "-C", "-F", str(bits)] + (["-n", label] if label else []) + [img, str(kb)]
A.tool(PKGS, argv, cwd=tmp)
items = [os.path.join(src, n) for n in os.listdir(src)]
A.tool(PKGS, ["mcopy", "-s", "-m", "-i", img] + items + ["::/"], cwd=tmp)
got = extract(img, os.path.join(tmp, "x%d-%d" % (bits, kb)), tmp)
ents = [{"name": d, "dir": True} for d in sorted(dirs)]
for n, d in tree:
assert got.get(n) == zlib.crc32(d), (img, n)
ents.append({"name": n, "size": len(d), "crc": zlib.crc32(d), "mtime": A.MTIME})
e = {"name": "fat%d %dK" % (bits, kb), "gz": gzip.compress(open(img, "rb").read(), mtime=0), "bits": bits, "entries": ents}
if label:
e["label"] = label
cases.append(e)
# a hard-disk image: MBR + a FAT16 partition at sector 2048
img = os.path.join(tmp, "hd.img")
A.tool(PKGS, ["mkfs.fat", "-C", "-F", "16", "--offset", "2048", "-n", "PART1", img, str(8192 + 1024)], cwd=tmp)
with open(img, "r+b") as f:
mbr = bytearray(512)
mbr[446:462] = struct.pack("<B3sB3sII", 0x80, b"\0\0\0", 0x06, b"\0\0\0", 2048, 8192 * 2)
mbr[510:512] = b"\x55\xaa"
f.write(mbr)
part = img + "@@1M"
A.tool(PKGS, ["mcopy", "-m", "-i", part, os.path.join(src, "README.TXT"), os.path.join(src, "big.bin"), "::/"], cwd=tmp)
got = extract(part, os.path.join(tmp, "xhd"), tmp)
cases.append({"name": "mbr fat16", "gz": gzip.compress(open(img, "rb").read(), mtime=0), "bits": 16, "label": "PART1",
"entries": [{"name": n, "size": len(d), "crc": got[n]} for n, d in tree if n in ("README.TXT", "big.bin")]})
ours = []
for size in (1440, 720):
img = os.path.join(tmp, "ours%d.img" % size)
open(img, "wb").write(A.ours(ctx, "fat", "size=%d,label=OURS" % size))
r = A.tool(PKGS, ["fsck.fat", "-n", img], cwd=tmp, check=False)
got = extract(img, os.path.join(tmp, "xo%d" % size), tmp)
ok = all(got.get(n) == zlib.crc32(d) for n, d in A.FILES)
ours.append({"size": size, "ok": ok, "fsck": r.decode(errors="replace")})
ctx.emit("fat", {"cases": cases, "ours": ours})
finally:
shutil.rmtree(tmp)