2015-06-04 01:29:22 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
|
2018-03-22 17:29:40 +00:00
|
|
|
* Copyright (C) 2016-2018 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-06-04 01:29:22 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 3 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
|
|
|
|
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"
|
|
|
|
#include "core/copysourcedevice.h"
|
|
|
|
#include "core/copytargetdevice.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
#include "util/externalcommand.h"
|
|
|
|
#include "util/report.h"
|
|
|
|
|
2018-04-12 22:43:12 +01:00
|
|
|
#include <QCryptographicHash>
|
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>
|
2015-06-04 01:29:22 +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>
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2018-04-12 22:43:12 +01:00
|
|
|
#include <QtCrypto>
|
|
|
|
|
2017-11-07 22:55:28 +00:00
|
|
|
#include <KAuth>
|
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;
|
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
|
2017-08-26 18:40:28 +01:00
|
|
|
setup(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();
|
|
|
|
|
2017-08-26 18:40:28 +01:00
|
|
|
setup(processChannelMode);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 22:47:40 +01:00
|
|
|
ExternalCommand::~ExternalCommand()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-26 18:40:28 +01:00
|
|
|
void ExternalCommand::setup(const QProcess::ProcessChannelMode processChannelMode)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2018-04-12 22:43:12 +01:00
|
|
|
// d->arguments.insert(QStringLiteral("processChannelMode"), processChannelMode); // FIXME
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2017-11-07 22:55:28 +00:00
|
|
|
// 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)
|
|
|
|
{
|
2018-03-22 05:32:59 +00:00
|
|
|
Q_UNUSED(timeout)
|
2015-07-13 15:16:36 +01:00
|
|
|
|
|
|
|
if (report()) {
|
2016-07-17 23:41:00 +01:00
|
|
|
report()->setCommand(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/") });
|
|
|
|
|
2018-03-19 10:33:20 +00:00
|
|
|
if (!QDBusConnection::systemBus().isConnected()) {
|
2018-03-21 17:01:40 +00:00
|
|
|
qWarning() << "Could not connect to DBus system bus";
|
2018-03-22 16:41:49 +00:00
|
|
|
return false;
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:32:59 +00:00
|
|
|
QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"),
|
|
|
|
QStringLiteral("/Helper"),
|
|
|
|
QStringLiteral("org.kde.kpmcore.externalcommand"),
|
|
|
|
QDBusConnection::systemBus());
|
|
|
|
|
2018-03-21 20:11:09 +00:00
|
|
|
iface.setTimeout(10 * 24 * 3600 * 1000); // 10 days
|
2018-03-22 05:32:59 +00:00
|
|
|
|
2018-03-22 16:41:49 +00:00
|
|
|
bool rval = false;
|
2018-03-19 10:33:20 +00:00
|
|
|
if (iface.isValid()) {
|
2018-04-12 22:43:12 +01:00
|
|
|
QByteArray request;
|
|
|
|
request.setNum(++CoreBackendManager::self()->counter());
|
|
|
|
|
|
|
|
request.append(cmd.toUtf8());
|
|
|
|
for (const auto &argument : qAsConst(d->m_Args))
|
|
|
|
request.append(argument.toLocal8Bit());
|
|
|
|
request.append(d->m_Input);
|
|
|
|
QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512);
|
|
|
|
|
2018-03-22 05:32:59 +00:00
|
|
|
QDBusPendingCall pcall = iface.asyncCall(QStringLiteral("start"),
|
2018-04-12 22:43:12 +01:00
|
|
|
CoreBackendManager::self()->privateKey().signMessage(hash, QCA::EMSA3_Raw),
|
2018-03-22 05:32:59 +00:00
|
|
|
cmd,
|
|
|
|
args(),
|
2018-04-11 22:47:40 +01:00
|
|
|
d->m_Input,
|
2018-03-22 05:32:59 +00:00
|
|
|
QStringList());
|
|
|
|
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
2017-11-07 22:55:28 +00:00
|
|
|
|
2018-03-22 05:32:59 +00:00
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
|
|
|
|
loop.exit();
|
|
|
|
|
|
|
|
if (watcher->isError())
|
|
|
|
qWarning() << watcher->error();
|
|
|
|
else {
|
|
|
|
QDBusPendingReply<QVariantMap> reply = *watcher;
|
|
|
|
|
2018-04-11 22:47:40 +01:00
|
|
|
d->m_Output = reply.value()[QStringLiteral("output")].toByteArray();
|
2018-03-22 05:32:59 +00:00
|
|
|
setExitCode(reply.value()[QStringLiteral("exitCode")].toInt());
|
2018-03-22 17:29:40 +00:00
|
|
|
rval = true;
|
2018-03-22 05:32:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-22 17:52:59 +00:00
|
|
|
bool ExternalCommand::copyBlocks(CopySource& source, CopyTarget& target)
|
|
|
|
{
|
|
|
|
bool rval = true;
|
|
|
|
const qint64 blockSize = 10 * 1024 * 1024; // number of bytes per block to copy
|
|
|
|
|
|
|
|
if (!QDBusConnection::systemBus().isConnected()) {
|
|
|
|
qWarning() << "Could not connect to DBus system bus";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO KF6:Use new signal-slot syntax
|
|
|
|
connect(CoreBackendManager::self()->job(), SIGNAL(percent(KJob*, unsigned long)), this, SLOT(emitProgress(KJob*, unsigned long)));
|
|
|
|
connect(CoreBackendManager::self()->job(), &KAuth::ExecuteJob::newData, this, &ExternalCommand::emitReport);
|
|
|
|
|
|
|
|
QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), QStringLiteral("/Helper"), QStringLiteral("org.kde.kpmcore.externalcommand"), QDBusConnection::systemBus());
|
|
|
|
iface.setTimeout(10 * 24 * 3600 * 1000); // 10 days
|
|
|
|
if (iface.isValid()) {
|
|
|
|
// Use asynchronous DBus calls, so that we can process reports and progress
|
|
|
|
QDBusPendingCall pcall= iface.asyncCall(QStringLiteral("copyblocks"),
|
|
|
|
CoreBackendManager::self()->Uuid(),
|
|
|
|
source.path(), source.firstByte(), source.length(),
|
|
|
|
target.path(), target.firstByte(), blockSize);
|
|
|
|
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
|
|
|
|
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)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2016-05-09 00:41:44 +01:00
|
|
|
/** Waits for the external command to finish.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param timeout timeout to wait until the process finishes.
|
|
|
|
@return true on success
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool ExternalCommand::waitFor(int timeout)
|
|
|
|
{
|
2017-11-07 22:55:28 +00:00
|
|
|
// closeWriteChannel();
|
|
|
|
/*
|
2016-05-09 00:41:44 +01:00
|
|
|
if (!waitForFinished(timeout)) {
|
|
|
|
if (report())
|
2016-07-17 23:41:00 +01:00
|
|
|
report()->line() << xi18nc("@info:status", "(Command timeout while running)");
|
2016-05-09 00:41:44 +01:00
|
|
|
return false;
|
2017-11-07 22:55:28 +00:00
|
|
|
}*/
|
2016-05-09 00:41:44 +01:00
|
|
|
|
2017-11-07 22:55:28 +00:00
|
|
|
// onReadOutput();
|
2018-03-22 05:32:59 +00:00
|
|
|
Q_UNUSED(timeout)
|
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)
|
|
|
|
{
|
2017-11-07 22:55:28 +00:00
|
|
|
return start(timeout) && waitFor(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
|
|
|
}
|
|
|
|
|
2016-05-18 19:54:36 +01:00
|
|
|
void ExternalCommand::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2016-05-18 19:54:36 +01:00
|
|
|
Q_UNUSED(exitStatus)
|
2015-07-13 15:16:36 +01:00
|
|
|
setExitCode(exitCode);
|
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;
|
|
|
|
}
|