[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

@ -92,42 +92,14 @@ System::mount( const QString& devicePath,
return QProcess::execute( program, args ); return QProcess::execute( program, args );
} }
int ProcessResult
System::targetEnvCall( const QStringList& args, System::targetEnvCommand(
const QStringList& args,
const QString& workingPath, const QString& workingPath,
const QString& stdInput, const QString& stdInput,
int timeoutSec ) int timeoutSec )
{ {
QString discard; QString output;
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 )
{
output.clear();
if ( !Calamares::JobQueue::instance() ) if ( !Calamares::JobQueue::instance() )
return -3; return -3;
@ -212,22 +184,7 @@ System::targetEnvOutput( const QStringList& args,
cLog() << "Target cmd:" << args; cLog() << "Target cmd:" << args;
cLog().noquote() << "Target output:\n" << output; cLog().noquote() << "Target output:\n" << output;
} }
return r; return ProcessResult(r, output);
}
int
System::targetEnvOutput( const QString& command,
QString& output,
const QString& workingPath,
const QString& stdInput,
int timeoutSec )
{
return targetEnvOutput( QStringList{ command },
output,
workingPath,
stdInput,
timeoutSec );
} }

View File

@ -21,10 +21,18 @@
#include "DllMacro.h" #include "DllMacro.h"
#include <QObject> #include <QObject>
#include <QPair>
#include <QString> #include <QString>
namespace CalamaresUtils 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 * @brief The System class is a singleton with utility functions that perform
@ -61,42 +69,75 @@ public:
const QString& filesystemName = QString(), const QString& filesystemName = QString(),
const QString& options = QString() ); const QString& options = QString() );
/** /**
* Runs the specified command in the chroot of the target system. * 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 * @param workingPath the current working directory for the QProcess
* call (optional). * call (optional).
* @param stdInput the input string to send to the running process as * @param stdInput the input string to send to the running process as
* standard input (optional). * standard input (optional).
* @param timeoutSec the timeout after which the process will be * @param timeoutSec the timeout after which the process will be
* killed (optional, default is 0 i.e. no timeout). * 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 * -1 = QProcess crash
* -2 = QProcess cannot start * -2 = QProcess cannot start
* -3 = bad arguments * -3 = bad arguments
* -4 = QProcess timeout * -4 = QProcess timeout
*/ */
DLLEXPORT int targetEnvCall( const QStringList& args, DLLEXPORT ProcessResult targetEnvCommand(
const QStringList &args,
const QString& workingPath = QString(), const QString& workingPath = QString(),
const QString& stdInput = QString(), const QString& stdInput = QString(),
int timeoutSec = 0 ); int timeoutSec = 0 );
DLLEXPORT int targetEnvCall( const QString& command, /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code */
inline int targetEnvCall( const QStringList& args,
const QString& workingPath = QString(), const QString& workingPath = QString(),
const QString& stdInput = QString(), const QString& stdInput = QString(),
int timeoutSec = 0 ); int timeoutSec = 0 )
{
return targetEnvCommand( args, workingPath, stdInput, timeoutSec ).first;
}
DLLEXPORT int targetEnvOutput( const QStringList& args, /** @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 )
{
return targetEnvCall( QStringList{ command }, workingPath, stdInput, timeoutSec );
}
/** @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, QString& output,
const QString& workingPath = QString(), const QString& workingPath = QString(),
const QString& stdInput = 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, QString& output,
const QString& workingPath = QString(), const QString& workingPath = QString(),
const QString& stdInput = 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. * @brief getTotalMemoryB returns the total main memory, in bytes.