From 217a43551fae3bff78e6fabef8f29919e252ac15 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Wed, 13 Nov 2013 15:29:31 +0100 Subject: update get_md5() and check_md5() to also return an error message if it fails unquoted arguments of get_md5 command to prevent failure, needs further updating --- src/luarocks/fs/lua.lua | 15 ++++++++------- src/luarocks/fs/unix/tools.lua | 7 +++++-- src/luarocks/fs/win32/tools.lua | 11 +++++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 51cbee03..f9ec43ba 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -123,18 +123,18 @@ end --- Check the MD5 checksum for a file. -- @param file string: The file to be checked. -- @param md5sum string: The string with the expected MD5 checksum. --- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false if not +-- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false + msg if not -- or if it could not perform the check for any reason. function check_md5(file, md5sum) file = dir.normalize(file) - local computed = fs.get_md5(file) + local computed, msg = fs.get_md5(file) if not computed then - return false + return false, msg end if computed:match("^"..md5sum) then return true else - return false + return false, "Mismatch MD5 hash for file "..file end end @@ -648,14 +648,15 @@ if md5_ok then --- Get the MD5 checksum for a file. -- @param file string: The file to be computed. --- @return string: The MD5 checksum +-- @return string: The MD5 checksum or nil + error function get_md5(file) file = fs.absolute_name(file) local file = io.open(file, "rb") - if not file then return false end + if not file then return nil, "Failed to compute MD5 hash for file "..file end local computed = md5.sumhexa(file:read("*a")) file:close() - return computed + if computed then return computed end + return nil, "Failed to compute MD5 hash for file "..file end end diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index e3468ab4..c857b093 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -328,8 +328,11 @@ function get_md5(file) local pipe = io.popen(cmd.." "..fs.absolute_name(file)) local computed = pipe:read("*a") pipe:close() - if not computed then return nil end - return computed:match("("..("%x"):rep(32)..")") + if computed then + computed = computed:match("("..("%x"):rep(32)..")") + end + if computed then return computed end + return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) end function get_permissions(filename) diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 9aa76851..1c0131c7 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -353,15 +353,18 @@ local md5_cmd = { --- Get the MD5 checksum for a file. -- @param file string: The file to be computed. --- @return string: The MD5 checksum +-- @return string: The MD5 checksum or nil + message function get_md5(file) local cmd = md5_cmd[cfg.md5checker] if not cmd then return nil end - local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) + local pipe = io.popen(cmd.." "..fs.absolute_name(file)) -- should be in fs.Q() local computed = pipe:read("*a") pipe:close() - if not computed then return nil end - return computed:match("("..("%x"):rep(32)..")") + if computed then + computed = computed:match("("..("%x"):rep(32)..")") + end + if computed then return computed end + return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) end --- Test for existance of a file. -- cgit v1.2.3-55-g6feb