ws-arch: containers/Tar + archive/tar (ustar / pax / GNU read + write, byte-identical to Python's tarfile) and archive/gzip (read + write, .tar.gz writing); tests incl. .tar.gz/.tgz/.tar.bz2/.tar.xz/.tar.zst/.tar.br unwrap through the api
This commit is contained in:
1 parent
945abe6d94
commit
a9110bdab2
9 files changed
+1298
No files matched your search
@@ -0,0 +1,187 @@
|
||||
--!strict
|
||||
-- archive/gzip: gzip files (RFC 1952) as a single-stream Archive: one entry, named after the header's FNAME (else the
|
||||
-- file name without .gz, else "data"), with the header's MTIME; concatenated members decode as one stream, as gzip -d
|
||||
-- does. Writing: an Archive with one entry (or a buffer / string) becomes one member with FNAME and MTIME; an Archive
|
||||
-- with several entries is written as a tar inside the gzip (a .tar.gz). The api unwraps a .tar.gz / .tgz on load (the
|
||||
-- inner tar, meta.compression = "gzip"). Deflate is codecs/Deflate. Not supported: FHCRC verification.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Deflate = require("@formats/codecs/Deflate")
|
||||
local Archive = require("@formats/containers/Archive")
|
||||
local Tar = require("@formats/containers/Tar")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Gzip = {
|
||||
name = "gzip",
|
||||
category = "archive",
|
||||
extensions = { "gz", "tgz", "gzip" },
|
||||
mime = { "application/gzip" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Crc32", "codecs/Deflate", "containers/Archive", "containers/Tar" },
|
||||
caps = { decode = true, encode = true, stream = true },
|
||||
}
|
||||
|
||||
export type DecodeOptions = { name: string? } -- the file name (the api passes it): names the entry without FNAME
|
||||
export type EncodeOptions = {
|
||||
level: number?, -- 0..9, default 6
|
||||
name: string?, -- FNAME (default: the entry's name; none for a buffer)
|
||||
mtime: number?, -- MTIME (default: the entry's mtime, else 0)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Gzip.sniff(r: Bytes.Reader): number
|
||||
return if r:peek(3) == "\31\139\8" then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- header(r) -> { name, comment, mtime, os } of the first member (nil fields when absent)
|
||||
local function header(b: buffer, o: number, e: number): { [string]: any }
|
||||
local h: { [string]: any } = {}
|
||||
if o + 10 > e then
|
||||
return h
|
||||
end
|
||||
local flg = buffer.readu8(b, o + 3)
|
||||
h.mtime = buffer.readu32(b, o + 4)
|
||||
h.os = buffer.readu8(b, o + 9)
|
||||
local q = o + 10
|
||||
if bit32.band(flg, 4) ~= 0 and q + 2 <= e then
|
||||
q += 2 + buffer.readu16(b, q)
|
||||
end
|
||||
for _, f in { { 8, "name" }, { 16, "comment" } } do
|
||||
if bit32.band(flg, f[1]) ~= 0 then
|
||||
local s = q
|
||||
while q < e and buffer.readu8(b, q) ~= 0 do
|
||||
q += 1
|
||||
end
|
||||
if q < e then
|
||||
h[f[2]] = buffer.readstring(b, s, q - s)
|
||||
end
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
return h
|
||||
end
|
||||
|
||||
function Gzip.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local out, err = Deflate.gunzip(r.buf, r.base, r.limit - r.base)
|
||||
if not out then
|
||||
return nil, err
|
||||
end
|
||||
local h = header(r.buf, r.base, r.limit)
|
||||
local a = Archive.new("gzip", function(arc: any, o: any?): (buffer?, Err.Err?)
|
||||
return Gzip.encode(arc, o)
|
||||
end)
|
||||
a.meta.stream = true
|
||||
a.meta.source = { format = "gzip", os = h.os }
|
||||
a.meta.comment = h.comment
|
||||
local name = h.name
|
||||
if not name or name == "" or string.find(name, "[/\\]") then
|
||||
name = Archive.streamName(opts, Gzip.extensions)
|
||||
end
|
||||
Archive.put(a, {
|
||||
name = name,
|
||||
size = buffer.len(out),
|
||||
compressedSize = r.limit - r.base,
|
||||
method = "deflate",
|
||||
mtime = if h.mtime and h.mtime > 0 then h.mtime else nil,
|
||||
dir = false,
|
||||
data = out,
|
||||
})
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Gzip.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local o = opts or {}
|
||||
local level = if o.level == nil then 6 else o.level
|
||||
if type(level) ~= "number" or level < 0 or level > 9 or level % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "level must be 0..9")
|
||||
end
|
||||
local data: buffer
|
||||
local name: string? = o.name
|
||||
local mtime: number = o.mtime or 0
|
||||
if type(value) == "buffer" then
|
||||
data = value
|
||||
elseif type(value) == "string" then
|
||||
data = buffer.fromstring(value)
|
||||
else
|
||||
local items = Archive.entries(value)
|
||||
if not items then
|
||||
return nil, Err.new(Err.ARG, "expected an Archive, a buffer or a string")
|
||||
end
|
||||
local files = {}
|
||||
for _, it in items do
|
||||
if not it.entry.dir then
|
||||
table.insert(files, it)
|
||||
end
|
||||
end
|
||||
if #files == 1 and #items == 1 then
|
||||
local b, err = files[1].read()
|
||||
if not b then
|
||||
return nil, err
|
||||
end
|
||||
data = b
|
||||
name = name or files[1].entry.name
|
||||
mtime = o.mtime or files[1].entry.mtime or 0
|
||||
else
|
||||
-- several entries: a tar inside
|
||||
local list = {}
|
||||
for _, it in items do
|
||||
local e: any = it.entry
|
||||
local b = nil
|
||||
if not e.dir then
|
||||
local rb, err = it.read()
|
||||
if not rb then
|
||||
return nil, err
|
||||
end
|
||||
b = rb
|
||||
end
|
||||
table.insert(list, { name = e.name, data = b, dir = e.dir, mtime = e.mtime, mode = e.mode })
|
||||
end
|
||||
local t, terr = Tar.write(list)
|
||||
if not t then
|
||||
return nil, terr
|
||||
end
|
||||
data = t
|
||||
end
|
||||
end
|
||||
if type(mtime) ~= "number" or mtime < 0 or mtime >= 2 ^ 32 then
|
||||
mtime = 0
|
||||
end
|
||||
local raw, cerr = Deflate.compress(data, { level = level })
|
||||
if not raw then
|
||||
return nil, cerr
|
||||
end
|
||||
local w = Bytes.writer(buffer.len(raw) + 32 + (if name then #name else 0))
|
||||
w:string("\31\139\8")
|
||||
w:u8(if name then 8 else 0)
|
||||
w:u32le(math.floor(mtime))
|
||||
w:u8(if level >= 9 then 2 elseif level <= 1 then 4 else 0)
|
||||
w:u8(255)
|
||||
if name then
|
||||
local z = string.find(name, "\0", 1, true)
|
||||
w:cstring(if z then string.sub(name, 1, z - 1) else name)
|
||||
end
|
||||
w:bytes(raw)
|
||||
w:u32le(Crc32.raw(data, 0, buffer.len(data)))
|
||||
w:u32le(buffer.len(data) % 4294967296)
|
||||
return w:finish(), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new(data?, name?) -> a gzip Archive holding one entry (add() replaces / adds; several entries write a .tar.gz)
|
||||
function Gzip.new(data: (buffer | string)?, name: string?): any
|
||||
local a = Archive.new("gzip", function(arc: any, o: any?): (buffer?, Err.Err?)
|
||||
return Gzip.encode(arc, o)
|
||||
end)
|
||||
a.meta.stream = true
|
||||
a.meta.source = { format = "gzip" }
|
||||
if data then
|
||||
a:add(name or "data", data)
|
||||
end
|
||||
return a
|
||||
end
|
||||
|
||||
return Gzip
|
||||
@@ -0,0 +1,120 @@
|
||||
--!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
|
||||
Reference in new issue
Block a user