From 9d3138098018a7606494cf2ab49f9cef0b0bea9e Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 28 Nov 2017 10:27:24 -0500 Subject: [PATCH] [libcalamares] Refactor target-environment calls - Add a more general targetEnvCommand() that returns both error code and process output. - Change existing targetEnvCall() and targetEnvOutput() to use general form while discarding some data. --- .../utils/CalamaresUtilsSystem.cpp | 59 +++-------------- src/libcalamares/utils/CalamaresUtilsSystem.h | 63 +++++++++++++++---- 2 files changed, 60 insertions(+), 62 deletions(-) diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.cpp b/src/libcalamares/utils/CalamaresUtilsSystem.cpp index 4103140f9..ca981459c 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.cpp +++ b/src/libcalamares/utils/CalamaresUtilsSystem.cpp @@ -92,42 +92,14 @@ System::mount( const QString& devicePath, return QProcess::execute( program, args ); } -int -System::targetEnvCall( const QStringList& args, - const QString& workingPath, - const QString& stdInput, - int timeoutSec ) -{ - QString discard; - return targetEnvOutput( args, - discard, - workingPath, - stdInput, - timeoutSec ); -} - - -int -System::targetEnvCall( const QString& command, - const QString& workingPath, - const QString& stdInput, - int timeoutSec ) -{ - return targetEnvCall( QStringList{ command }, - workingPath, - stdInput, - timeoutSec ); -} - - -int -System::targetEnvOutput( const QStringList& args, - QString& output, - const QString& workingPath, - const QString& stdInput, - int timeoutSec ) +ProcessResult +System::targetEnvCommand( + const QStringList& args, + const QString& workingPath, + const QString& stdInput, + int timeoutSec ) { - output.clear(); + QString output; if ( !Calamares::JobQueue::instance() ) return -3; @@ -212,22 +184,7 @@ System::targetEnvOutput( const QStringList& args, cLog() << "Target cmd:" << args; cLog().noquote() << "Target output:\n" << output; } - return r; -} - - -int -System::targetEnvOutput( const QString& command, - QString& output, - const QString& workingPath, - const QString& stdInput, - int timeoutSec ) -{ - return targetEnvOutput( QStringList{ command }, - output, - workingPath, - stdInput, - timeoutSec ); + return ProcessResult(r, output); } diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.h b/src/libcalamares/utils/CalamaresUtilsSystem.h index 34ab7ecbd..a1f71fef3 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.h +++ b/src/libcalamares/utils/CalamaresUtilsSystem.h @@ -21,10 +21,18 @@ #include "DllMacro.h" #include +#include #include namespace CalamaresUtils { +class ProcessResult : public QPair< int, QString > +{ +public: + /** @brief Implicit one-argument constructor has no output, only a return code */ + ProcessResult( int r ) : QPair< int, QString >( r, QString() ) {} + ProcessResult( int r, QString s ) : QPair< int, QString >( r, s ) {} +} ; /** * @brief The System class is a singleton with utility functions that perform @@ -61,42 +69,75 @@ public: const QString& filesystemName = QString(), const QString& options = QString() ); + /** * Runs the specified command in the chroot of the target system. - * @param args the call with arguments, as a string list. + * @param args the command with arguments, as a string list. * @param workingPath the current working directory for the QProcess * call (optional). * @param stdInput the input string to send to the running process as * standard input (optional). * @param timeoutSec the timeout after which the process will be * killed (optional, default is 0 i.e. no timeout). - * @returns the program's exit code, or: + * + * @returns the program's exit code and its output (if any). Special + * exit codes (which will never have any output) are: * -1 = QProcess crash * -2 = QProcess cannot start * -3 = bad arguments * -4 = QProcess timeout */ - DLLEXPORT int targetEnvCall( const QStringList& args, + DLLEXPORT ProcessResult targetEnvCommand( + const QStringList &args, + const QString& workingPath = QString(), + const QString& stdInput = QString(), + int timeoutSec = 0 ); + + /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code */ + inline int targetEnvCall( const QStringList& args, const QString& workingPath = QString(), const QString& stdInput = QString(), - int timeoutSec = 0 ); + int timeoutSec = 0 ) + { + return targetEnvCommand( args, workingPath, stdInput, timeoutSec ).first; + } - DLLEXPORT int targetEnvCall( const QString& command, + /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code */ + inline int targetEnvCall( const QString& command, const QString& workingPath = QString(), const QString& stdInput = QString(), - int timeoutSec = 0 ); + int timeoutSec = 0 ) + { + return targetEnvCall( QStringList{ command }, workingPath, stdInput, timeoutSec ); + } - DLLEXPORT int targetEnvOutput( const QStringList& args, + /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code + * + * Places the called program's output in the @p output string. + */ + int targetEnvOutput( const QStringList& args, QString& output, const QString& workingPath = QString(), const QString& stdInput = QString(), - int timeoutSec = 0 ); - - DLLEXPORT int targetEnvOutput( const QString& command, + int timeoutSec = 0 ) + { + auto r = targetEnvCommand( args, workingPath, stdInput, timeoutSec ); + output = r.second; + return r.first; + } + + /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code + * + * Places the called program's output in the @p output string. + */ + inline int targetEnvOutput( const QString& command, QString& output, const QString& workingPath = QString(), const QString& stdInput = QString(), - int timeoutSec = 0 ); + int timeoutSec = 0 ) + { + return targetEnvOutput( QStringList{ command }, output, workingPath, stdInput, timeoutSec ); + } /** * @brief getTotalMemoryB returns the total main memory, in bytes.