From 370a2943c09e70154862f382fe3de7f486a4a1c6 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Wed, 13 Nov 2013 09:33:10 +0100 Subject: fixes LR install in location with spaces, quoting LR commands NOTE: seems to be broken now, bug not found yet --- src/luarocks/fs.lua | 19 +++++++++++++++++++ src/luarocks/fs/lua.lua | 8 ++++++-- src/luarocks/fs/win32/tools.lua | 36 ++++++++++++++++++------------------ 3 files changed, 43 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs.lua b/src/luarocks/fs.lua index 467b1943..e002c7ba 100644 --- a/src/luarocks/fs.lua +++ b/src/luarocks/fs.lua @@ -38,3 +38,22 @@ load_fns(fs_lua) local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") if ok and fs_plat_tools then load_fns(fs_plat_tools) end +-- uncomment below for further debugging than 'verbose=true' in config file +-- code below will also catch commands outside of fs.execute() +-- especially uses of io.popen(). +--[[ +old_exec = os.execute +os.execute = function(cmd) + print("os.execute: ", cmd) + return old_exec(cmd) +end +old_popen = io.popen +io.popen = function(one, two) + if two == nil then + print("io.popen: ", one) + else + print("io.popen: ", one, "Mode:", two) + end + return old_popen(one, two) +end +--]] diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index d477d88f..51cbee03 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -108,12 +108,16 @@ end -- The command is executed in the current directory in the dir stack. -- @param command string: The command to be executed. No quoting/escaping -- is applied. --- @param ... Strings containing additional arguments, which are quoted. +-- @param ... Strings containing additional arguments, which will be quoted. -- @return boolean: true if command succeeds (status code 0), false -- otherwise. function execute_quiet(command, ...) assert(type(command) == "string") - return fs.execute_string(fs.quiet(quote_args(command, ...))) + if cfg.verbose then + return fs.execute_string(quote_args(command, ...)) + else + return fs.execute_string(fs.quiet(quote_args(command, ...))) + end end --- Check the MD5 checksum for a file. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 979aaff4..9aa76851 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -112,7 +112,7 @@ end function make_dir(directory) assert(directory) directory = dir.normalize(directory) - fs.execute_quiet(vars.MKDIR.." -p ", directory) + fs.execute_quiet(fs.Q(vars.MKDIR).." -p ", directory) if not fs.is_dir(directory) then return false, "failed making directory "..directory end @@ -125,7 +125,7 @@ end -- @param directory string: pathname of directory to remove. function remove_dir_if_empty(directory) assert(directory) - fs.execute_quiet(vars.RMDIR, directory) + fs.execute_quiet(fs.Q(vars.RMDIR), directory) end --- Remove a directory if it is empty. @@ -134,7 +134,7 @@ end -- @param directory string: pathname of directory to remove. function remove_dir_tree_if_empty(directory) assert(directory) - fs.execute_quiet(vars.RMDIR, directory) + fs.execute_quiet(fs.Q(vars.RMDIR), directory) end --- Copy a file. @@ -145,7 +145,7 @@ end function copy(src, dest) assert(src and dest) if dest:match("[/\\]$") then dest = dest:sub(1, -2) end - local ok = fs.execute(vars.CP, src, dest) + local ok = fs.execute(fs.Q(vars.CP), src, dest) if ok then return true else @@ -160,7 +160,7 @@ end -- plus an error message. function copy_contents(src, dest) assert(src and dest) - if fs.execute_quiet(vars.CP.." -dR "..src.."\\*.* "..fs.Q(dest)) then + if fs.execute_quiet(fs.Q(vars.CP).." -dR "..src.."\\*.* "..fs.Q(dest)) then return true else return false, "Failed copying "..src.." to "..dest @@ -191,7 +191,7 @@ function list_dir(at) return {} end local result = {} - local pipe = io.popen(command_at(at, vars.LS)) + local pipe = io.popen(command_at(at, fs.Q(vars.LS))) for file in pipe:lines() do table.insert(result, file) end @@ -214,7 +214,7 @@ function find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, vars.FIND.." 2> NUL")) + local pipe = io.popen(command_at(at, fs.Q(vars.FIND).." 2> NUL")) for file in pipe:lines() do -- Windows find is a bit different local first_two = file:sub(1,2) @@ -233,7 +233,7 @@ end -- additional arguments. -- @return boolean: true on success, false on failure. function zip(zipfile, ...) - return fs.execute_quiet(vars.SEVENZ.." -aoa a -tzip", zipfile, ...) + return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa a -tzip", zipfile, ...) end --- Uncompress files from a .zip archive. @@ -241,7 +241,7 @@ end -- @return boolean: true on success, false on failure. function unzip(zipfile) assert(zipfile) - return fs.execute_quiet(vars.SEVENZ.." -aoa x", zipfile) + return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa x", zipfile) end --- Test is pathname is a directory. @@ -257,7 +257,7 @@ end -- @return boolean: true if it is a regular file, false otherwise. function is_file(file) assert(file) - return fs.execute(vars.TEST.." -f", file) + return fs.execute(fs.Q(vars.TEST).." -f", file) end --- Download a remote file. @@ -276,7 +276,7 @@ function download(url, filename, cache) local ok if cfg.downloader == "wget" then - local wget_cmd = vars.WGET.." --no-check-certificate --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet " + local wget_cmd = fs.Q(vars.WGET).." --no-check-certificate --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet " if cache then -- --timestamping is incompatible with --output-document, -- but that's not a problem for our use cases. @@ -289,7 +289,7 @@ function download(url, filename, cache) ok = fs.execute(wget_cmd..fs.Q(url).." 2> NUL 1> NUL") end elseif cfg.downloader == "curl" then - ok = fs.execute_string(vars.CURL.." -L --user-agent \""..cfg.user_agent.." via curl\" "..fs.Q(url).." 2> NUL 1> "..fs.Q(filename)) + ok = fs.execute_string(fs.Q(vars.CURL).." -L --user-agent \""..cfg.user_agent.." via curl\" "..fs.Q(url).." 2> NUL 1> "..fs.Q(filename)) end if ok then return true, filename @@ -302,7 +302,7 @@ end -- @param archive string: Filename of archive. -- @return boolean : success status local function gunzip(archive) - return fs.execute_quiet(vars.SEVENZ.." -aoa x", archive) + return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa x", archive) end --- Unpack an archive. @@ -314,7 +314,7 @@ function unpack_archive(archive) assert(type(archive) == "string") local ok - local sevenzx = vars.SEVENZ.." -aoa x" + local sevenzx = fs.Q(vars.SEVENZ).." -aoa x" if archive:match("%.tar%.gz$") then ok = gunzip(archive) if ok then @@ -346,9 +346,9 @@ function unpack_archive(archive) end local md5_cmd = { - md5sum = vars.MD5SUM, - openssl = vars.OPENSSL.." md5", - md5 = vars.MD5, + md5sum = fs.Q(vars.MD5SUM), + openssl = fs.Q(vars.OPENSSL).." md5", + md5 = fs.Q(vars.MD5), } --- Get the MD5 checksum for a file. @@ -357,7 +357,7 @@ local md5_cmd = { 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 pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) local computed = pipe:read("*a") pipe:close() if not computed then return nil end -- cgit v1.2.3-55-g6feb 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(-) (limited to 'src') 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 From 722c03ae8b059cba8039e8066dd1a82e7c3ddcb3 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Tue, 19 Nov 2013 11:52:08 +0100 Subject: fixed double quotes in io.popen commands --- src/luarocks/fs/win32.lua | 9 +++++++++ src/luarocks/fs/win32/tools.lua | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 0280b3f0..9f85ac05 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -9,6 +9,15 @@ local cfg = require("luarocks.cfg") local dir = require("luarocks.dir") local util = require("luarocks.util") +-- Monkey patch io.popen and os.execute to make sure quoting +-- works as expected. +-- See http://lua-users.org/lists/lua-l/2013-11/msg00367.html +local _prefix = "type NUL && " +local _popen, _execute = io.popen, os.execute +io.popen = function(cmd, ...) return _popen(_prefix..cmd, ...) end +os.execute = function(cmd, ...) return _execute(_prefix..cmd, ...) end + + --- Annotate command string for quiet execution. -- @param cmd string: A command-line string. -- @return string: The command-line, with silencing annotation. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 1c0131c7..1d302288 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -357,7 +357,7 @@ local md5_cmd = { 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)) -- should be in fs.Q() + local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) local computed = pipe:read("*a") pipe:close() if computed then -- cgit v1.2.3-55-g6feb