Jump to content




Making Files (Almost) Read Only


  • You cannot reply to this topic
14 replies to this topic

#1 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

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

#2 lieudusty

  • Members
  • 419 posts

Posted 11 February 2013 - 03:32 PM

Oh nice tutorial! :D

#3 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

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.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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?

#5 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 12 February 2013 - 01:11 PM

View PostTheOriginalBIT, on 12 February 2013 - 03:11 AM, said:

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

#6 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 12 February 2013 - 02:59 PM

View PostPixelToast, on 12 February 2013 - 01:11 PM, said:

View PostTheOriginalBIT, on 12 February 2013 - 03:11 AM, said:

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.

#7 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 20 February 2013 - 02:30 PM

so why not _G.fs.open?

#8 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 28 February 2013 - 04:54 AM

You should probaly do io aswell

#9 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 28 February 2013 - 06:01 AM

View Postsuperaxander, on 28 February 2013 - 04:54 AM, said:

You should probaly do io aswell

io is a wrapper to fs, due to the sandboxing of ComputerCraft

#10 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 28 February 2013 - 12:26 PM

View Postimmibis, on 20 February 2013 - 02:30 PM, said:

so why not _G.fs.open?

It's a preference.

#11 JJBOOM

  • Members
  • 5 posts

Posted 19 May 2015 - 10:59 PM

Awesome really will help when i make my os ("JJOS")

#12 syfygirl

  • Members
  • 94 posts
  • LocationSomewhere in my mind, to which only makes sense to me.

Posted 20 May 2015 - 11:14 PM

or you could write a filesystem from scratch like I am for my kernel

#13 biggest yikes

  • Members
  • 573 posts

Posted 20 June 2015 - 04:20 PM

fs.isReadOnly() should be modified aswell so programs realize that the file is read-only.

View Postsyfyguy13, on 20 May 2015 - 11:14 PM, said:

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 by Atenefyr, 20 June 2015 - 04:28 PM.


#14 syfygirl

  • Members
  • 94 posts
  • LocationSomewhere in my mind, to which only makes sense to me.

Posted 20 June 2015 - 05:05 PM

View PostAtenefyr, on 20 June 2015 - 04:20 PM, said:

fs.isReadOnly() should be modified aswell so programs realize that the file is read-only.

View Postsyfyguy13, on 20 May 2015 - 11:14 PM, said:

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.

#15 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 20 June 2015 - 05:10 PM

There are actullay a few examples of this. One of them is in UberOS/packages/kernel/fs





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users