]> git.e2factory.org Git - e2factory.git/commitdiff
Quote e2lib.sha1sum() argument and improve error messages
authorTobias Ulmer <tu@emlix.com>
Tue, 10 Jul 2012 13:15:24 +0000 (15:15 +0200)
committerTobias Ulmer <tu@emlix.com>
Tue, 10 Jul 2012 13:15:24 +0000 (15:15 +0200)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua

index a1d46d3037e0989d6a43a611e89f86b2c98475de..6061f59dfb8048b7e50cc9b4cb36e5515f2fc13a 100644 (file)
@@ -1737,29 +1737,40 @@ function isfile(path)
   return true
 end
 
---- calculate sha1sum for a file
+--- calculate SHA1 sum for a file
 -- @param path string: path
--- @return bool
+-- @return string: sha1 sum of file
+-- @return an error object on failure
 function sha1sum(path)
-  local args = string.format("'%s'", path)
+  assert(type(path) == "string")
+
+  local e = new_error("calculating SHA1 checksum failed")
+
   local sha1sum, re = tools.get_tool("sha1sum")
   if not sha1sum then
-    return nil, re
+    return nil, e:cat(re)
   end
+
   local sha1sum_flags, re = tools.get_tool_flags("sha1sum")
   if not sha1sum_flags then
-    return nil, re
+    return nil, e:cat(re)
   end
-  local cmd = string.format("%s %s %s", sha1sum, sha1sum_flags, args)
+
+  -- TODO: sha1sum_flags should be quoted as well
+  local cmd = string.format("%s %s %s", e2lib.shquote(sha1sum), sha1sum_flags,
+    e2lib.shquote(path))
+
   local p, msg = io.popen(cmd, "r")
   if not p then
-    return nil, new_error(msg)
+    return nil, e:cat(msg)
   end
+
   local out, msg = p:read("*l")
   p:close()
+
   local sha1, file = out:match("(%S+)  (%S+)")
   if type(sha1) ~= "string" then
-    return nil, new_error(msg)
+    return nil, e:cat("parsing sha1sum output failed")
   end
   return sha1
 end