From 1cb6bb91efe3af86335453221b0f7cdcd2cb3be5 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko <mpeterval@gmail.com> Date: Sat, 7 May 2016 14:12:16 +0300 Subject: fs.tools: move common 'get_md5' function --- src/luarocks/fs/tools.lua | 22 ++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 22 ---------------------- src/luarocks/fs/win32/tools.lua | 22 ---------------------- 3 files changed, 22 insertions(+), 44 deletions(-) diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 08fa74a4..76b90383 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -100,4 +100,26 @@ function tools.use_downloader(url, filename, cache) end end +local md5_cmd = { + md5sum = fs.Q(vars.MD5SUM), + openssl = fs.Q(vars.OPENSSL).." md5", + md5 = fs.Q(vars.MD5), +} + +--- Get the MD5 checksum for a file. +-- @param file string: The file to be computed. +-- @return string: The MD5 checksum or nil + message +function tools.get_md5(file) + local cmd = md5_cmd[cfg.md5checker] + if not cmd then return nil, "no MD5 checker command configured" end + local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) + local computed = pipe:read("*a") + pipe:close() + 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 + return tools diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 2b5280d6..904dd0aa 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -228,28 +228,6 @@ function tools.unpack_archive(archive) return true end -local md5_cmd = { - md5sum = vars.MD5SUM, - openssl = vars.OPENSSL.." md5", - md5 = vars.MD5, -} - ---- Get the MD5 checksum for a file. --- @param file string: The file to be computed. --- @return string: The MD5 checksum -function tools.get_md5(file) - local cmd = md5_cmd[cfg.md5checker] - if not cmd then return nil, "no MD5 checker command configured" end - local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) - local computed = pipe:read("*a") - pipe:close() - 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 tools.get_permissions(filename) local pipe = io.popen(vars.STAT.." "..vars.STATFLAG.." "..fs.Q(filename)) local ret = pipe:read("*l") diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 19038e1f..f112ea3e 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -242,28 +242,6 @@ function tools.unpack_archive(archive) return true end -local md5_cmd = { - md5sum = fs.Q(vars.MD5SUM), - openssl = fs.Q(vars.OPENSSL).." md5", - md5 = fs.Q(vars.MD5), -} - ---- Get the MD5 checksum for a file. --- @param file string: The file to be computed. --- @return string: The MD5 checksum or nil + message -function tools.get_md5(file) - local cmd = md5_cmd[cfg.md5checker] - if not cmd then return nil, "no MD5 checker command configured" end - local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) - local computed = pipe:read("*a") - pipe:close() - 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. -- @param file string: filename to test -- @return boolean: true if file exists, false otherwise. -- cgit v1.2.3-55-g6feb