]> git.e2factory.org Git - e2factory.git/commitdiff
Use eio in hash_file()
authorTobias Ulmer <tu@emlix.com>
Wed, 13 Nov 2013 19:17:06 +0000 (20:17 +0100)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:41:17 +0000 (15:41 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/hash.lua

index f8bc12acb8c69e99accaf73fda4751ca726d1345..688176878c3bbe677a7748b51c55c888f70c2a37 100644 (file)
@@ -30,6 +30,7 @@
 ]]
 
 local hash = {}
+local eio = require("eio")
 local err = require("err")
 local strict = require("strict")
 local trace = require("trace")
@@ -73,26 +74,31 @@ function hash.hash_line(hc, data)
     hash.hash_append(hc, data .. "\n")
 end
 
---- hash a file
+--- Hash a file.
 -- @param hc the hash context
 -- @param path string: the full path to the file
--- @return true on success, nil on error
--- @return nil, error object on failure
+-- @return True on success, false on error.
+-- @return Error object on failure.
 function hash.hash_file(hc, path)
     assert(type(hc) == "table" and type(hc._ctx) == "userdata")
     assert(type(path) == "string")
 
-    local fd = io.open(path, "r")
-    if not fd then
-        return nil, err.new("could not open file '%s'", path)
+    local f, rc, re, buf
+
+    f, re = eio.fopen(path, "r")
+    if not f then
+        return false, re
     end
 
     trace.disable()
 
-    local buf = ""
     while true do
-        buf = fd:read(64*1024)
-        if buf == nil then
+        buf, re = eio.fread(f, 64*1024)
+        if not buf then
+            trace.enable()
+            eio.fclose(f)
+            return false, re
+        elseif buf == "" then
             break
         end
 
@@ -101,7 +107,10 @@ function hash.hash_file(hc, path)
 
     trace.enable()
 
-    fd:close()
+    rc, re = eio.fclose(f)
+    if not rc then
+        return false, re
+    end
 
     return true
 end