2019-10-27 23:34:02 +11:00
devfs = { }
devfs.files = { }
devfs.fds = { }
devfs.nextfd = 0
devfs.component = { }
local function rfalse ( )
return false
end
2019-11-09 13:15:50 +11:00
local function rzero ( )
return 0
end
2019-10-27 23:34:02 +11:00
function devfs . component . getLabel ( )
return " devfs "
end
2019-11-09 13:15:50 +11:00
devfs.component . spaceUsed , devfs.component . spaceTotal , devfs.component . isReadOnly , devfs.component . isDirectory , devfs.component . size , devfs.component . setLabel = rzero , rzero , rfalse , rfalse , rzero , rfalse
2019-10-27 23:34:02 +11:00
function devfs . component . exists ( fname )
return devfs.files [ fname ] ~= nil
end
function devfs . component . list ( )
local t = { }
for k , v in pairs ( devfs.files ) do
t [ # t + 1 ] = k
end
return t
end
function devfs . component . open ( fname , mode )
fname = fname : gsub ( " / " , " " )
if devfs.files [ fname ] then
local r , w , c , s = devfs.files [ fname ] ( mode )
devfs.fds [ devfs.nextfd ] = { [ " read " ] = r or rfalse , [ " write " ] = w or rfalse , [ " seek " ] = s or rfalse , [ " close " ] = c or rfalse }
devfs.nextfd = devfs.nextfd + 1
return devfs.nextfd - 1
end
return false
end
function devfs . component . read ( fd , count )
if devfs.fds [ fd ] then
return devfs.fds [ fd ] . read ( count )
end
end
function devfs . component . write ( fd , data )
if devfs.fds [ fd ] then
return devfs.fds [ fd ] . write ( data )
end
end
function devfs . component . close ( fd )
if devfs.fds [ fd ] then
devfs.fds [ fd ] . close ( )
end
devfs.fds [ fd ] = nil
end
function devfs . component . seek ( fd , ... )
if devfs.fds [ fd ] then
return devfs.fds [ fd ] . seek ( ... )
end
end
function devfs . component . remove ( fname )
end
2020-03-26 17:25:36 +11:00
devfs.component . address = " devfs "
devfs.component . type = " devfs "
2019-10-27 23:34:02 +11:00
2020-05-12 17:55:05 +10:00
function devfs . register ( fname , fopen ) -- string function -- -- Register a new devfs node with the name *fname* that will run the function *fopen* when opened. This function should return a function for read, a function for write, function for close, and optionally, a function for seek, in that order.
2019-10-27 23:34:02 +11:00
devfs.files [ fname ] = fopen
end
2019-11-08 12:42:49 +11:00
fs.makeDirectory ( " /dev " )
fs.mount ( " /dev " , devfs.component )
2019-10-27 23:34:02 +11:00
--#include "module/devfs/null.lua"