1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-20 07:34:50 +00:00
opus/sys/apis/fs/ramfs.lua

148 lines
2.3 KiB
Lua
Raw Normal View History

local Util = require('util')
2017-10-08 21:45:01 +00:00
local fs = _G.fs
2016-12-11 19:24:52 +00:00
local ramfs = { }
2017-10-08 21:45:01 +00:00
function ramfs.mount(_, nodeType)
2018-01-24 22:39:38 +00:00
if nodeType == 'directory' then
return {
nodes = { },
size = 0,
}
elseif nodeType == 'file' then
return {
size = 0,
}
end
error('ramfs syntax: [directory, file]')
2016-12-11 19:24:52 +00:00
end
function ramfs.delete(node, dir)
2018-01-24 22:39:38 +00:00
if node.mountPoint == dir then
fs.unmount(node.mountPoint)
end
2016-12-11 19:24:52 +00:00
end
function ramfs.exists(node, fn)
2018-01-24 22:39:38 +00:00
return node.mountPoint == fn
2016-12-11 19:24:52 +00:00
end
function ramfs.getSize(node)
2018-01-24 22:39:38 +00:00
return node.size
2016-12-11 19:24:52 +00:00
end
function ramfs.isReadOnly()
2018-01-24 22:39:38 +00:00
return false
2016-12-11 19:24:52 +00:00
end
2017-10-08 21:45:01 +00:00
function ramfs.makeDir(_, dir)
2018-01-24 22:39:38 +00:00
fs.mount(dir, 'ramfs', 'directory')
2016-12-11 19:24:52 +00:00
end
function ramfs.isDir(node)
2018-01-24 22:39:38 +00:00
return not not node.nodes
2016-12-11 19:24:52 +00:00
end
function ramfs.getDrive()
2018-01-24 22:39:38 +00:00
return 'ram'
2016-12-11 19:24:52 +00:00
end
2017-10-08 21:45:01 +00:00
function ramfs.list(node, dir)
2018-01-24 22:39:38 +00:00
if node.nodes and node.mountPoint == dir then
local files = { }
for k in pairs(node.nodes) do
table.insert(files, k)
end
return files
end
error('Not a directory')
2016-12-11 19:24:52 +00:00
end
function ramfs.open(node, fn, fl)
2018-01-24 22:39:38 +00:00
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
2016-12-11 19:24:52 +00:00
end
return ramfs