print("Running CC:Tweaked vm bootstrap") print("Virtualizing filesystem") local oldfs = fs local newfs = {} -- Mount table local mounts = { ["/"] = { fs = "ram", root = "/" }, ["/computer/"] = { fs = "oldfs", root = "/root/" }, ["/computer_boot/"] = { fs = "oldfs", root = "/$/" }, ["/eeprom"] = { fs = "oldfs", root = "/eeprom" } } -- Backing filesystems local disks = { oldfs = oldfs, ram = { type = "dir", children = {} } } -- Normalize virtual paths local function normalize(path) path = tostring(path or ""):gsub("\\", "/") local parts = {} for part in path:gmatch("[^/]+") do if part == ".." then if #parts > 0 then table.remove(parts) end elseif part ~= "." then parts[#parts + 1] = part end end return "/" .. table.concat(parts, "/") end -- Resolve virtual path into physical filesystem path local function resolve(path) path = normalize(path) local selected local selectedLength = -1 local selectedMount for mount, info in pairs(mounts) do local prefix = mount == "/" and "/" or mount:gsub("/+$", "") local matches if prefix == "/" then matches = true else matches = path == prefix or path:sub(1, #prefix + 1) == prefix .. "/" end if matches and #prefix > selectedLength then selected = info selectedLength = #prefix selectedMount = prefix end end if not selected then error("No filesystem mounted for " .. path, 2) end local relative if selectedMount == "/" then relative = path:sub(2) else relative = path:sub(#selectedMount + 1):gsub("^/", "") end local physical = selected.root if relative ~= "" then physical = physical:gsub("/+$", "") .. "/" .. relative end if physical == "" then physical = "/" end return disks[selected.fs], relative, selected.fs, physical end -- RAM filesystem helpers local function getNode(root, path) local node = root if path == "" then return node end for part in path:gmatch("[^/]+") do if not node or node.type ~= "dir" then return nil end node = node.children[part] end return node end local function getParent(root, path) local parentPath, name = path:match("^(.*)/([^/]+)$") if not name then return nil end local parent = getNode(root, parentPath or "") if not parent or parent.type ~= "dir" then return nil end return parent, name end local function ensureDir(root, path) local node = root for part in path:gmatch("[^/]+") do if not node.children[part] then node.children[part] = { type = "dir", children = {} } end node = node.children[part] if node.type ~= "dir" then return false end end return true end local function cloneNode(node) if node.type == "file" then return { type = "file", data = node.data } end local copy = { type = "dir", children = {} } for name, child in pairs(node.children) do copy.children[name] = cloneNode(child) end return copy end -- RAM file handles local function makeHandle(node, mode) local position = (mode == "a" or mode == "ab") and #node.data or 0 local closed = false local handle = {} local function check() if closed then error("attempt to use a closed file", 3) end end function handle.read(count) check() if position >= #node.data then return nil end count = count or 1 local result = node.data:sub(position + 1, position + count) position = position + #result return result end function handle.readAll() check() local result = node.data:sub(position + 1) position = #node.data return result end function handle.readLine() check() if position >= #node.data then return nil end local ending = node.data:find("\n", position + 1, true) if ending then local result = node.data:sub(position + 1, ending - 1) position = ending return result end local result = node.data:sub(position + 1) position = #node.data return result end function handle.write(value) check() if mode == "r" or mode == "rb" then error("attempt to write to a read-only file", 2) end value = tostring(value) if mode == "a" or mode == "ab" then node.data = node.data .. value position = #node.data else node.data = node.data:sub(1, position) .. value .. node.data:sub(position + #value + 1) position = position + #value end end function handle.writeLine(value) handle.write(tostring(value) .. "\n") end function handle.flush() check() end function handle.seek(whence, offset) check() whence = whence or "cur" offset = offset or 0 if whence == "set" then position = offset elseif whence == "cur" then position = position + offset elseif whence == "end" then position = #node.data + offset else error("invalid whence", 2) end position = math.max(0, position) return position end function handle.close() closed = true end return handle end -- Basic path functions function newfs.combine(...) local args = {...} local result = table.concat(args, "/") return normalize(result) end function newfs.getName(path) return normalize(path):match("([^/]+)$") or "" end function newfs.getDir(path) return normalize(path):match("^(.*)/[^/]+$") or "/" end function newfs.getDrive(path) local _, _, disk = resolve(path) return disk end -- Filesystem information function newfs.exists(path) local root, relative, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.exists(physical) end return getNode(root, relative) ~= nil end function newfs.isDir(path) local root, relative, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.isDir(physical) end local node = getNode(root, relative) return node ~= nil and node.type == "dir" end function newfs.isReadOnly(path) local _, _, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.isReadOnly(physical) end return false end function newfs.getSize(path) local root, relative, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.getSize(physical) end local node = getNode(root, relative) if not node then return 0 end if node.type == "file" then return #node.data end local function calculate(n) local size = 0 for _, child in pairs(n.children) do if child.type == "file" then size = size + #child.data else size = size + calculate(child) end end return size end return calculate(node) end function newfs.getFreeSpace(path) local _, _, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.getFreeSpace(physical) end return math.huge end function newfs.getCapacity(path) local _, _, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.getCapacity(physical) end return math.huge end -- Directory operations function newfs.makeDir(path) local root, relative, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.makeDir(physical) end return ensureDir(root, relative) end function newfs.list(path) local root, relative, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.list(physical) end local node = getNode(root, relative) if not node or node.type ~= "dir" then error("Not a directory", 2) end local result = {} for name in pairs(node.children) do result[#result + 1] = name end table.sort(result) return result end function newfs.delete(path) local root, relative, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.delete(physical) end if relative == "" then error("Cannot delete filesystem root", 2) end local parent, name = getParent(root, relative) if parent then parent.children[name] = nil end end -- File operations function newfs.open(path, mode) mode = mode or "r" local root, relative, disk, physical = resolve(path) if disk == "oldfs" then return oldfs.open(physical, mode) end local node = getNode(root, relative) if mode == "r" or mode == "rb" then if not node or node.type ~= "file" then return nil end else if relative == "" then return nil end local parent, name = getParent(root, relative) if not parent then return nil end if not node then node = { type = "file", data = "" } parent.children[name] = node elseif node.type ~= "file" then return nil end if mode == "w" or mode == "wb" then node.data = "" end end return makeHandle(node, mode) end -- Copy and move operations function newfs.copy(from, to) local src, srcPath, srcDisk, srcPhysical = resolve(from) local dst, dstPath, dstDisk, dstPhysical = resolve(to) if srcDisk == dstDisk then if srcDisk == "oldfs" then return oldfs.copy(srcPhysical, dstPhysical) end local source = getNode(src, srcPath) if not source then error("Source does not exist", 2) end local parent, name = getParent(dst, dstPath) if not parent then error("Destination parent does not exist", 2) end parent.children[name] = cloneNode(source) return end local function copyNode(source, destination) if source.type == "file" then local handle = newfs.open(destination, "w") if not handle then error("Cannot create destination: " .. destination) end handle.write(source.data) handle.close() return end newfs.makeDir(destination) for name, child in pairs(source.children) do copyNode(child, newfs.combine(destination, name)) end end local source if srcDisk == "ram" then source = getNode(src, srcPath) elseif oldfs.isDir(srcPhysical) then source = { type = "dir", children = {} } local function readDir(path, node) for _, name in ipairs(oldfs.list(path)) do local childPath = oldfs.combine(path, name) if oldfs.isDir(childPath) then local child = { type = "dir", children = {} } node.children[name] = child readDir(childPath, child) else local handle = oldfs.open(childPath, "r") local data = handle.readAll() handle.close() node.children[name] = { type = "file", data = data } end end end readDir(srcPhysical, source) else local handle = oldfs.open(srcPhysical, "r") if handle then source = { type = "file", data = handle.readAll() } handle.close() end end if not source then error("Source does not exist", 2) end copyNode(source, to) end function newfs.move(from, to) local _, _, srcDisk, srcPhysical = resolve(from) local _, _, dstDisk, dstPhysical = resolve(to) if srcDisk == dstDisk then if srcDisk == "oldfs" then return oldfs.move(srcPhysical, dstPhysical) end local src, srcPath = resolve(from) local dst, dstPath = resolve(to) if srcPath == "" then error("Cannot move filesystem root", 2) end local sourceParent, sourceName = getParent(src, srcPath) local destinationParent, destinationName = getParent(dst, dstPath) if not sourceParent or not destinationParent then error("Invalid move destination", 2) end local node = sourceParent.children[sourceName] if not node then error("Source does not exist", 2) end sourceParent.children[sourceName] = nil destinationParent.children[destinationName] = node return end newfs.copy(from, to) newfs.delete(from) end -- Search function newfs.find(path, pattern) local results = {} local function walk(current) for _, name in ipairs(newfs.list(current)) do local full = newfs.combine(current, name) if full:match(pattern) then results[#results + 1] = full end if newfs.isDir(full) then walk(full) end end end walk(path) return results end -- Completion function newfs.complete(path, location, includeFiles, includeDirs) local results = {} location = location or "/" path = path or "" local base = newfs.combine(location, newfs.getDir(path)) local partial = newfs.getName(path) if not newfs.exists(base) or not newfs.isDir(base) then return results end for _, name in ipairs(newfs.list(base)) do if name:sub(1, #partial) == partial then local full = newfs.combine(base, name) if newfs.isDir(full) and includeDirs ~= false then results[#results + 1] = name .. "/" elseif not newfs.isDir(full) and includeFiles ~= false then results[#results + 1] = name end end end return results end -- Attributes function newfs.attributes(path) local root, relative, disk, physical = resolve(path) if disk == "oldfs" then if oldfs.attributes then return oldfs.attributes(physical) end return nil end local node = getNode(root, relative) if not node then return nil end local now = os.epoch and os.epoch("utc") or 0 return { size = node.type == "file" and #node.data or 0, isDir = node.type == "dir", isReadOnly = false, created = now, modified = now } end -- Replace CC:Tweaked filesystem fs = newfs print("Installing packages") print("Moving boot partition") fs.makeDir("/$/") fs.move("/root/boot/", "/$/") print("RAM filesystem initialized")