galaxy brain FS upgrades

This commit is contained in:
Izaya 2019-11-08 12:42:49 +11:00
parent 00a0889842
commit b2d1379127
2 changed files with 36 additions and 37 deletions

View File

@ -63,6 +63,7 @@ function devfs.register(fname,fopen) -- Register a new devfs node with the name
devfs.files[fname] = fopen devfs.files[fname] = fopen
end end
fs.mounts.dev = devfs.component fs.makeDirectory("/dev")
fs.mount("/dev",devfs.component)
--#include "module/devfs/null.lua" --#include "module/devfs/null.lua"

View File

@ -1,5 +1,6 @@
do
fs = {} fs = {}
fs.mounts = {} local fsmounts = {}
-- basics -- basics
function fs.segments(path) -- splits *path* on each / function fs.segments(path) -- splits *path* on each /
@ -12,19 +13,24 @@ end
function fs.resolve(path) -- resolves *path* to a specific filesystem mount and path function fs.resolve(path) -- resolves *path* to a specific filesystem mount and path
if not path or path == "." then path = os.getenv("PWD") end if not path or path == "." then path = os.getenv("PWD") end
if path:sub(1,1) ~= "/" then path=(os.getenv("PWD") or "").."/"..path end if path:sub(1,1) ~= "/" then path=(os.getenv("PWD") or "").."/"..path end
local segments, rpath = fs.segments(path), "/" local segments, rpath, rfs= fs.segments(path)
for i = 2, #segments do local rc = #segments
rpath = rpath .. segments[i] .. "/" dprint(rc)
for i = #segments, 1, -1 do
dprint("testing "..table.concat(segments, "/", 1, i),tostring(fsmounts[table.concat(segments, "/", 1, i)]))
if fsmounts[table.concat(segments, "/", 1, i)] ~= nil then
dprint("ret",table.concat(segments, "/", 1, i), table.concat(segments, "/", i+1))
return table.concat(segments, "/", 1, i), table.concat(segments, "/", i+1)
end end
rpath = rpath:match("(.+)/") or rpath end
return segments[1] or "root",rpath return "/", table.concat(segments,"/")
end end
-- generate some simple functions -- generate some simple functions
for k,v in pairs({"makeDirectory","exists","isDirectory","list","lastModified","remove","size","spaceUsed","isReadOnly","getLabel"}) do for k,v in pairs({"makeDirectory","exists","isDirectory","list","lastModified","remove","size","spaceUsed","isReadOnly","getLabel"}) do
fs[v] = function(path) fs[v] = function(path)
local fsi,path = fs.resolve(path) local fsi,path = fs.resolve(path)
return fs.mounts[fsi][v](path) return fsmounts[fsi][v](path)
end end
end end
@ -35,28 +41,28 @@ local function fread(self,length)
if type(length) == "number" then if type(length) == "number" then
local rstr, lstr = "", "" local rstr, lstr = "", ""
repeat repeat
lstr = fs.mounts[self.fs].read(self.fid,math.min(2^16,length-rstr:len())) or "" lstr = fsmounts[self.fs].read(self.fid,math.min(2^16,length-rstr:len())) or ""
rstr = rstr .. lstr rstr = rstr .. lstr
until rstr:len() == length or lstr == "" until rstr:len() == length or lstr == ""
return rstr return rstr
end end
return fs.mounts[self.fs].read(self.fid,length) return fsmounts[self.fs].read(self.fid,length)
end end
local function fwrite(self,data) local function fwrite(self,data)
fs.mounts[self.fs].write(self.fid,data) fsmounts[self.fs].write(self.fid,data)
end end
local function fseek(self,dist) local function fseek(self,dist)
fs.mounts[self.fs].seek(self.fid,dist) fsmounts[self.fs].seek(self.fid,dist)
end end
local function fclose(self) local function fclose(self)
fs.mounts[self.fs].close(self.fid) fsmounts[self.fs].close(self.fid)
end end
function fs.open(path,mode) -- opens file *path* with mode *mode* function fs.open(path,mode) -- opens file *path* with mode *mode*
mode = mode or "rb" mode = mode or "rb"
local fsi,path = fs.resolve(path) local fsi,path = fs.resolve(path)
if not fs.mounts[fsi] then return false end if not fsmounts[fsi] then return false end
local fid = fs.mounts[fsi].open(path,mode) local fid = fsmounts[fsi].open(path,mode)
if fid then if fid then
local fobj = {["fs"]=fsi,["fid"]=fid,["seek"]=fseek,["close"]=fclose} local fobj = {["fs"]=fsi,["fid"]=fid,["seek"]=fseek,["close"]=fclose}
if mode:find("r") then if mode:find("r") then
@ -85,7 +91,7 @@ function fs.rename(from,to) -- moves file *from* to *to*
local ofsi, opath = fs.resolve(from) local ofsi, opath = fs.resolve(from)
local dfsi, dpath = fs.resolve(to) local dfsi, dpath = fs.resolve(to)
if ofsi == dfsi then if ofsi == dfsi then
fs.mounts[ofsi].rename(opath,dpath) fsmounts[ofsi].rename(opath,dpath)
return true return true
end end
fs.copy(from,to) fs.copy(from,to)
@ -93,31 +99,23 @@ function fs.rename(from,to) -- moves file *from* to *to*
return true return true
end end
function fs.mount(path,proxy)
fs.mounts.temp = component.proxy(computer.tmpAddress()) if fs.isDirectory(path) then
if computer.getBootAddress then fsmounts[table.concat(fs.segments(path),"/")] = proxy
fs.mounts.boot = component.proxy(computer.getBootAddress())
end
for addr, _ in component.list("filesystem") do
fs.mounts[addr:sub(1,3)] = component.proxy(addr)
end
local function rf()
return false
end
fs.mounts.root = {}
for k,v in pairs(fs.mounts.temp) do
fs.mounts.root[k] = rf
end
function fs.mounts.root.list()
local t = {}
for k,v in pairs(fs.mounts) do
t[#t+1] = k
end
t.n = #t
return t
end
function fs.mounts.root.isReadOnly()
return true return true
end end
return false, "path is not a directory"
end
fsmounts["/"] = component.proxy(computer.tmpAddress())
fs.makeDirectory("temp")
if computer.getBootAddress then
fs.makeDirectory("boot")
fs.mount("boot",component.proxy(computer.getBootAddress()))
end
for addr, _ in component.list("filesystem") do
fs.makeDirectory(addr:sub(1,3))
fs.mount(addr:sub(1,3),component.proxy(addr))
end
end