Self file protection
One of the things many people want is the Operating System to protect itself. There are many ways to do so, but for simplicity, I'll post a simple way and an advanced way.
- The simple way The simple way is to just use fs.open() on all the files that you want to protect, and then don't close them. When the computer reboots, the handles will be closed so not to worry. It would still be advised to close them before rebooting or shutting down. Example
-- We want to make sure that /myos/important and /myos/alsoimportant cannot be deleted.
local protected = {}
table.insert(protected,fs.open("/myos/important","r"))
table.insert(protected,fs.open("/myos/alsoimportant","r"))
In this method, by putting the handles in the table, we can still pull them out and close them if needed.
This method isn't 100% secure and still allows you to edit the file. I'm also getting reports that its not working for them.
local protected = {}
protected["myos/important"] = true
protected["myos/alsoimportant"] = true
local _fs_delete = fs.delete -- Backup the delete function
fs.delete = function(path)
if protected[shell.resolve(path)] ~= true then
_fs_delete(path)
else
error("Access Denied", 2)
end
end
Crash handlersIf your Operating System crashes for any reason, its important to make it not drop to shell. In the shell the user can roam free and do whatever they want to in your os.
It is also nice to have something to help the user or give a message as to why it crashed.
The way we do this is to wrap the Operating System code or files in a pcall.
error, message = pcall(shell.run,"/myos/bootup")
if error == false then -- It crashed!
print("MyOS has CRASHED! :(/>\n\n" .. message)
while true do sleep(0.05) end
end
Recommendation from Cloudy
If your OS is useless and not deserving of the name, then you should add this to your code.
error("Custom OS's are mostly useless and not deserving of the name")
Tutorials
If you are looking for some tutorials, please check out ComputerCraft's tutorials forum.
Tutorials