2020-10-01 00:33:19 +01:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
|
|
|
|
SPDX-FileCopyrightText: 2013-2020 Andrius Štikonas <andrius@stikonas.eu>
|
|
|
|
SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
|
|
|
|
SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
|
|
|
|
SPDX-FileCopyrightText: 2018 Huzaifa Faruqui <huzaifafaruqui@gmail.com>
|
|
|
|
SPDX-FileCopyrightText: 2018 Caio Jordão Carvalho <caiojcarvalho@gmail.com>
|
|
|
|
SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>
|
|
|
|
SPDX-FileCopyrightText: 2019 Yuri Chornoivan <yurchor@ukr.net>
|
2020-10-09 20:41:41 +01:00
|
|
|
SPDX-FileCopyrightText: 2020 David Edmundson <kde@davidedmundson.co.uk>
|
2020-10-01 00:33:19 +01:00
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*/
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2019-12-09 16:09:53 +00:00
|
|
|
#include "util/externalcommand.h"
|
2017-11-07 22:55:28 +00:00
|
|
|
#include "backend/corebackendmanager.h"
|
2018-01-24 15:22:42 +00:00
|
|
|
#include "core/device.h"
|
|
|
|
#include "core/copysource.h"
|
|
|
|
#include "core/copytarget.h"
|
2018-10-28 17:22:12 +00:00
|
|
|
#include "core/copytargetbytearray.h"
|
2018-01-24 15:22:42 +00:00
|
|
|
#include "core/copysourcedevice.h"
|
|
|
|
#include "core/copytargetdevice.h"
|
2018-07-15 00:09:39 +01:00
|
|
|
#include "util/globallog.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
#include "util/report.h"
|
|
|
|
|
2018-08-05 21:14:43 +01:00
|
|
|
#include "externalcommandhelper_interface.h"
|
|
|
|
|
2018-04-12 22:43:12 +01:00
|
|
|
#include <QCryptographicHash>
|
2018-07-15 00:09:39 +01:00
|
|
|
#include <QDBusConnection>
|
2018-03-19 10:33:20 +00:00
|
|
|
#include <QDBusInterface>
|
|
|
|
#include <QDBusReply>
|
2017-11-07 22:55:28 +00:00
|
|
|
#include <QEventLoop>
|
2017-10-09 13:39:34 +01:00
|
|
|
#include <QtGlobal>
|
2017-11-07 22:55:28 +00:00
|
|
|
#include <QStandardPaths>
|
2019-06-12 17:41:11 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
2017-11-07 22:55:28 +00:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QThread>
|
2018-01-27 18:54:48 +00:00
|
|
|
#include <QVariant>
|
2018-03-31 16:30:53 +01:00
|
|
|
#include <KJob>
|
2015-06-04 01:29:22 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2018-04-11 22:47:40 +01:00
|
|
|
struct ExternalCommandPrivate
|
|
|
|
{
|
|
|
|
Report *m_Report;
|
|
|
|
QString m_Command;
|
|
|
|
QStringList m_Args;
|
|
|
|
int m_ExitCode;
|
|
|
|
QByteArray m_Output;
|
|
|
|
QByteArray m_Input;
|
2018-07-15 17:37:15 +01:00
|
|
|
QProcess::ProcessChannelMode processChannelMode;
|
2018-04-11 22:47:40 +01:00
|
|
|
};
|
|
|
|
|
2015-06-04 01:29:22 +01:00
|
|
|
/** Creates a new ExternalCommand instance without Report.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param cmd the command to run
|
|
|
|
@param args the arguments to pass to the command
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
2017-08-26 18:40:28 +01:00
|
|
|
ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) :
|
2018-04-11 22:47:40 +01:00
|
|
|
d(std::make_unique<ExternalCommandPrivate>())
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2018-04-11 22:47:40 +01:00
|
|
|
d->m_Report = nullptr;
|
|
|
|
d->m_Command = cmd;
|
|
|
|
d->m_Args = args;
|
|
|
|
d->m_ExitCode = -1;
|
|
|
|
d->m_Output = QByteArray();
|
2018-07-15 17:37:15 +01:00
|
|
|
d->processChannelMode = processChannelMode;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a new ExternalCommand instance with Report.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param report the Report to write output to.
|
|
|
|
@param cmd the command to run
|
|
|
|
@param args the arguments to pass to the command
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
2017-08-26 18:40:28 +01:00
|
|
|
ExternalCommand::ExternalCommand(Report& report, const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) :
|
2018-04-11 22:47:40 +01:00
|
|
|
d(std::make_unique<ExternalCommandPrivate>())
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2018-04-11 22:47:40 +01:00
|
|
|
d->m_Report = report.newChild();
|
|
|
|
d->m_Command = cmd;
|
|
|
|
d->m_Args = args;
|
|
|
|
d->m_ExitCode = -1;
|
|
|
|
d->m_Output = QByteArray();
|
|
|
|
|
2018-07-15 17:37:15 +01:00
|
|
|
d->processChannelMode = processChannelMode;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 22:47:40 +01:00
|
|
|
ExternalCommand::~ExternalCommand()
|
|
|
|
{
|
2019-05-18 19:50:10 +01:00
|
|
|
|
2018-04-11 22:47:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-18 19:50:10 +01:00
|
|
|
/*
|
|
|
|
void ExternalCommand::setup()
|
|
|
|
{
|
|
|
|
connect(this, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &ExternalCommand::onFinished);
|
|
|
|
connect(this, &ExternalCommand::readyReadStandardOutput, this, &ExternalCommand::onReadOutput);
|
|
|
|
}
|
|
|
|
*/
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2018-03-22 16:41:49 +00:00
|
|
|
/** Executes the external command.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param timeout timeout to wait for the process to start
|
|
|
|
@return true on success
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool ExternalCommand::start(int timeout)
|
|
|
|
{
|
2019-09-26 23:47:34 +01:00
|
|
|
Q_UNUSED(timeout)
|
|
|
|
|
2018-08-16 22:32:33 +01:00
|
|
|
if (command().isEmpty())
|
|
|
|
return false;
|
|
|
|
|
2018-11-12 00:22:24 +00:00
|
|
|
if (report())
|
2016-07-17 23:41:00 +01:00
|
|
|
report()->setCommand(xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))));
|
2018-11-12 00:22:24 +00:00
|
|
|
|
|
|
|
if ( qEnvironmentVariableIsSet( "KPMCORE_DEBUG" ))
|
|
|
|
qDebug() << xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" ")));
|
2015-07-13 15:16:36 +01:00
|
|
|
|
2017-11-07 22:55:28 +00:00
|
|
|
QString cmd = QStandardPaths::findExecutable(command());
|
|
|
|
if (cmd.isEmpty())
|
|
|
|
cmd = QStandardPaths::findExecutable(command(), { QStringLiteral("/sbin/"), QStringLiteral("/usr/sbin/"), QStringLiteral("/usr/local/sbin/") });
|
|
|
|
|
2020-09-11 20:27:24 +01:00
|
|
|
auto interface = helperInterface();
|
|
|
|
if (!interface)
|
|
|
|
return false;
|
2018-03-22 05:32:59 +00:00
|
|
|
|
2018-03-22 16:41:49 +00:00
|
|
|
bool rval = false;
|
2018-08-05 21:14:43 +01:00
|
|
|
|
2020-10-11 22:45:13 +01:00
|
|
|
QDBusPendingCall pcall = interface->RunCommand(cmd, args(), d->m_Input, d->processChannelMode);
|
2018-08-05 21:14:43 +01:00
|
|
|
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
|
|
|
|
loop.exit();
|
|
|
|
|
|
|
|
if (watcher->isError())
|
|
|
|
qWarning() << watcher->error();
|
|
|
|
else {
|
|
|
|
QDBusPendingReply<QVariantMap> reply = *watcher;
|
|
|
|
|
|
|
|
d->m_Output = reply.value()[QStringLiteral("output")].toByteArray();
|
|
|
|
setExitCode(reply.value()[QStringLiteral("exitCode")].toInt());
|
|
|
|
rval = reply.value()[QStringLiteral("success")].toBool();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop);
|
|
|
|
loop.exec();
|
2018-03-22 17:29:40 +00:00
|
|
|
|
2018-03-22 16:41:49 +00:00
|
|
|
return rval;
|
2017-11-07 22:55:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 18:43:56 +01:00
|
|
|
bool ExternalCommand::copyBlocks(const CopySource& source, CopyTarget& target)
|
2018-03-22 17:52:59 +00:00
|
|
|
{
|
|
|
|
bool rval = true;
|
|
|
|
const qint64 blockSize = 10 * 1024 * 1024; // number of bytes per block to copy
|
|
|
|
|
2020-09-11 20:27:24 +01:00
|
|
|
auto interface = helperInterface();
|
|
|
|
if (!interface)
|
|
|
|
return false;
|
2018-08-05 21:14:43 +01:00
|
|
|
|
2020-10-11 23:46:20 +01:00
|
|
|
connect(interface, &OrgKdeKpmcoreExternalcommandInterface::progress, this, &ExternalCommand::progress);
|
|
|
|
connect(interface, &OrgKdeKpmcoreExternalcommandInterface::report, this, &ExternalCommand::reportSignal);
|
|
|
|
|
2020-10-11 22:45:13 +01:00
|
|
|
QDBusPendingCall pcall = interface->CopyBlocks(source.path(), source.firstByte(), source.length(),
|
2019-05-18 19:50:10 +01:00
|
|
|
target.path(), target.firstByte(), blockSize);
|
2018-08-05 21:14:43 +01:00
|
|
|
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
|
|
|
|
loop.exit();
|
|
|
|
if (watcher->isError())
|
|
|
|
qWarning() << watcher->error();
|
|
|
|
else {
|
2018-10-28 17:22:12 +00:00
|
|
|
QDBusPendingReply<QVariantMap> reply = *watcher;
|
|
|
|
rval = reply.value()[QStringLiteral("success")].toBool();
|
|
|
|
|
|
|
|
CopyTargetByteArray *byteArrayTarget = dynamic_cast<CopyTargetByteArray*>(&target);
|
|
|
|
if (byteArrayTarget)
|
|
|
|
byteArrayTarget->m_Array = reply.value()[QStringLiteral("targetByteArray")].toByteArray();
|
2018-08-05 21:14:43 +01:00
|
|
|
}
|
|
|
|
setExitCode(!rval);
|
|
|
|
};
|
|
|
|
|
|
|
|
connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop);
|
|
|
|
loop.exec();
|
2018-03-22 17:52:59 +00:00
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2018-11-25 20:50:22 +00:00
|
|
|
bool ExternalCommand::writeData(Report& commandReport, const QByteArray& buffer, const QString& deviceNode, const quint64 firstByte)
|
|
|
|
{
|
|
|
|
d->m_Report = commandReport.newChild();
|
|
|
|
if (report())
|
|
|
|
report()->setCommand(xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))));
|
|
|
|
|
2020-09-11 20:27:24 +01:00
|
|
|
auto interface = helperInterface();
|
|
|
|
if (!interface)
|
2018-11-25 20:50:22 +00:00
|
|
|
return false;
|
2020-09-10 23:54:23 +01:00
|
|
|
|
2020-10-11 22:45:13 +01:00
|
|
|
QDBusPendingCall pcall = interface->WriteData(buffer, deviceNode, firstByte);
|
2020-09-11 20:27:24 +01:00
|
|
|
return waitForDbusReply(pcall);
|
2020-09-10 23:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 22:39:47 +01:00
|
|
|
bool ExternalCommand::createFile(const QByteArray& fileContents, const QString& filePath)
|
2020-09-10 23:54:23 +01:00
|
|
|
{
|
2020-09-11 20:27:24 +01:00
|
|
|
auto interface = helperInterface();
|
|
|
|
if (!interface)
|
|
|
|
return false;
|
2020-09-10 23:54:23 +01:00
|
|
|
|
2020-10-11 22:45:13 +01:00
|
|
|
QDBusPendingCall pcall = interface->CreateFile(filePath, fileContents);
|
2020-09-11 20:27:24 +01:00
|
|
|
return waitForDbusReply(pcall);
|
|
|
|
}
|
2020-09-10 23:54:23 +01:00
|
|
|
|
2020-09-11 20:27:24 +01:00
|
|
|
OrgKdeKpmcoreExternalcommandInterface* ExternalCommand::helperInterface()
|
|
|
|
{
|
2020-09-10 23:54:23 +01:00
|
|
|
if (!QDBusConnection::systemBus().isConnected()) {
|
|
|
|
qWarning() << QDBusConnection::systemBus().lastError().message();
|
2020-09-11 20:27:24 +01:00
|
|
|
return nullptr;
|
2020-09-10 23:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 02:23:24 +01:00
|
|
|
auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.helperinterface"),
|
2020-09-10 23:54:23 +01:00
|
|
|
QStringLiteral("/Helper"), QDBusConnection::systemBus(), this);
|
|
|
|
interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days
|
2020-09-11 20:27:24 +01:00
|
|
|
return interface;
|
|
|
|
}
|
2020-09-10 23:54:23 +01:00
|
|
|
|
2020-09-11 20:27:24 +01:00
|
|
|
bool ExternalCommand::waitForDbusReply(QDBusPendingCall &pcall)
|
|
|
|
{
|
|
|
|
bool rval = true;
|
2020-09-10 23:54:23 +01:00
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
|
2018-11-25 20:50:22 +00:00
|
|
|
loop.exit();
|
|
|
|
if (watcher->isError())
|
|
|
|
qWarning() << watcher->error();
|
|
|
|
else {
|
|
|
|
QDBusPendingReply<bool> reply = *watcher;
|
|
|
|
rval = reply.argumentAt<0>();
|
|
|
|
}
|
|
|
|
setExitCode(!rval);
|
|
|
|
};
|
|
|
|
|
|
|
|
connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop);
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2017-11-07 22:55:28 +00:00
|
|
|
bool ExternalCommand::write(const QByteArray& input)
|
|
|
|
{
|
2019-02-09 13:39:33 +00:00
|
|
|
if ( qEnvironmentVariableIsSet( "KPMCORE_DEBUG" ))
|
|
|
|
qDebug() << "Command input:" << QString::fromLocal8Bit(input);
|
2018-04-11 22:47:40 +01:00
|
|
|
d->m_Input = input;
|
2015-07-13 15:16:36 +01:00
|
|
|
return true;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Runs the command.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param timeout timeout to use for waiting when starting and when waiting for the process to finish
|
|
|
|
@return true on success
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool ExternalCommand::run(int timeout)
|
|
|
|
{
|
2018-07-21 21:19:31 +01:00
|
|
|
return start(timeout) /* && exitStatus() == 0*/;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalCommand::onReadOutput()
|
|
|
|
{
|
2017-11-07 22:55:28 +00:00
|
|
|
// const QByteArray s = readAllStandardOutput();
|
|
|
|
//
|
|
|
|
// if(m_Output.length() > 10*1024*1024) { // prevent memory overflow for badly corrupted file systems
|
|
|
|
// if (report())
|
|
|
|
// report()->line() << xi18nc("@info:status", "(Command is printing too much output)");
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// m_Output += s;
|
|
|
|
//
|
|
|
|
// if (report())
|
|
|
|
// *report() << QString::fromLocal8Bit(s);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 22:47:40 +01:00
|
|
|
void ExternalCommand::setCommand(const QString& cmd)
|
|
|
|
{
|
|
|
|
d->m_Command = cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString& ExternalCommand::command() const
|
|
|
|
{
|
|
|
|
return d->m_Command;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList& ExternalCommand::args() const
|
|
|
|
{
|
|
|
|
return d->m_Args;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalCommand::addArg(const QString& s)
|
|
|
|
{
|
|
|
|
d->m_Args << s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalCommand::setArgs(const QStringList& args)
|
|
|
|
{
|
|
|
|
d->m_Args = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ExternalCommand::exitCode() const
|
|
|
|
{
|
|
|
|
return d->m_ExitCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString ExternalCommand::output() const
|
|
|
|
{
|
|
|
|
return QString::fromLocal8Bit(d->m_Output);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray& ExternalCommand::rawOutput() const
|
|
|
|
{
|
|
|
|
return d->m_Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
Report* ExternalCommand::report()
|
|
|
|
{
|
|
|
|
return d->m_Report;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalCommand::setExitCode(int i)
|
|
|
|
{
|
|
|
|
d->m_ExitCode = i;
|
|
|
|
}
|