SSH: Pass remote command as QString

The old implementation sent the command over the wire as-is, so we
declared it as a QByteArray and let the caller choose the encoding. This
doesn't make sense anymore, as the command is now passed to an external
process as a QString anyway.

Change-Id: Ib84bc0f871db2b45b93f71d924c4177cc28d3bb0
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2019-05-21 16:59:29 +02:00
parent be0d7aa3e9
commit 0a555018d1
17 changed files with 33 additions and 45 deletions

View File

@@ -309,7 +309,7 @@ SshConnection::~SshConnection()
delete d; delete d;
} }
SshRemoteProcessPtr SshConnection::createRemoteProcess(const QByteArray &command) SshRemoteProcessPtr SshConnection::createRemoteProcess(const QString &command)
{ {
QTC_ASSERT(state() == Connected, return SshRemoteProcessPtr()); QTC_ASSERT(state() == Connected, return SshRemoteProcessPtr());
return SshRemoteProcessPtr(new SshRemoteProcess(command, d->connectionArgs())); return SshRemoteProcessPtr(new SshRemoteProcess(command, d->connectionArgs()));
@@ -317,7 +317,7 @@ SshRemoteProcessPtr SshConnection::createRemoteProcess(const QByteArray &command
SshRemoteProcessPtr SshConnection::createRemoteShell() SshRemoteProcessPtr SshConnection::createRemoteShell()
{ {
return createRemoteProcess(QByteArray()); return createRemoteProcess({});
} }
SftpTransferPtr SshConnection::createUpload(const FilesToTransfer &files, SftpTransferPtr SshConnection::createUpload(const FilesToTransfer &files,

View File

@@ -111,7 +111,7 @@ public:
bool sharingEnabled() const; bool sharingEnabled() const;
~SshConnection(); ~SshConnection();
SshRemoteProcessPtr createRemoteProcess(const QByteArray &command); SshRemoteProcessPtr createRemoteProcess(const QString &command);
SshRemoteProcessPtr createRemoteShell(); SshRemoteProcessPtr createRemoteShell();
SftpTransferPtr createUpload(const FilesToTransfer &files, SftpTransferPtr createUpload(const FilesToTransfer &files,
FileTransferErrorHandling errorHandlingMode); FileTransferErrorHandling errorHandlingMode);

View File

@@ -54,13 +54,13 @@ using namespace Internal;
struct SshRemoteProcess::SshRemoteProcessPrivate struct SshRemoteProcess::SshRemoteProcessPrivate
{ {
QByteArray remoteCommand; QString remoteCommand;
QStringList connectionArgs; QStringList connectionArgs;
QString displayName; QString displayName;
bool useTerminal = false; bool useTerminal = false;
}; };
SshRemoteProcess::SshRemoteProcess(const QByteArray &command, const QStringList &connectionArgs) SshRemoteProcess::SshRemoteProcess(const QString &command, const QStringList &connectionArgs)
: d(new SshRemoteProcessPrivate) : d(new SshRemoteProcessPrivate)
{ {
d->remoteCommand = command; d->remoteCommand = command;
@@ -127,7 +127,7 @@ QStringList SshRemoteProcess::fullLocalCommandLine() const
if (!d->displayName.isEmpty()) if (!d->displayName.isEmpty())
args.prepend("-X"); args.prepend("-X");
if (!d->remoteCommand.isEmpty()) if (!d->remoteCommand.isEmpty())
args << QLatin1String(d->remoteCommand); args << d->remoteCommand;
args.prepend(SshSettings::sshFilePath().toString()); args.prepend(SshSettings::sshFilePath().toString());
return args; return args;
} }

View File

@@ -30,10 +30,6 @@
#include <QStringList> #include <QStringList>
QT_BEGIN_NAMESPACE
class QByteArray;
QT_END_NAMESPACE
namespace QSsh { namespace QSsh {
class SshConnection; class SshConnection;
@@ -56,7 +52,7 @@ signals:
void done(const QString &error); void done(const QString &error);
private: private:
SshRemoteProcess(const QByteArray &command, const QStringList &connectionArgs); SshRemoteProcess(const QString &command, const QStringList &connectionArgs);
void doStart(); void doStart();
struct SshRemoteProcessPrivate; struct SshRemoteProcessPrivate;

View File

@@ -50,7 +50,7 @@ public:
SshRemoteProcessPtr m_process; SshRemoteProcessPtr m_process;
SshConnection *m_connection; SshConnection *m_connection;
bool m_runInTerminal; bool m_runInTerminal;
QByteArray m_command; QString m_command;
QString m_lastConnectionErrorString; QString m_lastConnectionErrorString;
QProcess::ExitStatus m_exitStatus; QProcess::ExitStatus m_exitStatus;
QByteArray m_stdout; QByteArray m_stdout;
@@ -76,8 +76,7 @@ SshRemoteProcessRunner::~SshRemoteProcessRunner()
delete d; delete d;
} }
void SshRemoteProcessRunner::run(const QByteArray &command, void SshRemoteProcessRunner::run(const QString &command, const SshConnectionParameters &sshParams)
const SshConnectionParameters &sshParams)
{ {
QTC_ASSERT(d->m_state == Inactive, return); QTC_ASSERT(d->m_state == Inactive, return);
@@ -85,14 +84,14 @@ void SshRemoteProcessRunner::run(const QByteArray &command,
runInternal(command, sshParams); runInternal(command, sshParams);
} }
void SshRemoteProcessRunner::runInTerminal(const QByteArray &command, void SshRemoteProcessRunner::runInTerminal(const QString &command,
const SshConnectionParameters &sshParams) const SshConnectionParameters &sshParams)
{ {
d->m_runInTerminal = true; d->m_runInTerminal = true;
runInternal(command, sshParams); runInternal(command, sshParams);
} }
void SshRemoteProcessRunner::runInternal(const QByteArray &command, void SshRemoteProcessRunner::runInternal(const QString &command,
const SshConnectionParameters &sshParams) const SshConnectionParameters &sshParams)
{ {
setState(Connecting); setState(Connecting);
@@ -197,7 +196,7 @@ void SshRemoteProcessRunner::setState(int newState)
} }
} }
QByteArray SshRemoteProcessRunner::command() const { return d->m_command; } QString SshRemoteProcessRunner::command() const { return d->m_command; }
QString SshRemoteProcessRunner::lastConnectionErrorString() const { QString SshRemoteProcessRunner::lastConnectionErrorString() const {
return d->m_lastConnectionErrorString; return d->m_lastConnectionErrorString;
} }

View File

@@ -39,9 +39,9 @@ public:
SshRemoteProcessRunner(QObject *parent = 0); SshRemoteProcessRunner(QObject *parent = 0);
~SshRemoteProcessRunner(); ~SshRemoteProcessRunner();
void run(const QByteArray &command, const SshConnectionParameters &sshParams); void run(const QString &command, const SshConnectionParameters &sshParams);
void runInTerminal(const QByteArray &command, const SshConnectionParameters &sshParams); void runInTerminal(const QString &command, const SshConnectionParameters &sshParams);
QByteArray command() const; QString command() const;
QString lastConnectionErrorString() const; QString lastConnectionErrorString() const;
@@ -69,7 +69,7 @@ private:
void handleProcessFinished(const QString &error); void handleProcessFinished(const QString &error);
void handleStdout(); void handleStdout();
void handleStderr(); void handleStderr();
void runInternal(const QByteArray &command, const QSsh::SshConnectionParameters &sshParams); void runInternal(const QString &command, const QSsh::SshConnectionParameters &sshParams);
void setState(int newState); void setState(int newState);
Internal::SshRemoteProcessRunnerPrivate * const d; Internal::SshRemoteProcessRunnerPrivate * const d;

View File

@@ -188,7 +188,7 @@ void SshDeviceProcess::handleConnected()
d->process = runInTerminal() && d->runnable.executable.isEmpty() d->process = runInTerminal() && d->runnable.executable.isEmpty()
? d->connection->createRemoteShell() ? d->connection->createRemoteShell()
: d->connection->createRemoteProcess(fullCommandLine(d->runnable).toUtf8()); : d->connection->createRemoteProcess(fullCommandLine(d->runnable));
const QString display = d->displayName(); const QString display = d->displayName();
if (!display.isEmpty()) if (!display.isEmpty())
d->process->requestX11Forwarding(display); d->process->requestX11Forwarding(display);

View File

@@ -54,7 +54,7 @@ void SshDeviceProcessList::doUpdate()
this, &SshDeviceProcessList::handleConnectionError); this, &SshDeviceProcessList::handleConnectionError);
connect(&d->process, &SshRemoteProcessRunner::processClosed, connect(&d->process, &SshRemoteProcessRunner::processClosed,
this, &SshDeviceProcessList::handleListProcessFinished); this, &SshDeviceProcessList::handleListProcessFinished);
d->process.run(listProcessesCommandLine().toUtf8(), device()->sshParameters()); d->process.run(listProcessesCommandLine(), device()->sshParameters());
} }
void SshDeviceProcessList::doKillProcess(const DeviceProcessItem &process) void SshDeviceProcessList::doKillProcess(const DeviceProcessItem &process)

