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

FS - Method to check if "edit startup" is entered instead of "startup"

Started by Goof, 08 May 2013 - 08:00 AM
Goof #1
Posted 08 May 2013 - 10:00 AM
Hello!

im making a new update for AdvLock… and im trying to prevent editing startup files or boot files… but
would i be able to NOT prevent the way of "running" the files? like

right now when i type "edit startup" it says:
edit:50: AdvLock: You do not have access to edit this file!

but when i type in "startup" this is happening, too ( which i dont want)
edit:50: AdvLock: You do not have access to edit this file

could someone tell me how to check if the only argument for opening a file is "read" or if its "run"


Thanks in Advance.

code:
SpoilerPastebin:



function securePaths()
	del = 0
	local lockedFiles = {"startup"}
	local oldFsDelete = fs.delete
	fs.delete = function(...)
		local tArg = {...}
		

		if not fs.isLocked then
			return oldFsDelete(unpack(tArg))
		end
		for lockedFile = 1, #lockedFiles do
			if string.lower(tArg[1]) == string.lower(lockedFiles[lockedFile]) and not fs.isDir(tArg[1]) then
				error("AdvLock: You do not have access to delete this file!", 3)
			elseif string.lower(tArg[1]) == string.lower(lockedFiles[lockedFile]) and fs.isDir(tArg[1]) then	
				error("AdvLock: You do not have access to delete this folder!", 3)
			else
				del = del + 1
			end	
		end
		if del == #lockedFiles then
			del = 0
			return oldFsDelete(unpack(tArg))
		end	
	end
	del = 0
	local oldFsOpen = fs.open
	fs.open = function(...)
		local tArg = {...}
		

		if not fs.isLocked then
			return oldFsOpen(unpack(tArg))
		end
		for lockedFile = 1, #lockedFiles do
			if string.lower(tArg[1]) == string.lower(lockedFiles[lockedFile]) and not fs.isDir(tArg[1]) then
				error("AdvLock: You do not have access to edit this file!", 3)
			elseif string.lower(tArg[1]) == string.lower(lockedFiles[lockedFile]) and fs.isDir(tArg[1]) then	
				error("AdvLock: You do not have access to edit this folder!", 3)
			else
				del = del + 1
			end	
		end
		if del == #lockedFiles then
			del = 0
			return oldFsOpen(unpack(tArg))
		end	
	end
end		

digpoe #2
Posted 08 May 2013 - 10:06 AM
I've not read your code, so I don't know if you read the terminal commands, but if you do:


local blockedFiles = {"startup", "reboot"} --reboot is used as example so you can block more than one file from being edited.
for _, v in ipairs(blockedFiles) do
 if input:find("edit "..v) then
  print("You can't modify the "..v.." file!")
 end
end
theoriginalbit #3
Posted 08 May 2013 - 10:13 AM
I would suggest to changing the function override for fs.open so that if they are attempting to read it, it's allowed, but writing and deleting is denied

fs.open = function(path, mode)
  if mode == 'r' then
    -- allow open, they are trying to read it
  else
    -- reject
  end
end
digpoe #4
Posted 08 May 2013 - 10:14 AM
I would suggest to changing the function override for fs.open so that if they are attempting to read it, it's allowed, but writing and deleting is denied

fs.open = function(path, mode)
  if mode == 'r' then
	-- allow open, they are trying to read it
  else
	-- reject
  end
end

But that means you can't modify any kind of file?
theoriginalbit #5
Posted 08 May 2013 - 10:23 AM
But that means you can't modify any kind of file?
Well keeping OP original logic in there, no, it will allow it if it's not locked. the code I suggested is just some additional logic that would allow the program to be run, but not edited, if it is locked.
Goof #6
Posted 08 May 2013 - 10:24 AM
I would suggest to changing the function override for fs.open so that if they are attempting to read it, it's allowed, but writing and deleting is denied

fs.open = function(path, mode)
  if mode == 'r' then
	-- allow open, they are trying to read it
  else
	-- reject
  end
end

But that means you can't modify any kind of file?
Yes.. and thats exactly what i wanted… :D/>


Thanks everyone :D/>