finished vfs for a while

This commit is contained in:
2026-02-12 11:43:41 -05:00
parent 1c4f48bd65
commit 33cd291c21
31 changed files with 1083 additions and 1097 deletions

View File

@@ -1,5 +1,5 @@
--:Minify:--
local BOOT_DRIVE_PATH=({...})[1] or "/$"
-- :Minify:--
local BOOT_DRIVE_PATH = ({...})[1] or "/$"
---@diagnostic disable-next-line: undefined-global
local term = term
local os = os
@@ -38,7 +38,7 @@ local function write(text)
y = y + 1
end
if y-1 >= h then
if y - 1 >= h then
term.scroll(1)
y = h
term.setCursorPos(x, y)
@@ -61,7 +61,7 @@ end
term.setCursorBlink(false)
local ok, err = xpcall(function()
local apis={BOOT_DRIVE_PATH=BOOT_DRIVE_PATH}
local apis = {BOOT_DRIVE_PATH = BOOT_DRIVE_PATH}
local lua = {
coroutine = true,
@@ -93,14 +93,14 @@ local ok, err = xpcall(function()
tostring = true,
type = true,
xpcall = true,
_G=true
_G = true
}
local debug = debug
for i,v in pairs(_G) do
if not lua[i] or lua[i]==nil then
apis[i]=v
_G[i]=nil
for i, v in pairs(_G) do
if not lua[i] or lua[i] == nil then
apis[i] = v
_G[i] = nil
end
end
@@ -109,18 +109,18 @@ local ok, err = xpcall(function()
while stoptime > apis.os.clock() do end
end
apis.term.setPaletteColor(0x1, 0xFFFFFF) -- #000000
apis.term.setPaletteColor(0x2, 0xFF0000) -- #FFFFFF
apis.term.setPaletteColor(0x4, 0x00FF00) -- #FF0000
apis.term.setPaletteColor(0x8, 0x0000FF) -- #00FF00
apis.term.setPaletteColor(0x10, 0x00FFFF) -- #0000FF
apis.term.setPaletteColor(0x20, 0xFF00FF) -- #00FFFF
apis.term.setPaletteColor(0x40, 0xFFFF00) -- #FF00FF
apis.term.setPaletteColor(0x80, 0xFF6D00) -- #FFFF00
apis.term.setPaletteColor(0x100, 0x6DFF55) -- #FF6D00
apis.term.setPaletteColor(0x200, 0x24FFFF) -- #6DFF55
apis.term.setPaletteColor(0x400, 0x924900) -- #24FFFF
apis.term.setPaletteColor(0x800, 0x6D6D55) -- #924900
apis.term.setPaletteColor(0x1, 0xFFFFFF) -- #000000
apis.term.setPaletteColor(0x2, 0xFF0000) -- #FFFFFF
apis.term.setPaletteColor(0x4, 0x00FF00) -- #FF0000
apis.term.setPaletteColor(0x8, 0x0000FF) -- #00FF00
apis.term.setPaletteColor(0x10, 0x00FFFF) -- #0000FF
apis.term.setPaletteColor(0x20, 0xFF00FF) -- #00FFFF
apis.term.setPaletteColor(0x40, 0xFFFF00) -- #FF00FF
apis.term.setPaletteColor(0x80, 0xFF6D00) -- #FFFF00
apis.term.setPaletteColor(0x100, 0x6DFF55) -- #FF6D00
apis.term.setPaletteColor(0x200, 0x24FFFF) -- #6DFF55
apis.term.setPaletteColor(0x400, 0x924900) -- #24FFFF
apis.term.setPaletteColor(0x800, 0x6D6D55) -- #924900
apis.term.setPaletteColor(0x1000, 0xDBDBAA) -- #6D6D55
apis.term.setPaletteColor(0x2000, 0x6D00FF) -- #DBDBAA
apis.term.setPaletteColor(0x4000, 0xB6FF00) -- #6D00FF
@@ -128,28 +128,23 @@ local ok, err = xpcall(function()
local function getFile(path)
local file = apis.fs.open(path, "r")
if not file then displaySuperBadError("Could not open file: "..path) end
if not file then
displaySuperBadError("Could not open file: " .. path)
end
local content = file.readAll()
file.close()
return content
end
local Kernel = load(getFile(BOOT_DRIVE_PATH.."/boot/kernel.lua"),"@Kernel")
local initFs = load(getFile(BOOT_DRIVE_PATH.."/boot/cct/initdisks"),"@Init_disks")(apis)
local fs = load(getFile(BOOT_DRIVE_PATH.."/boot/initfs"),"@InitFs")()
local key = load(getFile(BOOT_DRIVE_PATH.."/boot/cct/keys.lua"),"@keyhelper")(apis)
if not Kernel then
displaySuperBadError("Could not load kernel.")
end
if not initFs then
displaySuperBadError("Could not load initdisks.")
end
if not fs then
displaySuperBadError("Could not load initfs.")
end
if not key then
displaySuperBadError("Could not load key helper.")
end
local Kernel = load(getFile(BOOT_DRIVE_PATH .. "/boot/kernel.lua"),"@Kernel")
local initFs = load(getFile(BOOT_DRIVE_PATH .. "/boot/cct/initdisks"),"@Init_disks")(apis)
local fs = load(getFile(BOOT_DRIVE_PATH .. "/boot/initfs"), "@InitFs")()
local key = load(getFile(BOOT_DRIVE_PATH .. "/boot/cct/keys.lua"),"@keyhelper")(apis)
if not Kernel then displaySuperBadError("Could not load kernel.") end
if not initFs then displaySuperBadError("Could not load initdisks.") end
if not fs then displaySuperBadError("Could not load initfs.") end
if not key then displaySuperBadError("Could not load key helper.") end
local eventQueue = {}
@@ -159,7 +154,7 @@ local ok, err = xpcall(function()
local computer = {
time = function() return apis.os.epoch("utc") end,
clock = function() return apis.os.clock()*1000 end,
clock = function() return apis.os.clock() * 1000 end,
shutdown = apis.os.shutdown,
reboot = apis.os.reboot,
getMachineEvent = function()
@@ -169,36 +164,34 @@ local ok, err = xpcall(function()
return nil
end
end,
getEEPROM = function()
return getFile("/startup.lua")
end,
setEEPROM = function(_,text)
getEEPROM = function() return getFile("/startup.lua") end,
setEEPROM = function(_, text)
local h = apis.fs.open("/startup.lua", "w")
h.write(text)
h.close()
end
}
local icolors={
[0x1] =1, -- #000000
[0x2] =2, -- #FFFFFF
[0x4] =3, -- #FF0000
[0x8] =4, -- #00FF00
[0x10] =5, -- #0000FF
[0x20] =6, -- #00FFFF
[0x40] =7, -- #FF00FF
[0x80] =8, -- #FFFF00
[0x100] =9, -- #FF6D00
[0x200] =10, -- #6DFF55
[0x400] =11, -- #24FFFF
[0x800] =12, -- #924900
[0x1000] =13, -- #6D6D55
[0x2000] =14, -- #DBDBAA
[0x4000] =15, -- #6D00FF
[0x8000] =16 -- #B6FF00
local icolors = {
[0x1] = 1, -- #000000
[0x2] = 2, -- #FFFFFF
[0x4] = 3, -- #FF0000
[0x8] = 4, -- #00FF00
[0x10] = 5, -- #0000FF
[0x20] = 6, -- #00FFFF
[0x40] = 7, -- #FF00FF
[0x80] = 8, -- #FFFF00
[0x100] = 9, -- #FF6D00
[0x200] = 10, -- #6DFF55
[0x400] = 11, -- #24FFFF
[0x800] = 12, -- #924900
[0x1000] = 13, -- #6D6D55
[0x2000] = 14, -- #DBDBAA
[0x4000] = 15, -- #6D00FF
[0x8000] = 16 -- #B6FF00
}
local colors={
local colors = {
0x0001, -- #000000
0x0002, -- #FFFFFF
0x0004, -- #FF0000
@@ -214,7 +207,7 @@ local ok, err = xpcall(function()
0x1000, -- #6D6D55
0x2000, -- #DBDBAA
0x4000, -- #6D00FF
0x8000 -- #B6FF00
0x8000 -- #B6FF00
}
apis.term.setBackgroundColor(0x8000)
@@ -224,21 +217,33 @@ local ok, err = xpcall(function()
local kernelCoro = coroutine.create(function()
---@diagnostic disable-next-line: param-type-mismatch
local ok, err = xpcall(Kernel, debug.traceback, apis, initFs, "cct", "/sbin/init", {
print=function(_,text) write(text.."\n") end,
printInline=function(_,text) write(text) end,
clear=function() apis.term.clear() apis.term.setCursorPos(1,1) end,
setCursorPos=function(_,x,y) apis.term.setCursorPos(x,y) end,
getCursorPos=function() return apis.term.getCursorPos() end,
getSize=function() return apis.term.getSize() end,
setBackgroundColor=function(_,color) apis.term.setBackgroundColor(colors[color]) end,
setTextColor=function(_,color) apis.term.setTextColor(colors[color]) end,
getBackgroundColor=function() return icolors[apis.term.getBackgroundColor()] end,
getTextColor=function() return icolors[apis.term.getTextColor()] end
local ok, err = xpcall(Kernel, debug.traceback, apis, initFs, "cct", "/sbin/init",
{
print = function(_, text) write(text .. "\n") end,
printInline = function(_, text) write(text) end,
clear = function()
apis.term.clear()
apis.term.setCursorPos(1, 1)
end,
setCursorPos = function(_, x, y)
apis.term.setCursorPos(x, y)
end,
getCursorPos = function() return apis.term.getCursorPos() end,
getSize = function() return apis.term.getSize() end,
setBackgroundColor = function(_, color)
apis.term.setBackgroundColor(colors[color])
end,
setTextColor = function(_, color)
apis.term.setTextColor(colors[color])
end,
getBackgroundColor = function()
return icolors[apis.term.getBackgroundColor()]
end,
getTextColor = function()
return icolors[apis.term.getTextColor()]
end
}, computer, fs, "$")
if not ok then
displaySuperBadError(err)
end
if not ok then displaySuperBadError(err) end
end)
-- time is in milliseconds
@@ -250,9 +255,9 @@ local ok, err = xpcall(function()
end
end, "", 1000)
local ret = {coroutine.resume(co, ...)}
if ret[1] and ret[2]=="timeout" then
if ret[1] and ret[2] == "timeout" then
return "timeout"
elseif ret[1]==false then
elseif ret[1] == false then
return "error", ret[2]
else
debug.sethook(co)
@@ -260,7 +265,7 @@ local ok, err = xpcall(function()
end
end
write("Loaded in "..tostring(apis.os.clock()).." seconds.\n")
write("Loaded in " .. tostring(apis.os.clock()) .. " seconds.\n")
while true do
local status, err = coroutine.resumeWithTimeout(kernelCoro, 50)
@@ -279,17 +284,15 @@ local ok, err = xpcall(function()
elseif event[1] == "disk_eject" then
queueEvent("componentRemoved", "disk")
elseif event[1] == "NoSleep" then
exit=true
exit = true
end
end
if status == "error" or coroutine.status(kernelCoro)=="dead" then
displaySuperBadError("Kernel error: "..tostring(err))
if status == "error" or coroutine.status(kernelCoro) == "dead" then
displaySuperBadError("Kernel error: " .. tostring(err))
coroutine.yield("key")
end
end
end, debug.traceback)
if not ok then
displaySuperBadError("Fatal error during boot: "..err)
end
while true do coroutine.yield() end
if not ok then displaySuperBadError("Fatal error during boot: " .. err) end
while true do coroutine.yield() end

View File

@@ -16,7 +16,6 @@ local BOOT_DRIVE_PATH=({...})[1] or "/$"
-- * `turtle.equip[Left|Right]`
-- Licensed under the MIT license
local args = {...}
if _HOST:find("UnBIOS") then return end
local keptAPIs = {keys=true, bit32 = true, bit = true, ccemux = true, config = true, coroutine = true, debug = true, fs = true, http = true, mounter = true, os = true, periphemu = true, peripheral = true, redstone = true, rs = true, term = true, utf8 = true, _HOST = true, _CC_DEFAULT_SETTINGS = true, _CC_DISABLE_LUA51_FEATURES = true, _VERSION = true, assert = true, collectgarbage = true, error = true, gcinfo = true, getfenv = true, getmetatable = true, ipairs = true, __inext = true,load = true, loadstring = true, math = true, newproxy = true, next = true, pairs = true, pcall = true, rawequal = true, rawget = true, rawlen = true, rawset = true, select = true, setfenv = true, setmetatable = true, string = true, table = true, tonumber = true, tostring = true, type = true, unpack = true, xpcall = true, turtle = true, pocket = true, commands = true, _G = true}
local t = {}
for k in pairs(_G) do if not keptAPIs[k] then table.insert(t, k) end end
@@ -32,7 +31,6 @@ if _G.commands then _G.commands = _G.commands.native end
if _G.turtle then _G.turtle.native, _G.turtle.craft = nil end
local delete = {os = {"version", "pullEventRaw", "pullEvent", "run", "loadAPI", "unloadAPI", "sleep"}, http = _G.http and {"get", "post", "put", "delete", "patch", "options", "head", "trace", "listen", "checkURLAsync", "websocketAsync"}, fs = {"complete", "isDriveRoot"}}
for k,v in pairs(delete) do for _,a in ipairs(v) do _G[k][a] = nil end end
_G._HOST = _G._HOST .. " (UnBIOS)"
-- Set up TLCO
-- This functions by crashing `rednet.run` by removing `os.pullEventRaw`. Normally
-- this would cause `parallel` to throw an error, but we replace `error` with an

View File

@@ -1,18 +1,17 @@
--:Minify:--
-- :Minify:--
local apis = ({...})[1]
local BOOT_DRIVE_PATH=apis.BOOT_DRIVE_PATH or "/$"
local BOOT_DRIVE_PATH = apis.BOOT_DRIVE_PATH or "/$"
local fs = apis.fs
local native = apis.peripheral
local peripheral = {}
local sides = {"top", "bottom", "left", "right", "front", "back"}
function peripheral.getType(name)
if native.isPresent(name) then
return native.getType(name)
end
if native.isPresent(name) then return native.getType(name) end
for n = 1, #sides do
local side = sides[n]
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
if native.hasType(side, "peripheral_hub") and
native.call(side, "isPresentRemote", name) then
return native.call(side, "getTypeRemote", name)
end
end
@@ -23,9 +22,7 @@ function peripheral.getNames()
local names = {}
for n = 1, #sides do
local side = sides[n]
if native.isPresent(side) then
table.insert(names, side)
end
if native.isPresent(side) then table.insert(names, side) end
if native.hasType(side, "peripheral_hub") then
local hubSides = native.call(side, "getConnectedSides")
for _, hubSide in ipairs(hubSides) do
@@ -36,37 +33,30 @@ function peripheral.getNames()
return names
end
---------------------------------------------------------
-- STORAGE
---------------------------------------------------------
local disks = {} -- real disks
local internal = {} -- "$" disk
local disks = {}
local internal = {}
---------------------------------------------------------
-- HELPERS
---------------------------------------------------------
local function norm(path)
if not path or path == "" then return "/" end
return fs.combine("/", path)
end
--- Creates a disk object given a base path
local function createDisk(id, basePath, readonly, periph)
basePath = norm(basePath)
local disk = {address=id,isReadOnly=function() return readonly end}
local disk = {address = id, isReadOnly = function() return readonly end}
function disk:spaceUsed()
return fs.getCapacity(basePath) - fs.getFreeSpace(basePath)
end
function disk:spaceTotal()
return fs.getCapacity(basePath)
end
function disk:spaceTotal() return fs.getCapacity(basePath) end
function disk:list(path)
local p = fs.combine(basePath, path)
if not fs.exists(p) or not fs.isDir(p) then return nil, "not directory" end
if not fs.exists(p) or not fs.isDir(p) then
return nil, "not directory"
end
return fs.list(p)
end
@@ -103,13 +93,9 @@ local function createDisk(id, basePath, readonly, periph)
return true
end
function disk:setLabel(label)
periph.setLabel(label)
end
function disk:setLabel(label) periph.setLabel(label) end
function disk:getLabel(label)
return periph.getLabel()
end
function disk:getLabel(label) return periph.getLabel() end
function disk:attributes(path)
local p = fs.combine(basePath, path)
@@ -120,39 +106,30 @@ local function createDisk(id, basePath, readonly, periph)
local p = fs.combine(basePath, path)
return fs.open(p, mode)
end
return disk
end
---------------------------------------------------------
-- INTERNAL DISK "$" (mapped to "/")
---------------------------------------------------------
internal["$"] = createDisk("$", BOOT_DRIVE_PATH, false, {
setLabel=function(label)
setLabel = function(label)
local h = fs.open("/.label", "w")
h.write(label)
h.close()
end,
getLabel=function()
getLabel = function()
local h = fs.open("/.label", "r")
if not h then return "$" end
local label = h.readAll()
h.close()
return label
end
})
---------------------------------------------------------
-- SCAN REAL DISK PERIPHERALS
---------------------------------------------------------
local function refresh()
-- remove disks that no longer exist
for id, _ in pairs(disks) do
if not peripheral.getType(id) then
disks[id] = nil
end
if not peripheral.getType(id) then disks[id] = nil end
end
-- detect new disks
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "disk" then
if not disks[name] then
@@ -165,28 +142,14 @@ local function refresh()
end
end
---------------------------------------------------------
-- ITERATOR
---------------------------------------------------------
local function iter()
refresh()
-- first internal
local combined = {}
for id, obj in pairs(internal) do
combined[id] = obj
end
for id, obj in pairs(disks) do
combined[id] = obj
end
for id, obj in pairs(internal) do combined[id] = obj end
for id, obj in pairs(disks) do combined[id] = obj end
return pairs(combined)
end
---------------------------------------------------------
-- MODULE RETURN
---------------------------------------------------------
return {
refresh = refresh,
list = iter
}
return {refresh = refresh, list = iter}

View File

@@ -1,6 +1,6 @@
--:Minify:--
local apis=...
local keys=apis.keys
-- :Minify:--
local apis = ...
local keys = apis.keys
local tKeys = {}
tKeys[keys.space] = ' '
tKeys[keys.grave] = '`'
@@ -63,13 +63,13 @@ tKeys[keys.pageUp] = '\x1b[5~'
tKeys[keys.pageDown] = '\x1b[6~'
tKeys[keys.home] = '\x1b[1~'
tKeys[keys["end"]] = '\x1b[4~'
--tKeys[keys.capsLock] = '\x1b[capsLock'
--tKeys[keys.scrollLock] = '\x1b[scrollLock'
--tKeys[keys.numLock] = '\x1b[numLock'
--if keys.printScreen then
-- tKeys[keys.capsLock] = '\x1b[capsLock'
-- tKeys[keys.scrollLock] = '\x1b[scrollLock'
-- tKeys[keys.numLock] = '\x1b[numLock'
-- if keys.printScreen then
-- tKeys[keys.printScreen] = '\x1b[printScreen'
--end
--tKeys[keys.pause] = '\x1b[pause'
-- end
-- tKeys[keys.pause] = '\x1b[pause'
tKeys[keys.f1] = '\x1b[11~'
tKeys[keys.f2] = '\x1b[12~'
tKeys[keys.f3] = '\x1b[13~'
@@ -82,19 +82,19 @@ tKeys[keys.f9] = '\x1b[20~'
tKeys[keys.f10] = '\x1b[21~'
tKeys[keys.f11] = '\x1b[23~'
tKeys[keys.f12] = '\x1b[24~'
--tKeys[keys.f13] = '\x1b[25~'
--tKeys[keys.f14] = '\x1b[26~'
--tKeys[keys.f15] = '\x1b[28~'
--tKeys[keys.f16] = '\x1b[29~'
--tKeys[keys.f17] = '\x1b[31~'
--tKeys[keys.f18] = '\x1b[32~'
--tKeys[keys.f19] = '\x1b[33~'
--tKeys[keys.f20] = '\x1b[34~'
--tKeys[keys.f21] = '\x1b[42~'
--tKeys[keys.f22] = '\x1b[43~'
--tKeys[keys.f23] = '\x1b[44~'
--tKeys[keys.f24] = '\x1b[45~'
--tKeys[keys.f25] = '\x1b[46~'
-- tKeys[keys.f13] = '\x1b[25~'
-- tKeys[keys.f14] = '\x1b[26~'
-- tKeys[keys.f15] = '\x1b[28~'
-- tKeys[keys.f16] = '\x1b[29~'
-- tKeys[keys.f17] = '\x1b[31~'
-- tKeys[keys.f18] = '\x1b[32~'
-- tKeys[keys.f19] = '\x1b[33~'
-- tKeys[keys.f20] = '\x1b[34~'
-- tKeys[keys.f21] = '\x1b[42~'
-- tKeys[keys.f22] = '\x1b[43~'
-- tKeys[keys.f23] = '\x1b[44~'
-- tKeys[keys.f24] = '\x1b[45~'
-- tKeys[keys.f25] = '\x1b[46~'
-- Numpad
tKeys[keys.numPad0] = '0'
@@ -108,15 +108,15 @@ tKeys[keys.numPad7] = '7'
tKeys[keys.numPad8] = '8'
tKeys[keys.numPad9] = '9'
--tKeys[340] = 'leftShift'
--tKeys[341] = 'leftCtrl'
--tKeys[342] = 'leftAlt'
--tKeys[343] = 'leftSuper'
--tKeys[344] = 'rightShift'
--tKeys[345] = 'rightCtrl'
--tKeys[346] = 'rightAlt'
--tKeys[347] = 'rightSuper'
--tKeys[348] = 'menu'
-- tKeys[340] = 'leftShift'
-- tKeys[341] = 'leftCtrl'
-- tKeys[342] = 'leftAlt'
-- tKeys[343] = 'leftSuper'
-- tKeys[344] = 'rightShift'
-- tKeys[345] = 'rightCtrl'
-- tKeys[346] = 'rightAlt'
-- tKeys[347] = 'rightSuper'
-- tKeys[348] = 'menu'
local shift = false
local ctrl = false
@@ -178,26 +178,34 @@ end
local function p()
local str = ""
if alt then
str=str.."\x1b"
end
if ctrl then
str=str.."^"
end
if alt then str = str .. "\x1b" end
if ctrl then str = str .. "^" end
return str
end
return function(event, q)
if event[1] == "key" then
if event[2]==keys.leftCtrl or event[2]==keys.rightCtrl then ctrl=true return
elseif event[2]==keys.leftAlt or event[2]==keys.rightAlt then alt=true return
elseif event[2]==keys.leftShift or event[2]==keys.rightCtrl then shift=true return
if event[2] == keys.leftCtrl or event[2] == keys.rightCtrl then
ctrl = true
return
elseif event[2] == keys.leftAlt or event[2] == keys.rightAlt then
alt = true
return
elseif event[2] == keys.leftShift or event[2] == keys.rightCtrl then
shift = true
return
end
q("keyTyped", 1, p()..s(tKeys[event[2]]))
q("keyTyped", 1, p() .. s(tKeys[event[2]]))
elseif event[1] == "key_up" then
if event[2]==keys.leftCtrl or event[2]==keys.rightCtrl then ctrl=false return
elseif event[2]==keys.leftAlt or event[2]==keys.rightAlt then alt=false return
elseif event[2]==keys.leftShift or event[2]==keys.rightCtrl then shift=false return
if event[2] == keys.leftCtrl or event[2] == keys.rightCtrl then
ctrl = false
return
elseif event[2] == keys.leftAlt or event[2] == keys.rightAlt then
alt = false
return
elseif event[2] == keys.leftShift or event[2] == keys.rightCtrl then
shift = false
return
end
end
end
end