diff --git a/partition/partman.lua b/partition/partman.lua new file mode 100644 index 0000000..0de2c44 --- /dev/null +++ b/partition/partman.lua @@ -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