opus/sys/modules/opus/fs/linkfs.lua

73 lines
1.4 KiB
Lua
Raw Normal View History

2017-10-08 21:45:01 +00:00
local fs = _G.fs
2016-12-11 19:24:52 +00:00
local linkfs = { }
2019-04-07 14:09:47 +00:00
-- TODO: implement broken links
local methods = { 'exists', 'getFreeSpace', 'getSize', 'attributes',
2018-01-24 22:39:38 +00:00
'isDir', 'isReadOnly', 'list', 'listEx', 'makeDir', 'open', 'getDrive' }
2016-12-11 19:24:52 +00:00
for _,m in pairs(methods) do
2018-01-24 22:39:38 +00:00
linkfs[m] = function(node, dir, ...)
dir = linkfs.resolve(node, dir)
2018-01-24 22:39:38 +00:00
return fs[m](dir, ...)
end
2016-12-11 19:24:52 +00:00
end
function linkfs.resolve(node, dir)
return dir:gsub(node.mountPoint, node.source, 1)
end
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
function linkfs.mount(_, source)
2018-01-24 22:39:38 +00:00
if not source then
error('Source is required')
end
source = fs.combine(source, '')
2019-11-02 18:23:40 +00:00
if not fs.exists(source) then
error('Source is missing')
end
2018-01-24 22:39:38 +00:00
if fs.isDir(source) then
return {
source = source,
nodes = { },
}
end
return {
source = source
}
2016-12-11 19:24:52 +00:00
end
function linkfs.copy(node, s, t)
2018-01-24 22:39:38 +00:00
s = s:gsub(node.mountPoint, node.source, 1)
t = t:gsub(node.mountPoint, node.source, 1)
return fs.copy(s, t)
2016-12-11 19:24:52 +00:00
end
function linkfs.delete(node, dir)
2018-01-24 22:39:38 +00:00
if dir == node.mountPoint then
fs.unmount(node.mountPoint)
else
dir = dir:gsub(node.mountPoint, node.source, 1)
return fs.delete(dir)
end
2016-12-11 19:24:52 +00:00
end
function linkfs.find(node, spec)
2018-01-24 22:39:38 +00:00
spec = spec:gsub(node.mountPoint, node.source, 1)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local list = fs.find(spec)
for k,f in ipairs(list) do
list[k] = f:gsub(node.source, node.mountPoint, 1)
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 linkfs.move(node, s, t)
2018-01-24 22:39:38 +00:00
s = s:gsub(node.mountPoint, node.source, 1)
t = t:gsub(node.mountPoint, node.source, 1)
return fs.move(s, t)
2016-12-11 19:24:52 +00:00
end
return linkfs