Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local nextSeq = 0
- local timeout = 2
- for _,v in ipairs(redstone.getSides()) do
- if peripheral.getType(v) == "modem" then
- rednet.open(v)
- end
- end
- local fs_rfs = {
- send = function(self, cmd, ...)
- local seq = nextSeq
- nextSeq = nextSeq + 1
- cmd = {"RFS", self.password, seq, cmd, ...}
- cmd = textutils.serialize(cmd)
- rednet.send(self.server, cmd)
- --print("sent ",cmd," to ",self.server)
- local _end = os.clock() + timeout
- --print("end ",_end," ",rednet.receive," ",coroutine.running())
- while true do
- local s, m = rednet.receive(_end - os.clock())
- --print(tostring(s)," ",tostring(m))
- if s == nil or m == nil then
- error("RFS timeout")
- end
- local t = textutils.unserialize(m)
- if s == self.server and type(t) == "table" and t[2] == seq then
- if t[1] == "RFS" then
- table.remove(t, 1)
- table.remove(t, 1)
- return unpack(t)
- elseif t[1] == "RFS-error" then
- error("Remote error: " .. t[3])
- end
- end
- end
- end,
- trpath = function(self, path)
- return self.prefix .. path
- end,
- isReadOnly = function(self, path) return self:send("isReadOnly", self:trpath(path)) end,
- exists = function(self, path)
- local ok, rv = pcall(self.send, self, "exists", self:trpath(path))
- return ok and rv
- end,
- isDir = function(self, path)
- local ok, rv = pcall(self.send, self, "isDir", self:trpath(path))
- return ok and rv
- end,
- list = function(self, path) return {self:send("list", self:trpath(path))} end,
- getDrive = function(self, path) return "RFS-"..self.server end,
- mkdir = function(self, path) self:send("mkdir", self:trpath(path)) end,
- delete = function(self, path) self:send("delete", self:trpath(path)) end,
- getSize = function(self, path) return self:send("getSize", self:trpath(path)) end,
- getFreeSpace = function(self, path) return self:send("getFreeSpace", self:trpath(path)) end,
- -- move/copy?
- open = function(self, path, mode)
- local id = self:send("open", self:trpath(path), mode)
- if id == nil then return nil end
- --print("Got ID "..tostring(id))
- local t = {}
- t.close = function() self:send("close", id) end
- if mode == "r" then
- t.readLine = function() return self:send("readLine", id) end
- t.readAll = function() return self:send("readAll", id) end
- elseif mode == "w" or mode == "a" then
- t.write = function(s) return self:send("write", id, s) end
- t.writeLine = function(s) return self:send("writeLine", id, s) end
- elseif mode == "rb" then
- t.read = function() return self:send("read", id) end
- elseif mode == "wb" or mode == "ab" then
- t.write = function(b) return self:send("write", id, b) end
- else
- error("invalid mode "..tostring(mode))
- end
- return t
- end,
- }
- vfs.registerFS("rfs", function(mount, opts)
- assert(type(opts.server) == "number", "server field must be number")
- assert(type(opts.password) == "string", "password field must be string")
- assert(type(opts.prefix) == "string" or opts.prefix == nil, "prefix field must be string or nil")
- local t = {server=opts.server, password=opts.password, prefix=opts.prefix or ""}
- setmetatable(t, {__index=fs_rfs})
- return t
- end)
- --vfs.unmount("/rfs/")
- --vfs.mount("/rfs/", "rfs", {server=131, password="rfs-test"})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement