Advertisement
einsteinK

CCraft - chroot

Dec 5th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.00 KB | None | 0 0
  1.  
  2. --[[chroot]]--
  3.  
  4. local args = {...}
  5. local cmd = args[1]
  6.  
  7. if chroot then
  8.     if #args == 0 then
  9.         error("chroot already ran",2)
  10.     end
  11. else
  12.     local fs,Path,Protected = fs
  13.  
  14.     local oldfs = {}
  15.     for k,v in pairs(fs) do
  16.         oldfs[k] = v
  17.     end
  18.  
  19.     local chroot = {}
  20.     _G.chroot = setmetatable({},{
  21.         __index = function(s,k)
  22.             local v = chroot[k]
  23.             if k == "Protected" then
  24.                 return not not Protected
  25.             elseif type(v) == "function" then
  26.                 return function(...)
  27.                     return v(...)
  28.                 end
  29.             end return v
  30.         end;
  31.     })
  32.  
  33.     local function up(path,err)
  34.         path = path:gsub("^/","")
  35.         path = path:gsub("/$","")
  36.         if not path:find("/") then
  37.             if err then
  38.                 error("Invalid Path",0)
  39.             end return path
  40.         end return path:match("^(.*)/.-$")
  41.     end
  42.     local function cleanPath(path,err)
  43.         local parts = {}
  44.         for v in path:gmatch("[^/]+") do
  45.             table.insert(parts,v)
  46.         end
  47.         local start = ""
  48.         for i=1,#parts do
  49.             local p = parts[i]
  50.             if p == ".." then
  51.                 start = up(start,err)
  52.             else
  53.                 start = start.."/"..p
  54.             end
  55.         end return (start:gsub("^/",""))
  56.     end
  57.  
  58.     local function checkPath(path,err)
  59.         local level = 0
  60.         for v in path:gmatch("[^/]") do
  61.             if v == ".." then
  62.                 level = level - 1
  63.             else
  64.                 level = level + 1
  65.             end
  66.         end
  67.         if err and level < 0 then
  68.             error("Invalid Path",4)
  69.         end return path
  70.     end
  71.  
  72.     shareds = {"rom","chroot"}
  73.     local function shared(p)
  74.         for k,v in pairs(shareds) do
  75.             if p:match("^(%w+)/") == v or p == v then
  76.                 return true
  77.             end
  78.         end
  79.     end
  80.  
  81.     local function makePath(path,err)
  82.         if not Path then return path end
  83.         if type(path) ~= "string" then return path end
  84.         path = cleanPath(path,err)
  85.         if shared(path) then
  86.             return path
  87.         end return Path.."/"..checkPath(path,err):gsub("^/","")
  88.     end
  89.  
  90.     local newfs = {
  91.         getName = oldfs.getName;
  92.         combine =  oldfs.combine;
  93.         move = function(a,b)
  94.             return oldfs.move(makePath(a,true),makePath(b,true))
  95.         end;
  96.         copy = function(a,b)
  97.             return oldfs.copy(makePath(a,true),makePath(b,true))
  98.         end;
  99.         complete = function(a,b,...)
  100.             local s,e = pcall(makePath,b,true)
  101.             if not s then return {} end
  102.             return oldfs.complete(a,makePath(b))
  103.         end;
  104.         list = function(p)
  105.             if not Path then return oldfs.list(p) end
  106.             p = makePath(cleanPath(p),true)
  107.             local res = oldfs.list(p)
  108.             if shared(p) then return res end
  109.             for k,v in pairs(res) do
  110.                 if v == "rom" then
  111.                     return res
  112.                 end
  113.             end
  114.             if p:sub(1,#Path) == Path then
  115.                 table.insert(res,1,"rom")
  116.             end return res
  117.         end;
  118.         exists = function(p)
  119.             local s,e = pcall(makePath,b,true)
  120.             if not s then return false end
  121.             return oldfs.exists(makePath(p))
  122.         end;
  123.         isDir = function(p)
  124.             local s,e = pcall(makePath,b,true)
  125.             if not s then return false end
  126.             return oldfs.isDir(makePath(p))
  127.         end;
  128.     }
  129.  
  130.     for k,v in pairs(oldfs) do
  131.         if newfs[k] then
  132.             fs[k] = newfs[k]
  133.         else
  134.             fs[k] = function(p,...)
  135.                 return v(makePath(p,true),...)
  136.             end;
  137.         end
  138.     end
  139.  
  140.     function chroot:exit()
  141.         if Protected then
  142.             error("No permission to chroot",0)
  143.         end Path = nil
  144.     end
  145.  
  146.     function chroot:activate(folder,prot)
  147.         if Protected then
  148.             error("No permission to chroot",0)
  149.         elseif not fs.isDir(folder) then
  150.             error("Folder Expected",2)
  151.         end self:exit() Protected = prot
  152.         Path = cleanPath(folder:gsub("/$",""))
  153.     end
  154. end
  155.  
  156. local usage = [[
  157. Usage:
  158.     chroot exit
  159.         Stop the current chroot
  160.     chroot <folder>
  161.         Set the current chroot
  162. ]]
  163.  
  164. local function syntax()
  165.     local col = term.getTextColor()
  166.     if term.isColor() then
  167.         term.setTextColor(colors.red)
  168.     end print(usage)
  169.     term.setTextColor(col)
  170. end
  171.  
  172. if type(cmd) == "string" then
  173.     if chroot.Protected then
  174.         error("No permission to chroot",0)
  175.     elseif #args == 1 and cmd:lower() == "exit" then
  176.         chroot:exit()
  177.     elseif #args > 0 and cmd ~= "help" and cmd ~= "?" then
  178.         local prot = args[2]=="true"
  179.         if not fs.isDir(cmd) then
  180.             error("Folder Expected",0)
  181.         end chroot:activate(cmd,prot)
  182.         prot = prot and " (protected)
  183.         print("Chroot set to ",cmd,prot)
  184.     else
  185.         syntax()
  186.     end
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement