83 lines
1.5 KiB
Lua
83 lines
1.5 KiB
Lua
local fs = _G.fs
|
|
|
|
local linkfs = { }
|
|
|
|
-- TODO: implement broken links
|
|
|
|
local methods = { 'exists', 'getFreeSpace', 'getSize', 'attributes',
|
|
'isDir', 'isReadOnly', 'list', 'makeDir', 'open', 'getDrive' }
|
|
|
|
for _,m in pairs(methods) do
|
|
linkfs[m] = function(node, dir, ...)
|
|
dir = linkfs.resolve(node, dir)
|
|
return fs[m](dir, ...)
|
|
end
|
|
end
|
|
|
|
function linkfs.resolve(node, dir)
|
|
local mp = node.mountPoint
|
|
if dir:sub(1, #mp) == mp then
|
|
return node.source .. dir:sub(#mp + 1)
|
|
end
|
|
return dir
|
|
end
|
|
|
|
function linkfs.mount(path, source)
|
|
if not source then
|
|
error('Source is required')
|
|
end
|
|
source = fs.combine(source, '')
|
|
if not fs.exists(source) then
|
|
error('Source is missing')
|
|
end
|
|
if path == source then
|
|
return
|
|
end
|
|
if fs.isDir(source) then
|
|
return {
|
|
source = source,
|
|
nodes = { },
|
|
}
|
|
end
|
|
return {
|
|
source = source
|
|
}
|
|
end
|
|
|
|
function linkfs.copy(node, s, t)
|
|
s = linkfs.resolve(node, s)
|
|
t = linkfs.resolve(node, t)
|
|
return fs.copy(s, t)
|
|
end
|
|
|
|
function linkfs.delete(node, dir)
|
|
if dir == node.mountPoint then
|
|
fs.unmount(node.mountPoint)
|
|
else
|
|
dir = linkfs.resolve(node, dir)
|
|
return fs.delete(dir)
|
|
end
|
|
end
|
|
|
|
function linkfs.find(node, spec)
|
|
spec = linkfs.resolve(node, spec)
|
|
|
|
local list = fs.find(spec)
|
|
local src = node.source
|
|
local mp = node.mountPoint
|
|
for k,f in ipairs(list) do
|
|
if f:sub(1, #src) == src then
|
|
list[k] = mp .. f:sub(#src + 1)
|
|
end
|
|
end
|
|
|
|
return list
|
|
end
|
|
|
|
function linkfs.move(node, s, t)
|
|
s = linkfs.resolve(node, s)
|
|
t = linkfs.resolve(node, t)
|
|
return fs.move(s, t)
|
|
end
|
|
|
|
return linkfs |