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

Mount path

Started by Sewbacca, 05 April 2016 - 09:18 PM
Sewbacca #1
Posted 05 April 2016 - 11:18 PM
I want to refer paths, do ever access have to refer to a predefined path for example for virtual machines.

So if they do this:


local handle = fs.open('/foo.cfg')
handle.write(foo)
handle.close()

This happens:


local handle = fs.open('/virtual hard disks/CC/foo.cfg')
handle.write(foo)
handle.close()

I have no idea, how :(/>

P.S. Maybe the header is missed
Edited on 05 April 2016 - 09:20 PM
KingofGamesYami #2
Posted 05 April 2016 - 11:45 PM
You will need to overwrite most functions in the fs API to refer to the virtual directory instead of /. For example, fs.open would become something like this:


local virtualPath = "/virtual"
local fs_open = fs.open
fs.open = function( path, mode )
  return fs_open( fs.combine( virtualPath, path ), mode )
end

If you change each function on it's own, and do not create a new table for fs, all APIs that use fs (such as io) will respect the change.
EveryOS #3
Posted 06 April 2016 - 12:52 PM
Also, if you're making a virtual machine, I suggest you replRedstonehutdown with os.reboot and some other stuff involving wether to use redstone
Sewbacca #4
Posted 07 April 2016 - 01:18 PM
You will need to overwrite most functions in the fs API to refer to the virtual directory instead of /. For example, fs.open would become something like this:


local virtualPath = "/virtual"
local fs_open = fs.open
fs.open = function( path, mode )
  return fs_open( fs.combine( virtualPath, path ), mode )
end

If you change each function on it's own, and do not create a new table for fs, all APIs that use fs (such as io) will respect the change.

I have to override the whole fs api? That sounds like fun :(/>
EveryOS #5
Posted 07 April 2016 - 01:47 PM
fun :(/>
Dragon53535 #6
Posted 07 April 2016 - 01:58 PM
Don't most fs functions just have a path variable as the first argument?

Loop through the fs table, and edit the path since that's the first argument and we don't care about the rest.
Mind you this isn't secure really, you'd need to sanitize the path so that it doesn't break out of your sandbox files.

for a,v in pairs(fs) do
  local oldfs = v
  fs[a] = function(path,...)
    --#I'm too lazy to write this.
    path = "/CC/" .. path
    return oldfs(path,...)
  end
end
KingofGamesYami #7
Posted 07 April 2016 - 02:34 PM
That won't work; you cannot change fs.combine.
Sewbacca #8
Posted 07 April 2016 - 11:34 PM
I have created a little override generator, but I am to lazy to use it:
Usage: overrideGenerator <path> <api name>


local file = io.open(({...})[1], 'w')
file:write('--Override the ' .. ({...})[2]:upper() .. ' API')
file:write(
[[
local hardisk = '{0}'
local function checkPath(...)
  local hardisks = list('/')
 
  for n, path in ipars({...}) do
    for _, name in ipars(hardisks) do
	  if path:find(name) == 1 then
	    return true
	  end
    end
  end
  return combine(hardisk, )
end
]]
)
for k, v in pairs(fs) do
  if type(v) == 'function' then
  file:write(
[[local ]] .. k .. [[ = ]] .. ({...})[2] ..[[.]] .. k .. [[
]] .. ({...})[2] .. [[[']] .. k .. [['] = function(...)
  local ok, pathRes = checkPath(...)
  if ok then return ]] .. k .. [[(...) else return ]] .. k .. [[(table.unpack(pathRes)) end
end
]]
)
  end
end