forked from izaya/OC-PsychOS2
115 lines
2.4 KiB
Lua
115 lines
2.4 KiB
Lua
local mtar = require "libmtar"
|
|
local w, lz16 = pcall(require, "liblz16")
|
|
if not w then lz16 = nil end
|
|
|
|
pkgfs = {}
|
|
local findex = {}
|
|
local handles = {}
|
|
local hc = 0
|
|
|
|
local function rfalse()
|
|
return false
|
|
end
|
|
local function rzero()
|
|
return 0
|
|
end
|
|
|
|
pkgfs.component = {seek = rfalse, makeDirectory = rfalse, write = rfalse, rename = rfalse, setlabel = rfalse, spaceUsed = rzero, spaceTotal = rzero, lastModified = rzero}
|
|
|
|
local function fopen(path,comp)
|
|
local f
|
|
if comp and lz16 then
|
|
f = lz16.open(path,"rb")
|
|
else
|
|
f = io.open(path,"rb")
|
|
end
|
|
return f
|
|
end
|
|
|
|
local function fnormalize(s)
|
|
return table.concat(fs.segments(s),"/")
|
|
end
|
|
|
|
function pkgfs.component.list(path)
|
|
path = fnormalize(path).."/"
|
|
local ft,rt = {},{}
|
|
for k,v in pairs(findex) do
|
|
k="/"..k
|
|
if k:match(path.."([^/]+)/.+") then
|
|
ft[k:match(path.."([^/]+)/.+").."/"] = true
|
|
elseif k:match(path.."([^/]+)") then
|
|
ft[k:match(path.."([^/]+)")] = true
|
|
end
|
|
end
|
|
for k,v in pairs(ft) do
|
|
rt[#rt+1] = k
|
|
end
|
|
return rt
|
|
end
|
|
function pkgfs.component.isDirectory(path)
|
|
path = fnormalize(path).."/"
|
|
for k,v in pairs(findex) do
|
|
k="/"..k
|
|
if k:match(path.."([^/]+)/.+") then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
function pkgfs.component.size(path)
|
|
path=fnormalize(path)
|
|
if not findex[path] then return false end
|
|
local f = fopen(findex[path][1], findex[path][2])
|
|
for fname, read, fsize in mtar.iter(f) do
|
|
if fname == path then
|
|
return fsize
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
function pkgfs.component.open(path,mode)
|
|
path=fnormalize(path)
|
|
if mode:find("w") or mode:find("a") or not findex[path] then
|
|
return false
|
|
end
|
|
local f = fopen(findex[path][1],findex[path][2])
|
|
for fname,read,fsize in mtar.iter(f) do
|
|
if fname == path then
|
|
hc = hc + 1
|
|
handles[hc] = {read, f}
|
|
return hc
|
|
end
|
|
end
|
|
end
|
|
|
|
function pkgfs.component.read(handle, n)
|
|
if not handles[handle] then return false end
|
|
local rv = handles[handle][1](n)
|
|
if not rv then return nil end
|
|
if rv:len() < 1 then return nil end
|
|
return rv
|
|
end
|
|
|
|
function pkgfs.component.close(handle)
|
|
if not handles[handle] then return false end
|
|
handles[handle][2]:close()
|
|
handles[handle] = nil
|
|
return true
|
|
end
|
|
|
|
function pkgfs.add(fname,comp)
|
|
if fname:sub(1,1) ~= "/" then
|
|
fname = "/"..fnormalize(os.getenv("PWD").."/"..fname)
|
|
end
|
|
local f = fopen(fname,comp)
|
|
if not fname then error("unable to open file") end
|
|
print(fname)
|
|
for name, read, fsize in mtar.iter(f) do
|
|
findex[fnormalize(name)] = {fname,comp}
|
|
end
|
|
f:close()
|
|
end
|
|
|
|
return pkgfs
|