diff --git a/src/ProcessUtils.cpp b/src/ProcessUtils.cpp index 2e4cf75..75b0c09 100644 --- a/src/ProcessUtils.cpp +++ b/src/ProcessUtils.cpp @@ -268,28 +268,37 @@ int ProcessUtils::runElevatedMac(const std::string& executable, } #endif -#ifdef PLATFORM_WINDOWS -int ProcessUtils::runElevatedWindows(const std::string& executable, - const std::list& arguments) +// convert a list of arguments in a space-separated string. +// Arguments containing spaces are enclosed in quotes +std::string quoteArgs(const std::list& arguments) { - std::string args; - - // quote process arguments + std::string quotedArgs; for (std::list::const_iterator iter = arguments.begin(); iter != arguments.end(); iter++) { std::string arg = *iter; - if (!arg.empty() && arg.at(0) != '"' && arg.at(arg.size()-1) != '"') - { + bool isQuoted = !arg.empty() && + arg.at(0) == '"' && + arg.at(arg.size()-1) == '"'; + + if (!isQuoted && arg.find(' ') != std::string::npos) + { arg.insert(0,"\""); arg.append("\""); - } + } + quotedArgs += arg; + quotedArgs += " "; + } + return quotedArgs; +} - args += arg; - args += " "; - } +#ifdef PLATFORM_WINDOWS +int ProcessUtils::runElevatedWindows(const std::string& executable, + const std::list& arguments) +{ + std::string args = quoteArgs(arguments); SHELLEXECUTEINFO executeInfo; ZeroMemory(&executeInfo,sizeof(executeInfo)); @@ -350,18 +359,12 @@ PLATFORM_PID ProcessUtils::runAsyncUnix(const std::string& executable, #ifdef PLATFORM_WINDOWS int ProcessUtils::runWindows(const std::string& executable, - const std::list& args, + const std::list& _args, RunMode runMode) { - std::string commandLine = executable; - for (std::list::const_iterator iter = args.begin(); iter != args.end(); iter++) - { - if (!commandLine.empty()) - { - commandLine.append(" "); - } - commandLine.append(*iter); - } + std::list args(_args); + args.push_front(executable); + std::string commandLine = quoteArgs(args); STARTUPINFO startupInfo; ZeroMemory(&startupInfo,sizeof(startupInfo)); diff --git a/src/tests/test-update.rb b/src/tests/test-update.rb index 962cdb2..db8b7d4 100755 --- a/src/tests/test-update.rb +++ b/src/tests/test-update.rb @@ -3,7 +3,10 @@ require 'fileutils.rb' require 'rbconfig' -INSTALL_DIR = File.expand_path("install-dir/") +# Install directory - this contains a space to check +# for correct escaping of paths when passing comamnd +# line arguments under Windows +INSTALL_DIR = File.expand_path("install dir/") PACKAGE_DIR = File.expand_path("package-dir/") if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/) @@ -86,8 +89,11 @@ FileUtils.cp("../#{UPDATER_NAME}","#{PACKAGE_DIR}/#{UPDATER_NAME}") # make sure that it looks in the correct directory for # the file_list.xml file and packages # +install_path = File.absolute_path(INSTALL_DIR) Dir.chdir(INSTALL_DIR) do - system("#{PACKAGE_DIR}/#{UPDATER_NAME} --install-dir #{INSTALL_DIR} --package-dir #{PACKAGE_DIR} --script file_list.xml") + cmd = "#{PACKAGE_DIR}/#{UPDATER_NAME} --install-dir \"#{install_path}\" --package-dir \"#{PACKAGE_DIR}\" --script file_list.xml" + puts "Running '#{cmd}'" + system(cmd) end # TODO - Correctly wait until updater has finished