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

Modifying file handle functions

Started by The Blue daNoob, 16 December 2012 - 02:09 PM
The Blue daNoob #1
Posted 16 December 2012 - 03:09 PM
For a security system, I am trying to replace the functions of file handles. e.g.


fileHandle = fs.open("derp","r")
contents = fileHandle.readAll()
fileHandle.close()

function newReadAll()
    derpAround()
end
I want to make it so that calling fileHandle.readAll() will call newReadAll(). Is this possible?
theoriginalbit #2
Posted 16 December 2012 - 03:12 PM
you could try this


fileHandle:readAll() = newReadAll()

but I'm not sure if you are allowed to override fs
The Blue daNoob #3
Posted 16 December 2012 - 03:40 PM
Hold on… is fs just a wrapper to io? It seems like if it is, then that would be easier to modify, as it uses "proper" Lua OOP.
theoriginalbit #4
Posted 16 December 2012 - 03:49 PM
yes fs is an io wrapper.
Cloudy #5
Posted 16 December 2012 - 10:57 PM
yes fs is an io wrapper.

Nope. Reverse.
theoriginalbit #6
Posted 16 December 2012 - 11:15 PM
yes fs is an io wrapper.

Nope. Reverse.

really? ok
theoriginalbit #7
Posted 17 December 2012 - 02:29 AM
yes fs is an io wrapper.

Nope. Reverse.

I cant find any Lua documentation to confirm this.
CoolisTheName007 #8
Posted 17 December 2012 - 02:35 AM
snip
fs and other APIS are built java-side, because CC's Lua runs on Java, so official Lua documentation won't talk about it. Regarding CC docs, since it is a mod for a game (e.g. not real work / releasing technical info might increase chances of hacking), most technical or more complicated aspects are left undocumented. But, hey, you can contribute for the wiki!Btw, when Cloudy is telling you something, you would better believe it.
KaoS #9
Posted 17 December 2012 - 02:47 AM
as well as to successfully secure this you should not edit an existing file handle, you should edit the fs.open function and make it return your function in stead


local function myread()
  --your code
end
local oldopen=fs.open
fs.open=function(...)
  local temp=oldopen(...)
  if temp.read then
	temp.read=myread
  end
end

oh and

you could try this


fileHandle:readAll() = newReadAll()

but I'm not sure if you are allowed to override fs
you should not call the function you are modifying or replacing with. no brackets there
theoriginalbit #10
Posted 17 December 2012 - 03:18 AM
oh and

you could try this


fileHandle:readAll() = newReadAll()

but I'm not sure if you are allowed to override fs
you should not call the function you are modifying or replacing with. no brackets there

yeh i realised my typo too late.
The Blue daNoob #11
Posted 17 December 2012 - 07:44 AM
Well, here is the whole idea of modifying the file handles. I want to encode all data written, and decode all data read. This is not possible without modifying the functions called by file handles. Oh well, it was a good idea.
theoriginalbit #12
Posted 17 December 2012 - 11:11 AM
well. why cant there be an encode process then the encoded string parsed the the existing file handler?
and the same for decode, read data, use that data, decode with a function, done.
why do you have to modify the handles?
The Blue daNoob #13
Posted 20 December 2012 - 01:06 PM
This is for a secure operating system. The idea is that when programs are running in it, they write encoded data and decode data that is read. That way, if someone bypasses the operating system, then all they would get is meaningless gibberish.