2019-06-28 17:50:02 +00:00
|
|
|
local Util = require('opus.util')
|
2017-09-05 06:09:31 +00:00
|
|
|
|
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,
|
2020-04-27 21:44:09 +00:00
|
|
|
created = os.epoch('utc'),
|
|
|
|
modification = os.epoch('utc'),
|
2018-01-24 22:39:38 +00:00
|
|
|
}
|
|
|
|
elseif nodeType == 'file' then
|
|
|
|
return {
|
|
|
|
size = 0,
|
2020-04-27 21:44:09 +00:00
|
|
|
created = os.epoch('utc'),
|
|
|
|
modification = os.epoch('utc'),
|
2018-01-24 22:39:38 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
error('ramfs syntax: [directory, file]')
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2020-04-27 21:44:09 +00:00
|
|
|
function ramfs.attributes(node)
|
|
|
|
return {
|
|
|
|
created = node.created,
|
|
|
|
isDir = not not node.nodes,
|
|
|
|
modification = node.modification,
|
|
|
|
size = node.size,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
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
|
|
|
|
|
2020-06-01 05:50:30 +00:00
|
|
|
function ramfs.isDir(node, dir)
|
|
|
|
if node.mountPoint == dir then
|
|
|
|
return not not node.nodes
|
|
|
|
end
|
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
|
|
|
|
|
2019-11-08 01:00:54 +00:00
|
|
|
function ramfs.getFreeSpace()
|
|
|
|
return math.huge
|
|
|
|
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
|
|
|
|
|
2020-06-01 05:50:30 +00:00
|
|
|
local c = type(node.contents) == 'table'
|
|
|
|
and string.char(table.unpack(node.contents))
|
|
|
|
or node.contents
|
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local ctr = 0
|
|
|
|
local lines
|
|
|
|
return {
|
2020-06-01 05:50:30 +00:00
|
|
|
read = function(n)
|
|
|
|
n = n or 1
|
|
|
|
if ctr >= node.size then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local t = c:sub(ctr + 1, ctr + n)
|
|
|
|
ctr = ctr + n
|
|
|
|
return t
|
2019-12-05 20:44:35 +00:00
|
|
|
end,
|
2018-01-24 22:39:38 +00:00
|
|
|
readLine = function()
|
|
|
|
if not lines then
|
2020-06-01 05:50:30 +00:00
|
|
|
lines = Util.split(c)
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
ctr = ctr + 1
|
|
|
|
return lines[ctr]
|
|
|
|
end,
|
|
|
|
readAll = function()
|
2020-06-01 05:50:30 +00:00
|
|
|
return c
|
2018-01-24 22:39:38 +00:00
|
|
|
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
|
|
|
|
|
2020-06-01 05:50:30 +00:00
|
|
|
local c = node.contents
|
|
|
|
if type(node.contents) == 'string' then
|
|
|
|
c = { }
|
|
|
|
for i = 1, node.size do
|
|
|
|
c[i] = node.contents:sub(i, i):byte()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local ctr = 0
|
|
|
|
return {
|
2020-06-01 22:53:01 +00:00
|
|
|
readAll = function()
|
|
|
|
return string.char(table.unpack(c))
|
|
|
|
end,
|
2020-06-01 05:50:30 +00:00
|
|
|
read = function(n)
|
|
|
|
if n and n > 1 and ctr < node.size then
|
|
|
|
-- some programs open in rb, when it should have
|
|
|
|
-- been opened in r - attempt to support multiple read
|
|
|
|
-- if nils are present in data, this will fail
|
|
|
|
local t = string.char(table.unpack(c, ctr + 1, ctr + n))
|
|
|
|
ctr = ctr + n
|
|
|
|
return t
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
ctr = ctr + 1
|
2020-06-01 05:50:30 +00:00
|
|
|
return c[ctr]
|
2018-01-24 22:39:38 +00:00
|
|
|
end,
|
|
|
|
close = function()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
elseif fl == 'wb' then
|
|
|
|
node = fs.mount(fn, 'ramfs', 'file')
|
|
|
|
|
|
|
|
local c = { }
|
|
|
|
return {
|
|
|
|
write = function(b)
|
2020-06-01 05:50:30 +00:00
|
|
|
if type(b) == 'number' then
|
|
|
|
table.insert(c, b)
|
|
|
|
else
|
|
|
|
for i = 1, #b do
|
|
|
|
table.insert(c, b:sub(i, i):byte())
|
|
|
|
end
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
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
|