mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
exec
- moved function that executes external commands from Transport.cpp to util.cpp since it is not dedicated to push/pull/merge anymore
This commit is contained in:
parent
398371d324
commit
7e1f8591f6
3 changed files with 54 additions and 45 deletions
|
@ -27,8 +27,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include "util.h"
|
||||||
#include <sys/wait.h>
|
|
||||||
#include "Transport.h"
|
#include "Transport.h"
|
||||||
#include "TransportSSH.h"
|
#include "TransportSSH.h"
|
||||||
#include "TransportRSYNC.h"
|
#include "TransportRSYNC.h"
|
||||||
|
@ -70,49 +69,7 @@ Transport* Transport::getTransport(const Uri& uri)
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int Transport::execute()
|
int Transport::execute()
|
||||||
{
|
{
|
||||||
if (executable == "")
|
return ::execute(executable, arguments);
|
||||||
return -1;
|
|
||||||
|
|
||||||
pid_t child_pid = fork();
|
|
||||||
|
|
||||||
if (child_pid == 0)
|
|
||||||
{
|
|
||||||
// this is done by the child process
|
|
||||||
char shell[] = "bash";
|
|
||||||
char opt[] = "-c";
|
|
||||||
|
|
||||||
std::string cmdline = executable;
|
|
||||||
|
|
||||||
std::vector <std::string>::iterator it;
|
|
||||||
for (it = arguments.begin(); it != arguments.end(); ++it)
|
|
||||||
{
|
|
||||||
std::string tmp = *it;
|
|
||||||
cmdline += " " + tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
char** argv = new char*[4];
|
|
||||||
argv[0] = shell; // sh
|
|
||||||
argv[1] = opt; // -c
|
|
||||||
argv[2] = (char*)cmdline.c_str(); // e.g. scp undo.data user@host:.task/
|
|
||||||
argv[3] = NULL; // required by execv
|
|
||||||
|
|
||||||
int ret = execvp(shell, argv);
|
|
||||||
delete[] argv;
|
|
||||||
|
|
||||||
exit(ret);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// this is done by the parent process
|
|
||||||
int child_status;
|
|
||||||
|
|
||||||
pid_t pid = waitpid(child_pid, &child_status, 0);
|
|
||||||
|
|
||||||
if (pid == -1)
|
|
||||||
return -1;
|
|
||||||
else
|
|
||||||
return child_status;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
51
src/util.cpp
51
src/util.cpp
|
@ -31,6 +31,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -380,3 +381,53 @@ std::string compressIds (const std::vector <int>& ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Run an external executable with execvp. This means stdio goes to
|
||||||
|
// the child process, so that it can receive user input (e.g. passwords).
|
||||||
|
//
|
||||||
|
int execute(const std::string& executable, std::vector<std::string> arguments)
|
||||||
|
{
|
||||||
|
if (executable == "")
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
pid_t child_pid = fork();
|
||||||
|
|
||||||
|
if (child_pid == 0)
|
||||||
|
{
|
||||||
|
// this is done by the child process
|
||||||
|
char shell[] = "bash";
|
||||||
|
char opt[] = "-c";
|
||||||
|
|
||||||
|
std::string cmdline = executable;
|
||||||
|
|
||||||
|
std::vector <std::string>::iterator it;
|
||||||
|
for (it = arguments.begin(); it != arguments.end(); ++it)
|
||||||
|
{
|
||||||
|
cmdline += " " + (std::string)*it;
|
||||||
|
}
|
||||||
|
|
||||||
|
char** argv = new char*[4];
|
||||||
|
argv[0] = shell; // bash
|
||||||
|
argv[1] = opt; // -c
|
||||||
|
argv[2] = (char*)cmdline.c_str(); // e.g. scp undo.data user@host:.task/
|
||||||
|
argv[3] = NULL; // required by execv
|
||||||
|
|
||||||
|
int ret = execvp(shell, argv);
|
||||||
|
delete[] argv;
|
||||||
|
|
||||||
|
exit(ret);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// this is done by the parent process
|
||||||
|
int child_status;
|
||||||
|
|
||||||
|
pid_t pid = waitpid(child_pid, &child_status, 0);
|
||||||
|
|
||||||
|
if (pid == -1)
|
||||||
|
return -1;
|
||||||
|
else
|
||||||
|
return child_status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -58,6 +58,7 @@ void delay (float);
|
||||||
std::string formatBytes (size_t);
|
std::string formatBytes (size_t);
|
||||||
int autoComplete (const std::string&, const std::vector<std::string>&, std::vector<std::string>&);
|
int autoComplete (const std::string&, const std::vector<std::string>&, std::vector<std::string>&);
|
||||||
const std::string uuid ();
|
const std::string uuid ();
|
||||||
|
int execute (const std::string&, std::vector<std::string>);
|
||||||
|
|
||||||
#ifdef SOLARIS
|
#ifdef SOLARIS
|
||||||
#define LOCK_SH 1
|
#define LOCK_SH 1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue