2017-07-31 12:10:07 +10:00
|
|
|
do
|
|
|
|
_G.fs = {}
|
2017-09-15 15:38:16 +10:00
|
|
|
local dfsattr = {read = "*", write = "*"}
|
|
|
|
local fsattr = {}
|
2017-07-31 12:10:07 +10:00
|
|
|
local fT,hT = {},{["_c"]=0}
|
2017-09-15 15:58:42 +10:00
|
|
|
local function parseacl(acl)
|
|
|
|
acl = acl or ""
|
|
|
|
local tacl = {}
|
|
|
|
if acl == "*" then
|
|
|
|
setmetatable(tacl,{__index = function() return true end})
|
|
|
|
else
|
|
|
|
for n in acl:gmatch("[^,]+") do
|
|
|
|
tacl[n] = true
|
|
|
|
log(n)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tacl
|
|
|
|
end
|
2017-09-15 15:38:16 +10:00
|
|
|
local function getattr(fn,k)
|
2017-09-15 15:58:42 +10:00
|
|
|
if os.getuid() ~= "superuser" then
|
|
|
|
if not parseacl(fsattr[fn].read or "")[os.getuid()] then return false end
|
|
|
|
end
|
2017-09-15 15:38:16 +10:00
|
|
|
if fsattr[fn] then
|
|
|
|
return fsattr[fn][k]
|
|
|
|
else
|
|
|
|
return dfsattr[k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local function setattr(fn,k,v)
|
|
|
|
if k:find("\t") or v:find("\t") then return false end
|
2017-09-15 15:58:42 +10:00
|
|
|
log(parseacl(getattr(fn,"write"))[os.getuid()])
|
|
|
|
if os.getuid() ~= "superuser" then
|
|
|
|
if not parseacl(getattr(fn,"write"))[os.getuid()] then return false end
|
|
|
|
end
|
2017-09-15 15:38:16 +10:00
|
|
|
if not fsattr[fn] then
|
|
|
|
fsattr[fn] = {}
|
|
|
|
setmetatable(fsattr[fn],{__index=dfsattr})
|
|
|
|
end
|
|
|
|
fsattr[fn][k] = v
|
|
|
|
end
|
2017-07-31 12:10:07 +10:00
|
|
|
function fs.mount(mp,pr)
|
|
|
|
fT[mp] = pr
|
|
|
|
end
|
|
|
|
function fs.simplify(p)
|
|
|
|
local pt,npt,rp = {},{},""
|
|
|
|
for P in p:gmatch("[^/]+") do
|
|
|
|
pt[#pt+1] = P
|
|
|
|
end
|
|
|
|
for k,v in pairs(pt) do
|
|
|
|
if v == ".." then
|
|
|
|
npt[#npt] = nil
|
|
|
|
elseif v ~= "." then
|
|
|
|
npt[#npt+1] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for k,v in pairs(npt) do
|
|
|
|
rp=rp.."/"..v
|
|
|
|
end
|
|
|
|
if p:sub(1,1) ~= "/" then rp = rp:sub(2) end
|
|
|
|
return rp
|
|
|
|
end
|
|
|
|
function fs.resolve(p)
|
|
|
|
if p:sub(1,1) ~= "/" then -- absolute/relative path
|
|
|
|
p=(os.getenv("PWD") or "").."/"..p
|
|
|
|
end
|
|
|
|
p=fs.simplify(p)
|
2017-08-01 15:45:22 +10:00
|
|
|
local pt,spt = {},""
|
2017-07-31 12:10:07 +10:00
|
|
|
for P in p:gmatch("[^/]+") do
|
|
|
|
pt[#pt+1] = P
|
|
|
|
end
|
2017-08-01 15:45:22 +10:00
|
|
|
for i = 2, #pt do
|
2017-08-01 18:13:31 +10:00
|
|
|
spt=spt.."/"..pt[i]
|
2017-08-01 15:45:22 +10:00
|
|
|
end
|
|
|
|
return pt, pt[1], spt
|
|
|
|
end
|
|
|
|
function fs.exec(fc,m,...)
|
|
|
|
return fT[fc][m](...)
|
2017-07-31 12:10:07 +10:00
|
|
|
end
|
|
|
|
function fs.open(p,m)
|
|
|
|
local _,d,p = fs.resolve(p)
|
|
|
|
local d = fT[d]
|
2017-08-04 17:44:10 +10:00
|
|
|
if d then
|
|
|
|
local f,C=d.open(p,m),hT._c
|
|
|
|
if f then
|
|
|
|
hT._c = C + 1
|
|
|
|
hT[C] = {d,f}
|
|
|
|
return C
|
|
|
|
end
|
|
|
|
return false
|
2017-07-31 12:10:07 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
function fs.close(h)
|
|
|
|
if hT[h] then
|
|
|
|
hT[h][1].close(hT[h][2])
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
function fs.read(h,n)
|
|
|
|
if hT[h] then
|
|
|
|
return hT[h][1].read(hT[h][2],n)
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
function fs.readall(f)
|
|
|
|
local s=""
|
2017-08-03 17:54:38 +10:00
|
|
|
while true do
|
2017-07-31 12:10:07 +10:00
|
|
|
c=fs.read(f,2048)
|
2017-08-03 17:54:38 +10:00
|
|
|
if not c then break end
|
2017-07-31 12:10:07 +10:00
|
|
|
s=s..c
|
2017-08-03 17:54:38 +10:00
|
|
|
end
|
2017-07-31 12:10:07 +10:00
|
|
|
return s
|
|
|
|
end
|
|
|
|
function fs.write(h,d)
|
|
|
|
if hT[h] then
|
|
|
|
return hT[h][1].write(hT[h][2],d)
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2017-08-01 15:45:22 +10:00
|
|
|
function fs.list(s)
|
2017-08-01 16:29:56 +10:00
|
|
|
s=s or ""
|
2017-08-01 14:36:38 +10:00
|
|
|
local _,d,p = fs.resolve(s)
|
2017-08-01 15:45:22 +10:00
|
|
|
if not d then
|
|
|
|
local kt = {}
|
|
|
|
for k,v in pairs(fT) do
|
|
|
|
kt[#kt+1] = k
|
|
|
|
end
|
|
|
|
return kt
|
|
|
|
end
|
2017-08-01 14:36:38 +10:00
|
|
|
return fT[d].list(p or "/")
|
|
|
|
end
|
|
|
|
function fs.mkdir(s)
|
|
|
|
local _,d,p = fs.resolve(s)
|
|
|
|
return fT[d].makeDirectory(p or "/")
|
|
|
|
end
|
|
|
|
function fs.rm(s)
|
2017-08-01 15:45:22 +10:00
|
|
|
local _,d,p = fs.resolve(s)
|
2017-08-01 14:36:38 +10:00
|
|
|
return fT[d].remove(p)
|
|
|
|
end
|
|
|
|
function fs.exists(s)
|
2017-08-01 15:45:22 +10:00
|
|
|
local _,d,p = fs.resolve(s)
|
2017-08-04 17:44:10 +10:00
|
|
|
if d then
|
|
|
|
return fT[d].exists(p)
|
|
|
|
end
|
2017-08-01 14:36:38 +10:00
|
|
|
end
|
|
|
|
function fs.isdir(s)
|
2017-08-01 15:45:22 +10:00
|
|
|
local _,d,p = fs.resolve(s)
|
2017-08-01 14:36:38 +10:00
|
|
|
return fT[d].isDirectory(p)
|
|
|
|
end
|
2017-09-15 15:38:16 +10:00
|
|
|
local function flushattr()
|
|
|
|
local f = fs.open("/boot/sys/fsattr.dat","wb")
|
|
|
|
if f then
|
|
|
|
for k,v in pairs(fsattr) do
|
|
|
|
fs.write(f,k.."\t")
|
|
|
|
for l,m in pairs(v) do
|
|
|
|
fs.write(f,"\t"..l.."="..m)
|
|
|
|
end
|
|
|
|
fs.write(f,"\n")
|
|
|
|
end
|
|
|
|
fs.close(f)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local function readattr()
|
|
|
|
local f=fs.open("/boot/sys/fsattr.dat","rb")
|
|
|
|
if not f then return false end
|
|
|
|
local C=fs.readall(f)
|
|
|
|
fs.close(f)
|
|
|
|
for line in C:gmatch("[^\n]+") do
|
|
|
|
local ifn = true
|
|
|
|
local fn = ""
|
|
|
|
for kv in line:gmatch("[^\t]+") do
|
|
|
|
if ifn then
|
|
|
|
fn = kv
|
|
|
|
ifn = false
|
|
|
|
else
|
|
|
|
local k,v = kv:match("(.+)%=(.+)")
|
|
|
|
setattr(fn,k,v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
spawn("read fs attributes",readattr)
|
|
|
|
function fs.getattr(fn,k)
|
|
|
|
if fs.exists(fn) then
|
|
|
|
return getattr(fn,k)
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
function fs.setattr(fn,k,v)
|
|
|
|
if fs.exists(fn) then
|
|
|
|
local res={setattr(fn,k,v)}
|
|
|
|
flushattr()
|
|
|
|
return table.unpack(res)
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2017-07-31 12:10:07 +10:00
|
|
|
end
|
2017-08-01 16:29:56 +10:00
|
|
|
function fs.cd(p)
|
|
|
|
if p:sub(1,1) ~= "/" then
|
|
|
|
p=(os.getenv("PWD") or "").."/"..p
|
|
|
|
end
|
|
|
|
p=fs.simplify(p)
|
|
|
|
if fs.exists(p) and fs.isdir(p) then
|
|
|
|
os.setenv("PWD",p)
|
|
|
|
else
|
|
|
|
error("non-existent/not a dir")
|
|
|
|
end
|
|
|
|
end
|
2017-08-31 12:31:54 +10:00
|
|
|
function fs.cp(s,d)
|
|
|
|
local df = fs.open(d,"wb")
|
|
|
|
local sf = fs.open(s,"rb")
|
|
|
|
fs.write(df,fs.readall(sf))
|
|
|
|
fs.close(df)
|
|
|
|
fs.close(sf)
|
|
|
|
end
|
|
|
|
function fs.mv(s,d)
|
|
|
|
fs.cp(s,d)
|
|
|
|
fs.rm(s)
|
|
|
|
end
|