Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local evt = require("event")
- local serialization = require("serialization")
- local modem = component.modem
- modem.setStrength(128)
- modem.open(999)
- local fileHost = nil
- while fileHost == nil do
- local type, _, from, _, _, message = evt.pull("modem_message")
- fileHost = message
- end
- modem.close(999)
- print("Found a file host: ", fileHost)
- function getResult(type, path)
- modem.open(7766)
- modem.send(fileHost, 7766, serialization.serialize({ type = type, path = path }))
- local _, _, _, _, _, message = evt.pull("modem_message")
- modem.close(7766)
- return serialization.unserialize(message)
- end
- local fs = require("filesystem")
- local networkMount = { _files = {} }
- function networkMount.getLabel()
- return "NetworkFS"
- end
- function networkMount.setLabel(label)
- return nil, "filesystem does not support labeling"
- end
- function networkMount.isReadOnly()
- return true
- end
- function networkMount.spaceTotal()
- return getResult("info", nil).spaceTotal
- end
- function networkMount.spaceUsed()
- return getResult("info", nil).spaceUsed
- end
- function networkMount.exists(path)
- local result = getResult("type", path)
- return result.type ~= "error"
- end
- function networkMount.size(path)
- local result = getResult("type", path)
- if result.type == "error" then return nil, result.error end
- return result.size
- end
- function networkMount.isDirectory(path)
- local result = getResult("type", path)
- if result.type == "error" then return false end
- return result.isDirectory
- end
- function networkMount.lastModified(path)
- local result = getResult("type", path)
- if result.type == "error" then return nil, result.error end
- return result.lastModified
- end
- function networkMount.list(path)
- local result = getResult("list", path)
- if result.type == "error" then return nil, result.error end
- return result.list
- end
- function networkMount.makeDirectory(path) return false end
- function networkMount.remove(path) return false end
- function networkMount.rename(path) return false end
- function networkMount.close(handle)
- networkMount._files[handle] = nil
- end
- function networkMount.open(path)
- local result = getResult("get", path)
- if result.type == "error" then return nil, result.error end
- local handle = nil
- while handle == nil do
- handle = math.random()
- if networkMount._files[handle] ~= nil then handle = nil end
- end
- networkMount._files[handle] = { ptr = 1, data = result.data, size = result.data:len() }
- return handle
- end
- function networkMount.read(handle, n)
- local data = networkMount._files[handle]
- local toRead = math.min(data.size - data.ptr, math.max(0, n))
- if toRead == 0 then return nil end
- local ret = data.data:sub(data.ptr, data.ptr + toRead)
- data.ptr = data.ptr + toRead
- return ret
- end
- function networkMount.seek(handle, type, n)
- local data = networkMount._files[handle]
- if type == "cur" then
- data.ptr = data.ptr + n
- elseif type == "set" then
- data.ptr = n
- elseif type == "end" then
- data.ptr = data.size - n
- else
- return nil, "invalid mode"
- end
- if data.ptr <= 0 then data.ptr = 1 end
- if data.ptr > data.size then data.ptr = data.size end
- end
- function networkMount.write(handle)
- return nil
- end
- fs.mount(networkMount, ...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement