diff options
| author | hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> | 2009-10-04 16:08:01 +0000 |
|---|---|---|
| committer | hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> | 2009-10-04 16:08:01 +0000 |
| commit | 34131f1c7f6740f87f4f3120ee7142b07d25bbf3 (patch) | |
| tree | f98ea5c203c37f786e438cec157af7f17db6f164 | |
| parent | ca15323a477a81e5f20c4b3195e318b36cbc71b4 (diff) | |
| download | luarocks-34131f1c7f6740f87f4f3120ee7142b07d25bbf3.tar.gz luarocks-34131f1c7f6740f87f4f3120ee7142b07d25bbf3.tar.bz2 luarocks-34131f1c7f6740f87f4f3120ee7142b07d25bbf3.zip | |
add get_md5
git-svn-id: http://luarocks.org/svn/luarocks/trunk@58 9ca3f7c1-7366-0410-b1a3-b5c78f85698c
| -rw-r--r-- | src/luarocks/fs/lua.lua | 36 | ||||
| -rw-r--r-- | 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, ...) | |||
| 89 | return fs.execute_string(command) | 89 | return fs.execute_string(command) |
| 90 | end | 90 | end |
| 91 | 91 | ||
| 92 | --- Check the MD5 checksum for a file. | ||
| 93 | -- @param file string: The file to be checked. | ||
| 94 | -- @param md5sum string: The string with the expected MD5 checksum. | ||
| 95 | -- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false if not | ||
| 96 | -- or if it could not perform the check for any reason. | ||
| 97 | function check_md5(file, md5sum) | ||
| 98 | local computed = fs.get_md5(file) | ||
| 99 | if not computed then | ||
| 100 | return false | ||
| 101 | end | ||
| 102 | if computed:match("^"..md5sum) then | ||
| 103 | return true | ||
| 104 | else | ||
| 105 | return false | ||
| 106 | end | ||
| 107 | end | ||
| 108 | |||
| 92 | --------------------------------------------------------------------- | 109 | --------------------------------------------------------------------- |
| 93 | -- LuaFileSystem functions | 110 | -- LuaFileSystem functions |
| 94 | --------------------------------------------------------------------- | 111 | --------------------------------------------------------------------- |
| @@ -444,25 +461,16 @@ end | |||
| 444 | 461 | ||
| 445 | if md5_ok then | 462 | if md5_ok then |
| 446 | 463 | ||
| 447 | --- Check the MD5 checksum for a file. | 464 | --- Get the MD5 checksum for a file. |
| 448 | -- @param file string: The file to be checked. | 465 | -- @param file string: The file to be computed. |
| 449 | -- @param md5sum string: The string with the expected MD5 checksum. | 466 | -- @return string: The MD5 checksum |
| 450 | -- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false if not | 467 | function get_md5(file) |
| 451 | -- or if it could not perform the check for any reason. | ||
| 452 | function check_md5(file, md5sum) | ||
| 453 | file = fs.absolute_name(file) | 468 | file = fs.absolute_name(file) |
| 454 | local file = io.open(file, "r") | 469 | local file = io.open(file, "r") |
| 455 | if not file then return false end | 470 | if not file then return false end |
| 456 | local computed = md5.sumhexa(file:read("*a")) | 471 | local computed = md5.sumhexa(file:read("*a")) |
| 457 | file:close() | 472 | file:close() |
| 458 | if not computed then | 473 | return computed |
| 459 | return false | ||
| 460 | end | ||
| 461 | if computed:match("^"..md5sum) then | ||
| 462 | return true | ||
| 463 | else | ||
| 464 | return false | ||
| 465 | end | ||
| 466 | end | 474 | end |
| 467 | 475 | ||
| 468 | end | 476 | 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) | |||
| 283 | return true | 283 | return true |
| 284 | end | 284 | end |
| 285 | 285 | ||
| 286 | --- Check the MD5 checksum for a file. | 286 | --- Get the MD5 checksum for a file. |
| 287 | -- @param file string: The file to be checked. | 287 | -- @param file string: The file to be computed. |
| 288 | -- @param md5sum string: The string with the expected MD5 checksum. | 288 | -- @return string: The MD5 checksum |
| 289 | -- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false if not | 289 | function get_md5(file, md5sum) |
| 290 | -- or if it could not perform the check for any reason. | ||
| 291 | function check_md5(file, md5sum) | ||
| 292 | file = fs.absolute_name(file) | 290 | file = fs.absolute_name(file) |
| 293 | local computed | 291 | local computed |
| 294 | if cfg.md5checker == "md5sum" then | 292 | if cfg.md5checker == "md5sum" then |
| 295 | local pipe = io.popen("md5sum "..file) | 293 | local pipe = io.popen("md5sum "..file) |
| 296 | computed = pipe:read("*l"):gsub("[^%x]+", "") | 294 | computed = pipe:read("*l") |
| 297 | pipe:close() | 295 | pipe:close() |
| 298 | if computed then | 296 | if computed then |
| 299 | computed = computed:sub(1,32) | 297 | computed = computed:gsub("[^%x]+", ""):sub(1,32) |
| 300 | end | 298 | end |
| 301 | elseif cfg.md5checker == "openssl" then | 299 | elseif cfg.md5checker == "openssl" then |
| 302 | local pipe = io.popen("openssl md5 "..file) | 300 | local pipe = io.popen("openssl md5 "..file) |
| @@ -313,14 +311,7 @@ function check_md5(file, md5sum) | |||
| 313 | computed = computed:sub(-32) | 311 | computed = computed:sub(-32) |
| 314 | end | 312 | end |
| 315 | end | 313 | end |
| 316 | if not computed then | 314 | return computed |
| 317 | return false | ||
| 318 | end | ||
| 319 | if computed:match("^"..md5sum) then | ||
| 320 | return true | ||
| 321 | else | ||
| 322 | return false | ||
| 323 | end | ||
| 324 | end | 315 | end |
| 325 | 316 | ||
| 326 | --- Unpack an archive. | 317 | --- Unpack an archive. |
