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

Don't Delete File if Access is Denied.

Started by PixelFox, 13 April 2015 - 09:04 PM
PixelFox #1
Posted 13 April 2015 - 11:04 PM
in the delete function for my file explorer/finder type program, if the person were to delete like, I dunno, some important file, it wouldn't crash the thing, and give me an error, but I want it to keep going, is there a "catch" for C# type thing, I want it to return a variable, and I can't do it, code:

function deleteFile(File)
fs.delete(File)
-- Here, I want it to check to see if access is denied.
end

(Sorry if this isn't too good, I'm copying from code.)
Lupus590 #2
Posted 13 April 2015 - 11:08 PM
try this http://www.computercraft.info/forums2/index.php?/topic/9964-error-handling-in-lua/
PixelFox #3
Posted 13 April 2015 - 11:12 PM
I dunno how I'd use that.
Lupus590 #4
Posted 13 April 2015 - 11:36 PM
It's a tutorial for protected call (pcall), catches the error for you and can tell you if it worked

another is assert

http://www.lua.org/pil/8.3.html

google around for lua error catching
Edited on 13 April 2015 - 09:39 PM
HPWebcamAble #5
Posted 14 April 2015 - 12:51 AM
You can use fs.isReadOnly for some files, like in the rom, but there are some cases where that won't work, so you should probably just use pcall()


function deleteFile(path)
  local state,err = pcall(fs.delete,path) --# Pass it a function, then arguments for the function. It returns true/false, then the error if there was one
  return state
end
Note that when you pass the function, you DON'T use parenthesis (), since you want to pass the value of the variable (fs.delete), not the result of the function
Edited on 13 April 2015 - 10:52 PM
jakejakey #6
Posted 14 April 2015 - 02:34 AM
This function should work great:
function deleteFile(path)
if fs.isReadOnly(path) then
return false
else
fs.delete(path)
return true
end
end
this will make it so if they try to delete lets say rom it will return false
otherwise if they delete a file like test it will return true.
HPWebcamAble #7
Posted 14 April 2015 - 03:31 AM
This function should work great:
function deleteFile(path)
if fs.isReadOnly(path) then
return false
else
fs.delete(path)
return true
end
end
this will make it so if they try to delete lets say rom it will return false
otherwise if they delete a file like test it will return true.

That won't work

When a file has been opened by the fs API or somthing similar, deleting it will error, but fs.isReadOnly() can still return false
Creator #8
Posted 14 April 2015 - 02:38 PM
if you want to protect custom files do this:


local filesToProtect = {
putPathsHere = true
}
local function newDelete(path)
if filesToProtect[path] then
return false
else
fs.delete(path)
end
end
fs.delete = newDelete

This should work ;)/>

By the way, this is my 256th post. 16*16 = 256 and 2^8 = 256, and a lot of other useful stuff
Edited on 14 April 2015 - 12:39 PM
Dragon53535 #9
Posted 14 April 2015 - 03:42 PM
if you want to protect custom files do this:


local filesToProtect = {
putPathsHere = true
}
local function newDelete(path)
if filesToProtect[path] then
return false
else
fs.delete(path)
end
end
fs.delete = newDelete

This should work ;)/>

By the way, this is my 256th post. 16*16 = 256 and 2^8 = 256, and a lot of other useful stuff
This code will fail. By overriding fs.delete you are ruining the function of your code, since the fs.delete inside your newDelete function will now be pointing to the newDelete function, nothing will ever be deleted, and you'll probably fill the function stack quite quickly. What you would want to do is copy fs.delete into a dummy local variable, and call that inside your newDelete function, like so:

local filesToProtect = {
putPathsHere = true
}
local oldDelete = fs.delete --#Right here
local function newDelete(path)
if filesToProtect[path] then
return false
else
oldDelete(path) --# See i'm calling it here
end
end
fs.delete = newDelete
Edited on 14 April 2015 - 01:43 PM
Creator #10
Posted 14 April 2015 - 03:49 PM
Oops, sorry.
Hayden_Almeida #11
Posted 07 June 2015 - 01:34 AM
in the delete function for my file explorer/finder type program, if the person were to delete like, I dunno, some important file, it wouldn't crash the thing, and give me an error, but I want it to keep going, is there a "catch" for C# type thing, I want it to return a variable, and I can't do it, code:

function deleteFile(File)
fs.delete(File)
-- Here, I want it to check to see if access is denied.
end

(Sorry if this isn't too good, I'm copying from code.)

I solved this problem!
ACESS DENIED occurs when before in your program or in usage of the computer, you are playing to changing or setting other directories..
Look at this example:

if fs.exists("/contas/"..conta.."/emails/"..assunto) then
When i use this, the WORKING directory is now inside the variable "assunto"!
So when you try to use

fs.delete("/contas/"..conta.."/emails/"..assunto)
It will try to go into more folders in who are already open!

How to Solve? Look:


shell.setDir("/contas/"..conta.."/emails/")
fs.delete("assunto")


Hope this works with you too :D/>