←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Forcing all OS files to be installed in a...

Windows10User's Photo Windows10User 30 May 2018

So, right now I have this code:

local nOption = 1

function drawMenu()
   term.clear()
   term.setCursorPos(1, 1)
   paintutils.drawFilledBox(0, 0, 51, 19, colors.cyan)
   term.setCursorPos(1, 1)
   term.setBackgroundColor(colors.blue)
   term.setTextColor(colors.white)
   for i=1, 51 do
	  term.write(" ")
   end

   printCentered("BIOS SETUP UTILITY")
   print("")
   term.setCursorPos(3, 3)
   term.setBackgroundColor(colors.cyan)
   printStayX(nOption == 1 and "[ Set Default OS ]" or "Set Default OS")
   printStayX("")
   printStayX(nOption == 2 and "[ Install Pastebin OS ]" or "Install Pastebin OS")
   printStayX("")
   printStayX(nOption == 3 and "[ Exit and Reboot ]" or "Exit and Reboot")
   term.setCursorPos(30, 3)
   printStayX(textutils.formatTime(os.time()))
   printStayX("")
   printStayX("Free Space: "..math.ceil(fs.getFreeSpace("/") / 1024).." KB")
end

drawMenu()

function pasteget(code, file)
   local response = http.get("https://pastebin.com/raw/"..textutils.urlEncode(code))

   if response then
	  local data = response.readAll()
	  response.close()
	  local hfile = fs.open(file, "w")
	  hfile.write(data)
	  hfile.close()
	  return true
   else
	  return false
   end
end

function pastebinMenu()
   term.setBackgroundColor(colors.black)
   term.clear()
   term.setCursorPos(1, 1)
   print("OS name:")
   local name = read()
   print("OS Pastebin code:")
   local code = read()

   if pasteget(code, "bios/"..name.."/installer") then
	  print("Installer successfully downloaded, about to install...")
	  local oldpath = shell.path()
	  shell.setPath(oldpath..":bios/"..name)
	  shell.run("bios/"..name.."/installer")
	  shell.setPath(oldpath)
   end
end

while true do
   local event, key, _ = os.pullEvent("key")
   if key == keys.up then
	  if nOption > 1 then
		 nOption = nOption - 1
		 drawMenu()
	  end
   elseif key == keys.down then
	  if nOption < 3 then
		 nOption = nOption + 1
		 drawMenu()
	  end
   elseif key == keys.enter then
	  if nOption == 3 then
		 sleep(1)
		 os.reboot()
	  elseif nOption == 2 then
		 pastebinMenu()
	  end
   end
end

When I go install an OS, it installs fine, but even though I used setPath, it copies the files into the root directory.

Is there any way around this?
Quote

SquidDev's Photo SquidDev 30 May 2018

shell.setPath doesn't have any impact on where the files are installed, it just changes what folders the shell will look in when trying to find a program. You probably meant to use shell.setDir instead. That being said, that will have little effect on many installers as they'll be using the fs API directly. You're probably better off creating a filesystem sandbox which operates within a sub-directory. It might be worth checking out something like Wilma's VirtualOS.
Quote

Windows10User's Photo Windows10User 30 May 2018

 SquidDev, on 30 May 2018 - 01:43 PM, said:

shell.setPath doesn't have any impact on where the files are installed, it just changes what folders the shell will look in when trying to find a program. You probably meant to use shell.setDir instead. That being said, that will have little effect on many installers as they'll be using the fs API directly. You're probably better off creating a filesystem sandbox which operates within a sub-directory. It might be worth checking out something like Wilma's VirtualOS.

Will overwriting the fs functions to the same things, but adding bios/name to their beginnings work?
Quote

SquidDev's Photo SquidDev 30 May 2018

 Windows10User, on 30 May 2018 - 01:56 PM, said:

Will overwriting the fs functions to the same things, but adding bios/name to their beginnings work?
Technically yes. However, it's generally best to avoid it as there's all sorts of ways it can go wrong: you have to restore the previous handle between event yields and after your program exists (even in the event of a crash) in order to prevent breaking the computer for everyone else.
Quote

Windows10User's Photo Windows10User 30 May 2018

 SquidDev, on 30 May 2018 - 02:27 PM, said:

 Windows10User, on 30 May 2018 - 01:56 PM, said:

Will overwriting the fs functions to the same things, but adding bios/name to their beginnings work?
Technically yes. However, it's generally best to avoid it as there's all sorts of ways it can go wrong: you have to restore the previous handle between event yields and after your program exists (even in the event of a crash) in order to prevent breaking the computer for everyone else.

Then what do I do?
Quote