ws-arch2-unix: containers/Iso9660 (volume descriptors, directory records, multi-extent, Joliet, Rock Ridge SUSP / RRIP incl. CE, NM, PX, PN, TF, SL, CL / RE relocation, El Torito catalog) + archive/iso, tests vs xorriso / genisoimage images, the source tree and pycdlib
This commit is contained in:
1 parent
6eadacce64
commit
197ddc561a
6 files changed
+942
No files matched your search
@@ -614,6 +614,18 @@ local list: { Entry } = {
|
||||
return require("@formats/archive/gzip")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "iso",
|
||||
category = "archive",
|
||||
path = "archive/iso",
|
||||
extensions = { "iso" },
|
||||
mime = { "application/x-iso9660-image" },
|
||||
deps = { "core/Err", "core/Bytes", "containers/Archive", "containers/Iso9660" },
|
||||
caps = { decode = true, encode = false, extensions = { "joliet", "rockridge", "eltorito" } },
|
||||
load = function()
|
||||
return require("@formats/archive/iso")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "rpm",
|
||||
category = "archive",
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
--!strict
|
||||
-- archive/iso: ISO 9660 CD / DVD images (.iso) read into the standard Archive value through containers/Iso9660:
|
||||
-- plain ISO 9660 names, Joliet and Rock Ridge (names, modes, owners, times, symlinks, relocated deep directories;
|
||||
-- Rock Ridge is used when present, else Joliet, else the plain names: opts.prefer picks another), multi-extent files.
|
||||
-- Directories are dir entries; file data reads lazily (a copy of the extents). meta.iso holds the volume facts
|
||||
-- (volumeId, systemId, publisher, blockSize, blocks, created, joliet level, rockRidge, tree) and meta.boot the El
|
||||
-- Torito boot catalog (platform, bootable, media, load segment, sector count, image LBA) when there is one.
|
||||
-- Not supported: writing, UDF, interleaved files (read: Err.UNSUPPORTED), zisofs-compressed files (read: the stored
|
||||
-- bytes), multi-session images (the first session).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Archive = require("@formats/containers/Archive")
|
||||
local Iso9660 = require("@formats/containers/Iso9660")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Iso = {
|
||||
name = "iso",
|
||||
category = "archive",
|
||||
extensions = { "iso" },
|
||||
mime = { "application/x-iso9660-image" },
|
||||
deps = { "core/Err", "core/Bytes", "containers/Archive", "containers/Iso9660" },
|
||||
caps = { decode = true, encode = false, extensions = { "joliet", "rockridge", "eltorito" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
prefer: ("rockridge" | "joliet" | "iso9660")?, -- the name set (default: Rock Ridge, else Joliet, else plain)
|
||||
maxEntries: number?, -- default 1e6
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Iso.sniff(r: Bytes.Reader): number
|
||||
return if r:peek(5, 16 * 2048 + 1) == "CD001" then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Iso.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local list, info = Iso9660.read(r.buf, r.base, r.limit - r.base, opts)
|
||||
if not list then
|
||||
return nil, info
|
||||
end
|
||||
local a = Archive.new("iso")
|
||||
local b, limit = r.buf, r.limit
|
||||
for _, e in list do
|
||||
local data = e.type == "0" and e.size > 0
|
||||
Archive.put(a, {
|
||||
name = e.name,
|
||||
size = e.size,
|
||||
dir = e.dir,
|
||||
mtime = e.mtime,
|
||||
type = e.type,
|
||||
mode = e.mode,
|
||||
uid = e.uid,
|
||||
gid = e.gid,
|
||||
nlink = e.nlink,
|
||||
link = e.link,
|
||||
rdev = e.rdev,
|
||||
hidden = e.hidden,
|
||||
method = if e.zisofs then "zisofs" else "store",
|
||||
load = if data
|
||||
then function(): (buffer?, Err.Err?)
|
||||
return Iso9660.extract(b, e, limit)
|
||||
end
|
||||
else nil,
|
||||
})
|
||||
end
|
||||
a.meta.boot = info.boot
|
||||
info.boot = nil
|
||||
a.meta.iso = info
|
||||
a.meta.source = { format = "iso", tree = info.tree }
|
||||
return a, nil
|
||||
end
|
||||
|
||||
return Iso
|
||||
@@ -0,0 +1,525 @@
|
||||
--!strict
|
||||
-- containers/Iso9660: ISO 9660 (ECMA-119) CD / DVD images, read. The volume descriptors from sector 16 (primary,
|
||||
-- supplementary, boot record, terminator); the directory tree walked through directory records (records never cross
|
||||
-- a sector; multi-extent files are joined; associated files skipped; interleaved files listed, their read is
|
||||
-- Err.UNSUPPORTED). Three name sets: plain ISO 9660 (";1" versions and a lone trailing "." dropped), Joliet (UCS-2 names
|
||||
-- of a supplementary descriptor with the %/@, %/C or %/E escape) and Rock Ridge (SUSP 1.12 / RRIP 1.12 in the
|
||||
-- primary tree: SP, CE continuation areas, NM names, PX modes / links / ids, PN devices, TF times, SL symlinks, CL / RE
|
||||
-- / PL relocated deep directories, with the rr_moved directory hidden when it ends up empty). The tree used is Rock
|
||||
-- Ridge when present, else Joliet, else plain (opts.prefer overrides). El Torito: the boot record's catalog
|
||||
-- (validation entry, initial / default entry) in info.boot. Written from ECMA-119, the SUSP / RRIP 1.12 and Joliet
|
||||
-- specifications and El Torito 1.0; no code ported. Not supported: zisofs (ZF) compressed files (listed; read returns
|
||||
-- the stored bytes), UDF, writing.
|
||||
--
|
||||
-- local entries, info = Iso9660.read(buf, offset?, length?, opts?) -- { Entry }, Info | nil, Err
|
||||
-- local data, err = Iso9660.extract(buf, base, entry) -- the entry's bytes (a copy of its extents)
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Text = require("@formats/codecs/text/Text")
|
||||
|
||||
local Iso9660 = {}
|
||||
|
||||
export type Extent = { at: number, size: number } -- byte offset (from the image start) and length
|
||||
export type Entry = {
|
||||
name: string, -- the path, "/" separated, no leading "/"
|
||||
dir: boolean,
|
||||
size: number,
|
||||
extents: { Extent },
|
||||
mtime: number?, -- unix seconds
|
||||
type: string, -- tar letters: "0" file, "5" directory, "2" symlink, "3" / "4" devices, "6" fifo, "s" socket
|
||||
mode: number?, -- Rock Ridge: the permission bits
|
||||
uid: number?,
|
||||
gid: number?,
|
||||
nlink: number?,
|
||||
link: string?, -- Rock Ridge symlink target
|
||||
rdev: number?, -- Rock Ridge PN: high * 2^32 + low
|
||||
hidden: boolean?,
|
||||
interleaved: boolean?,
|
||||
zisofs: boolean?,
|
||||
}
|
||||
export type ReadOptions = {
|
||||
prefer: ("rockridge" | "joliet" | "iso9660")?, -- the name set (default: Rock Ridge, else Joliet, else plain)
|
||||
maxEntries: number?, -- default 1e6
|
||||
}
|
||||
|
||||
local SECTOR = 2048
|
||||
local S_TYPES: { [number]: string } = { [0x8000] = "0", [0x4000] = "5", [0xA000] = "2", [0x2000] = "3", [0x6000] = "4", [0x1000] = "6", [0xC000] = "s" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fields
|
||||
local function u16(b: buffer, o: number): number
|
||||
return buffer.readu16(b, o)
|
||||
end
|
||||
local function u32(b: buffer, o: number): number
|
||||
return buffer.readu32(b, o)
|
||||
end
|
||||
local function str(b: buffer, o: number, n: number): string
|
||||
return (string.gsub(buffer.readstring(b, o, n), "[ %z]+$", ""))
|
||||
end
|
||||
|
||||
-- a 7-byte directory record date -> unix seconds
|
||||
local function recDate(b: buffer, o: number): number?
|
||||
local y = buffer.readu8(b, o)
|
||||
local m, d = buffer.readu8(b, o + 1), buffer.readu8(b, o + 2)
|
||||
if m < 1 or m > 12 or d < 1 or d > 31 then
|
||||
return nil
|
||||
end
|
||||
local off = buffer.readi8(b, o + 6)
|
||||
return Types.Date.toUnix({ kind = "datetime", year = 1900 + y, month = m, day = d, hour = buffer.readu8(b, o + 3), minute = buffer.readu8(b, o + 4), second = buffer.readu8(b, o + 5), offset = off * 15 })
|
||||
end
|
||||
|
||||
-- a 17-byte descriptor date ("YYYYMMDDHHMMSScc" + offset) -> unix seconds
|
||||
local function longDate(b: buffer, o: number): number?
|
||||
local s = buffer.readstring(b, o, 16)
|
||||
local y, mo, d, h, mi, se = string.match(s, "^(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)(%d%d)%d%d$")
|
||||
if not y or tonumber(y) == 0 then
|
||||
return nil
|
||||
end
|
||||
local off = buffer.readi8(b, o + 16)
|
||||
return Types.Date.toUnix({ kind = "datetime", year = tonumber(y), month = tonumber(mo), day = tonumber(d), hour = tonumber(h), minute = tonumber(mi), second = tonumber(se), offset = off * 15 })
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ SUSP / Rock Ridge
|
||||
type Susp = { [string]: any }
|
||||
|
||||
-- the SUSP entries of a system use area (following CE continuation areas) -> { { sig, at, len } }
|
||||
local function suspEntries(b: buffer, base: number, limit: number, o: number, e: number, bs: number, budget: { number }): { { any } }
|
||||
local out = {}
|
||||
while o + 4 <= e do
|
||||
local sig = buffer.readstring(b, o, 2)
|
||||
local len = buffer.readu8(b, o + 2)
|
||||
if len < 4 or o + len > e then
|
||||
break
|
||||
end
|
||||
if sig == "ST" then
|
||||
break
|
||||
elseif sig == "CE" and len >= 28 then
|
||||
local blk, off, n = u32(b, o + 4), u32(b, o + 12), u32(b, o + 20)
|
||||
local at = base + blk * bs + off
|
||||
budget[1] -= 1
|
||||
if budget[1] < 0 or at < base or at + n > limit or n > 65536 then
|
||||
break
|
||||
end
|
||||
-- the rest of this area first, then the continuation (it holds what did not fit)
|
||||
local rest = suspEntries(b, base, limit, o + len, e, bs, budget)
|
||||
for _, x in rest do
|
||||
table.insert(out, x)
|
||||
end
|
||||
local cont = suspEntries(b, base, limit, at, at + n, bs, budget)
|
||||
for _, x in cont do
|
||||
table.insert(out, x)
|
||||
end
|
||||
return out
|
||||
end
|
||||
table.insert(out, { sig, o, len })
|
||||
o += len
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- Rock Ridge fields of a record: { name, mode, uid, gid, nlink, mtime, link, rdev, child, relocated, zisofs }
|
||||
local function rockRidge(b: buffer, base: number, limit: number, o: number, e: number, bs: number): Susp
|
||||
local rr: Susp = {}
|
||||
local name: { string }? = nil
|
||||
local slParts: { string }? = nil
|
||||
local slRoot = false
|
||||
local slCont = false
|
||||
for _, x in suspEntries(b, base, limit, o, e, bs, { 32 }) do
|
||||
local sig, at, len = x[1], x[2], x[3]
|
||||
if sig == "NM" and len >= 5 then
|
||||
local flags = buffer.readu8(b, at + 4)
|
||||
if bit32.band(flags, 6) == 0 then
|
||||
name = name or {}
|
||||
table.insert(name :: { string }, buffer.readstring(b, at + 5, len - 5))
|
||||
end
|
||||
elseif sig == "PX" and len >= 36 then
|
||||
rr.mode, rr.nlink, rr.uid, rr.gid = u32(b, at + 4), u32(b, at + 12), u32(b, at + 20), u32(b, at + 28)
|
||||
elseif sig == "PN" and len >= 20 then
|
||||
rr.rdev = u32(b, at + 4) * 4294967296 + u32(b, at + 12)
|
||||
elseif sig == "TF" and len >= 5 then
|
||||
local flags = buffer.readu8(b, at + 4)
|
||||
local w = if bit32.band(flags, 0x80) ~= 0 then 17 else 7
|
||||
local q = at + 5
|
||||
for bit = 0, 6 do
|
||||
if bit32.band(flags, bit32.lshift(1, bit)) ~= 0 then
|
||||
if q + w > at + len then
|
||||
break
|
||||
end
|
||||
if bit == 1 then
|
||||
rr.mtime = if w == 17 then longDate(b, q) else recDate(b, q)
|
||||
end
|
||||
q += w
|
||||
end
|
||||
end
|
||||
elseif sig == "SL" and len >= 5 then
|
||||
slParts = slParts or {}
|
||||
local parts = slParts :: { string }
|
||||
local q = at + 5
|
||||
while q + 2 <= at + len do
|
||||
local cf, cl = buffer.readu8(b, q), buffer.readu8(b, q + 1)
|
||||
if q + 2 + cl > at + len then
|
||||
break
|
||||
end
|
||||
local piece = if bit32.band(cf, 2) ~= 0 then "." elseif bit32.band(cf, 4) ~= 0 then ".." else buffer.readstring(b, q + 2, cl)
|
||||
if bit32.band(cf, 8) ~= 0 then
|
||||
slRoot = #parts == 0
|
||||
piece = ""
|
||||
end
|
||||
if slCont and #parts > 0 then
|
||||
parts[#parts] ..= piece
|
||||
elseif bit32.band(cf, 8) == 0 or #parts > 0 then
|
||||
table.insert(parts, piece)
|
||||
end
|
||||
slCont = bit32.band(cf, 1) ~= 0
|
||||
q += 2 + cl
|
||||
end
|
||||
elseif sig == "CL" and len >= 12 then
|
||||
rr.child = u32(b, at + 4)
|
||||
elseif sig == "RE" then
|
||||
rr.relocated = true
|
||||
elseif sig == "ZF" then
|
||||
rr.zisofs = true
|
||||
end
|
||||
end
|
||||
if name then
|
||||
rr.name = table.concat(name)
|
||||
end
|
||||
if slParts then
|
||||
rr.link = (if slRoot then "/" else "") .. table.concat(slParts, "/")
|
||||
end
|
||||
return rr
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ directories
|
||||
type Record = {
|
||||
name: string,
|
||||
dir: boolean,
|
||||
lba: number,
|
||||
size: number,
|
||||
ear: number,
|
||||
flags: number,
|
||||
interleave: boolean,
|
||||
mtime: number?,
|
||||
rr: Susp?,
|
||||
}
|
||||
|
||||
-- the records of a directory extent (without "." and "..") | nil, Err
|
||||
local function records(b: buffer, base: number, limit: number, at: number, size: number, bs: number, joliet: boolean, rrSkip: number?): ({ Record }?, Err.Err?)
|
||||
if at + size > limit then
|
||||
return nil, Err.new(Err.TRUNCATED, "directory past the end", at)
|
||||
end
|
||||
local out: { Record } = {}
|
||||
local p = at
|
||||
local e = at + size
|
||||
while p < e do
|
||||
local len = buffer.readu8(b, p)
|
||||
if len == 0 then
|
||||
p = at + ((p - at) // SECTOR + 1) * SECTOR -- the rest of the sector is padding
|
||||
continue
|
||||
end
|
||||
if len < 34 or p + len > e then
|
||||
return nil, Err.new(Err.CORRUPT, "bad directory record", p)
|
||||
end
|
||||
local nl = buffer.readu8(b, p + 32)
|
||||
if 33 + nl > len then
|
||||
return nil, Err.new(Err.CORRUPT, "bad name length", p + 32)
|
||||
end
|
||||
local raw = buffer.readstring(b, p + 33, nl)
|
||||
if raw ~= "\0" and raw ~= "\1" then
|
||||
local flags = buffer.readu8(b, p + 25)
|
||||
local name
|
||||
if joliet then
|
||||
name = Text.utf16(b, p + 33, nl, true)
|
||||
else
|
||||
name = raw
|
||||
end
|
||||
name = string.gsub(name, ";%d*$", "")
|
||||
if not joliet and bit32.band(flags, 2) == 0 then
|
||||
name = string.gsub(name, "%.$", "")
|
||||
end
|
||||
local r: Record = {
|
||||
name = name,
|
||||
dir = bit32.band(flags, 2) ~= 0,
|
||||
lba = u32(b, p + 2),
|
||||
size = u32(b, p + 10),
|
||||
ear = buffer.readu8(b, p + 1),
|
||||
flags = flags,
|
||||
interleave = buffer.readu8(b, p + 26) ~= 0 or buffer.readu8(b, p + 27) ~= 0,
|
||||
mtime = recDate(b, p + 18),
|
||||
rr = nil,
|
||||
}
|
||||
if rrSkip then
|
||||
local su = p + 33 + nl + (if nl % 2 == 0 then 1 else 0) + rrSkip
|
||||
r.rr = rockRidge(b, base, limit, su, p + len, bs)
|
||||
end
|
||||
table.insert(out, r)
|
||||
end
|
||||
p += len
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- the root record of a volume descriptor at vd -> lba, size
|
||||
local function root(b: buffer, vd: number): (number, number)
|
||||
return u32(b, vd + 156 + 2), u32(b, vd + 156 + 10)
|
||||
end
|
||||
|
||||
-- Rock Ridge in use: the root's "." record starts its system use area with SP (BE EF); its skip length
|
||||
local function rockRidgeSkip(b: buffer, base: number, limit: number, lba: number, bs: number): number?
|
||||
local p = base + lba * bs
|
||||
if p + 34 > limit then
|
||||
return nil
|
||||
end
|
||||
local len = buffer.readu8(b, p)
|
||||
local nl = buffer.readu8(b, p + 32)
|
||||
local su = p + 33 + nl + (if nl % 2 == 0 then 1 else 0)
|
||||
if len < 34 or su + 7 > p + len or p + len > limit then
|
||||
return nil
|
||||
end
|
||||
if buffer.readstring(b, su, 2) == "SP" and buffer.readu8(b, su + 4) == 0xBE and buffer.readu8(b, su + 5) == 0xEF then
|
||||
return buffer.readu8(b, su + 6)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ El Torito
|
||||
local function boot(b: buffer, base: number, limit: number, vd: number, bs: number): { [string]: any }?
|
||||
if str(b, vd + 7, 32) ~= "EL TORITO SPECIFICATION" then
|
||||
return nil
|
||||
end
|
||||
local cat = u32(b, vd + 0x47)
|
||||
local p = base + cat * bs
|
||||
local info: { [string]: any } = { catalog = cat }
|
||||
if p + 64 > limit or buffer.readu8(b, p) ~= 1 or buffer.readu8(b, p + 30) ~= 0x55 or buffer.readu8(b, p + 31) ~= 0xAA then
|
||||
return info
|
||||
end
|
||||
local sum = 0
|
||||
for i = 0, 30, 2 do
|
||||
sum += u16(b, p + i)
|
||||
end
|
||||
info.valid = sum % 65536 == 0
|
||||
info.platform = buffer.readu8(b, p + 1)
|
||||
info.id = str(b, p + 4, 24)
|
||||
local d = p + 32
|
||||
info.bootable = buffer.readu8(b, d) == 0x88
|
||||
info.media = buffer.readu8(b, d + 1) -- 0 no emulation, 1..3 floppies, 4 hard disk
|
||||
info.loadSegment = u16(b, d + 2)
|
||||
info.systemType = buffer.readu8(b, d + 4)
|
||||
info.sectors = u16(b, d + 6) -- 512-byte virtual sectors
|
||||
info.lba = u32(b, d + 8)
|
||||
return info
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ read
|
||||
-- read(buf, offset?, length?, opts?) -> { Entry }, Info | nil, Err
|
||||
function Iso9660.read(b: buffer, offset: number?, length: number?, opts: ReadOptions?): ({ Entry }?, any)
|
||||
local base = offset or 0
|
||||
local limit = math.min(buffer.len(b), base + (length or (buffer.len(b) - base)))
|
||||
local o: ReadOptions = opts or {}
|
||||
local maxEntries = o.maxEntries or 1e6
|
||||
local pvd: number? = nil
|
||||
local svd: number? = nil
|
||||
local jolietLevel: number? = nil
|
||||
local bootInfo = nil
|
||||
local p = base + 16 * SECTOR
|
||||
for _ = 1, 64 do
|
||||
if p + SECTOR > limit then
|
||||
return nil, Err.new(Err.TRUNCATED, "volume descriptors past the end", p)
|
||||
end
|
||||
if buffer.readstring(b, p + 1, 5) ~= "CD001" then
|
||||
return nil, Err.new(Err.CORRUPT, "bad volume descriptor", p)
|
||||
end
|
||||
local t = buffer.readu8(b, p)
|
||||
if t == 255 then
|
||||
break
|
||||
elseif t == 1 and not pvd then
|
||||
pvd = p
|
||||
elseif t == 2 then
|
||||
local esc = buffer.readstring(b, p + 88, 3)
|
||||
if esc == "%/@" or esc == "%/C" or esc == "%/E" then
|
||||
svd = p
|
||||
jolietLevel = if esc == "%/@" then 1 elseif esc == "%/C" then 2 else 3
|
||||
end
|
||||
elseif t == 0 then
|
||||
bootInfo = bootInfo or { vd = p }
|
||||
end
|
||||
p += SECTOR
|
||||
end
|
||||
if not pvd then
|
||||
return nil, Err.new(Err.CORRUPT, "no primary volume descriptor", base + 16 * SECTOR)
|
||||
end
|
||||
local pv = pvd :: number
|
||||
local bs = u16(b, pv + 128)
|
||||
if bs ~= 512 and bs ~= 1024 and bs ~= 2048 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad logical block size " .. bs, pv + 128)
|
||||
end
|
||||
local info: { [string]: any } = {
|
||||
blockSize = bs,
|
||||
blocks = u32(b, pv + 80),
|
||||
systemId = str(b, pv + 8, 32),
|
||||
volumeId = str(b, pv + 40, 32),
|
||||
volumeSet = str(b, pv + 190, 128),
|
||||
publisher = str(b, pv + 318, 128),
|
||||
preparer = str(b, pv + 446, 128),
|
||||
application = str(b, pv + 574, 128),
|
||||
created = longDate(b, pv + 813),
|
||||
modified = longDate(b, pv + 830),
|
||||
joliet = jolietLevel,
|
||||
}
|
||||
if bootInfo then
|
||||
info.boot = boot(b, base, limit, bootInfo.vd, bs)
|
||||
end
|
||||
local rlba, rsize = root(b, pv)
|
||||
local rrSkip = rockRidgeSkip(b, base, limit, rlba, bs)
|
||||
info.rockRidge = rrSkip ~= nil
|
||||
local tree = o.prefer or (if rrSkip then "rockridge" elseif svd then "joliet" else "iso9660")
|
||||
if tree == "joliet" and not svd then
|
||||
tree = "iso9660"
|
||||
elseif tree == "rockridge" and not rrSkip then
|
||||
tree = "iso9660"
|
||||
end
|
||||
info.tree = tree
|
||||
local joliet = tree == "joliet"
|
||||
local useRR = if tree == "rockridge" then rrSkip else nil
|
||||
if joliet then
|
||||
rlba, rsize = root(b, svd :: number)
|
||||
end
|
||||
if tree == "joliet" then
|
||||
info.volumeId = (string.gsub(Text.utf16(b, (svd :: number) + 40, 32, true), "[ %z]+$", ""))
|
||||
end
|
||||
local out: { Entry } = {}
|
||||
local visited: { [number]: boolean } = {}
|
||||
local stack = { { lba = rlba, size = rsize, path = "" } }
|
||||
local relocatedDirs: { [string]: boolean } = {}
|
||||
while #stack > 0 do
|
||||
local d = table.remove(stack) :: any
|
||||
if visited[d.lba] then
|
||||
continue
|
||||
end
|
||||
visited[d.lba] = true
|
||||
local list, err = records(b, base, limit, base + d.lba * bs, d.size, bs, joliet, useRR)
|
||||
if not list then
|
||||
return nil, err
|
||||
end
|
||||
local i = 1
|
||||
local subdirs = {}
|
||||
while i <= #list do
|
||||
local r = list[i]
|
||||
-- multi-extent: this record and the following ones of the same name
|
||||
local extents: { Extent } = { { at = base + (r.lba + r.ear) * bs, size = r.size } }
|
||||
local total = r.size
|
||||
local last = r
|
||||
while bit32.band(last.flags, 0x80) ~= 0 and i < #list and list[i + 1].name == r.name do
|
||||
i += 1
|
||||
last = list[i]
|
||||
table.insert(extents, { at = base + (last.lba + last.ear) * bs, size = last.size })
|
||||
total += last.size
|
||||
end
|
||||
i += 1
|
||||
if bit32.band(r.flags, 4) ~= 0 then
|
||||
continue -- an associated file (resource fork)
|
||||
end
|
||||
local rr: any = r.rr
|
||||
if rr and rr.relocated then
|
||||
continue -- shown where its CL placeholder is
|
||||
end
|
||||
local name = if rr and rr.name then rr.name else r.name
|
||||
local path = d.path .. name
|
||||
local isDir = r.dir
|
||||
local lba: number, size: number = r.lba, r.size
|
||||
if rr and rr.child then
|
||||
-- a deep directory relocated by Rock Ridge: the placeholder points at it; its size is its "." record's
|
||||
isDir = true
|
||||
lba = rr.child
|
||||
local q = base + lba * bs
|
||||
size = if q + 14 <= limit then u32(b, q + 10) else 0
|
||||
end
|
||||
local typ = if isDir then "5" else "0"
|
||||
if rr and rr.mode then
|
||||
typ = S_TYPES[bit32.band(rr.mode, 0xF000)] or typ
|
||||
end
|
||||
local e: Entry = {
|
||||
name = path,
|
||||
dir = isDir,
|
||||
size = if isDir or typ ~= "0" then 0 else total,
|
||||
extents = if isDir or typ ~= "0" then {} else extents,
|
||||
mtime = if rr and rr.mtime then rr.mtime else r.mtime,
|
||||
type = typ,
|
||||
mode = if rr and rr.mode then bit32.band(rr.mode, 0xFFF) else nil,
|
||||
uid = rr and rr.uid,
|
||||
gid = rr and rr.gid,
|
||||
nlink = rr and rr.nlink,
|
||||
link = if rr and typ == "2" then rr.link else nil,
|
||||
rdev = rr and rr.rdev,
|
||||
hidden = if bit32.band(r.flags, 1) ~= 0 then true else nil,
|
||||
interleaved = if r.interleave then true else nil,
|
||||
zisofs = if rr and rr.zisofs then true else nil,
|
||||
}
|
||||
table.insert(out, e)
|
||||
if #out > maxEntries then
|
||||
return nil, Err.new(Err.LIMIT, "more than " .. maxEntries .. " entries")
|
||||
end
|
||||
if isDir then
|
||||
table.insert(subdirs, { lba = lba, size = size, path = path .. "/" })
|
||||
end
|
||||
end
|
||||
for k = #subdirs, 1, -1 do
|
||||
table.insert(stack, subdirs[k])
|
||||
end
|
||||
if useRR then
|
||||
for _, r in list do
|
||||
if r.rr and r.rr.relocated then
|
||||
relocatedDirs[d.path] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- rr_moved (genisoimage's home of relocated directories): hidden when nothing else is in it
|
||||
if useRR then
|
||||
for dirPath in relocatedDirs do
|
||||
local name = string.sub(dirPath, 1, -2)
|
||||
local holds = false
|
||||
for _, e in out do
|
||||
if string.sub(e.name, 1, #dirPath) == dirPath then
|
||||
holds = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if not holds then
|
||||
for k, e in out do
|
||||
if e.name == name then
|
||||
table.remove(out, k)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- archive order: depth-first walk order is kept; directories sorted as the image stores them
|
||||
return out, info
|
||||
end
|
||||
|
||||
-- extract(buf, entry, limit?) -> a copy of the entry's data | nil, Err
|
||||
function Iso9660.extract(b: buffer, e: Entry, limit: number?): (buffer?, Err.Err?)
|
||||
if e.interleaved then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "interleaved file " .. e.name)
|
||||
end
|
||||
local lim = limit or buffer.len(b)
|
||||
local out = buffer.create(e.size)
|
||||
local q = 0
|
||||
for _, x in e.extents do
|
||||
if x.at < 0 or x.at + x.size > lim then
|
||||
return nil, Err.new(Err.TRUNCATED, "file " .. e.name .. " data past the end", x.at)
|
||||
end
|
||||
buffer.copy(out, q, b, x.at, x.size)
|
||||
q += x.size
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- valid(buf, offset?) -> true when a volume descriptor "CD001" sits at sector 16
|
||||
function Iso9660.valid(b: buffer, offset: number?): boolean
|
||||
local p = (offset or 0) + 16 * SECTOR
|
||||
return p + 6 <= buffer.len(b) and buffer.readstring(b, p + 1, 5) == "CD001"
|
||||
end
|
||||
|
||||
return Iso9660
|
||||
Reference in new issue
Block a user