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

Chroot (fs.combine?)

Started by Sxw, 25 February 2013 - 03:37 AM
Sxw #1
Posted 25 February 2013 - 04:37 AM
Hi. I was wondering if anyone could help me fix my program at https://raw.github.com/Sxw1212/ccprograms/master/chrootapi
Its like unix chroot except in a api. It loads fine with os.loadAPI, but when you run the chroot function, it freezes and gets terminated on line 44, which is fs.combine. I was wondering if this is an fs.combine bug or something else. Afterwards, all commands in the terminal get frozen and after 10 seconds, shut down. Heres what I ran in the lua prompt:

lua> os.loadAPI("/chrootapi")
lua> chrootapi.chroot("/rom")
--10 Seconds pass
--Error: to long without yielding on line 44
I don't know what could cause this problem.
theoriginalbit #2
Posted 25 February 2013 - 04:52 AM
This is a problem I have seen a few people make. the issue is with the line
local rootfs=fs

The variables that tables (yes apis are tables too) are accessed with are not the actual table themselves, they are a pointer to the table in the memory.

So that code is actually saying that rootfs points to the same location as fs, meaning later when you modify the fs functions you are also modifying the ones that your rootfs uses. So that means when you call any function in rootfs its pointing to itself, obviously causing an issue.

So what does this mean? well it means that we have to perform a deep copy on a table, not just a shallow copy. this copies all the keys and values from one table to another and can be done like so

local rootfs = {}

for k,v in pairs(fs) do
  rootfs[k] = v
end

This makes a copy of all the fs functions and variables and puts them in the rootfs table. now you can modify the fs table without having to worry about your functions recursively calling themselves.
Sxw #3
Posted 25 February 2013 - 05:28 AM
Thanks!
tesla1889 #4
Posted 25 February 2013 - 06:37 PM
make sure you repeat this process for any tables within the table you are copying. i know fs doesnt have any, but keep that in mind for the future