From 976b3bcf69b6d09d677c8cc16217bc7e4f5f9e87 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Tue, 13 Dec 2022 18:59:58 +0200 Subject: [PATCH] partitionmanager: simplify luks mounting soliduiserver uses the same method - instead of piping it sets the key to stdin and writes the password to it, bonus points for simplifying the internal command runner (ExternalCommand class) aswell Signed-off-by: Ivailo Monev --- partitionmanager/src/fs/luks.cpp | 15 ++- partitionmanager/src/util/externalcommand.cpp | 91 +++++-------------- partitionmanager/src/util/externalcommand.h | 22 +---- 3 files changed, 32 insertions(+), 96 deletions(-) diff --git a/partitionmanager/src/fs/luks.cpp b/partitionmanager/src/fs/luks.cpp index dc4f5cf5..7d21a38d 100644 --- a/partitionmanager/src/fs/luks.cpp +++ b/partitionmanager/src/fs/luks.cpp @@ -101,15 +101,14 @@ namespace FS if (dlg->exec() == KDialog::Accepted) { - std::vector commands; - commands.push_back("echo"); - commands.push_back("cryptsetup"); - std::vector args; - args.push_back(QStringList() << dlg->luksPassphrase().text()); - args.push_back(QStringList() << "luksOpen" << deviceNode << dlg->luksName().text()); - ExternalCommand cmd(commands, args); + QString name = dlg->luksName().text(); + QByteArray pass = dlg->luksPassphrase().text().toLocal8Bit(); delete dlg; - return cmd.run(-1) && cmd.exitCode() == 0; + ExternalCommand cmd("cryptsetup", QStringList() << "luksOpen"<< deviceNode << name << "--key-file=-"); + cmd.start(); + cmd.write(pass); + cmd.waitFor(-1); + return cmd.exitCode() == 0; } delete dlg; diff --git a/partitionmanager/src/util/externalcommand.cpp b/partitionmanager/src/util/externalcommand.cpp index 06d48ff5..d4b6ad37 100644 --- a/partitionmanager/src/util/externalcommand.cpp +++ b/partitionmanager/src/util/externalcommand.cpp @@ -36,8 +36,8 @@ ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args) : m_ExitCode(-1), m_Output() { - m_Command.push_back(cmd); - m_Args.push_back(args); + m_Command = cmd; + m_Args = args; setup(); } @@ -51,58 +51,22 @@ ExternalCommand::ExternalCommand(Report& report, const QString& cmd, const QStri m_ExitCode(-1), m_Output() { - m_Command.push_back(cmd); - m_Args.push_back(args); - setup(); -} - -/** Creates a new ExternalCommand instance without Report. - @param cmd the vector of the piped commands to run - @param args the vector of the arguments to pass to the commands -*/ -ExternalCommand::ExternalCommand(const std::vector cmd, const std::vector args) : - m_Report(NULL), - m_Command(cmd), - m_Args(args), - m_ExitCode(-1), - m_Output() -{ - setup(); -} - -/** Creates a new ExternalCommand instance with Report. - @param report the Report to write output to. - @param cmd the vector of the piped commands to run - @param args the vector of the arguments to pass to the commands - */ -ExternalCommand::ExternalCommand(Report& report, const std::vector cmd, const std::vector args) : - m_Report(report.newChild()), - m_Command(cmd), - m_Args(args), - m_ExitCode(-1), - m_Output() -{ + m_Command = cmd; + m_Args = args; setup(); } ExternalCommand::~ExternalCommand() { - delete[] processes; } void ExternalCommand::setup() { setEnvironment(QStringList() << "LC_ALL=C" << QString("PATH=") + qgetenv("PATH")); - setProcessChannelMode(MergedChannels); + setProcessChannelMode(QProcess::MergedChannels); - processes = new QProcess[command().size()]; - connect(&processes[command().size()-1], SIGNAL(readyReadStandardOutput()), SLOT(onReadOutput())); - connect(&processes[command().size()-1], SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(onFinished(int))); - - for (unsigned int i = 0; i < command().size() - 1; i++) - { - processes[i].setStandardOutputProcess(&processes[i+1]); - } + connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadOutput())); + connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onFinished(int))); } /** Starts the external commands. @@ -111,30 +75,20 @@ void ExternalCommand::setup() */ bool ExternalCommand::start(int timeout) { - for (unsigned int i = 0; i < command().size(); i++) - processes[i].start(command()[i], args()[i]); + QProcess::start(command(), args()); if (report()) { - QString s; - for (unsigned int i = 0; i < command().size(); i++) - { - s += command()[i] + " " + args()[i].join(" "); - if (i < command().size()-1) - s += " | "; - } + QString s = command() + " " + args().join(" "); report()->setCommand(i18nc("@info/plain", "Command: %1", s)); } - for (unsigned int i = 0; i < command().size(); i++) + if (!waitForStarted(timeout)) { - if (!processes[i].waitForStarted(timeout)) - { - if (report()) - report()->line() << i18nc("@info/plain", "(Command timeout while starting \"%1\")", command()[i] + " " + args()[i].join(" ")); + if (report()) + report()->line() << i18nc("@info/plain", "(Command timeout while starting \"%1\")", command() + " " + args().join(" ")); - return false; - } + return false; } return true; @@ -146,18 +100,15 @@ bool ExternalCommand::start(int timeout) */ bool ExternalCommand::waitFor(int timeout) { - for (unsigned int i = 0; i < command().size(); i++) - { - processes[i].closeWriteChannel(); + closeWriteChannel(); - if (!processes[i].waitForFinished(timeout)) - { - if (report()) - report()->line() << i18nc("@info/plain", "(Command timeout while running \"%1\")", command()[i] + " " + args()[i].join(" ")); - return false; - } - onReadOutput(); + if (!waitForFinished(timeout)) + { + if (report()) + report()->line() << i18nc("@info/plain", "(Command timeout while running \"%1\")", command() + " " + args().join(" ")); + return false; } + onReadOutput(); return true; } @@ -172,7 +123,7 @@ bool ExternalCommand::run(int timeout) void ExternalCommand::onReadOutput() { - const QString s = QString(processes[command().size()-1].readAllStandardOutput()); + const QString s = QString(readAllStandardOutput()); m_Output += s; diff --git a/partitionmanager/src/util/externalcommand.h b/partitionmanager/src/util/externalcommand.h index 73143c94..4119f57c 100644 --- a/partitionmanager/src/util/externalcommand.h +++ b/partitionmanager/src/util/externalcommand.h @@ -23,12 +23,9 @@ #include "util/libpartitionmanagerexport.h" -#include - #include #include #include -#include class Report; @@ -47,21 +44,9 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT ExternalCommand : public QProcess public: explicit ExternalCommand(const QString& cmd = QString(), const QStringList& args = QStringList()); explicit ExternalCommand(Report& report, const QString& cmd = QString(), const QStringList& args = QStringList()); - explicit ExternalCommand(const std::vector cmd, const std::vector args); - explicit ExternalCommand(Report& report, const std::vector cmd, const std::vector args); ~ExternalCommand(); public: - void setCommand(const std::vector cmd) { m_Command = cmd; } /**< @param cmd the command to run */ - const std::vector command() const { return m_Command; } /**< @return the command to run */ - - /** @param s the argument to add - @param i the command to which the argument is added - */ - void addArg(const QString& s, const int i = 0) { m_Args[i] << s; } - const std::vector args() const { return m_Args; } /**< @return the arguments */ - void setArgs(const std::vector args) { m_Args = args; } /**< @param args the new arguments */ - bool start(int timeout = 30000); bool waitFor(int timeout = 30000); bool run(int timeout = 30000); @@ -71,6 +56,8 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT ExternalCommand : public QProcess const QString& output() const { return m_Output; } /**< @return the command output */ Report* report() { return m_Report; } /**< @return pointer to the Report or NULL */ + QString command() { return m_Command; } /**< @return the command to run */ + QStringList args() { return m_Args; } /**< @return the arguments */ protected: void setExitCode(int i) { m_ExitCode = i; } @@ -81,10 +68,9 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT ExternalCommand : public QProcess void onReadOutput(); private: - QProcess *processes; Report *m_Report; - std::vector m_Command; - std::vector m_Args; + QString m_Command; + QStringList m_Args; int m_ExitCode; QString m_Output; };