added significantly larger file support to libmtar, as well as versioning support

This commit is contained in:
Izaya 2021-05-26 17:07:10 +10:00
parent 8e84eb0c67
commit 2707ecc155
1 changed files with 13 additions and 5 deletions

View File

@ -1,4 +1,8 @@
local mtar = {} local mtar = {}
local versions = {}
versions[0] = {nlf = ">I2", flf = ">I2"} -- original version format
versions[1] = {nlf = ">I2", flf = ">I8"} -- extended file size format
mtar.versions = versions
local function cleanPath(path) local function cleanPath(path)
local pt = {} local pt = {}
@ -12,8 +16,9 @@ local function cleanPath(path)
return table.concat(pt,"/") return table.concat(pt,"/")
end end
function mtar.genHeader(fname,len) -- string number -- string -- generate a header for file *fname* when provided with file length *len* function mtar.genHeader(fname,len,version) -- string number -- string -- generate a header for file *fname* when provided with file length *len*
return string.format("%s%s%s", string.pack(">I2",fname:len()), fname, string.pack(">I2",len)) version=version or 1
return string.format("\255\255%s%s%s%s", string.char(version), string.pack(versions[version].nlf,fname:len()), fname, string.pack(versions[version].flf,len))
end end
function mtar.iter(stream) -- table -- function -- Given buffer *stream*, returns an iterator suitable for use with *for* that returns, for each iteration, the file name, a function to read from the file, and the length of the file. function mtar.iter(stream) -- table -- function -- Given buffer *stream*, returns an iterator suitable for use with *for* that returns, for each iteration, the file name, a function to read from the file, and the length of the file.
@ -27,14 +32,17 @@ function mtar.iter(stream) -- table -- function -- Given buffer *stream*, return
while remain > 0 do while remain > 0 do
remain=remain-#stream:read(math.min(remain,2048)) remain=remain-#stream:read(math.min(remain,2048))
end end
local version = 0
local nlen = string.unpack(">I2", stream:read(2) or "\0\0") local nlen = string.unpack(">I2", stream:read(2) or "\0\0")
if nlen == 0 then if nlen == 0 then
return return
elseif nlen == 65535 then -- versioned header
version = string.byte(stream:read(1))
nlen = string.unpack(versions[version].nlf, stream:read(string.packsize(versions[version].nlf)))
end end
local name = cleanPath(stream:read(nlen)) local name = cleanPath(stream:read(nlen))
local fsize = string.unpack(">I2", stream:read(2)) remain = string.unpack(versions[version].flf, stream:read(string.packsize(versions[version].flf)))
remain = fsize return name, read, remain
return name, read, fsize
end end
end end