View File

@@ -292,12 +292,9 @@ void QnxDeployQtLibrariesDialog::checkRemoteDirectoryExistance()
QTC_CHECK(m_state == Inactive); QTC_CHECK(m_state == Inactive);
m_state = CheckingRemoteDirectory; m_state = CheckingRemoteDirectory;
m_ui->deployLogWindow->appendPlainText(tr("Checking existence of \"%1\"") m_ui->deployLogWindow->appendPlainText(tr("Checking existence of \"%1\"")
.arg(fullRemoteDirectory())); .arg(fullRemoteDirectory()));
m_processRunner->run("test -d " + fullRemoteDirectory(), m_device->sshParameters());
const QByteArray cmd = "test -d " + fullRemoteDirectory().toLatin1();
m_processRunner->run(cmd, m_device->sshParameters());
} }
void QnxDeployQtLibrariesDialog::removeRemoteDirectory() void QnxDeployQtLibrariesDialog::removeRemoteDirectory()
@@ -305,11 +302,8 @@ void QnxDeployQtLibrariesDialog::removeRemoteDirectory()
QTC_CHECK(m_state == CheckingRemoteDirectory); QTC_CHECK(m_state == CheckingRemoteDirectory);
m_state = RemovingRemoteDirectory; m_state = RemovingRemoteDirectory;
m_ui->deployLogWindow->appendPlainText(tr("Removing \"%1\"").arg(fullRemoteDirectory())); m_ui->deployLogWindow->appendPlainText(tr("Removing \"%1\"").arg(fullRemoteDirectory()));
m_processRunner->run("rm -rf " + fullRemoteDirectory(), m_device->sshParameters());
const QByteArray cmd = "rm -rf " + fullRemoteDirectory().toLatin1();
m_processRunner->run(cmd, m_device->sshParameters());
} }
} // namespace Internal } // namespace Internal

