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

[QUESTION] [LUA] How can I change a file to be Read-Only?

Started by anonimo182, 08 October 2012 - 09:58 PM
anonimo182 #1
Posted 08 October 2012 - 11:58 PM
It is there any possible ways to change a file so it can only be Read-Only and it can't be modified?

Thanks :D/>/>
faubiguy #2
Posted 09 October 2012 - 12:02 AM
You could modify the functions of the fs API (during runtime) to act as if the file was read only.

For example:

readOnly = {"file/I/want/to/be/read/only" = true, "another/file" = true}
local oldFS = fs
fs.open = function(filename, mode)
 if readOnly[filename] and (mode == "w" or mode == "wb" or mode =="a" or mode == "ab") then
  return nil
 else
  return oldFS.open(filename, mode)
 end
end
anonimo182 #3
Posted 09 October 2012 - 12:19 AM
so I need to paste that in my file?
faubiguy #4
Posted 09 October 2012 - 12:23 AM
You need to write a version of every fs function that is affected by read-only files (open, isReadOnly, copy, and delete)
anonimo182 #5
Posted 09 October 2012 - 12:28 AM
Where I write them?
faubiguy #6
Posted 09 October 2012 - 12:49 AM
The files will be made read-only when the code runs, so if you want them to be read only all the time, you should put it in your startup file.