add partition manager for OpenOS. basically the same as the PsychOS version

This commit is contained in:
Izaya 2023-10-11 10:18:57 +10:00
parent d3c053b661
commit 7f9c8d410d
1 changed files with 49 additions and 0 deletions

49
partition/partman.lua Normal file
View File

@ -0,0 +1,49 @@
local vcomponent = require "vcomponent"
local component = require "component"
local diskpart = require "diskpart"
local event = require "event"
local listeners = {}
local function attachDrive(_,a,t)
if t ~= "drive" then return end
for k,v in ipairs(diskpart.getPartitions(a)) do
if v[4] > 0 then
vcomponent.register(string.format("%s/%i",a,k), "partition", diskpart.proxyPartition(a, k))
local w,l = pcall(require,string.format("fs.%s",v[2]))
if w and l and l.mount then
local w, f = pcall(l.mount, string.format("%s/%i",a,k))
if w then
vcomponent.register(string.format("%s/%i/%s",a,k,v[2]), f.type or "filesystem", f)
end
end
end
end
end
local function detachDrive(_,a,t)
if t ~= "drive" then return end
for ca,t in component.list() do
if (t == "partition" or t == "filesystem") and ca:sub(1,a:len()) == a then
vcomponent.unregister(ca)
end
end
end
function start()
listeners[#listeners+1] = event.listen("component_added", attachDrive)
listeners[#listeners+1] = event.listen("component_removed", detachDrive)
for k,v in component.list("drive",true) do
attachDrive(nil,k,v)
end
end
function stop()
for k,v in ipairs(listeners) do
event.cancel(v)
listeners[k] = nil
end
for k,v in component.list("drive",true) do
detachDrive(nil,k,"drive")
end
end