I’ve (sort of) gotten around it with PCall() to trap the error and ignore it.
I’ll post my code below:
local path = system.pathForFile("", system.DocumentsDirectory); lfs.chdir(path) local isSuccess = lfs.chdir("/") local currentDir = lfs.currentdir(); local backupFiles = {} local directories ={} local function splitBits(protection) local table = {} local i = 1; while i \<= 9 do table[i]= protection:sub(i, i) i = i + 1 end return table end local function traverseDir(currentDir, backupFiles, directories) directories["Files"] = {} local i = 1; for file in lfs.dir( currentDir ) do -- "file" is the current file or directory name if (tostring(file) ~= "." and tostring(file) ~= "..") then print(currentDir) local mode = lfs.attributes(file, "mode") local prot = lfs.attributes(file, "permissions") print(file, mode, prot) --print(file.." "..mode.." "..prot) if (mode == "directory" or mode == nil) then directories[file] = {} directories[file]["root"] = {} directories[file]["root"]["Address"] = currentDir..file if (prot ~= nil) then directories[file]["root"]["Protection"] = splitBits(prot) directories[file]["root"]["Protection"]["Base"] = prot end err, message, directories[file]["root"]["Tree"] = pcall(function() traverseDir(currentDir.."/"..file, backupFiles, directories[file]) end) else if (file == "Backup.zip") then backupFiles[#backupFiles] = {} backupFiles[#backupFiles - 1]["Address"] = currentDir..file break; end --directories["Files"][i] = {} --directories["Files"][i]["File"] = file; --directories["Files"][i]["Protection"] = splitBits(prot) --directories["Files"][i]["Mode"] = mode; --i = i + 1 end end end return directories, backupFiles end for file in lfs.dir( "/Users/matthewharrop/" ) do if (tostring(file) ~= "." and tostring(file) ~= "..") then local dir = "" dir = "/Users/matthewharrop/"..file local err, message, directories, backupFiles = pcall(function() traverseDir(dir, backupFiles, directories) end) --print(err, message, directories, backupFiles) if (err) then print("work"); else print("fail") --break; end end end --local directories, backupFiles = traverseDir("/Users/matthewharrop/", backupFiles, directories) local jsonPath = system.pathForFile("map.json", system.DocumentsDirectory) local openJSON = io.open(jsonPath, "w") local c = json.encode(directories) openJSON:write(c) openJSON:close() jsonPath = system.pathForFile("backfiles.json", system.DocumentsDirectory) openJSON = io.open(jsonPath, "w") c = json.encode(backupFiles) openJSON:write(c) openJSON:close()
Ignore the commented out calls, I’m prototyping something and those calls will be included in the full program, for now they’re just place holders of sorts.
What “print(file, mode, prot)” returns is:
/Users/matthewharrop/Documents/Tarzender Publishing nil nil
However with base directories (located in /) it returns:
/dev directory rwxr-xr-x