File indexing completed on 2024-04-28 05:45:58

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2013-2020 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
0005     SPDX-FileCopyrightText: 2018 Huzaifa Faruqui <huzaifafaruqui@gmail.com>
0006     SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org>
0007     SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>
0008 
0009     SPDX-License-Identifier: GPL-3.0-or-later
0010 */
0011 
0012 #ifndef KPMCORE_EXTERNALCOMMAND_H
0013 #define KPMCORE_EXTERNALCOMMAND_H
0014 
0015 #include "util/libpartitionmanagerexport.h"
0016 
0017 #include <QDebug>
0018 #include <QProcess>
0019 #include <QString>
0020 #include <QStringList>
0021 #include <QtGlobal>
0022 #include <QThread>
0023 #include <QVariant>
0024 
0025 #include <memory>
0026 
0027 class KJob;
0028 class Report;
0029 class CopySource;
0030 class CopySourceDevice;
0031 class CopyTarget;
0032 class QDBusInterface;
0033 class QDBusPendingCall;
0034 class OrgKdeKpmcoreExternalcommandInterface;
0035 
0036 struct ExternalCommandPrivate;
0037 
0038 /** An external command.
0039 
0040     Runs an external command as a child process.
0041 
0042     @author Volker Lanz <vl@fidra.de>
0043     @author Andrius Štikonas <andrius@stikonas.eu>
0044 */
0045 class LIBKPMCORE_EXPORT ExternalCommand : public QObject
0046 {
0047     Q_OBJECT
0048     Q_DISABLE_COPY(ExternalCommand)
0049 
0050 public:
0051     explicit ExternalCommand(const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = QProcess::MergedChannels);
0052     explicit ExternalCommand(Report& report, const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = QProcess::MergedChannels);
0053 
0054     ~ExternalCommand() override;
0055 
0056 public:
0057     bool copyBlocks(const CopySource& source, CopyTarget& target);
0058     QByteArray readData(const CopySourceDevice& source);
0059     bool writeData(Report& commandReport, const QByteArray& buffer, const QString& deviceNode, const quint64 firstByte); // same as copyBlocks but from QByteArray
0060     bool writeFstab(const QByteArray& fileContents);
0061 
0062     /**< @param cmd the command to run */
0063     void setCommand(const QString& cmd);
0064      /**< @return the command to run */
0065     const QString& command() const;
0066 
0067     /**< @return the arguments */
0068     const QStringList& args() const;
0069 
0070     /**< @param s the argument to add */
0071     void addArg(const QString& s);
0072     /**< @param args the new arguments */
0073     void setArgs(const QStringList& args);
0074 
0075     bool write(const QByteArray& input); /**< @param input the input for the program */
0076 
0077     bool start(int timeout = 30000);
0078     bool run(int timeout = 30000);
0079 
0080     /**< @return the exit code */
0081     int exitCode() const;
0082 
0083     /**< @return the command output */
0084     const QString output() const;
0085     /**< @return the command output */
0086     const QByteArray& rawOutput() const;
0087 
0088     /**< @return pointer to the Report or nullptr */
0089     Report* report();
0090 
0091 Q_SIGNALS:
0092     void progress(int);
0093     void reportSignal(const QString&);
0094 
0095 private:
0096     void setExitCode(int i);
0097     void onReadOutput();
0098     bool waitForDbusReply(QDBusPendingCall &pcall);
0099     OrgKdeKpmcoreExternalcommandInterface* helperInterface();
0100 
0101 private:
0102     std::unique_ptr<ExternalCommandPrivate> d;
0103 
0104 };
0105 
0106 #endif