1
0
mirror of https://github.com/ShadowKatStudios/OC-Minitel.git synced 2024-11-23 10:38:05 +11:00

initial mtfs stuff

This commit is contained in:
Izaya 2020-10-12 18:09:06 +11:00
parent c6c471cfcf
commit ba9c9a3dd7
3 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,17 @@
local fsproxy = require "fsproxy"
local rpc = require "rpc"
local fs = require "filesystem"
local tA = {...}
if #tA < 1 then
print("Usage: exportfs <directory> [directory] ...")
return
end
for k,v in pairs(tA) do
local px = fsproxy.new(v)
for l,m in pairs(px) do
rpc.register("fs_"..v.."_"..l,m)
end
print(v)
end

View File

@ -0,0 +1,25 @@
local fs = require "filesystem"
local rpc = require "rpc"
local tA = {...}
local rpath, lpath = tA[1], tA[2], tA[3]
if #tA < 2 then
print("Usage: importfs <remote path> <local path>")
return
end
local function parsePath(path)
return path:match("(.+):(.+)")
end
local host, saddr = parsePath(rpath)
local px = rpc.proxy(host,"fs_"..saddr.."_")
local mc = 0
for k,v in pairs(px) do
mc = mc + 1
end
if mc < 1 then
error("no such remote filesystem: "..addr)
end
px.address = rpath
fs.mount(px, lpath)

View File

@ -0,0 +1,100 @@
local fs = require "filesystem"
local fsproxy = {}
local fsfunc = {}
local function sanitisePath(path)
local pt = {}
for s in path:gmatch("([^/]+)") do
if s == ".." then
pt[#pt] = nil
elseif s == "." then
else
pt[#pt+1] = s
end
end
return table.concat(pt,"/")
end
function fsproxy.new(path,wp) -- string boolean -- table -- Returns a proxy object for a given path that acts like a filesystem.
local cpath = fs.canonical(path).."/"
if not fs.exists(path) or not fs.isDirectory(path) then
error("invalid directory")
end
local originalProxy = fs.get(path)
local proxy = {}
for k,v in pairs(originalProxy) do
if type(v) ~= "table" then
proxy[k] = v
end
end
local handles = {}
function proxy.isReadOnly()
return originalProxy.isReadOnly()
end
function proxy.getLabel()
return originalProxy.getLabel()
end
function proxy.spaceUsed()
return originalProxy.spaceUsed()
end
function proxy.spaceTotal()
return originalProxy.spaceTotal()
end
function proxy.exists(path)
return fs.exists(cpath..sanitisePath(path))
end
function proxy.isDirectory(path)
return fs.isDirectory(cpath..sanitisePath(path))
end
function proxy.makeDirectory(path)
if wp then return false, "read-only filesystem" end
return fs.makeDirectory(cpath..sanitisePath(path))
end
function proxy.rename(from, to)
if wp then return false, "read-only filesystem" end
return fs.rename(cpath..sanitisePath(from),cpath..sanitisePath(to))
end
function proxy.list(path)
local rt = {}
local iter, err = fs.list(cpath..sanitisePath(path))
if not iter then return nil, err end
for name in iter do
rt[#rt+1] = name
end
return rt
end
function proxy.lastModified(path)
return fs.lastModified(cpath..sanitisePath(path))
end
function proxy.remove(path)
if wp then return false, "read-only filesystem" end
return fs.remove(cpath..sanitisePath(path))
end
function proxy.size(path)
return fs.size(cpath..sanitisePath(path))
end
function proxy.open(path,mode)
if wp and mode:find("[wa]") then return false, "read-only filesystem" end
local h, e = fs.open(cpath..sanitisePath(path),mode)
if not h then return h, e end
handles[#handles+1] = h
return #handles
end
function proxy.seek(handle, whence, offset)
return handles[handle]:seek(whence,offset)
end
function proxy.write(handle, data)
return handles[handle]:write(data)
end
function proxy.read(handle, count)
return handles[handle]:read(count)
end
function proxy.close(handle)
return handles[handle]:close()
end
return proxy
end
return fsproxy