View File

@@ -113,8 +113,7 @@ void QnxDeviceTester::handleGenericTestFinished(TestResult result)
m_state = VarRunTest; m_state = VarRunTest;
emit progressMessage(tr("Checking that files can be created in /var/run...")); emit progressMessage(tr("Checking that files can be created in /var/run..."));
m_processRunner->run(QStringLiteral("rm %1 > /dev/null 2>&1; echo ABC > %1 && rm %1") m_processRunner->run(QStringLiteral("rm %1 > /dev/null 2>&1; echo ABC > %1 && rm %1")
.arg("/var/run/qtc_xxxx.pid") .arg("/var/run/qtc_xxxx.pid"),
.toLatin1(),
m_deviceConfiguration->sshParameters()); m_deviceConfiguration->sshParameters());
} }
@@ -189,7 +188,7 @@ void QnxDeviceTester::testNextCommand()
QString command = m_commandsToTest[m_currentCommandIndex]; QString command = m_commandsToTest[m_currentCommandIndex];
emit progressMessage(tr("Checking for %1...").arg(command)); emit progressMessage(tr("Checking for %1...").arg(command));
m_processRunner->run("command -v " + command.toLatin1(), m_deviceConfiguration->sshParameters()); m_processRunner->run("command -v " + command, m_deviceConfiguration->sshParameters());
} }
void QnxDeviceTester::setFinished() void QnxDeviceTester::setFinished()

