OC-misc/cpioloader/tapeloader.lua

61 lines
1.3 KiB
Lua
Raw Normal View History

local fs = component.proxy(computer.tmpAddress())
local init = function() end
do
local tape = component.proxy(component.list("tape_drive")())
local rev,namesize,filesize,name,mode = false, 0, 0, "", 0
local function readint(amt)
local tmp = 0
for i=(rev and amt) or 1, (rev and 1) or amt, (rev and -1) or 1 do
tmp = tmp | (tape.read(1):byte() << ((i-1)*8))
end
return tmp
end
local function fwrite()
local dir = name:match("(.+)/.*%.?.+")
if (dir) then
fs.makeDirectory("/"..dir)
end
local fh = fs.open(name, "w")
fs.write(fh,tape.read(filesize))
fs.close(fh)
end
tape.seek(-math.huge)
while true do
local magic = readint(2)
if magic == 51057 then rev = true end
tape.seek(4)
mode = readint(2)
tape.seek(12)
namesize = readint(2)
filesize = (readint(2) << 16) | readint(2)
name = tape.read(namesize):sub(1, namesize-1)
if name == "TRAILER!!!" then break end
if (namesize % 2 ~= 0) then
tape.seek(1)
end
if (mode & 32768 ~= 0) then
fwrite()
end
if (filesize % 2 ~= 0) then
tape.seek(1)
end
computer.beep()
end
end
do
local f=fs.open("/init.lua","rb")
if not f then error("no init.lua") end
local initstr = ""
local data = ""
repeat
initstr = initstr .. data
data = fs.read(f,2048)
until data == nil
init = load(initstr)
end
computer.getBootAddress = computer.tmpAddress
init()