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