ramfs bugfixes

This commit is contained in:
kepler155c@gmail.com 2020-05-31 23:50:30 -06:00
parent 18b7f540ab
commit 4796e9e77a
1 changed files with 43 additions and 11 deletions

View File

@ -53,8 +53,10 @@ function ramfs.makeDir(_, dir)
fs.mount(dir, 'ramfs', 'directory') fs.mount(dir, 'ramfs', 'directory')
end end
function ramfs.isDir(node) function ramfs.isDir(node, dir)
return not not node.nodes if node.mountPoint == dir then
return not not node.nodes
end
end end
function ramfs.getDrive() function ramfs.getDrive()
@ -77,7 +79,6 @@ function ramfs.list(node, dir)
end end
function ramfs.open(node, fn, fl) function ramfs.open(node, fn, fl)
if fl ~= 'r' and fl ~= 'w' and fl ~= 'rb' and fl ~= 'wb' then if fl ~= 'r' and fl ~= 'w' and fl ~= 'rb' and fl ~= 'wb' then
error('Unsupported mode') error('Unsupported mode')
end end
@ -87,22 +88,31 @@ function ramfs.open(node, fn, fl)
return return
end end
local c = type(node.contents) == 'table'
and string.char(table.unpack(node.contents))
or node.contents
local ctr = 0 local ctr = 0
local lines local lines
return { return {
read = function() read = function(n)
ctr = ctr + 1 n = n or 1
return node.contents:sub(ctr, ctr) if ctr >= node.size then
return
end
local t = c:sub(ctr + 1, ctr + n)
ctr = ctr + n
return t
end, end,
readLine = function() readLine = function()
if not lines then if not lines then
lines = Util.split(node.contents) lines = Util.split(c)
end end
ctr = ctr + 1 ctr = ctr + 1
return lines[ctr] return lines[ctr]
end, end,
readAll = function() readAll = function()
return node.contents return c
end, end,
close = function() close = function()
lines = nil lines = nil
@ -134,11 +144,27 @@ function ramfs.open(node, fn, fl)
return return
end end
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
local ctr = 0 local ctr = 0
return { return {
read = function() 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
ctr = ctr + 1 ctr = ctr + 1
return node.contents[ctr] return c[ctr]
end, end,
close = function() close = function()
end, end,
@ -150,7 +176,13 @@ function ramfs.open(node, fn, fl)
local c = { } local c = { }
return { return {
write = function(b) write = function(b)
table.insert(c, b) 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
end, end,
flush = function() flush = function()
node.contents = c node.contents = c