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

Making Files (Almost) Read Only

Started by Grim Reaper, 11 February 2013 - 02:23 PM
Grim Reaper #1
Posted 11 February 2013 - 03:23 PM
The 'Ask a Pro' section is continually sprinkled with questions from new users as to if they can make their files read only.
To answer this question as literally as possible, the only way to make a file entirely read only is by placing the actual file
in the /rom/ folder of your ComputerCraft installation.

However, it is possible to create a temporary sense of a file being read only.

How?:
If you want to make a file read only, a simple way to do this is to append your startup script with the following code:



local PROTECTED_FILE = "/myFile"
local old_fsOpen = _G["fs"]["open"]

_G["fs"]["open"] = function (path, mode)
	mode = string.lower (mode)

	if shell.resolveProgram (path) == shell.resolveProgram (PROTECTED_FILE) and  (mode == "w" or mode == "wb") then
		return nil
	end

	return old_fsOpen (path, mode)
end



Breakdown:
PROTECTED_FILE: This is the full path on your comptuer to the file that you want to be read only.
old_fsOpen : This is a variable housing the fs.open function which is implemented in the mod itself rather than the bios.

What the hell is _G?!:
_G is the table where all global data is stored during run-time. You can find native functions like 'unpack' and 'next' here. For our purposes, we'll be grabbing the "open" function from the "fs" table in _G.

What's happening?
Once we have a copy of the actual function, we can override the identifier that all other programs will be using to obtain file handles: fs.open.
In this example, we're attaching a function which takes the same arguments as fs.open and simply makes sure that the call isn't trying to get any kind of a handle where any kind of modification to the file is possible.

In the case an attempt at opening the file with writing permissions occurs, then 'nil' will be returned to the calling function so that they cannot invoke any kind of writing function on our file.

Running this script at startup will make it impossible for any program to edit your file!
Have fun and keep your programs safe.

- PaymentOption
lieudusty #2
Posted 11 February 2013 - 03:32 PM
Oh nice tutorial! :D/>
MudkipTheEpic #3
Posted 12 February 2013 - 03:01 AM
Maybe instead of if (mode=="w" or mode=="wb"), do if(mode~="r" and mode~="rb"), cuz u can use append.
theoriginalbit #4
Posted 12 February 2013 - 03:11 AM
firstly, nice tutorial.

I do have a question though… what is the functional advantage of using
_G["fs"]["open"]
over just doing
fs.open
since fs.open is the global reference, is it not?
PixelToast #5
Posted 12 February 2013 - 01:11 PM
firstly, nice tutorial.

I do have a question though… what is the functional advantage of using
_G["fs"]["open"]
over just doing
fs.open
since fs.open is the global reference, is it not?
only if the _G table is the callers fenv
Grim Reaper #6
Posted 12 February 2013 - 02:59 PM
firstly, nice tutorial.

I do have a question though… what is the functional advantage of using
_G["fs"]["open"]
over just doing
fs.open
since fs.open is the global reference, is it not?
only if the _G table is the callers fenv

Exactly. Since

_G["fs"]["open"]
Is the location where the function value for fs.open is kept, the running of the example code at startup will handle any references to the function associated with gaining a file handle by any other program.
immibis #7
Posted 20 February 2013 - 02:30 PM
so why not _G.fs.open?
superaxander #8
Posted 28 February 2013 - 04:54 AM
You should probaly do io aswell
Dlcruz129 #9
Posted 28 February 2013 - 06:01 AM
You should probaly do io aswell

io is a wrapper to fs, due to the sandboxing of ComputerCraft
Grim Reaper #10
Posted 28 February 2013 - 12:26 PM
so why not _G.fs.open?

It's a preference.
JJBOOM #11
Posted 19 May 2015 - 11:59 PM
Awesome really will help when i make my os ("JJOS")
syfygirl #12
Posted 21 May 2015 - 12:14 AM
or you could write a filesystem from scratch like I am for my kernel
biggest yikes #13
Posted 20 June 2015 - 05:20 PM
fs.isReadOnly() should be modified aswell so programs realize that the file is read-only.
or you could write a filesystem from scratch like I am for my kernel
I'm a little confused on what you're talking about.
Edited on 20 June 2015 - 03:28 PM
syfygirl #14
Posted 20 June 2015 - 06:05 PM
fs.isReadOnly() should be modified aswell so programs realize that the file is read-only.
or you could write a filesystem from scratch like I am for my kernel
I'm a little confused on what you're talking about.

oh this is from last month (please excuse me i just woke up), what I meant was you could write a new filesystem with compatibility for computercraft that implements stuff like read only, ownership, and last time opened.
Creator #15
Posted 20 June 2015 - 06:10 PM
There are actullay a few examples of this. One of them is in UberOS/packages/kernel/fs