Files
rosetta/tools/fixtures/_arch2_ours.luau
T

51 lines
1.8 KiB
Luau

-- tools/fixtures/_arch2_ours.luau: our archive writers' output for the reference tools to read (run by the ws-arch2
-- fixture generators through `luau tools/fixtures/_arch2_ours.luau -a <format> [<options as k=v,k=v>]`). Writes the
-- standard file set (the same names and bytes as _arch2.FILES) with the format's encoder and prints the file as hex.
local F = require("@rosetta")
local which, optstr = ...
local function content(n: number, seed: number): buffer
local b = buffer.create(n)
local s = seed
for i = 0, n - 1 do
s = (s * 75 + 74) % 65537
-- runs and noise, so compressors have something to find
buffer.writeu8(b, i, if (i // 64) % 3 == 0 then (i // 7) % 256 else s % 256)
end
return b
end
local opts: { [string]: any } = {}
for k, v in string.gmatch(optstr or "", "([%w_]+)=([^,]*)") do
opts[k] = tonumber(v) or (if v == "true" then true elseif v == "false" then false else v)
end
local mod = F.format(which) :: any
local a = if mod.new then mod.new() else nil
local files = {
{ "hello.txt", buffer.fromstring("hello, archive\n") },
{ "data.bin", content(5000, 7) },
{ "empty.txt", buffer.create(0) },
}
if opts.single then
files = { files[2] }
end
for _, f in files do
a:add(f[1], f[2], { mtime = 1700000000, mode = 0x1A4 })
end
if opts.mac then
-- a Mac file: type / creator / flags on the data fork, a resource fork entry
local e = a._byName["hello.txt"]
e.macType, e.macCreator, e.finderFlags = "TEXT", "ttxt", 0x0100
a:add("hello.txt.rsrc", content(700, 3), { mtime = 1700000000 })
a._byName["hello.txt.rsrc"].fork = "resource"
end
local out, err = a:write(opts)
if not out then
error(tostring(err))
end
local t = table.create(buffer.len(out))
for i = 0, buffer.len(out) - 1 do
t[i + 1] = string.format("%02x", buffer.readu8(out, i))
end
print(table.concat(t))