From 1ae81cea573bcaf3baefbd2cdba6b5a5976716d5 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 23 Nov 2010 22:34:51 -0200 Subject: Use unified matcher for md5 commands, as suggested by Doug Currie. --- src/luarocks/fs/unix/tools.lua | 40 ++++++++++++++-------------------------- src/luarocks/fs/win32/tools.lua | 40 ++++++++++++++-------------------------- 2 files changed, 28 insertions(+), 52 deletions(-) diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index c805fb48..691c2648 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -287,35 +287,23 @@ function unpack_archive(archive) return true end +local md5_cmd = { + md5sum = "md5sum ", + openssl = "openssl md5 ", + md5 = "md5 ", +} + --- Get the MD5 checksum for a file. -- @param file string: The file to be computed. -- @return string: The MD5 checksum -function get_md5(file, md5sum) - file = fs.absolute_name(file) - local computed - if cfg.md5checker == "md5sum" then - local pipe = io.popen("md5sum "..file) - computed = pipe:read("*a") - pipe:close() - if computed then - computed = computed:gsub("[^%x]+", ""):sub(1,32) - end - elseif cfg.md5checker == "openssl" then - local pipe = io.popen("openssl md5 "..file) - computed = pipe:read("*l") - pipe:close() - if computed then - computed = computed:sub(-32) - end - elseif cfg.md5checker == "md5" then - local pipe = io.popen("md5 "..file) - computed = pipe:read("*l") - pipe:close() - if computed then - computed = computed:sub(-32) - end - end - return computed +function get_md5(file) + local cmd = md5_cmd[cfg.md5checker] + if not cmd then return nil end + 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)..")") end --- Unpack an archive. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index d2613b67..9e891fca 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -62,35 +62,23 @@ function is_file(file) return fs.execute("test -f", file) end +local md5_cmd = { + md5sum = "md5sum ", + openssl = "openssl md5 ", + md5 = "md5 ", +} + --- Get the MD5 checksum for a file. -- @param file string: The file to be computed. -- @return string: The MD5 checksum -function get_md5(file, md5sum) - file = fs.absolute_name(file) - local computed - if cfg.md5checker == "md5sum" then - local pipe = io.popen("md5sum "..file) - computed = pipe:read("*l") - pipe:close() - if computed then - computed = computed:gsub("[^%x]+", ""):sub(1,32) - end - elseif cfg.md5checker == "openssl" then - local pipe = io.popen("openssl md5 "..file) - computed = pipe:read("*l") - pipe:close() - if computed then - computed = computed:sub(-32) - end - elseif cfg.md5checker == "md5" then - local pipe = io.popen("md5 "..file) - computed = pipe:read("*l") - pipe:close() - if computed then - computed = computed:sub(-32) - end - end - return computed +function get_md5(file) + local cmd = md5_cmd[cfg.md5checker] + if not cmd then return nil end + 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)..")") end --- Change the current directory. -- cgit v1.2.3-55-g6feb