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

Make folder like root (/) ?

Started by LewisTehMinerz, 26 August 2015 - 07:23 AM
LewisTehMinerz #1
Posted 26 August 2015 - 09:23 AM
Hi, I want to make my authentication software to protect my servers files. The thing is, I need it so that it makes a users folder (.BLAH) to act like root (/) so you can't CD to the server's files. Also, the user file folders are in the root folder. Thanks for your help

~LewisTehMinerz
Bomb Bloke #2
Posted 26 August 2015 - 09:35 AM
Every system that's reliant on "paths" eventually finds itself dependant on the FS API. You'll therefore need to assign the original functions from that API to your own variables (inaccessible to your users), then fill the fs table with functions that take the user's personal folder locations into account.

This is a multi-step process. If you get stuck with it, you're better off asking rather more… specific questions. On this case, you've neglected to ask any question at all…
LewisTehMinerz #3
Posted 26 August 2015 - 05:18 PM
Well, Mr Bomb Bloke, if you think it's something to do with FS, how about you modify my existing code? Please? I don't have the knowledge to modify FS… (please note: all code was done by someone else.)

Pastebin: bap5mu0f
KingofGamesYami #4
Posted 26 August 2015 - 05:27 PM

local protected_path = "%.bla" --#the % escapes the . character in patterns, which is used by match
local open = fs.open
fs.open = function( path, mode )
  if path:match( protected_path ) and mode:sub( 1, 1 ) == "w" then
    error( "You can't edit that!", 2 )
  end
  return open( path, mode )
end
local delete = fs.delete
function fs.delete( path )
  if path:match( protected_path ) then
    error( "You can't delete that!" , 2 )
  end
  return delete( path )
end
--#etc.

^ That sort of thing is what BB recommended. FYI you should hold control+t on the script you posted… see how that works out.
Exerro #5
Posted 26 August 2015 - 05:33 PM
Well I have a filesystem library in Phoenix which does exactly that and more, so here's a very simplified (and untested, I just cut some sections out of the larger API) solution to what you're asking: pastebin.

You would load it like this:

os.loadAPI "filesystem"

Assuming you downloaded it as 'filesystem', then you'd create a redirect using:

local redirect = filesystem.redirect "Some/Other/Folder"

Now if you use

redirect.open "a"
… it should actually open "Some/Other/Folder/a"

The only issue with this is that you can open the file "../a" and it will open "Some/Other/a", but that's more effort than I'm willing to give here, sorry.

If you want to modify the global fs table, you should be able to do

_G.fs = redirect
or if not that, certainly this

for k, v in pairs( redirect ) do
	fs[k] = v
end
Edited on 26 August 2015 - 03:33 PM
LewisTehMinerz #6
Posted 13 September 2015 - 12:56 PM

local protected_path = "%.bla" --#the % escapes the . character in patterns, which is used by match
local open = fs.open
fs.open = function( path, mode )
  if path:match( protected_path ) and mode:sub( 1, 1 ) == "w" then
	error( "You can't edit that!", 2 )
  end
  return open( path, mode )
end
local delete = fs.delete
function fs.delete( path )
  if path:match( protected_path ) then
	error( "You can't delete that!" , 2 )
  end
  return delete( path )
end
--#etc.

^ That sort of thing is what BB recommended. FYI you should hold control+t on the script you posted… see how that works out.

The thing is, i need it the other way around. I need to protect / and allow only modifications to %.bla. Would i not the path:match?
KnightMiner #7
Posted 15 September 2015 - 04:38 AM
Yes, adding a not before a condition will essentially make it check for the opposite.
valithor #8
Posted 16 September 2015 - 02:08 AM
The solution posted really would not be ideal assuming I understand what exactly you are trying to do. You want to make another folder act like the root folder, but all the provided solution will do is check to make sure there is specific file within the path passed to the function, which has the same name as the new "root" file. Not to mention the user would have to put the new root at the beginning of that path anytime they try to use the fs api. An example would be:


fs.open(".BLAH/file","w") --# this would work
fs.open("file","w") --# this would not

The solution provided is better suited for protecting a file instead of creating a new root, which would work if you wanted to protect your server files instead of creating a new root as you asked in the op.

Suggested solution:


local oldOpen = fs.open
fs.open = function(path,...)
  path = fs.combine("/",path) --# makes it to where people can't use ".." to get around the overwrite
  if path:sub(1,4):match("/?rom") then --# checks to see if it is accessing a file in rom, which would mean you dont want to change the path
	return oldOpen(path,...)
  end
  return oldOpen(".BLAH/"..path,...) --# the file is not in the rom, so you need to change the path to automatically point to the new root file
end

This solution would need to be modified a little for you to be able to access the data files you want to protect, but it would be very similar to how it does the check for the rom. You would need to do it for most of the fs functions that require paths.
Edited on 16 September 2015 - 01:29 AM