Files
qt-creator/src/plugins/remotelinux/linuxdeviceprocess.cpp
Jarek Kobus 8af62eb256 DeviceProcess: Minimize the usage of DeviceProcess
DeviceProcess doesn't provide any public API, so
replace all usages of it with QtcProcess.
Keep using DeviceProcess only for reimplementations.

Change-Id: I35a14251a81dd0dde426f56ca2e809b527cc863c
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
2022-02-28 13:10:42 +00:00

115 lines
3.9 KiB
C++

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "linuxdeviceprocess.h"
#include <projectexplorer/runcontrol.h>
#include <utils/environment.h>
using namespace ProjectExplorer;
using namespace Utils;
namespace RemoteLinux {
const QByteArray pidMarker = "__qtc";
LinuxDeviceProcess::LinuxDeviceProcess(const QSharedPointer<const ProjectExplorer::IDevice> &device,
QObject *parent)
: ProjectExplorer::SshDeviceProcess(device, parent)
{
connect(this, &QtcProcess::finished, this, [this]() {
m_processId = 0;
});
connect(this, &QtcProcess::started, this, [this]() {
m_pidParsed = false;
m_output.clear();
});
}
QByteArray LinuxDeviceProcess::readAllStandardOutput()
{
QByteArray output = SshDeviceProcess::readAllStandardOutput();
if (m_pidParsed || usesTerminal())
return output;
m_output.append(output);
static const QByteArray endMarker = pidMarker + '\n';
const int endMarkerOffset = m_output.indexOf(endMarker);
if (endMarkerOffset == -1)
return {};
const int startMarkerOffset = m_output.indexOf(pidMarker);
if (startMarkerOffset == endMarkerOffset) // Only theoretically possible.
return {};
const int pidStart = startMarkerOffset + pidMarker.length();
const QByteArray pidString = m_output.mid(pidStart, endMarkerOffset - pidStart);
m_pidParsed = true;
m_processId = pidString.toLongLong();
// We don't want to show output from e.g. /etc/profile.
const QByteArray actualOutput = m_output.mid(endMarkerOffset + endMarker.length());
m_output.clear();
return actualOutput;
}
qint64 LinuxDeviceProcess::processId() const
{
return m_processId < 0 ? 0 : m_processId;
}
QString LinuxDeviceProcess::fullCommandLine() const
{
CommandLine cmd;
const QStringList rcFilesToSource = {"/etc/profile", "$HOME/.profile"};
for (const QString &filePath : rcFilesToSource) {
cmd.addArgs({"test", "-f", filePath});
cmd.addArgs("&&", CommandLine::Raw);
cmd.addArgs({".", filePath});
cmd.addArgs(";", CommandLine::Raw);
}
if (!workingDirectory().isEmpty()) {
cmd.addArgs({"cd", workingDirectory().path()});
cmd.addArgs("&&", CommandLine::Raw);
}
if (!usesTerminal())
cmd.addArgs(QString("echo ") + pidMarker + "$$" + pidMarker + " && ", CommandLine::Raw);
const Environment &env = remoteEnvironment();
for (auto it = env.constBegin(); it != env.constEnd(); ++it)
cmd.addArgs(env.key(it) + "='" + env.expandedValueForKey(env.key(it)) + '\'', CommandLine::Raw);
if (!usesTerminal())
cmd.addArg("exec");
cmd.addCommandLineAsArgs(commandLine(), CommandLine::Raw);
return cmd.arguments();
}
} // namespace RemoteLinux