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

I need help with a protection program

Started by LDDestroier, 23 February 2015 - 11:32 PM
LDDestroier #1
Posted 24 February 2015 - 12:32 AM
I'm trying to make a file protection program that prevents people from writing to the file, while allowing read. However I am having problems trying to get that to work. I HAVE found that people had problems with fs.delete() where it outputs "Access denied", but I couldn't find anything to replicate it with. Can someone tell me how to make a file read-only, maybe if only for the current session?
Agent Silence #2
Posted 24 February 2015 - 01:16 AM
This sloppy code should work

local olddelete = fs.delete
local oldopen = fs.open
local oldcheck = fs.isReadOnly
local list = {["bm"] = true}

fs.delete = function(sFile)
  if list[sFile] then
    error("Access denied",0)
  end
  return olddelete(sFile)
end

fs.isReadOnly = function(sFile)
  if list[sFile] then
   return true
  else
   return oldcheck(sFile)
  end
end

fs.open = function(sFile, sMode)
  if list[sFile] and sMode ~= "r" and sMode ~= "rb" then
    return nil
  end
  return oldopen(sFile,sMode)
end

EDIT : Haven't tested
EDIT 2: Fixed it
Edited on 24 February 2015 - 05:38 PM
LDDestroier #3
Posted 24 February 2015 - 02:07 AM
That appears to made every single command say 'File not found'.
Bomb Bloke #4
Posted 24 February 2015 - 04:16 AM
fs.open() returns a file handle. Your replacement will need to return something, too.

The "override" table should be localised to prevent users altering its contents to fit their whims.

You seem to be "blacklisting" files in your "whitelist".

If fs.open() decides not to open a file in write mode, better that it return nil than a file handle in read mode.

It would be more efficient speed-wise to store your files as key-names (simply set to "true") - this would allow you to check if they're in the table without iterating through it.
Wojbie #5
Posted 24 February 2015 - 09:52 AM
Also to note that lets assume you have file-path like "folder/file". You blacklisted that path. Someone could still open that file using file-path "folder/./file" or "/././././././folder/././././././file" or "/././././././folder/../folder/./././././file" that still points to same file due to how dot works but is different string as far as blacklist is concerned.

You can get around that by using fs.combine("",sPath) witch will gladly remove all unnecessary dots and parts of path from path you gave it.making "/././././././folder/././././././file", "folder/./file" and "/././././././folder/../folder/./././././file" back into "folder/file". Just something i learned working on my shell.

EDIT: Wait.. This is not "Ask a Pro"? I got lost.
Edited on 24 February 2015 - 09:57 AM
Agent Silence #6
Posted 24 February 2015 - 06:25 PM
Ahh, sorry about that. I really didn't put effort into it. I will patch up the code now.
Bomb Bloke #7
Posted 25 February 2015 - 12:22 AM
You can get around that by using fs.combine("",sPath)

Although that should work, I feel shell.resolve(sPath) would be more elegant.
Wojbie #8
Posted 25 February 2015 - 12:52 AM
Writing that post I assumed that it would only work on full/absolute file paths. Personally I tend not to use shell api if I can.
Edited on 24 February 2015 - 11:52 PM