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

is it possible to make a isreadonly file ?

Started by Goof, 14 December 2012 - 09:08 PM
Goof #1
Posted 14 December 2012 - 10:08 PM
Hello

i was wondering how to make a file
to
fs.isReadOnly()



but is that possible Without wditing bios/ shell ?

thanks in advance

-mikk809h
GopherAtl #2
Posted 14 December 2012 - 10:54 PM
Short answer: no.

Long answer: Nooooooooooooooooooooooooo.
Kingdaro #3
Posted 15 December 2012 - 02:26 AM
If you can't open it with writing mode, then it's read only.


local file = fs.open(someFilePath, 'a')
if not file then
  print 'File is read only'
else
  print 'File is writable'
  file.close()
end

As to not erase the entire file, I'm using append mode here instead.
Goof #4
Posted 15 December 2012 - 06:44 AM
i dont want to check if a file is read only… i want to make my Startup file ReadOnly.. but asGopherAtl said:

Short answer: no.

Long answer: Nooooooooooooooooooooooooo.

then i dont think its possible.
Sammich Lord #5
Posted 15 December 2012 - 06:50 AM
You will have to over-write the FS lib to do it. That is the only way I can think of.

local oldFsOpen = fs.open
fs.open = function({...})
  if args[1] == "/startup" or "startup" then
    print("Not allowed")
  else
    oldFsOpen(args)
  end
end
theoriginalbit #6
Posted 15 December 2012 - 06:55 AM
I think it may be possible to do it if you have access to the CC computer files and make it read-only through Windows/Mac/Linux
billysback #7
Posted 15 December 2012 - 06:56 AM
You will have to over-write the FS lib to do it. That is the only way I can think of.

local oldFsOpen = fs.open
fs.open = function({...})
  if args[1] == "/startup" or "startup" then
	print("Not allowed")
  else
	oldFsOpen(args)
  end
end
you can still override this with io.open, you would have to override io.open as fs wraps around io doesn't it???

you could check the first letter of the file and if it starts with "%" or any of the folders in the path start with "%" or another character and return nil if it does.
You could also edit .list and make it remove all files/folders that are hidden.
Goof #8
Posted 15 December 2012 - 06:57 AM
well.. i have to make 2 specific codes, now? one for FS and one for IO ???
Sammich Lord #9
Posted 15 December 2012 - 07:01 AM
well.. i have to make 2 specific codes, now? one for FS and one for IO ???
Just modify my code and expand it to fit your needs.
Goof #10
Posted 15 December 2012 - 07:04 AM
EDIT: now my computer broke, when using the code


local oldFsOpen = fs.open
local args = {}
fs.open = function()
  if args[1] == "/hel" or "hel" then
	print("Not allowed")
  else
	oldFsOpen(args)
  end
end


"broke" = that if i type in any programs that EXISTS, then it prints Not Allowed :o/>
i have to reboot my pc to get out of that issue :(/>
and if i just ran your code, without editing it says :

bios:338: [string "test"]:2: <name> or '…' expected
billysback #11
Posted 15 December 2012 - 07:08 AM
is it not:

  if args[1] == "/hel" or args[1] == "hel" then
also you do not set args{}
{…} is args.
PixelToast #12
Posted 15 December 2012 - 07:18 AM
you can also just do

do
local file=fs.open("startup",w)
end

and if you dont close it than it will keep erroring
BigSHinyToys #13
Posted 15 December 2012 - 07:21 AM

local oldFsOpen = fs.open
fs.open = function(...)
    local tArgs = {...}
    if tArgs[1] == "/startup" or "startup" then
	    print("Not allowed")
    else
	    oldFsOpen(args)
    end
end
you forgot to define args proprely
Goof #14
Posted 15 December 2012 - 07:41 AM
EDIT. when using BigSHinyToys code then if i run "test" and then run "help" it just says Not allowed. File not found.



local oldFsOpen = fs.open
fs.open = function(...)
    local tArgs = {...}
    if tArgs[1] == "/startup" or "startup" then
            print("Not allowed")
    else
            oldFsOpen(args)
    end
end
BigSHinyToys #15
Posted 15 December 2012 - 08:08 AM
I have fixed the errors and tested fully this time

local oldFsOpen = fs.open
fs.open = function(...)
    local tArgs = {...}
    if tArgs[1] == "/startup" or tArgs[1] == "startup" then
	    print("Not allowed")
    else
	    return oldFsOpen(unpack(tArgs))
    end
end

you can use edit startup but when you try to save it will print that error
Goof #16
Posted 15 December 2012 - 08:10 AM
you are right, but when i edit, is it then true that it should print
edit:51: attempt to index ? (a nil value)

? is that correct, it should print that, or?
PixelToast #17
Posted 15 December 2012 - 08:18 AM
its because the function isnt returning anything
replace the print line with
error("Access denied.",2)
Goof #18
Posted 15 December 2012 - 08:26 AM
and one thing more….

i was trying to lock more folders/files with an table, but it seems like not to work out…



local oldFsOpen = fs.open
fs.open = function(...)
    local tArgs = {...}
local lockFiles = {
"startup",
"/startup",
"update",
"/update",
"pass",
"/pass",
"/.@main@./config.cfg",
".@main@./config.cfg",
"/.@main@./passwordSys", 
".@main@./passwordSys", 
".@main@./version.ver", 
"/.@main@./version.ver", 
".@main@./Uninstall", 
"/.@main@./Uninstall",
".@main@./update",
"/.@main@./update",
".@main@./animations/LockTF",
"/.@main@./animations/LockTF", 
".@main@./animations/Uninstall",
"/.@main@./animations/Uninstall",
".@main@./animations/download",
"/.@main@./animations/download",
".@main@./updater/update",
"/.@main@./updater/update"
}
    if tArgs[1] == #lockFiles then
            error("Access denied!", 2)
    else
            return oldFsOpen(unpack(tArgs))
    end
end

then when i type in startup it locks fine, but when i type edit update then it just run the edit tool.. and then i can edit the file :(/>
edit: now when i reboot i can edit startup…. :(/>
Lyqyd #19
Posted 15 December 2012 - 09:02 AM
You'd have to iterate through the table to check each entry rather than checking the file name against the length of the table.
Goof #20
Posted 15 December 2012 - 09:13 AM
so i would have to say
if tArgs[1] == lockFiles[1] or lockFiles[2] or … etc???


Edit: never mind, i figured it out… Thanks :D/>