ws-arch: containers/Archive (the in-memory Archive value shared by archive formats); api: F.load passes the file name to decoders (opts.name) and unwraps single-stream archives whose entry sniffs as an archive (.tar.gz -> tar, meta.compression)
This commit is contained in:
1 parent
ccb06775c5
commit
69c69c8264
2 files changed
+218
-1
No files matched your search
@@ -0,0 +1,176 @@
|
||||
--!strict
|
||||
-- containers/Archive: the in-memory Archive value (Types.Archive) that archive formats return. Entries hold their data
|
||||
-- or a lazy loader (decompressed on read); add() appends or replaces an entry; write() calls the format's encoder
|
||||
-- (attached by the format), so an archive read from a .tar writes back as a .tar, and any Archive (a zip, a 7z) can be
|
||||
-- encoded by any writable archive format through its list() / read().
|
||||
--
|
||||
-- local a = Archive.new("tar", Tar.encode) -- encode(archive, opts) -> buffer | nil, Err
|
||||
-- Archive.put(a, { name = "a.txt", size = 3, data = buf, mtime = 0 })
|
||||
-- Archive.put(a, { name = "b.bin", size = n, load = function() return inflate(...) end })
|
||||
-- a:list() a:read("a.txt") a:add("c.txt", "text") a:write()
|
||||
--
|
||||
-- Single-stream archives (gzip, zst, xz, bz2, br) set meta.stream = true and hold one entry; the api unwraps them
|
||||
-- (a .tar.gz loads as the tar inside). Never throws: read of a missing entry or a failed loader returns nil, Err.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Archive = {}
|
||||
|
||||
-- an entry: the Types.ArchiveEntry fields plus format extras (mode, uid, gid, link, type, ...) and its data source
|
||||
export type Entry = {
|
||||
name: string,
|
||||
size: number,
|
||||
compressedSize: number?,
|
||||
method: string?,
|
||||
mtime: number?,
|
||||
dir: boolean,
|
||||
data: buffer?,
|
||||
load: (() -> (buffer?, Err.Err?))?,
|
||||
[string]: any,
|
||||
}
|
||||
export type Encoder = (archive: any, opts: any?) -> (buffer?, Err.Err?)
|
||||
|
||||
local Methods = {}
|
||||
Methods.__index = Methods
|
||||
|
||||
-- the public fields of an entry (no data / loader)
|
||||
local function public(e: Entry): Types.ArchiveEntry
|
||||
local out: any = {}
|
||||
for k, v in e :: any do
|
||||
if k ~= "data" and k ~= "load" then
|
||||
out[k] = v
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function Methods.list(self: any): { Types.ArchiveEntry }
|
||||
local out = table.create(#self._entries)
|
||||
for i, e in self._entries do
|
||||
out[i] = public(e)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- read(name) -> buffer | nil, Err (directories read as an empty buffer)
|
||||
function Methods.read(self: any, name: string): (buffer?, Err.Err?)
|
||||
local e: Entry? = self._byName[name]
|
||||
if not e then
|
||||
return nil, Err.new(Err.ARG, "no entry named " .. tostring(name))
|
||||
end
|
||||
if e.data then
|
||||
return e.data
|
||||
end
|
||||
if e.load then
|
||||
local ok, b, err = pcall(e.load)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "entry " .. name .. ": " .. tostring(b))
|
||||
end
|
||||
if not b then
|
||||
return nil, err or Err.new(Err.CORRUPT, "entry " .. name .. ": decode failed")
|
||||
end
|
||||
return b
|
||||
end
|
||||
return buffer.create(0)
|
||||
end
|
||||
|
||||
-- add(name, data, opts?) -> (): opts.mtime (unix seconds), opts.dir, opts.mode; replaces an entry of the same name
|
||||
function Methods.add(self: any, name: string, data: buffer | string, opts: any?)
|
||||
local b: buffer = if type(data) == "string" then buffer.fromstring(data) elseif type(data) == "buffer" then data else buffer.create(0)
|
||||
local o = opts or {}
|
||||
local e: Entry = {
|
||||
name = name,
|
||||
size = buffer.len(b),
|
||||
dir = o.dir == true,
|
||||
mtime = o.mtime,
|
||||
mode = o.mode,
|
||||
data = b,
|
||||
}
|
||||
Archive.put(self, e)
|
||||
end
|
||||
|
||||
-- write(opts?) -> buffer | nil, Err: the archive's own format (opts.format is handled by the api)
|
||||
function Methods.write(self: any, opts: any?): (buffer?, Err.Err?)
|
||||
local enc = self._encode
|
||||
if not enc then
|
||||
return nil, Err.new(Err.UNSUPPORTED, self.format .. " archives are read-only")
|
||||
end
|
||||
return enc(self, opts)
|
||||
end
|
||||
|
||||
-- new(format, encode?) -> an empty Archive
|
||||
function Archive.new(format: string, encode: Encoder?): any
|
||||
return setmetatable({
|
||||
kind = "archive",
|
||||
format = format,
|
||||
meta = {},
|
||||
_entries = {} :: { Entry },
|
||||
_byName = {} :: { [string]: Entry },
|
||||
_encode = encode,
|
||||
}, Methods)
|
||||
end
|
||||
|
||||
-- put(archive, entry): append (or replace by name) an entry
|
||||
function Archive.put(a: any, e: Entry)
|
||||
local old = a._byName[e.name]
|
||||
if old then
|
||||
local i = table.find(a._entries, old)
|
||||
if i then
|
||||
a._entries[i] = e
|
||||
end
|
||||
else
|
||||
table.insert(a._entries, e)
|
||||
end
|
||||
a._byName[e.name] = e
|
||||
end
|
||||
|
||||
-- entries(value) -> { { entry, read: () -> (buffer?, Err?) } } for any Archive-shaped value (ours or another
|
||||
-- format's): what encoders iterate. nil when the value is not an archive.
|
||||
function Archive.entries(value: any): { { entry: Types.ArchiveEntry, read: () -> (buffer?, Err.Err?) } }?
|
||||
if type(value) ~= "table" or value.kind ~= "archive" or type(value.list) ~= "function" or type(value.read) ~= "function" then
|
||||
return nil
|
||||
end
|
||||
local ok, list = pcall(value.list, value)
|
||||
if not ok or type(list) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
local out = {}
|
||||
for _, e in list do
|
||||
if type(e) == "table" and type(e.name) == "string" then
|
||||
table.insert(out, {
|
||||
entry = e,
|
||||
read = function(): (buffer?, Err.Err?)
|
||||
local rok, b, err = pcall(value.read, value, e.name)
|
||||
if not rok then
|
||||
return nil, Err.new(Err.CORRUPT, tostring(b))
|
||||
end
|
||||
return b, err
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- streamName(opts, ext) -> the entry name for a single-stream archive: opts.name without its compression extension
|
||||
-- (".tgz" -> ".tar"), else "data"
|
||||
function Archive.streamName(opts: any?, exts: { string }): string
|
||||
local name = if type(opts) == "table" and type(opts.name) == "string" then opts.name else nil
|
||||
if not name then
|
||||
return "data"
|
||||
end
|
||||
local base = string.match(name, "([^/\\]+)$") or name
|
||||
local stem, ext = string.match(base, "^(.+)%.([%w]+)$")
|
||||
if stem and ext then
|
||||
local e = string.lower(ext)
|
||||
if e == "tgz" or e == "tbz2" or e == "tbz" or e == "txz" or e == "tzst" then
|
||||
return stem .. ".tar"
|
||||
end
|
||||
if table.find(exts, e) then
|
||||
return stem
|
||||
end
|
||||
end
|
||||
return base
|
||||
end
|
||||
|
||||
return Archive
|
||||
+42
-1
@@ -164,6 +164,42 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ API
|
||||
export type Options = { [string]: any } -- format (name or extension), category, path, data, name, + format options
|
||||
|
||||
local load: (src: any, opts: Options?) -> (any, Err.Err?)
|
||||
|
||||
-- single-stream archives (gzip / zst / xz / bz2 / br: meta.stream) whose one entry sniffs as another archive (a
|
||||
-- .tar.gz) load as that inner archive, with meta.compression = the outer format. opts.unwrap = false keeps the outer
|
||||
-- one; an explicit opts.format also does.
|
||||
local function unwrap(value: any, formatName: string, o: Options): any
|
||||
if o.unwrap == false or o.format ~= nil or (o._depth or 0) >= 3 then
|
||||
return value
|
||||
end
|
||||
if type(value) ~= "table" or value.kind ~= "archive" or type(value.meta) ~= "table" or not value.meta.stream then
|
||||
return value
|
||||
end
|
||||
local ok, list = pcall(value.list, value)
|
||||
if not ok or type(list) ~= "table" or #list ~= 1 then
|
||||
return value
|
||||
end
|
||||
local rok, inner = pcall(value.read, value, list[1].name)
|
||||
if not rok or type(inner) ~= "buffer" then
|
||||
return value
|
||||
end
|
||||
local e = detect(inner, nil, "archive", nil)
|
||||
if not e then
|
||||
return value
|
||||
end
|
||||
local io = table.clone(o)
|
||||
io.category = "archive"
|
||||
io.name = list[1].name
|
||||
io._depth = (o._depth or 0) + 1
|
||||
local v = load(inner, io)
|
||||
if type(v) ~= "table" or type(v.meta) ~= "table" then
|
||||
return value
|
||||
end
|
||||
v.meta.compression = formatName
|
||||
return v
|
||||
end
|
||||
|
||||
-- load(src, opts?) -> value | nil, Err
|
||||
function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
local o = opts or {}
|
||||
@@ -171,6 +207,10 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
if not buf then
|
||||
return nil, ierr
|
||||
end
|
||||
if name and o.name == nil then
|
||||
o = table.clone(o)
|
||||
o.name = name -- formats may use the file name (single-stream archives name their entry after it)
|
||||
end
|
||||
local entry, derr = detect(buf, name, o.category, o.format)
|
||||
if not entry then
|
||||
return nil, derr
|
||||
@@ -202,8 +242,9 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
return nil, Err.wrap(err or Err.new(Err.CORRUPT, "decode failed"), entry.name)
|
||||
end
|
||||
tag(value, entry.name)
|
||||
return value
|
||||
return unwrap(value, entry.name, o)
|
||||
end
|
||||
load = F.load
|
||||
|
||||
-- open(src, opts?) -> a streaming decoder (audio Decoder / VideoDecoder / Demuxer) | nil, Err
|
||||
function F.open(src: any, opts: Options?): (any, Err.Err?)
|
||||
|
||||
Reference in new issue
Block a user