- Fixed escaping of spaces for scp and curl
This commit is contained in:
Johannes Schlatow 2011-11-30 00:12:20 +01:00
parent edad4d2ee6
commit d8d2060b39
5 changed files with 69 additions and 23 deletions

View file

@ -72,6 +72,22 @@ Transport* Transport::getTransport(const Uri& uri)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int Transport::execute() int Transport::execute()
{ {
// quote arguments
std::vector<std::string>::iterator it = _arguments.begin ();
for (; it != _arguments.end (); it++)
{
// quote until the first appearance of '{'
size_t pos = it->find('{');
if (pos != 0)
{
// '{' is not the first character
it->insert(0, "\"");
if (pos != std::string::npos)
it->insert(pos+1, "\"");
else
it->append("\"");
}
}
return ::execute(_executable, _arguments); return ::execute(_executable, _arguments);
} }

View file

@ -63,12 +63,12 @@ void TransportCurl::send(const std::string& source)
throw format (STRING_TRANSPORT_URI_NODIR, _uri._path); throw format (STRING_TRANSPORT_URI_NODIR, _uri._path);
_arguments.push_back ("-T"); _arguments.push_back ("-T");
_arguments.push_back ("\"" + source + "\""); _arguments.push_back ("\"" + escape (source, ' ') + "\"");
} }
else else
{ {
_arguments.push_back ("-T"); _arguments.push_back ("-T");
_arguments.push_back (source); _arguments.push_back (escape (source, ' '));
} }
// cmd line is: curl -T source protocol://host:port/path // cmd line is: curl -T source protocol://host:port/path
@ -103,6 +103,7 @@ void TransportCurl::recv(std::string target)
_arguments.push_back(_uri._user); _arguments.push_back(_uri._user);
} }
std::vector<std::string> targetargs;
if (is_filelist(_uri._path)) if (is_filelist(_uri._path))
{ {
@ -124,15 +125,16 @@ void TransportCurl::recv(std::string target)
suffix = toSplit.substr (pos+1); suffix = toSplit.substr (pos+1);
split (splitted, toSplit.substr(0, pos), ','); split (splitted, toSplit.substr(0, pos), ',');
target = "";
std::vector <std::string>::iterator file; std::vector <std::string>::iterator file;
for (file = splitted.begin (); file != splitted.end (); ++file) for (file = splitted.begin (); file != splitted.end (); ++file) {
target += " -o " + prefix + *file + suffix; targetargs.push_back ("-o");
targetargs.push_back (prefix + *file + suffix);
}
} }
else else
{ {
target = "-o " + target; targetargs.push_back ("-o");
targetargs.push_back (target);
} }
// cmd line is: curl protocol://host:port/path/to/source/file -o path/to/target/file // cmd line is: curl protocol://host:port/path/to/source/file -o path/to/target/file
@ -145,7 +147,7 @@ void TransportCurl::recv(std::string target)
_arguments.push_back (_uri._protocol + "://" + _uri._host + "/" + _uri._path); _arguments.push_back (_uri._protocol + "://" + _uri._host + "/" + _uri._path);
} }
_arguments.push_back (target); _arguments.insert (_arguments.end (), targetargs.begin (), targetargs.end ());
int result = execute(); int result = execute();
if (result) if (result)

View file

@ -31,6 +31,7 @@
#include <text.h> #include <text.h>
#include <i18n.h> #include <i18n.h>
#include <TransportSSH.h> #include <TransportSSH.h>
#include <util.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
TransportSSH::TransportSSH(const Uri& uri) : Transport(uri) TransportSSH::TransportSSH(const Uri& uri) : Transport(uri)
@ -60,11 +61,11 @@ void TransportSSH::send(const std::string& source)
if (_uri._user != "") if (_uri._user != "")
{ {
_arguments.push_back (_uri._user + "@" + _uri._host + ":" + _uri._path); _arguments.push_back (_uri._user + "@" + _uri._host + ":" + escape (_uri._path, ' '));
} }
else else
{ {
_arguments.push_back (_uri._host + ":" + _uri._path); _arguments.push_back (_uri._host + ":" + escape (_uri._path, ' '));
} }
if (execute()) if (execute())
@ -91,11 +92,11 @@ void TransportSSH::recv(std::string target)
if (_uri._user != "") if (_uri._user != "")
{ {
_arguments.push_back (_uri._user + "@" + _uri._host + ":" + _uri._path); _arguments.push_back (_uri._user + "@" + _uri._host + ":" + escape (_uri._path, ' '));
} }
else else
{ {
_arguments.push_back (_uri._host + ":" + _uri._path); _arguments.push_back (_uri._host + ":" + escape (_uri._path, ' '));
} }
_arguments.push_back (target); _arguments.push_back (target);

View file

@ -405,11 +405,8 @@ int execute(const std::string& executable, std::vector<std::string> arguments)
if (executable == "") if (executable == "")
return -1; return -1;
pid_t child_pid = fork(); // create command line before forking because the parent process also needs this for
// calling context.debug()
if (child_pid == 0)
{
// this is done by the child process
char shell[] = "bash"; char shell[] = "bash";
char opt[] = "-c"; char opt[] = "-c";
@ -421,6 +418,13 @@ int execute(const std::string& executable, std::vector<std::string> arguments)
cmdline += " " + (std::string)*it; cmdline += " " + (std::string)*it;
} }
context.debug ("Executing: " + std::string(shell) + " " + std::string(opt) + " " + cmdline);
pid_t child_pid = fork();
if (child_pid == 0)
{
// this is done by the child process
char** argv = new char*[4]; char** argv = new char*[4];
argv[0] = shell; // bash argv[0] = shell; // bash
argv[1] = opt; // -c argv[1] = opt; // -c
@ -534,6 +538,27 @@ const std::string decode (const std::string& value)
return modified; return modified;
} }
////////////////////////////////////////////////////////////////////////////////
// Escapes any unescaped character of type c within the given string
// e.g. ' ' -> '\ '
const std::string escape (const std::string& value, char c)
{
std::string modified = value;
char tmp[2] = {c, '\0'};
std::string search = tmp;
std::string replace = "\\" + search;
std::string::size_type pos = modified.find (search);
while (pos != std::string::npos) {
if ( modified[pos-1] != '\\' )
modified.replace (pos, 1, replace);
pos = modified.find (search, pos+1);
}
return modified;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -80,5 +80,7 @@ unsigned burndown_size (unsigned ntasks);
const std::string encode (const std::string&); const std::string encode (const std::string&);
const std::string decode (const std::string&); const std::string decode (const std::string&);
const std::string escape (const std::string&, char);
#endif #endif
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////