View File

@@ -238,8 +238,7 @@ void GenericDirectUploadService::queryFiles()
continue; continue;
} }
// We'd like to use --format=%Y, but it's not supported by busybox. // We'd like to use --format=%Y, but it's not supported by busybox.
const QByteArray statCmd = "stat -t " const QString statCmd = "stat -t " + Utils::QtcProcess::quoteArgUnix(file.remoteFilePath());
+ Utils::QtcProcess::quoteArgUnix(file.remoteFilePath()).toUtf8();
SshRemoteProcess * const statProc = connection()->createRemoteProcess(statCmd).release(); SshRemoteProcess * const statProc = connection()->createRemoteProcess(statCmd).release();
statProc->setParent(this); statProc->setParent(this);
connect(statProc, &SshRemoteProcess::done, this, connect(statProc, &SshRemoteProcess::done, this,
@@ -328,7 +327,7 @@ void GenericDirectUploadService::chmod()
const QString command = QLatin1String("chmod a+x ") const QString command = QLatin1String("chmod a+x ")
+ Utils::QtcProcess::quoteArgUnix(f.remoteFilePath()); + Utils::QtcProcess::quoteArgUnix(f.remoteFilePath());
SshRemoteProcess * const chmodProc SshRemoteProcess * const chmodProc
= connection()->createRemoteProcess(command.toUtf8()).release(); = connection()->createRemoteProcess(command).release();
chmodProc->setParent(this); chmodProc->setParent(this);
connect(chmodProc, &SshRemoteProcess::done, this, connect(chmodProc, &SshRemoteProcess::done, this,
[this, chmodProc, state = d->state](const QString &error) { [this, chmodProc, state = d->state](const QString &error) {

View File

@@ -122,7 +122,7 @@ void RemoteLinuxCheckForFreeDiskSpaceService::doDeploy()
this, &RemoteLinuxCheckForFreeDiskSpaceService::handleStdErr); this, &RemoteLinuxCheckForFreeDiskSpaceService::handleStdErr);
const QString command = QString::fromLatin1("df -k %1 |tail -n 1 |sed 's/ */ /g' " const QString command = QString::fromLatin1("df -k %1 |tail -n 1 |sed 's/ */ /g' "
"|cut -d ' ' -f 4").arg(d->pathToCheck); "|cut -d ' ' -f 4").arg(d->pathToCheck);
d->processRunner->run(command.toUtf8(), deviceConfiguration()->sshParameters()); d->processRunner->run(command, deviceConfiguration()->sshParameters());
} }
void RemoteLinuxCheckForFreeDiskSpaceService::stopDeployment() void RemoteLinuxCheckForFreeDiskSpaceService::stopDeployment()

View File

@@ -91,7 +91,7 @@ void RemoteLinuxCustomCommandDeployService::doDeploy()
emit progressMessage(tr("Starting remote command \"%1\"...").arg(d->commandLine)); emit progressMessage(tr("Starting remote command \"%1\"...").arg(d->commandLine));
d->state = Running; d->state = Running;
d->runner->run(d->commandLine.toUtf8(), deviceConfiguration()->sshParameters()); d->runner->run(d->commandLine, deviceConfiguration()->sshParameters());
} }
void RemoteLinuxCustomCommandDeployService::stopDeployment() void RemoteLinuxCustomCommandDeployService::stopDeployment()

