From 34131f1c7f6740f87f4f3120ee7142b07d25bbf3 Mon Sep 17 00:00:00 2001 From: hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> Date: Sun, 4 Oct 2009 16:08:01 +0000 Subject: add get_md5 git-svn-id: http://luarocks.org/svn/luarocks/trunk@58 9ca3f7c1-7366-0410-b1a3-b5c78f85698c --- src/luarocks/fs/lua.lua | 36 ++++++++++++++++++++++-------------- src/luarocks/fs/unix/tools.lua | 23 +++++++---------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index f989b652..dd9857a0 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -89,6 +89,23 @@ function execute(command, ...) return fs.execute_string(command) 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 +-- or if it could not perform the check for any reason. +function check_md5(file, md5sum) + local computed = fs.get_md5(file) + if not computed then + return false + end + if computed:match("^"..md5sum) then + return true + else + return false + end +end + --------------------------------------------------------------------- -- LuaFileSystem functions --------------------------------------------------------------------- @@ -444,25 +461,16 @@ end if md5_ok then ---- 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 --- or if it could not perform the check for any reason. -function check_md5(file, md5sum) +--- Get the MD5 checksum for a file. +-- @param file string: The file to be computed. +-- @return string: The MD5 checksum +function get_md5(file) file = fs.absolute_name(file) local file = io.open(file, "r") if not file then return false end local computed = md5.sumhexa(file:read("*a")) file:close() - if not computed then - return false - end - if computed:match("^"..md5sum) then - return true - else - return false - end + return computed end end diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index c8fefd4d..31827910 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -283,20 +283,18 @@ function unpack_archive(archive) return true 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 --- or if it could not perform the check for any reason. -function check_md5(file, md5sum) +--- 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"):gsub("[^%x]+", "") + computed = pipe:read("*l") pipe:close() if computed then - computed = computed:sub(1,32) + computed = computed:gsub("[^%x]+", ""):sub(1,32) end elseif cfg.md5checker == "openssl" then local pipe = io.popen("openssl md5 "..file) @@ -313,14 +311,7 @@ function check_md5(file, md5sum) computed = computed:sub(-32) end end - if not computed then - return false - end - if computed:match("^"..md5sum) then - return true - else - return false - end + return computed end --- Unpack an archive. -- cgit v1.2.3-55-g6feb