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

making a program read-only

Started by dan14941, 27 January 2013 - 12:46 PM
dan14941 #1
Posted 27 January 2013 - 01:46 PM
how to i make a program a read-only program so no one can modify it? would help allot
GravityScore #2
Posted 27 January 2013 - 02:38 PM
There is no real way to make a file completely read only, and Cloudy has stated he will never add read only files to the mod. So it's not possible.
Grim Reaper #3
Posted 27 January 2013 - 05:49 PM
Well, one way that you could go about making a file read only is by overwriting the global fs.open function to throw an error in the case that there is a call to get a handle on your file. However, this would only work if you had this code executing on startup and no one had a variable which was the old fs.open function value.

This is the code I'm talking about:


-- Making files almost entirely read only.
-- Place this as your startup program so the changes to the global
-- table will be made when the computer turns on.
-- NOTE: Other programs can also overwrite these changes if they have access to the memory locations where these
--       functions we are replacing exist.


local readOnlyPath     = "/myFile/"
local old_fsOpen       = _G["fs"]["open"]
local old_fsIsReadOnly = _G["fs"]["isReadOnly"]


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

    if shell.resolveProgram(path) ~= shell.resolveProgram(readOnlyPath) then
        return old_fsOpen(path, mode)
    elseif mode == 'w' then
        return nil
    else
        return old_fsOpen(path, 'r')
    end

end

_G["fs"]["isReadOnly"] = function(path)

    if shell.resolveProgram(path) ~= shell.resolveProgram(readOnlyPath) then
        return old_fsIsReadOnly(path)
    else
        return true
    end

end
Zoinky #4
Posted 27 January 2013 - 06:37 PM
Well, if you're not on a server and you have windows it's pretty somewhat simple (If you know what you're doing).
Minecraft folder > saves > World > computer > computer id > program (Right click) > Properties > Attributes > Read-Only

Side note: I've never actually tried doing this. But I'm guessing it should work. Wouldn't hurt to try anyway.
Cranium #5
Posted 27 January 2013 - 06:39 PM
Well, if you're not on a server and have windows it's pretty somewhat simple (If you know what you're doing).
Minecraft folder > saves > World > computer > computer id > program > Properties > Attributes > Read-Only

Side note: I've never actually tried doing this. But I'm guessing it should work. Wouldn't hurt to try anyway.
Nope. That will not work.
Zoinky #6
Posted 27 January 2013 - 06:41 PM
-snip-
Nope. That will not work.
Oh, okay. Never mind then :P/>

EDIT: Tested a bit with it on CCEmu, seems to have some odd behaviour. Images below.

Edited on 27 January 2013 - 06:01 PM
ChunLing #7
Posted 28 January 2013 - 05:45 PM
Altering the fs API will work, but you also have to alter the io API, since that will be using the old fs.open (since it loaded before you changed it). As noted, any other rom\apis that use fs.open will also still be using the old versions.

It is really better to simply set up the server in a secure location and have it accessed only over rednet, so that you can fully control what actions are available.
Grim Reaper #8
Posted 19 February 2013 - 07:53 AM
Altering the fs API will work, but you also have to alter the io API, since that will be using the old fs.open (since it loaded before you changed it). As noted, any other rom\apis that use fs.open will also still be using the old versions.

It is really better to simply set up the server in a secure location and have it accessed only over rednet, so that you can fully control what actions are available.

Actually (sorry for bumping an old thread), the io API will be referencing the replaced fs API functions because we have changed them in _G table. I just tested this, but if you did the same and were able to get around it by simply using the io API, then I'd like to see.
ChunLing #9
Posted 19 February 2013 - 04:56 PM
Oh…I must have been thinking that the io API created a new reference for fs.open(), but it just uses whatever reference is the current reference.

Though if the fs api were altered in rom, then it wouldn't matter anyway since the fs.open redefinition would occur before io copied it (which it doesn't do, it just uses the currently referenced function).

Which is all to say…no you don't need to alter io, it's not doing what I thought.