266 lines
7.7 KiB
Luau
266 lines
7.7 KiB
Luau
--!strict
|
|
-- containers/Archive: the in-memory Archive value (Types.Archive) archive formats return. Entries hold their data or
|
|
-- a lazy loader; write() calls the encoder the format attached, and any Archive can be encoded by any archive writer.
|
|
-- 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("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Tar = require("@rosetta/containers/Tar")
|
|
|
|
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
|
|
|
|
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) :: { Types.ArchiveEntry }
|
|
for i, e in self._entries do
|
|
out[i] = public(e)
|
|
end
|
|
return out
|
|
end
|
|
|
|
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, nil
|
|
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, nil
|
|
end
|
|
return buffer.create(0), nil
|
|
end
|
|
|
|
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
|
|
|
|
function Methods.write(self: any, opts: any?): (buffer?, Err.Err?)
|
|
local enc: Encoder? = self._encode
|
|
if not enc then
|
|
return nil, Err.new(Err.UNSUPPORTED, self.format .. " archives are read-only")
|
|
end
|
|
return enc(self, opts)
|
|
end
|
|
|
|
function Archive.new(format: string, encode: Encoder?): any
|
|
return setmetatable({
|
|
kind = "archive",
|
|
format = format,
|
|
meta = {},
|
|
_entries = {} :: { Entry },
|
|
_byName = {} :: { [string]: Entry },
|
|
_encode = encode,
|
|
}, Methods)
|
|
end
|
|
|
|
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
|
|
|
|
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
|
|
|
|
function Archive.slice(buf: buffer, at: number, n: number): () -> (buffer?, Err.Err?)
|
|
return function(): (buffer?, Err.Err?)
|
|
local out = buffer.create(n)
|
|
buffer.copy(out, 0, buf, at, n)
|
|
return out, nil
|
|
end
|
|
end
|
|
|
|
function Archive.collect(value: any): ({ { entry: Types.ArchiveEntry, data: buffer } }?, Err.Err?)
|
|
local items = Archive.entries(value)
|
|
if not items then
|
|
return nil, Err.new(Err.ARG, "expected an Archive")
|
|
end
|
|
local out: { { entry: Types.ArchiveEntry, data: buffer } } = {}
|
|
for _, it in items do
|
|
local b: buffer = buffer.create(0)
|
|
if not it.entry.dir then
|
|
local d, err = it.read()
|
|
if not d then
|
|
return nil, err or Err.new(Err.CORRUPT, "entry " .. it.entry.name .. ": read failed")
|
|
end
|
|
b = d
|
|
end
|
|
table.insert(out, { entry = it.entry, data = b })
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
function Archive.files(value: any, name: string?): ({ { name: string, data: buffer, entry: any } }?, Err.Err?)
|
|
if type(value) == "buffer" or type(value) == "string" then
|
|
local b = if type(value) == "string" then buffer.fromstring(value) else value
|
|
return { { name = name or "data", data = b, entry = { name = name or "data", size = buffer.len(b), dir = false } } }, nil
|
|
end
|
|
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 out = {}
|
|
for _, it in items do
|
|
if not it.entry.dir then
|
|
local b, err = it.read()
|
|
if not b then
|
|
return nil, err
|
|
end
|
|
table.insert(out, { name = it.entry.name, data = b, entry = it.entry })
|
|
end
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
function Archive.streamData(value: any): (buffer?, Err.Err?)
|
|
if type(value) == "buffer" then
|
|
return value, nil
|
|
elseif type(value) == "string" then
|
|
return buffer.fromstring(value), nil
|
|
end
|
|
local items = Archive.entries(value)
|
|
if not items then
|
|
return nil, Err.new(Err.ARG, "expected an Archive, a buffer or a string")
|
|
end
|
|
if #items == 1 and not items[1].entry.dir then
|
|
return items[1].read()
|
|
end
|
|
local list = {}
|
|
for _, it in items do
|
|
local e: any = it.entry
|
|
local data: buffer? = nil
|
|
if not e.dir and (e.type == nil or e.type == "0") then
|
|
local b, err = it.read()
|
|
if not b then
|
|
return nil, err
|
|
end
|
|
data = b
|
|
end
|
|
local typ = if type(e.type) == "string" and #e.type == 1 then e.type else nil
|
|
table.insert(list, { name = e.name, data = data, dir = e.dir, type = typ, link = e.link, mtime = e.mtime, mode = e.mode })
|
|
end
|
|
return Tar.write(list)
|
|
end
|
|
|
|
-- MS-DOS date / time: stamp = date * 65536 + time (LHA / ARJ / ZIP); local time without a zone, read as UTC
|
|
function Archive.fromDos(stamp: number): number?
|
|
local time, date = stamp % 65536, stamp // 65536
|
|
local y, m, d = 1980 + bit32.rshift(date, 9), bit32.band(bit32.rshift(date, 5), 15), bit32.band(date, 31)
|
|
if date == 0 or m < 1 or m > 12 or d < 1 then
|
|
return nil
|
|
end
|
|
local secs = bit32.rshift(time, 11) * 3600 + bit32.band(bit32.rshift(time, 5), 63) * 60 + bit32.band(time, 31) * 2
|
|
return Types.Date.daysFromCivil(y, m, d) * 86400 + secs
|
|
end
|
|
|
|
-- MS-DOS dates cover 1980-01-01 .. 2107-12-31
|
|
function Archive.toDos(t: number?): number
|
|
if type(t) ~= "number" or t ~= t or t < 315532800 then
|
|
return 0x210000 -- 1980-01-01 00:00
|
|
end
|
|
local s = math.floor(t)
|
|
local days = s // 86400
|
|
local rem = s - days * 86400
|
|
local y, m, d = Types.Date.civilFromDays(days)
|
|
if y > 2107 then
|
|
y, m, d, rem = 2107, 12, 31, 86398
|
|
end
|
|
return ((y - 1980) * 512 + m * 32 + d) * 65536 + (rem // 3600) * 2048 + ((rem // 60) % 60) * 32 + (rem % 60) // 2
|
|
end
|
|
|
|
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
|