ws-arch2-unix: docs/guide/archive.md sections for cpio, ar / .deb, rpm, .Z / .lz4, ISO (inputs: tools/fixtures/docs_arch_unix.py); archive/cpio: variant option (default: the source's variant)

This commit is contained in:
4DBug committed 2026-10-31 14:56:18 -05:00
1 parent 411261470a
commit 29afbfb165
3 files changed
+186 -9

No files matched your search

+99 -6
View File
@@ -25,7 +25,7 @@ assert(F.archive.load(out):read("notes/todo.txt"))
| Field / method | Type | Meaning |
|---|---|---|
| `kind` | `"archive"` | |
| `format` | string | `"zip"`, `"tar"`, `"7z"`, `"gzip"`, `"xz"`, `"zst"`, `"bz2"`, `"br"` |
| `format` | string | `"zip"`, `"tar"`, `"7z"`, `"gzip"`, `"xz"`, `"zst"`, `"bz2"`, `"br"`, `"cpio"`, `"ar"`, `"rpm"`, `"z"`, `"lz4"`, `"iso"` |
| `list(self)` | `{ ArchiveEntry }` | the entries in archive order (directories included) |
| `read(self, name)` | `buffer?, Err?` | a fresh copy of the entry's bytes, decompressed (lazily, on each call) |
| `add(self, name, data, opts?)` | | add or replace an entry; `data` is a buffer or a string |
@@ -43,7 +43,7 @@ assert(F.archive.load(out):read("notes/todo.txt"))
| `mtime` | number? | modification time, Unix seconds (UTC) |
| `dir` | boolean | a directory entry |
| `crc32`, `comment` | number?, string? | zip only |
| `mode`, `uid`, `gid`, `uname`, `gname`, `type`, `link` | | tar only (`mode` is the Unix permission bits, e.g. `420` = `0644`) |
| `mode`, `uid`, `gid`, `uname`, `gname`, `type`, `link` | | tar, cpio, ar, rpm, iso (`mode` is the Unix permission bits, e.g. `420` = `0644`) |
## Load
@@ -85,11 +85,12 @@ Options for loading:
| Option | Formats | Meaning |
|---|---|---|
| `unwrap` | gzip, xz, zst, bz2, br | `false`: do not open an archive inside a single-stream file (also off when `format` is given) |
| `unwrap` | gzip, xz, zst, bz2, br, z, lz4, deb | `false`: do not open an archive inside a single-stream file or a `.deb` (also off when `format` is given) |
| `name` | single-stream | the file name, used to name the entry (set for you when loading a path) |
| `verify` | zip | check each entry's CRC-32 on read (default `true`) |
| `maxEntries` | tar | refuse more entries (default 1e6) |
| `maxBytes` | bz2, br | refuse a bigger decompressed size |
| `verify` | zip, lz4, cpio | check each entry's CRC-32 on read (zip), the XXH32 checksums (lz4), the crc sums (cpio) (default `true`) |
| `maxEntries` | tar, cpio, ar, rpm, iso | refuse more entries (default 1e6) |
| `prefer` | iso | the name set: `"rockridge"`, `"joliet"` or `"iso9660"` (default: the richest present) |
| `maxBytes` | bz2, br, rpm, z, lz4 | refuse a bigger decompressed size |
A path works on hosts with a file system:
@@ -185,6 +186,92 @@ local gzipped = assert(F.save("some text", nil, { format = "gzip", name = "note.
assert(F.archive.load(gzipped, { unwrap = false }):list()[1].name == "note.txt")
```
## Unix, package and disc archives
### cpio
GNU cpio's and bsdtar's newc, crc, odc and old binary archives read; newc, crc and odc write (`variant`, default the
source's). Entries carry the Unix fields: `type` (tar letters: `"0"` file, `"5"` directory, `"2"` symlink, ...),
`mode`, `uid`, `gid`, `nlink`, `ino`, device numbers, `link`, and `hardlink` (every link of a hard-linked file reads
its data).
```luau
local cpio = assert(F.archive.load(cpioBytes))
local by = {}
for _, e in cpio:list() do
by[e.name] = e
end
assert(by["bin"].dir and by["bin/run.sh"].mode == 493) -- 493 = 0755
assert(by["run"].type == "2" and by["run"].link == "bin/run.sh") -- a symlink
local odc = assert(cpio:write({ variant = "odc" }))
assert(F.archive.load(odc).meta.source.variant == "odc")
```
### ar and .deb
Unix `ar` archives (GNU / System V long names and symbol tables, BSD `#1/` names) read and write (`variant = "gnu"`
or `"bsd"`); symbols land in `meta.symbols`. A `.deb` loads as its `data.tar.*` (any compression), with the ar
archive in `meta.outer` and the control file's fields in `meta.outer.meta.control`:
```luau
local deb = assert(F.archive.load(debBytes))
assert(deb.format == "tar" and deb.meta.container == "ar")
assert(deb:read("./usr/bin/hello"))
local control = deb.meta.outer.meta.control
assert(control.Package == "hello" and control.Version == "1.0-1")
local ar = assert(F.archive.load(debBytes, { unwrap = false })) -- the ar itself
assert(ar:list()[1].name == "debian-binary" and ar.meta.payload == "data.tar.xz")
```
### rpm
RPM packages list from their header (no decompression); the payload (gzip, bzip2, xz, lzma or zstd) is decompressed
on the first `read`. The header fields are in `meta.rpm` (`name`, `version`, `release`, `arch`, `summary`, ... and
every tag by number in `meta.rpm.tags`). Read only.
```luau
local rpm = assert(F.archive.load(rpmBytes))
assert(rpm.meta.rpm.name == "hello" and rpm.meta.rpm.version == "1.0" and rpm.meta.rpm.arch == "noarch")
assert(rpm:list()[1].name == "usr/bin/hello" and rpm:list()[1].mode == 493)
local hello = buffer.tostring(assert(rpm:read("usr/bin/hello")))
assert(string.find(hello, "echo hello", 1, true))
```
### .Z and .lz4
Unix `compress` (`.Z`, `.taz`) and LZ4 frame (`.lz4`) files are single-stream archives like gzip: a `.tar.Z` or
`.tar.lz4` loads as the tar. Both write: `.Z` byte for byte as ncompress (`maxBits` 9..16), `.lz4` as one frame of
independent blocks (`blockSize`, `blockChecksum`, `contentChecksum`, `contentSize`).
```luau
local tar = assert(F.archive.load(tarZBytes))
assert(tar.format == "tar" and tar.meta.compression == "z")
local lz = assert(F.archive.load(lz4Bytes, { name = "notes.txt.lz4" }))
assert(lz.meta.stream and lz:list()[1].name == "notes.txt")
local z = assert(F.save(tar, nil, { format = "z" })) -- a tar inside
assert(F.archive.load(z).format == "tar")
local l4 = assert(F.save("some text", nil, { format = "lz4", blockChecksum = true }))
assert(buffer.tostring(F.archive.load(l4):read("data")) == "some text")
```
### ISO images
ISO 9660 images read with Rock Ridge names, modes and symlinks when present, else Joliet's Unicode names, else the
plain ISO names (`prefer = "joliet"` / `"iso9660"` picks another set). `meta.iso` has the volume facts and
`meta.boot` an El Torito boot catalog. Read only.
```luau
local iso = assert(F.archive.load(isoBytes))
assert(iso.meta.iso.volumeId == "DOCS" and iso.meta.iso.rockRidge and iso.meta.iso.joliet)
assert(iso.meta.boot and iso.meta.boot.bootable)
local by = {}
for _, e in iso:list() do
by[e.name] = e
end
assert(by["bin"].dir and by["bin/run.sh"].mode == 493 and by["run"].link == "bin/run.sh")
assert(F.archive.load(isoBytes, { prefer = "joliet" }).meta.iso.tree == "joliet")
```
## Formats
| Format | Read | Write | Notes |
@@ -197,6 +284,12 @@ assert(F.archive.load(gzipped, { unwrap = false }):list()[1].name == "note.txt")
| zstd (`.zst`, `.tzst`) | yes | - | single stream; no dictionaries |
| bzip2 (`.bz2`, `.tbz2`) | yes | - | single stream |
| Brotli (`.br`) | yes | - | single stream; no magic number, so pass a `.br` name or `format = "br"` |
| cpio (`.cpio`) | yes | yes | newc, crc, odc, old binary (read); hard links, symlinks, devices |
| ar (`.a`, `.ar`, `.deb`) | yes | yes | GNU / BSD names, symbol tables; a `.deb` loads as its data tar |
| rpm (`.rpm`) | yes | - | gzip / bzip2 / xz / lzma / zstd payloads |
| compress (`.Z`, `.taz`) | yes | yes | single stream; 9..16-bit LZW |
| LZ4 (`.lz4`) | yes | yes | single stream; frames, legacy frames, XXH32 checks; no dictionaries |
| ISO 9660 (`.iso`) | yes | - | Joliet, Rock Ridge, El Torito, multi-extent; no UDF |
Full capability list: [FORMATS.md](../FORMATS.md).
+6 -3
View File
@@ -27,7 +27,8 @@ export type DecodeOptions = {
verify: boolean?, -- crc archives: check the data sums (default true)
}
export type EncodeOptions = {
format: string?, -- "newc" (default), "crc" or "odc" (any other value: newc)
variant: ("newc" | "crc" | "odc")?, -- default: the source archive's (old binary: newc), else "newc"
format: string?, -- "newc", "crc" or "odc" here too; any other value (the api's "cpio") means the variant default
block: number?, -- pad to a multiple of this many bytes (default 512, as GNU cpio; 1 = none)
}
@@ -152,9 +153,11 @@ function CpioFormat.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Er
rdevminor = num(e.rdevminor),
})
end
-- opts.format may also be the api's format name ("cpio"): the newc default then
-- opts.format may also be the api's format name ("cpio"): the variant default then
local o: any = opts or {}
local variant = if o.format == "crc" or o.format == "odc" then o.format else "newc"
local src = type(archive.meta) == "table" and type(archive.meta.source) == "table" and archive.meta.source
local own = if src and src.format == "cpio" and (src.variant == "crc" or src.variant == "odc") then src.variant else "newc"
local variant = o.variant or (if o.format == "crc" or o.format == "odc" or o.format == "newc" then o.format else own)
return Cpio.write(list, { format = variant, block = o.block })
end
+81
View File
@@ -0,0 +1,81 @@
# Docs inputs for the archive page's Unix / package sections (tools/fixtures.py; tools/docs_check.py explains the
# inputs): a newc cpio by GNU cpio, a .deb by dpkg-deb, an .rpm by rpmbuild, a .tar.Z by ncompress, an ISO image by
# xorriso (-J -R, with an El Torito boot image) and an .lz4 by the lz4 tool, all of one small tree.
# tests/gen/docs_arch_unix.luau { files = { [name] = bytes }, vars = { cpioBytes = "tree.cpio", ... } }
import importlib.util, io, os, subprocess, tarfile, tempfile
INPUTS = ["tools/docs_check.py"]
MTIME = 1767225600 # 2026-01-01T00:00:00Z
SPEC = """Name: hello
Version: 1.0
Release: 1
Summary: a greeting
License: MIT
BuildArch: noarch
%description
prints a greeting
%install
mkdir -p %{buildroot}/usr/bin
printf '#!/bin/sh\\necho hello\\n' > %{buildroot}/usr/bin/hello
chmod 755 %{buildroot}/usr/bin/hello
%files
/usr/bin/hello
"""
def docs_check(ctx):
spec = importlib.util.spec_from_file_location("docs_check", os.path.join(ctx.root, "tools", "docs_check.py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
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 generate(ctx):
dc = docs_check(ctx)
files = {}
env = dict(os.environ, SOURCE_DATE_EPOCH=str(MTIME))
with tempfile.TemporaryDirectory() as d:
tree = os.path.join(d, "tree")
os.makedirs(os.path.join(tree, "bin"))
with open(os.path.join(tree, "readme.txt"), "wb") as f:
f.write(b"formats-luau unix archives\n")
with open(os.path.join(tree, "bin", "run.sh"), "wb") as f:
f.write(b"#!/bin/sh\necho hi\n")
os.chmod(os.path.join(tree, "bin", "run.sh"), 0o755)
os.symlink("bin/run.sh", os.path.join(tree, "run"))
with open(os.path.join(tree, "boot.img"), "wb") as f:
f.write(b"\xfa\xeb\xfe" + bytes(2045))
for dirpath, dirs, fs in os.walk(tree):
for n in dirs + fs:
os.utime(os.path.join(dirpath, n), (MTIME, MTIME), follow_symlinks=False)
files["tree.cpio"] = sh(["cpio", "findutils"], "find readme.txt bin run | LC_ALL=C sort | cpio -o -H newc --reproducible 2>/dev/null", tree)
files["disc.iso"] = sh(["xorriso"], "xorriso -as mkisofs -quiet -no-pad -J -R -V DOCS -b boot.img -no-emul-boot -boot-load-size 4 -o - .", tree)
b = io.BytesIO()
with tarfile.open(fileobj=b, mode="w", format=tarfile.PAX_FORMAT) as t:
for n in ("readme.txt", "bin/run.sh"):
t.add(os.path.join(tree, n), arcname=n)
files["src.tar.Z"] = subprocess.run(["nix", "shell", "nixpkgs#ncompress", "-c", "compress", "-c", "-f"], input=b.getvalue(),
capture_output=True, check=True).stdout
files["notes.txt.lz4"] = subprocess.run(["nix", "shell", "nixpkgs#lz4", "-c", "lz4", "-c"], input=b"formats-luau unix archives\n" * 8,
capture_output=True, check=True).stdout
pkg = os.path.join(d, "pkg")
os.makedirs(os.path.join(pkg, "DEBIAN"))
os.makedirs(os.path.join(pkg, "usr", "bin"))
with open(os.path.join(pkg, "DEBIAN", "control"), "w") as f:
f.write("Package: hello\nVersion: 1.0-1\nArchitecture: all\nMaintainer: Ada <[email protected]>\nDescription: a greeting\n")
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)
files["hello.deb"] = sh(["dpkg"], "dpkg-deb --root-owner-group -Zxz --build pkg out.deb >/dev/null && cat out.deb", d, env)
with open(os.path.join(d, "hello.spec"), "w") as f:
f.write(SPEC)
sh(["rpm"], "rpmbuild --define '_topdir %s/top' --define '__os_install_post %%{nil}' --define '_build_id_links none' -bb hello.spec >/dev/null 2>&1" % d, d, env)
files["hello.rpm"] = open(os.path.join(d, "top", "RPMS", "noarch", "hello-1.0-1.noarch.rpm"), "rb").read()
dc.emit_inputs(ctx, files, {"cpioBytes": "tree.cpio", "isoBytes": "disc.iso", "tarZBytes": "src.tar.Z", "lz4Bytes": "notes.txt.lz4",
"debBytes": "hello.deb", "rpmBytes": "hello.rpm"})