View File

@@ -76,7 +76,7 @@ void AbstractRemoteLinuxPackageInstaller::installPackage(const IDevice::ConstPtr
QString cmdLine = installCommandLine(packageFilePath); QString cmdLine = installCommandLine(packageFilePath);
if (removePackageFile) if (removePackageFile)
cmdLine += QLatin1String(" && (rm ") + packageFilePath + QLatin1String(" || :)"); cmdLine += QLatin1String(" && (rm ") + packageFilePath + QLatin1String(" || :)");
d->installer->run(cmdLine.toUtf8(), deviceConfig->sshParameters()); d->installer->run(cmdLine, deviceConfig->sshParameters());
d->isRunning = true; d->isRunning = true;
} }
@@ -86,7 +86,7 @@ void AbstractRemoteLinuxPackageInstaller::cancelInstallation()
if (!d->killProcess) if (!d->killProcess)
d->killProcess = new SshRemoteProcessRunner(this); d->killProcess = new SshRemoteProcessRunner(this);
d->killProcess->run(cancelInstallationCommandLine().toUtf8(), d->deviceConfig->sshParameters()); d->killProcess->run(cancelInstallationCommandLine(), d->deviceConfig->sshParameters());
setFinished(); setFinished();
} }

View File

@@ -60,7 +60,7 @@ void RemoteLinuxSignalOperation::run(const QString &command)
this, &RemoteLinuxSignalOperation::runnerProcessFinished); this, &RemoteLinuxSignalOperation::runnerProcessFinished);
connect(m_runner, &QSsh::SshRemoteProcessRunner::connectionError, connect(m_runner, &QSsh::SshRemoteProcessRunner::connectionError,
this, &RemoteLinuxSignalOperation::runnerConnectionError); this, &RemoteLinuxSignalOperation::runnerConnectionError);
m_runner->run(command.toLatin1(), m_sshParameters); m_runner->run(command, m_sshParameters);
} }
void RemoteLinuxSignalOperation::finish() void RemoteLinuxSignalOperation::finish()

View File

@@ -101,7 +101,7 @@ void RsyncDeployService::createRemoteDirectories()
remoteDirs.sort(); remoteDirs.sort();
remoteDirs.removeDuplicates(); remoteDirs.removeDuplicates();
m_mkdir = connection()->createRemoteProcess("mkdir -p " + QtcProcess::Arguments m_mkdir = connection()->createRemoteProcess("mkdir -p " + QtcProcess::Arguments
::createUnixArgs(remoteDirs).toString().toUtf8()); ::createUnixArgs(remoteDirs).toString());
connect(m_mkdir.get(), &SshRemoteProcess::done, this, [this](const QString &error) { connect(m_mkdir.get(), &SshRemoteProcess::done, this, [this](const QString &error) {
QString userError; QString userError;
if (!error.isEmpty()) if (!error.isEmpty())

View File

@@ -67,9 +67,10 @@ void SshKeyDeployer::deployPublicKey(const SshConnectionParameters &sshParams,
this, &SshKeyDeployer::handleConnectionFailure); this, &SshKeyDeployer::handleConnectionFailure);
connect(&d->deployProcess, &SshRemoteProcessRunner::processClosed, connect(&d->deployProcess, &SshRemoteProcessRunner::processClosed,
this, &SshKeyDeployer::handleKeyUploadFinished); this, &SshKeyDeployer::handleKeyUploadFinished);
const QByteArray command = "test -d .ssh " const QString command = "test -d .ssh "
"|| mkdir .ssh && chmod 0700 .ssh && echo '" "|| mkdir .ssh && chmod 0700 .ssh && echo '"
+ reader.data() + "' >> .ssh/authorized_keys && chmod 0600 .ssh/authorized_keys"; + QString::fromLocal8Bit(reader.data())
+ "' >> .ssh/authorized_keys && chmod 0600 .ssh/authorized_keys";
d->deployProcess.run(command, sshParams); d->deployProcess.run(command, sshParams);
} }