From 2fdecb7b40de4e2c74da895f48a76ddf76fff29f Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Tue, 25 Sep 2012 12:19:34 +0100 Subject: [PATCH] Don't assert() around io.popen() and use generic FileExists(). --- fileinfo.lua | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/fileinfo.lua b/fileinfo.lua index d030c1cf9..b78620cae 100644 --- a/fileinfo.lua +++ b/fileinfo.lua @@ -35,10 +35,20 @@ function FileInfo:FormatSize(size) end end +function FileExists(path) + local f = io.open(path, "r") + if f then + f:close() + return true + else + return false + end +end + function getUnpackedZipSize(zipfile) -- adding quotes allows us to avoid crash on zips which filename contains space(s) local cmd='unzip -l \"'..zipfile..'\" | tail -1 | sed -e "s/^ *\\([0-9][0-9]*\\) *.*/\\1/"' - local p = assert(io.popen(cmd, "r")) + local p = io.popen(cmd, "r") local res = assert(p:read("*a")) p:close() res = string.gsub(res, "[\n\r]+", "") @@ -52,22 +62,22 @@ end function FileInfo:formatDiskSizeInfo() local s = getDiskSizeInfo() - if s then - return self:FormatSize(s.free)..string.format(", %.2f", 100*s.free/s.total).."%" + if s then + return self:FormatSize(s.free)..string.format(", %.2f", 100*s.free/s.total).."%" end return "?" end function FileInfo:getFolderContent() InfoMessage:show("Scanning folder...", 1) - local tmp = assert(io.popen('du -a \"'..self.pathfile..'\"', "r")) + local tmp = io.popen('du -a \"'..self.pathfile..'\"', "r") local dirs, files, books, size, name, output, ftype, j = -1, 0, 0, 0 for output in tmp:lines() do j = output:find("/") name = output:sub(j, -1) size = tonumber(output:sub(1, j-1)) -- in kB j = lfs.attributes(name, "mode") - if j == "file" then + if j == "file" then files = files + 1 ftype = string.match(name, ".+%.([^.]+)") if ftype and ext:getReader(ftype) then @@ -119,7 +129,7 @@ function FileInfo:init(path, fname) if fname then -- file info table.insert(self.result, {dir = "Path", name = path.."/"} ) table.insert(self.result, {dir = "Size", name = self:FormatSize(lfs.attributes(self.pathfile, "size"))} ) - -- total size of all unzipped entries for zips + -- total size of all unzipped entries for zips local match = string.match(fname, ".+%.([^.]+)") if match and string.lower(match) == "zip" then table.insert(self.result, {dir = "Unpacked", name = self:FormatSize(getUnpackedZipSize(self.pathfile))} ) @@ -136,8 +146,7 @@ function FileInfo:init(path, fname) if fname then -- if the document was already opened local history = DocToHistory(self.pathfile) - local file, msg = io.open(history, "r") - if not file then + if not FileExists(history) then table.insert(self.result, {dir = "Last read", name = "Never"}) else table.insert(self.result, {dir = "Last read", name = self:FileCreated(history, "change")}) @@ -161,7 +170,7 @@ end function FileInfo:show(path, name) -- at first, one has to test whether the file still exists or not: necessary for last documents - if name and not io.open(path.."/"..name,"r") then return nil end + if name and not FileExists(path.."/"..name) then return nil end -- then goto main functions self:init(path,name) -- local variables @@ -236,7 +245,7 @@ function FileInfo:addAllCommands() FileHistory:choose("") self.pagedirty = true end - ) + ) self.commands:add({KEY_BACK, KEY_FW_LEFT}, nil, "Back", "back", function(self)