mirror of
https://github.com/kepler155c/opus
synced 2024-12-28 01:20:27 +00:00
146 lines
2.6 KiB
Lua
146 lines
2.6 KiB
Lua
local Util = require('util')
|
|
|
|
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 = { }
|
|
for k,v in pairs(node.nodes) do
|
|
table.insert(files, k)
|
|
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
|