Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mod = peripheral.wrap("back")
- mod.open(418)
- local dir = "net"
- if not fs.exists("tmp") then
- fs.makeDir("tmp")
- end
- local con = 0
- local oFs = {}
- for i,v in pairs(fs) do
- oFs[i] = v
- end
- local nFs = {}
- local function transmit(mess)
- mod.transmit(418, os.getComputerID(), textutils.serialize(mess))
- local event, side, channel, repl, mess
- repeat
- event, side, channel, repl, mess = os.pullEvent("modem_message")
- until channel == 418 and repl == os.getComputerID()
- local r = textutils.unserialize(mess)
- if type(r) == "string" then
- error(mess, 0)
- end
- return r
- end
- local function getPath(path)
- path = fs.combine(path, "")
- if path == dir or path:sub(1, #dir + 1) == dir .. "/" then
- return true, fs.combine(path:sub(#dir + 1), "")
- end
- return false
- end
- local function upload(name, path)
- if oFs.isDir(path) then
- -- Recursive stuff
- else
- local file = oFs.open(path, "rb")
- local dat = {}
- for on = 1, math.huge do
- if on%10000 == 0 then
- os.queueEvent("")
- coroutine.yield()
- end
- local by = file.read()
- if by then
- table.insert(dat, by)
- else
- file.close()
- break
- end
- end
- transmit({"upf", name, dat})
- end
- end
- local function downloadD(path1, path2)
- local dat = transmit({"downd", path1})
- oFs.makeDir(path2)
- local check
- function check(pat, fi)
- for i,v in pairs(fi) do
- if v[1] == "dir" then
- oFs.makeDir(pat .. "/" .. i)
- check(pat .. "/" .. i, v[2])
- else
- local file = oFs.open(pat .. "/" .. i, "wb")
- for i,v in pairs(v[2]) do
- if i%10000 == 0 then
- os.queueEvent("")
- coroutine.yield()
- end
- file.write(v)
- end
- file.close()
- end
- end
- end
- check(path2, dat)
- end
- local function uploadD(path1, path2)
- if not fs.isDir(path1) then
- upload(path2, path1)
- return
- end
- local files = {}
- local check
- function check(pat, tab)
- for i,v in pairs(oFs.list(pat)) do
- if oFs.isDir(pat .. "/" .. v) then
- tab[v] = {"dir", {}}
- check(pat .. "/" .. v, tab[v][2])
- else
- local file = oFs.open(pat .. "/" .. v, "rb")
- tab[v] = {"file", {}}
- for on = 1, math.huge do
- if on%10000 == 0 then
- os.queueEvent("")
- coroutine.yield()
- end
- local by = file.read()
- if by then
- table.insert(tab[v][2], by)
- else
- file.close()
- break
- end
- end
- end
- end
- end
- check(path1, files)
- transmit({"upd", path2, files})
- end
- function nFs.copy(rpath1, rpath2)
- local tr1, path1 = getPath(rpath1)
- local tr2, path2 = getPath(rpath2)
- if tr1 and not tr2 then
- downloadD(path1, rpath2)
- elseif tr2 and not tr1 then
- uploadD(rpath1, path2)
- elseif tr1 and tr2 then
- transmit({"copy", path1, path2})
- else
- return oFs.copy(rpath1, rpath2)
- end
- end
- function nFs.move(rpath1, rpath2)
- local tr1, path1 = getPath(rpath1)
- local tr2, path2 = getPath(rpath2)
- if tr1 and not tr2 then
- downloadD(path1, rpath2)
- fs.delete(rpath1)
- elseif tr2 and not tr1 then
- uploadD(rpath1, path2)
- oFs.delete(rpath1)
- elseif tr1 and tr2 then
- transmit({"move", path1, path2})
- else
- return oFs.move(rpath1, rpath2)
- end
- end
- function nFs.open(rpath, ty)
- local tr, path = getPath(rpath)
- if tr then
- local name = "tmp/" .. con
- if not ty:find("w") then
- local da = transmit({"downf", path})
- local file = oFs.open(name, "wb")
- for i,v in pairs(da) do
- file.write(v)
- end
- file.close()
- con = con + 1
- end
- local hand = oFs.open(name, ty)
- local ocl = hand.close
- hand.close = function()
- ocl()
- upload(path, name)
- end
- return hand
- end
- return oFs.open(rpath, ty)
- end
- function nFs.delete(rpath)
- local tr, path = getPath(rpath)
- if tr then
- return transmit({"delete", path})
- end
- return oFs.delete(rpath)
- end
- function nFs.getSize(rpath)
- local tr, path = getPath(rpath)
- if tr then
- return transmit({"getSize", path})
- end
- return oFs.getSize(rpath)
- end
- function nFs.getFreeSpace(rpath)
- local tr, path = getPath(rpath)
- if tr then
- return transmit({"getFreeSpace", path})
- end
- return oFs.getFreeSpace(rpath)
- end
- function nFs.exists(rpath)
- local tr, path = getPath(rpath)
- if tr then
- return transmit({"exists", path})
- end
- local r = oFs.exists(rpath)
- if fs.combine(rpath, "") == "net" then
- r = true
- end
- return r
- end
- function nFs.isReadOnly(rpath)
- local tr, path = getPath(rpath)
- if tr then
- return transmit({"isReadOnly", path})
- end
- return oFs.isReadOnly(rpath)
- end
- function nFs.list(rpath)
- local tr, path = getPath(rpath)
- if tr then
- return transmit({"list", path})
- end
- local r = oFs.list(rpath)
- if fs.combine(rpath, "") == "" then
- table.insert(r, "net")
- end
- return r
- end
- function nFs.isDir(rpath)
- local tr, path = getPath(rpath)
- if tr then
- return transmit({"isDir", path})
- end
- local r = oFs.isDir(rpath)
- if fs.combine(rpath, "") == "" then
- r = true
- end
- return r
- end
- function nFs.makeDir(rpath)
- local tr, path = getPath(rpath)
- if tr then
- return transmit({"makeDir", path})
- end
- return oFs.makeDir(rpath)
- end
- for i,v in pairs(nFs) do
- fs[i] = v
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement