Files
rosetta/src/Formats/archive/tar/init.luau
T

121 lines
4.2 KiB
Luau

--!strict
-- archive/tar: tar archives (ustar, pax, GNU long names; see containers/Tar), read into the standard Archive value
-- (entries read lazily: a copy of the stored bytes) and written from any Archive value (a tar, a zip, a 7z: list() +
-- read()). Compressed tars (.tar.gz / .tgz / .tar.zst / .tar.xz / .tar.bz2 / .tar.br) load through the api, which
-- unwraps the single-stream archive and sniffs again (meta.compression names the outer format).
-- Not supported: sparse files, multi-volume archives.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Archive = require("@formats/containers/Archive")
local Tar = require("@formats/containers/Tar")
-- ------------------------------------------------------------------------------------------------ contract
local TarFormat = {
name = "tar",
category = "archive",
extensions = { "tar" },
mime = { "application/x-tar" },
deps = { "core/Err", "core/Bytes", "containers/Archive", "containers/Tar" },
caps = { decode = true, encode = true, variants = { "ustar", "pax", "gnu" } },
}
export type DecodeOptions = { maxEntries: number? } -- default 1e6
export type EncodeOptions = { format: ("pax" | "gnu" | "ustar")? } -- default "pax": ustar + pax records when needed
-- ------------------------------------------------------------------------------------------------ sniff
function TarFormat.sniff(r: Bytes.Reader): number
if r:remaining() < 512 then
return 0
end
if not Tar.valid(r.buf, r.pos) then
return 0
end
return if r:peek(5, 257) == "ustar" then 0.95 else 0.6
end
-- ------------------------------------------------------------------------------------------------ decode
local function lazy(b: buffer, at: number, n: number): () -> (buffer?, Err.Err?)
return function(): (buffer?, Err.Err?)
local out = buffer.create(n)
buffer.copy(out, 0, b, at, n)
return out, nil
end
end
local function encode(archive: any, opts: any?): (buffer?, Err.Err?)
return TarFormat.encode(archive, opts)
end
function TarFormat.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local list, err = Tar.read(r.buf, r.base, r.limit - r.base, opts)
if not list then
return nil, err
end
local a = Archive.new("tar", encode)
for _, e in list do
Archive.put(a, {
name = e.name,
size = e.size,
dir = e.dir,
mtime = e.mtime,
mode = e.mode,
uid = e.uid,
gid = e.gid,
uname = e.uname,
gname = e.gname,
type = e.type,
link = e.link,
method = "store",
load = if e.dir or e.size == 0 then nil else lazy(r.buf, e.offset, e.size),
})
end
a.meta.source = { format = "tar" }
return a, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function TarFormat.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local items = Archive.entries(archive)
if not items then
return nil, Err.new(Err.ARG, "expected an Archive")
end
local list: { Tar.WriteEntry } = {}
for _, it in items do
local e: any = it.entry
local data: buffer? = nil
if not e.dir and e.type ~= "1" and e.type ~= "2" then
local b, err = it.read()
if not b then
return nil, err
end
data = b
end
table.insert(list, {
name = e.name,
data = data,
dir = e.dir,
type = if type(e.type) == "string" and #e.type == 1 then e.type else nil,
link = if type(e.link) == "string" then e.link else nil,
mode = if type(e.mode) == "number" then e.mode else nil,
uid = if type(e.uid) == "number" then e.uid else nil,
gid = if type(e.gid) == "number" then e.gid else nil,
uname = if type(e.uname) == "string" then e.uname else nil,
gname = if type(e.gname) == "string" then e.gname else nil,
mtime = if type(e.mtime) == "number" then e.mtime else nil,
})
end
return Tar.write(list, opts)
end
-- ------------------------------------------------------------------------------------------------ extras
-- new() -> an empty tar Archive: a:add(name, data, { mtime, mode, dir }); a:write()
function TarFormat.new(): any
local a = Archive.new("tar", encode)
a.meta.source = { format = "tar" }
return a
end
return TarFormat