PsychOSPackages/diskpart/lib/part/mtpt.lua

57 lines
1.4 KiB
Lua

local mtpt = {}
local eformat = "c20c4>I4>I4"
local function aop(td,n,mn,...)
local rts = td.seek(-math.huge)
td.seek(n)
local rv = {td[mn](...)}
td.seek(-math.huge)
td.seek(rts)
return table.unpack(rv)
end
function mtpt.parse(s)
local rt = {}
for i = 1, s:len(), 32 do
local n, t, start, length = string.unpack(eformat, s, i)
n = n:gsub("\0", "")
if n ~= "" then
rt[#rt+1] = {n,t,start,length}
end
end
return rt
end
function mtpt.generate(pt)
local ps = ""
for k,v in ipairs(pt) do
ps = ps .. string.pack(eformat, table.unpack(v))
end
return ps
end
function mtpt.getPartitions(drive)
local rv
if drive.type == "tape_drive" then
local ts = drive.getSize()
local lso = (math.floor(ts / 512) - 1) * 512
rv = mtpt.parse(aop(drive, lso, "read", ts - lso))
else
rv = mtpt.parse(drive.readSector(drive.getCapacity() / drive.getSectorSize()))
end
return rv[1][2] == "mtpt" and rv
end
function mtpt.setPartitions(drive, pt, name)
name = name or ""
if pt[1][2] ~= "mtpt" then table.insert(pt, 1, {name, "mtpt", 0, 0}) end
local ns = mtpt.generate(pt)
if drive.type == "tape_drive" then
local ts = drive.getSize()
local lso = (math.floor(ts / 512) - 1) * 512
return aop(drive, lso, "write", ns .. ("\0"):rep(ts - lso - #ns))
end
return drive.writeSector(drive.getCapacity() / drive.getSectorSize(), ns .. ("\0"):rep(drive.getSectorSize() - #ns))
end
return mtpt