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 <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-12-13 18:59:58 +02:00
parent d3ea9485ee
commit 976b3bcf69
3 changed files with 32 additions and 96 deletions

View file

@ -101,15 +101,14 @@ namespace FS
if (dlg->exec() == KDialog::Accepted)
{
std::vector<QString> commands;
commands.push_back("echo");
commands.push_back("cryptsetup");
std::vector<QStringList> 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;

View file

@ -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<QString> cmd, const std::vector<QStringList> 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<QString> cmd, const std::vector<QStringList> 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;

View file

@ -23,12 +23,9 @@
#include "util/libpartitionmanagerexport.h"
#include <vector>
#include <QProcess>
#include <QStringList>
#include <QString>
#include <qglobal.h>
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<QString> cmd, const std::vector<QStringList> args);
explicit ExternalCommand(Report& report, const std::vector<QString> cmd, const std::vector<QStringList> args);
~ExternalCommand();
public:
void setCommand(const std::vector<QString> cmd) { m_Command = cmd; } /**< @param cmd the command to run */
const std::vector<QString> 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<QStringList> args() const { return m_Args; } /**< @return the arguments */
void setArgs(const std::vector<QStringList> 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<QString> m_Command;
std::vector<QStringList> m_Args;
QString m_Command;
QStringList m_Args;
int m_ExitCode;
QString m_Output;
};