[libcalamares] Introduce enum class for special process exit values

- Replace magic numbers like -3 with named enum values
   (NoWorkingDirectory, for -3).
 - Downside is big-ugly static_casts, but that's what you get
   for having an int as return value for processes.
This commit is contained in:
Adriaan de Groot 2019-06-07 12:10:22 +02:00
parent 61b78d8895
commit 92d03c2cf7
2 changed files with 29 additions and 21 deletions

View File

@ -114,14 +114,14 @@ System::mount( const QString& devicePath,
const QString& options )
{
if ( devicePath.isEmpty() || mountPoint.isEmpty() )
return -3;
return static_cast<int>(ProcessResult::Code::NoWorkingDirectory);
QDir mountPointDir( mountPoint );
if ( !mountPointDir.exists() )
{
bool ok = mountPointDir.mkpath( mountPoint );
if ( !ok )
return -3;
return static_cast<int>(ProcessResult::Code::NoWorkingDirectory);
}
QString program( "mount" );
@ -147,14 +147,14 @@ System::runCommand(
QString output;
if ( !Calamares::JobQueue::instance() )
return -3;
return ProcessResult::Code::NoWorkingDirectory;
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
if ( ( location == System::RunLocation::RunInTarget ) &&
( !gs || !gs->contains( "rootMountPoint" ) ) )
{
cWarning() << "No rootMountPoint in global storage";
return -3;
return ProcessResult::Code::NoWorkingDirectory;
}
QProcess process;
@ -167,7 +167,7 @@ System::runCommand(
if ( !QDir( destDir ).exists() )
{
cWarning() << "rootMountPoint points to a dir which does not exist";
return -3;
return ProcessResult::Code::NoWorkingDirectory;
}
program = "chroot";
@ -190,7 +190,7 @@ System::runCommand(
process.setWorkingDirectory( QDir( workingPath ).absolutePath() );
else
cWarning() << "Invalid working directory:" << workingPath;
return -3;
return ProcessResult::Code::NoWorkingDirectory;
}
cDebug() << "Running" << program << RedactedList( arguments );
@ -198,7 +198,7 @@ System::runCommand(
if ( !process.waitForStarted() )
{
cWarning() << "Process failed to start" << process.error();
return -2;
return ProcessResult::Code::FailedToStart;
}
if ( !stdInput.isEmpty() )
@ -211,7 +211,7 @@ System::runCommand(
{
cWarning().noquote().nospace() << "Timed out. Output so far:\n" <<
process.readAllStandardOutput();
return -4;
return ProcessResult::Code::TimedOut;
}
output.append( QString::fromLocal8Bit( process.readAllStandardOutput() ).trimmed() );
@ -219,7 +219,7 @@ System::runCommand(
if ( process.exitStatus() == QProcess::CrashExit )
{
cWarning().noquote().nospace() << "Process crashed. Output so far:\n" << output;
return -1;
return ProcessResult::Code::Crashed;
}
auto r = process.exitCode();
@ -306,22 +306,22 @@ ProcessResult::explainProcess( int ec, const QString& command, const QString& ou
? QCoreApplication::translate( "ProcessResult", "\nThere was no output from the command.")
: (QCoreApplication::translate( "ProcessResult", "\nOutput:\n") + output);
if ( ec == -1 ) //Crash!
if ( ec == static_cast<int>(ProcessResult::Code::Crashed) ) //Crash!
return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command crashed." ),
QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> crashed." )
.arg( command )
+ outputMessage );
if ( ec == -2 )
if ( ec == static_cast<int>(ProcessResult::Code::FailedToStart) )
return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command failed to start." ),
QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> failed to start." )
.arg( command ) );
if ( ec == -3 )
if ( ec == static_cast<int>(ProcessResult::Code::NoWorkingDirectory) )
return JobResult::error( QCoreApplication::translate( "ProcessResult", "Internal error when starting command." ),
QCoreApplication::translate( "ProcessResult", "Bad parameters for process job call." ) );
if ( ec == -4 )
if ( ec == static_cast<int>(ProcessResult::Code::TimedOut) )
return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command failed to finish." ),
QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> failed to finish in %2 seconds." )
.arg( command )

View File

@ -32,8 +32,16 @@ namespace CalamaresUtils
class ProcessResult : public QPair< int, QString >
{
public:
enum class Code : int
{
Crashed = -1, // Must match special return values from QProcess
FailedToStart = -2, // Must match special return values from QProcess
NoWorkingDirectory = -3,
TimedOut = -4
} ;
/** @brief Implicit one-argument constructor has no output, only a return code */
ProcessResult( int r ) : QPair< int, QString >( r, QString() ) {}
ProcessResult( Code r ) : QPair< int, QString >( static_cast<int>(r), QString() ) {}
ProcessResult( int r, QString s ) : QPair< int, QString >( r, s ) {}
int getExitCode() const { return first; }
@ -93,9 +101,9 @@ public:
* @param filesystemName the name of the filesystem (optional).
* @param options any additional options as passed to mount -o (optional).
* @returns the program's exit code, or:
* -1 = QProcess crash
* -2 = QProcess cannot start
* -3 = bad arguments
* Crashed = QProcess crash
* FailedToStart = QProcess cannot start
* NoWorkingDirectory = bad arguments
*/
DLLEXPORT int mount( const QString& devicePath,
const QString& mountPoint,
@ -120,10 +128,10 @@ public:
*
* @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
* Crashed = QProcess crash
* FailedToStart = QProcess cannot start
* NoWorkingDirectory = bad arguments
* TimedOut = QProcess timeout
*/
static DLLEXPORT ProcessResult runCommand(
RunLocation location,