Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[chroot]]--
- local args = {...}
- local cmd = args[1]
- if chroot then
- if #args == 0 then
- error("chroot already ran",2)
- end
- else
- local fs,Path,Protected = fs
- local oldfs = {}
- for k,v in pairs(fs) do
- oldfs[k] = v
- end
- local chroot = {}
- _G.chroot = setmetatable({},{
- __index = function(s,k)
- local v = chroot[k]
- if k == "Protected" then
- return not not Protected
- elseif type(v) == "function" then
- return function(...)
- return v(...)
- end
- end return v
- end;
- })
- local function up(path,err)
- path = path:gsub("^/","")
- path = path:gsub("/$","")
- if not path:find("/") then
- if err then
- error("Invalid Path",0)
- end return path
- end return path:match("^(.*)/.-$")
- end
- local function cleanPath(path,err)
- local parts = {}
- for v in path:gmatch("[^/]+") do
- table.insert(parts,v)
- end
- local start = ""
- for i=1,#parts do
- local p = parts[i]
- if p == ".." then
- start = up(start,err)
- else
- start = start.."/"..p
- end
- end return (start:gsub("^/",""))
- end
- local function checkPath(path,err)
- local level = 0
- for v in path:gmatch("[^/]") do
- if v == ".." then
- level = level - 1
- else
- level = level + 1
- end
- end
- if err and level < 0 then
- error("Invalid Path",4)
- end return path
- end
- shareds = {"rom","chroot"}
- local function shared(p)
- for k,v in pairs(shareds) do
- if p:match("^(%w+)/") == v or p == v then
- return true
- end
- end
- end
- local function makePath(path,err)
- if not Path then return path end
- if type(path) ~= "string" then return path end
- path = cleanPath(path,err)
- if shared(path) then
- return path
- end return Path.."/"..checkPath(path,err):gsub("^/","")
- end
- local newfs = {
- getName = oldfs.getName;
- combine = oldfs.combine;
- move = function(a,b)
- return oldfs.move(makePath(a,true),makePath(b,true))
- end;
- copy = function(a,b)
- return oldfs.copy(makePath(a,true),makePath(b,true))
- end;
- complete = function(a,b,...)
- local s,e = pcall(makePath,b,true)
- if not s then return {} end
- return oldfs.complete(a,makePath(b))
- end;
- list = function(p)
- if not Path then return oldfs.list(p) end
- p = makePath(cleanPath(p),true)
- local res = oldfs.list(p)
- if shared(p) then return res end
- for k,v in pairs(res) do
- if v == "rom" then
- return res
- end
- end
- if p:sub(1,#Path) == Path then
- table.insert(res,1,"rom")
- end return res
- end;
- exists = function(p)
- local s,e = pcall(makePath,b,true)
- if not s then return false end
- return oldfs.exists(makePath(p))
- end;
- isDir = function(p)
- local s,e = pcall(makePath,b,true)
- if not s then return false end
- return oldfs.isDir(makePath(p))
- end;
- }
- for k,v in pairs(oldfs) do
- if newfs[k] then
- fs[k] = newfs[k]
- else
- fs[k] = function(p,...)
- return v(makePath(p,true),...)
- end;
- end
- end
- function chroot:exit()
- if Protected then
- error("No permission to chroot",0)
- end Path = nil
- end
- function chroot:activate(folder,prot)
- if Protected then
- error("No permission to chroot",0)
- elseif not fs.isDir(folder) then
- error("Folder Expected",2)
- end self:exit() Protected = prot
- Path = cleanPath(folder:gsub("/$",""))
- end
- end
- local usage = [[
- Usage:
- chroot exit
- Stop the current chroot
- chroot <folder>
- Set the current chroot
- ]]
- local function syntax()
- local col = term.getTextColor()
- if term.isColor() then
- term.setTextColor(colors.red)
- end print(usage)
- term.setTextColor(col)
- end
- if type(cmd) == "string" then
- if chroot.Protected then
- error("No permission to chroot",0)
- elseif #args == 1 and cmd:lower() == "exit" then
- chroot:exit()
- elseif #args > 0 and cmd ~= "help" and cmd ~= "?" then
- local prot = args[2]=="true"
- if not fs.isDir(cmd) then
- error("Folder Expected",0)
- end chroot:activate(cmd,prot)
- prot = prot and " (protected)
- print("Chroot set to ",cmd,prot)
- else
- syntax()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement