[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.
This commit is contained in:
Adriaan de Groot 2017-11-28 10:27:24 -05:00
parent f5aec1ad8a
commit 9d31380980
2 changed files with 59 additions and 61 deletions

View file

@ -21,10 +21,18 @@
#include "DllMacro.h"
#include <QObject>
#include <QPair>
#include <QString>
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 );
int timeoutSec = 0 )
{
auto r = targetEnvCommand( args, workingPath, stdInput, timeoutSec );
output = r.second;
return r.first;
}
DLLEXPORT int targetEnvOutput( const QString& command,
/** @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.