1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 13:14:49 +00:00
opus/sys/apis/fs/netfs.lua

166 lines
3.0 KiB
Lua
Raw Normal View History

2017-10-08 21:45:01 +00:00
local Socket = require('socket')
2018-10-31 23:38:09 +00:00
local synchronized = require('sync').sync
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
local fs = _G.fs
2016-12-11 19:24:52 +00:00
local netfs = { }
local function remoteCommand(node, msg)
2018-01-24 22:39:38 +00:00
for _ = 1, 2 do
if not node.socket then
node.socket = Socket.connect(node.id, 139)
end
if not node.socket then
error('netfs: Unable to establish connection to ' .. node.id)
fs.unmount(node.mountPoint)
return
end
local ret
synchronized(node.socket, function()
node.socket:write(msg)
ret = node.socket:read(1)
end)
if ret then
return ret.response
end
node.socket:close()
node.socket = nil
end
error('netfs: Connection failed', 2)
2016-12-11 19:24:52 +00:00
end
2017-08-05 08:17:06 +00:00
local methods = { 'delete', 'exists', 'getFreeSpace', 'makeDir', 'list', 'listEx' }
2016-12-11 19:24:52 +00:00
local function resolveDir(dir, node)
2018-01-24 22:39:38 +00:00
dir = dir:gsub(node.mountPoint, '', 1)
return fs.combine(node.directory, dir)
2016-12-11 19:24:52 +00:00
end
for _,m in pairs(methods) do
2018-01-24 22:39:38 +00:00
netfs[m] = function(node, dir)
dir = resolveDir(dir, node)
return remoteCommand(node, {
fn = m,
args = { dir },
})
end
2016-12-11 19:24:52 +00:00
end
2017-10-08 21:45:01 +00:00
function netfs.mount(_, id, directory)
2018-01-24 22:39:38 +00:00
if not id or not tonumber(id) then
error('ramfs syntax: computerId [directory]')
end
return {
id = tonumber(id),
nodes = { },
directory = directory or '',
}
2016-12-11 19:24:52 +00:00
end
function netfs.getDrive()
2018-01-24 22:39:38 +00:00
return 'net'
2016-12-11 19:24:52 +00:00
end
function netfs.complete(node, partial, dir, includeFiles, includeSlash)
2018-01-24 22:39:38 +00:00
dir = resolveDir(dir, node)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
return remoteCommand(node, {
fn = 'complete',
args = { partial, dir, includeFiles, includeSlash },
})
2016-12-11 19:24:52 +00:00
end
function netfs.copy(node, s, t)
2018-01-24 22:39:38 +00:00
s = resolveDir(s, node)
t = resolveDir(t, node)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
return remoteCommand(node, {
fn = 'copy',
args = { s, t },
})
2016-12-11 19:24:52 +00:00
end
function netfs.isDir(node, dir)
2018-01-24 22:39:38 +00:00
if dir == node.mountPoint and node.directory == '' then
return true
end
return remoteCommand(node, {
fn = 'isDir',
args = { resolveDir(dir, node) },
})
2016-12-11 19:24:52 +00:00
end
function netfs.isReadOnly(node, dir)
2018-01-24 22:39:38 +00:00
if dir == node.mountPoint and node.directory == '' then
return false
end
return remoteCommand(node, {
fn = 'isReadOnly',
args = { resolveDir(dir, node) },
})
2017-05-22 02:19:01 +00:00
end
function netfs.getSize(node, dir)
2018-01-24 22:39:38 +00:00
if dir == node.mountPoint and node.directory == '' then
return 0
end
return remoteCommand(node, {
fn = 'getSize',
args = { resolveDir(dir, node) },
})
2016-12-11 19:24:52 +00:00
end
function netfs.find(node, spec)
2018-01-24 22:39:38 +00:00
spec = resolveDir(spec, node)
local list = remoteCommand(node, {
fn = 'find',
args = { spec },
})
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
for k,f in ipairs(list) do
list[k] = fs.combine(node.mountPoint, f)
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
return list
2016-12-11 19:24:52 +00:00
end
function netfs.move(node, s, t)
2018-01-24 22:39:38 +00:00
s = resolveDir(s, node)
t = resolveDir(t, node)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
return remoteCommand(node, {
fn = 'move',
args = { s, t },
})
2016-12-11 19:24:52 +00:00
end
function netfs.open(node, fn, fl)
2018-01-24 22:39:38 +00:00
fn = resolveDir(fn, node)
local vfh = remoteCommand(node, {
fn = 'open',
args = { fn, fl },
})
if vfh then
vfh.node = node
for _,m in ipairs(vfh.methods) do
vfh[m] = function(...)
return remoteCommand(node, {
fn = 'fileOp',
args = { vfh.fileUid, m, ... },
})
end
end
end
return vfh
2016-12-11 19:24:52 +00:00
end
return netfs