Initial commit
This commit is contained in:
16
sys/apis/fs/gitfs.lua
Normal file
16
sys/apis/fs/gitfs.lua
Normal file
@@ -0,0 +1,16 @@
|
||||
local git = require('git')
|
||||
|
||||
local gitfs = { }
|
||||
|
||||
function gitfs.mount(dir, user, repo, branch)
|
||||
if not user or not repo then
|
||||
error('gitfs syntax: user, repo, [branch]')
|
||||
end
|
||||
|
||||
local list = git.list(user, repo, branch)
|
||||
for path, url in pairs(list) do
|
||||
fs.mount(fs.combine(dir, path), 'urlfs', url)
|
||||
end
|
||||
end
|
||||
|
||||
return gitfs
|
||||
61
sys/apis/fs/linkfs.lua
Normal file
61
sys/apis/fs/linkfs.lua
Normal file
@@ -0,0 +1,61 @@
|
||||
local linkfs = { }
|
||||
|
||||
local methods = { 'exists', 'getFreeSpace', 'getSize',
|
||||
'isDir', 'isReadOnly', 'list', 'makeDir', 'open', 'getDrive' }
|
||||
|
||||
for _,m in pairs(methods) do
|
||||
linkfs[m] = function(node, dir, ...)
|
||||
dir = dir:gsub(node.mountPoint, node.source, 1)
|
||||
return fs[m](dir, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function linkfs.mount(dir, source)
|
||||
if not source then
|
||||
error('Source is required')
|
||||
end
|
||||
source = fs.combine(source, '')
|
||||
if fs.isDir(source) then
|
||||
return {
|
||||
source = source,
|
||||
nodes = { },
|
||||
}
|
||||
end
|
||||
return {
|
||||
source = source
|
||||
}
|
||||
end
|
||||
|
||||
function linkfs.copy(node, s, t)
|
||||
s = s:gsub(node.mountPoint, node.source, 1)
|
||||
t = t:gsub(node.mountPoint, node.source, 1)
|
||||
return fs.copy(s, t)
|
||||
end
|
||||
|
||||
function linkfs.delete(node, dir)
|
||||
if dir == node.mountPoint then
|
||||
fs.unmount(node.mountPoint)
|
||||
else
|
||||
dir = dir:gsub(node.mountPoint, node.source, 1)
|
||||
return fs.delete(dir)
|
||||
end
|
||||
end
|
||||
|
||||
function linkfs.find(node, spec)
|
||||
spec = spec:gsub(node.mountPoint, node.source, 1)
|
||||
|
||||
local list = fs.find(spec)
|
||||
for k,f in ipairs(list) do
|
||||
list[k] = f:gsub(node.source, node.mountPoint, 1)
|
||||
end
|
||||
|
||||
return list
|
||||
end
|
||||
|
||||
function linkfs.move(node, s, t)
|
||||
s = s:gsub(node.mountPoint, node.source, 1)
|
||||
t = t:gsub(node.mountPoint, node.source, 1)
|
||||
return fs.move(s, t)
|
||||
end
|
||||
|
||||
return linkfs
|
||||
161
sys/apis/fs/netfs.lua
Normal file
161
sys/apis/fs/netfs.lua
Normal file
@@ -0,0 +1,161 @@
|
||||
local Socket = require('socket')
|
||||
local synchronized = require('sync')
|
||||
|
||||
local netfs = { }
|
||||
|
||||
local function remoteCommand(node, msg)
|
||||
|
||||
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(2)
|
||||
end)
|
||||
|
||||
if ret then
|
||||
return ret.response
|
||||
end
|
||||
node.socket:close()
|
||||
node.socket = nil
|
||||
error('netfs: Connection failed', 2)
|
||||
end
|
||||
|
||||
local methods = { 'delete', 'exists', 'getFreeSpace', 'getSize', 'makeDir' }
|
||||
|
||||
local function resolveDir(dir, node)
|
||||
dir = dir:gsub(node.mountPoint, '', 1)
|
||||
return fs.combine(node.directory, dir)
|
||||
end
|
||||
|
||||
for _,m in pairs(methods) do
|
||||
netfs[m] = function(node, dir)
|
||||
dir = resolveDir(dir, node)
|
||||
|
||||
return remoteCommand(node, {
|
||||
fn = m,
|
||||
args = { dir },
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
function netfs.mount(dir, id, directory)
|
||||
if not id or not tonumber(id) then
|
||||
error('ramfs syntax: computerId [directory]')
|
||||
end
|
||||
return {
|
||||
id = tonumber(id),
|
||||
nodes = { },
|
||||
directory = directory or '',
|
||||
}
|
||||
end
|
||||
|
||||
function netfs.getDrive()
|
||||
return 'net'
|
||||
end
|
||||
|
||||
function netfs.complete(node, partial, dir, includeFiles, includeSlash)
|
||||
dir = resolveDir(dir, node)
|
||||
|
||||
return remoteCommand(node, {
|
||||
fn = 'complete',
|
||||
args = { partial, dir, includeFiles, includeSlash },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.copy(node, s, t)
|
||||
s = resolveDir(s, node)
|
||||
t = resolveDir(t, node)
|
||||
|
||||
return remoteCommand(node, {
|
||||
fn = 'copy',
|
||||
args = { s, t },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.isDir(node, dir)
|
||||
if dir == node.mountPoint and node.directory == '' then
|
||||
return true
|
||||
end
|
||||
return remoteCommand(node, {
|
||||
fn = 'isDir',
|
||||
args = { resolveDir(dir, node) },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.isReadOnly(node, dir)
|
||||
if dir == node.mountPoint and node.directory == '' then
|
||||
return false
|
||||
end
|
||||
return remoteCommand(node, {
|
||||
fn = 'isReadOnly',
|
||||
args = { resolveDir(dir, node) },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.find(node, spec)
|
||||
spec = resolveDir(spec, node)
|
||||
local list = remoteCommand(node, {
|
||||
fn = 'find',
|
||||
args = { spec },
|
||||
})
|
||||
|
||||
for k,f in ipairs(list) do
|
||||
list[k] = fs.combine(node.mountPoint, f)
|
||||
end
|
||||
|
||||
return list
|
||||
end
|
||||
|
||||
function netfs.list(node, dir, full)
|
||||
dir = resolveDir(dir, node)
|
||||
|
||||
local r = remoteCommand(node, {
|
||||
fn = 'list',
|
||||
args = { dir, full },
|
||||
})
|
||||
return r
|
||||
end
|
||||
|
||||
function netfs.move(node, s, t)
|
||||
s = resolveDir(s, node)
|
||||
t = resolveDir(t, node)
|
||||
|
||||
return remoteCommand(node, {
|
||||
fn = 'move',
|
||||
args = { s, t },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.open(node, fn, fl)
|
||||
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
|
||||
end
|
||||
|
||||
return netfs
|
||||
153
sys/apis/fs/ramfs.lua
Normal file
153
sys/apis/fs/ramfs.lua
Normal file
@@ -0,0 +1,153 @@
|
||||
local ramfs = { }
|
||||
|
||||
function ramfs.mount(dir, nodeType)
|
||||
if nodeType == 'directory' then
|
||||
return {
|
||||
nodes = { },
|
||||
size = 0,
|
||||
}
|
||||
elseif nodeType == 'file' then
|
||||
return {
|
||||
size = 0,
|
||||
}
|
||||
end
|
||||
error('ramfs syntax: [directory, file]')
|
||||
end
|
||||
|
||||
function ramfs.delete(node, dir)
|
||||
if node.mountPoint == dir then
|
||||
fs.unmount(node.mountPoint)
|
||||
end
|
||||
end
|
||||
|
||||
function ramfs.exists(node, fn)
|
||||
return node.mountPoint == fn
|
||||
end
|
||||
|
||||
function ramfs.getSize(node)
|
||||
return node.size
|
||||
end
|
||||
|
||||
function ramfs.isReadOnly()
|
||||
return false
|
||||
end
|
||||
|
||||
function ramfs.makeDir(node, dir)
|
||||
fs.mount(dir, 'ramfs', 'directory')
|
||||
end
|
||||
|
||||
function ramfs.isDir(node)
|
||||
return not not node.nodes
|
||||
end
|
||||
|
||||
function ramfs.getDrive()
|
||||
return 'ram'
|
||||
end
|
||||
|
||||
function ramfs.list(node, dir, full)
|
||||
if node.nodes and node.mountPoint == dir then
|
||||
local files = { }
|
||||
if full then
|
||||
for f,n in pairs(node.nodes) do
|
||||
table.insert(files, {
|
||||
name = f,
|
||||
isDir = fs.isDir(fs.combine(dir, f)),
|
||||
size = fs.getSize(fs.combine(dir, f)),
|
||||
})
|
||||
end
|
||||
else
|
||||
for k,v in pairs(node.nodes) do
|
||||
table.insert(files, k)
|
||||
end
|
||||
end
|
||||
return files
|
||||
end
|
||||
error('Not a directory')
|
||||
end
|
||||
|
||||
function ramfs.open(node, fn, fl)
|
||||
|
||||
if fl ~= 'r' and fl ~= 'w' and fl ~= 'rb' and fl ~= 'wb' then
|
||||
error('Unsupported mode')
|
||||
end
|
||||
|
||||
if fl == 'r' then
|
||||
if node.mountPoint ~= fn then
|
||||
return
|
||||
end
|
||||
|
||||
local ctr = 0
|
||||
local lines
|
||||
return {
|
||||
readLine = function()
|
||||
if not lines then
|
||||
lines = Util.split(node.contents)
|
||||
end
|
||||
ctr = ctr + 1
|
||||
return lines[ctr]
|
||||
end,
|
||||
readAll = function()
|
||||
return node.contents
|
||||
end,
|
||||
close = function()
|
||||
lines = nil
|
||||
end,
|
||||
}
|
||||
elseif fl == 'w' then
|
||||
node = fs.mount(fn, 'ramfs', 'file')
|
||||
|
||||
local c = ''
|
||||
return {
|
||||
write = function(str)
|
||||
c = c .. str
|
||||
end,
|
||||
writeLine = function(str)
|
||||
c = c .. str .. '\n'
|
||||
end,
|
||||
flush = function()
|
||||
node.contents = c
|
||||
node.size = #c
|
||||
end,
|
||||
close = function()
|
||||
node.contents = c
|
||||
node.size = #c
|
||||
c = nil
|
||||
end,
|
||||
}
|
||||
elseif fl == 'rb' then
|
||||
if node.mountPoint ~= fn or not node.contents then
|
||||
return
|
||||
end
|
||||
|
||||
local ctr = 0
|
||||
return {
|
||||
read = function()
|
||||
ctr = ctr + 1
|
||||
return node.contents[ctr]
|
||||
end,
|
||||
close = function()
|
||||
end,
|
||||
}
|
||||
|
||||
elseif fl == 'wb' then
|
||||
node = fs.mount(fn, 'ramfs', 'file')
|
||||
|
||||
local c = { }
|
||||
return {
|
||||
write = function(b)
|
||||
table.insert(c, b)
|
||||
end,
|
||||
flush = function()
|
||||
node.contents = c
|
||||
node.size = #c
|
||||
end,
|
||||
close = function()
|
||||
node.contents = c
|
||||
node.size = #c
|
||||
c = nil
|
||||
end,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
return ramfs
|
||||
78
sys/apis/fs/urlfs.lua
Normal file
78
sys/apis/fs/urlfs.lua
Normal file
@@ -0,0 +1,78 @@
|
||||
local synchronized = require('sync')
|
||||
|
||||
local urlfs = { }
|
||||
|
||||
function urlfs.mount(dir, url)
|
||||
if not url then
|
||||
error('URL is required')
|
||||
end
|
||||
return {
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
|
||||
function urlfs.delete(node, dir)
|
||||
fs.unmount(dir)
|
||||
end
|
||||
|
||||
function urlfs.exists()
|
||||
return true
|
||||
end
|
||||
|
||||
function urlfs.getSize(node)
|
||||
return node.size or 0
|
||||
end
|
||||
|
||||
function urlfs.isReadOnly()
|
||||
return true
|
||||
end
|
||||
|
||||
function urlfs.isDir()
|
||||
return false
|
||||
end
|
||||
|
||||
function urlfs.getDrive()
|
||||
return 'url'
|
||||
end
|
||||
|
||||
function urlfs.open(node, fn, fl)
|
||||
|
||||
if fl ~= 'r' then
|
||||
error('Unsupported mode')
|
||||
end
|
||||
|
||||
local c = node.cache
|
||||
if not c then
|
||||
synchronized(node.url, function()
|
||||
c = Util.download(node.url)
|
||||
end)
|
||||
if c and #c > 0 then
|
||||
node.cache = c
|
||||
node.size = #c
|
||||
end
|
||||
end
|
||||
|
||||
if not c or #c == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local ctr = 0
|
||||
local lines
|
||||
return {
|
||||
readLine = function()
|
||||
if not lines then
|
||||
lines = Util.split(c)
|
||||
end
|
||||
ctr = ctr + 1
|
||||
return lines[ctr]
|
||||
end,
|
||||
readAll = function()
|
||||
return c
|
||||
end,
|
||||
close = function()
|
||||
lines = nil
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
return urlfs
|
||||
Reference in New Issue
Block a user