rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,172 @@
|
||||
--!strict
|
||||
-- archive/cpio: cpio archives (newc, crc, odc, old binary in both byte orders; see containers/Cpio) read into the
|
||||
-- standard Archive value (entries read lazily: a copy of the stored bytes) and written as newc (default), crc or odc
|
||||
-- from any Archive value (a tar, a zip, ...: list() + read()). Entries carry the cpio fields: type (tar letters),
|
||||
-- mode (permission bits; the type is in type), uid, gid, nlink, ino, devmajor / devminor, rdevmajor / rdevminor,
|
||||
-- link (symlinks), hardlink (the first link of a hard link group; every link reads the data). Tar hard links ("1")
|
||||
-- written as cpio share an inode with their target. Not supported: tar-only fields (uname, gname: dropped).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Archive = require("@rosetta/containers/Archive")
|
||||
local Cpio = require("@rosetta/containers/Cpio")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local CpioFormat = {
|
||||
name = "cpio",
|
||||
category = "archive",
|
||||
extensions = { "cpio" },
|
||||
mime = { "application/x-cpio" },
|
||||
deps = { "core/Err", "core/Bytes", "containers/Archive", "containers/Cpio" },
|
||||
caps = { decode = true, encode = true, variants = { "newc", "crc", "odc", "bin" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxEntries: number?, -- default 1e6
|
||||
verify: boolean?, -- crc archives: check the data sums (default true)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
variant: ("newc" | "crc" | "odc")?, -- default: the source archive's (old binary: newc), else "newc"
|
||||
format: string?, -- "newc", "crc" or "odc" here too; any other value (the api's "cpio") means the variant default
|
||||
block: number?, -- pad to a multiple of this many bytes (default 512, as GNU cpio; 1 = none)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function CpioFormat.sniff(r: Bytes.Reader): number
|
||||
local m = r:peek(6)
|
||||
if m == "070701" or m == "070702" or m == "070707" then
|
||||
return 1
|
||||
end
|
||||
local b2 = r:peek(2)
|
||||
if (b2 == "\199\113" or b2 == "\113\199") and r:remaining() >= 26 then
|
||||
return 0.5
|
||||
end
|
||||
return 0
|
||||
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 CpioFormat.encode(archive, opts)
|
||||
end
|
||||
|
||||
function CpioFormat.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local list, info = Cpio.read(r.buf, r.base, r.limit - r.base, opts)
|
||||
if not list then
|
||||
return nil, info
|
||||
end
|
||||
local a = Archive.new("cpio", encode)
|
||||
for _, e in list do
|
||||
local data = e.type == "0" and e.size > 0
|
||||
Archive.put(a, {
|
||||
name = e.name,
|
||||
size = if data then e.size else 0,
|
||||
dir = e.dir,
|
||||
mtime = e.mtime,
|
||||
mode = bit32.band(e.mode, 0xFFF),
|
||||
uid = e.uid,
|
||||
gid = e.gid,
|
||||
type = e.type,
|
||||
link = e.link,
|
||||
hardlink = e.hardlink,
|
||||
nlink = e.nlink,
|
||||
ino = e.ino,
|
||||
devmajor = e.devmajor,
|
||||
devminor = e.devminor,
|
||||
rdevmajor = e.rdevmajor,
|
||||
rdevminor = e.rdevminor,
|
||||
method = "store",
|
||||
load = if data then lazy(r.buf, e.offset, e.size) else nil,
|
||||
})
|
||||
end
|
||||
a.meta.source = { format = "cpio", variant = info.format }
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function num(v: any): number?
|
||||
return if type(v) == "number" then v else nil
|
||||
end
|
||||
|
||||
function CpioFormat.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 index: { [string]: number } = {}
|
||||
for i, it in items do
|
||||
index[it.entry.name] = index[it.entry.name] or i
|
||||
end
|
||||
-- tar hard links ("1"): the same inode as their target, which counts the links
|
||||
local links: { [number]: number } = {}
|
||||
local target: { [number]: number } = {}
|
||||
for i, it in items do
|
||||
local e: any = it.entry
|
||||
if e.type == "1" and type(e.link) == "string" and index[e.link] then
|
||||
local t = index[e.link]
|
||||
target[i] = t
|
||||
links[t] = (links[t] or 1) + 1
|
||||
end
|
||||
end
|
||||
local list: { Cpio.WriteEntry } = {}
|
||||
for i, it in items do
|
||||
local e: any = it.entry
|
||||
local typ = if type(e.type) == "string" and Cpio.TYPE_MODE[e.type] then e.type elseif e.dir then "5" else "0"
|
||||
local src = target[i]
|
||||
local ino, nlink = num(e.ino), num(e.nlink)
|
||||
if src then
|
||||
typ, ino, nlink = "0", src, links[src]
|
||||
elseif links[i] then
|
||||
ino, nlink = i, links[i]
|
||||
end
|
||||
local data: buffer? = nil
|
||||
if typ == "0" then
|
||||
local b, err = (items[src or i] :: any).read()
|
||||
if not b then
|
||||
return nil, err
|
||||
end
|
||||
data = b
|
||||
end
|
||||
local mode = num(e.mode)
|
||||
table.insert(list, {
|
||||
name = e.name,
|
||||
data = data,
|
||||
type = typ,
|
||||
link = if type(e.link) == "string" and typ == "2" then e.link else nil,
|
||||
mode = if mode then bit32.band(mode, 0xFFF) else nil,
|
||||
uid = num(e.uid),
|
||||
gid = num(e.gid),
|
||||
nlink = nlink,
|
||||
mtime = num(e.mtime),
|
||||
ino = ino,
|
||||
devmajor = num(e.devmajor),
|
||||
devminor = num(e.devminor),
|
||||
rdevmajor = num(e.rdevmajor),
|
||||
rdevminor = num(e.rdevminor),
|
||||
})
|
||||
end
|
||||
-- opts.format may also be the api's format name ("cpio"): the variant default then
|
||||
local o: any = opts or {}
|
||||
local src = type(archive.meta) == "table" and type(archive.meta.source) == "table" and archive.meta.source
|
||||
local own = if src and src.format == "cpio" and (src.variant == "crc" or src.variant == "odc") then src.variant else "newc"
|
||||
local variant = o.variant or (if o.format == "crc" or o.format == "odc" or o.format == "newc" then o.format else own)
|
||||
return Cpio.write(list, { format = variant, block = o.block })
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty cpio Archive: a:add(name, data, { mtime, mode, dir }); a:write()
|
||||
function CpioFormat.new(): any
|
||||
local a = Archive.new("cpio", encode)
|
||||
a.meta.source = { format = "cpio" }
|
||||
return a
|
||||
end
|
||||
|
||||
return CpioFormat
|
||||
Reference in new issue
Block a user