This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Microwarrior's profile picture

Sandbox problems with programs

Started by Microwarrior, 02 July 2015 - 08:12 PM
Microwarrior #1
Posted 02 July 2015 - 10:12 PM
I have this sand boxing probram so far thanks to the people on this forum:


local sPath = "wow"
local _fs = {}
for k, v in pairs( fs ) do
				 _fs[k] = v																							 --# Store the old, unmodified version of the function
				 fs[k] = function( path, ... )											  --# Override the function
								 _fs[k]( _fs.combine( sPath, path ), ... ) --# And make it point to the 'sandbox_directory'
				 return _fs[k]( _fs.combine( sPath, path ), ... )
   end
end

term.setCursorPos(1,1)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.yellow)
term.clear()
print(os.version())
while true do
  term.setTextColor(colors.yellow)
  write("> ")
  term.setTextColor(colors.white)
  local input = read()
  shell.run(input)
end

--# Restore the native functions
for k, v in pairs( _fs ) do
		 fs[k] = v
end

It is supposed to make the 'wow' directory act as the root directory. The problem is whenever you type any program, even if it is in the 'wow' directory, it will say program not found. I have tried everything I can think of but it is not working!
Grim Reaper #2
Posted 02 July 2015 - 11:53 PM
Try this


local dir = "wow"
local oldFs = {}

if not fs.isDir(dir) then
	fs.makeDir(dir)

	if not fs.isDir(dir) then
		printError("Could not initialize sandbox.")
		return false
	end
end

for name, func in pairs(_G.fs) do
  oldFs[name] = func

  if name ~= "combine" then
	_G.fs[name] = function(path, ...)
	  path = shell.resolve(path)

	  if oldFs.isReadOnly(path) then
		return oldFs[name](path, ...)
	  end

	  path = dir .. "/" .. path
	  return oldFs[name](path, ...)
	end
  end
end

term.clear()
term.setCursorPos(1, 1)

while true do
  term.setTextColor(colors.yellow)
  term.write("> ")
  term.setTextColor(colors.white)

  local input = read()

  if input == "exit" then
	break
  end

  shell.run(input)
end

-- Restore fs api.
for name, func in pairs(oldFs) do
  _G.fs[name] = func
end

print("Finished.")
return true
Edited on 02 July 2015 - 09:53 PM
Microwarrior #3
Posted 03 July 2015 - 07:36 AM
Thanks for the sandbox program, it will help a lot!