[libcalamares] Replace runCommand internals by Runner

This commit is contained in:
Adriaan de Groot 2021-11-02 22:16:10 +01:00
parent 89824a9e0d
commit 870009e815

View File

@ -13,7 +13,7 @@
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include "JobQueue.h" #include "JobQueue.h"
#include "Settings.h" #include "Runner.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include <QCoreApplication> #include <QCoreApplication>
@ -75,114 +75,9 @@ System::runCommand( System::RunLocation location,
const QString& stdInput, const QString& stdInput,
std::chrono::seconds timeoutSec ) std::chrono::seconds timeoutSec )
{ {
if ( args.isEmpty() ) Calamares::Utils::Runner r( args );
{ r.setLocation( location ).setInput( stdInput ).setTimeout( timeoutSec ).setWorkingDirectory( workingPath );
cWarning() << "Cannot run an empty program list"; return r.run();
return ProcessResult::Code::FailedToStart;
}
Calamares::GlobalStorage* gs
= Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr;
if ( ( location == System::RunLocation::RunInTarget ) && ( !gs || !gs->contains( "rootMountPoint" ) ) )
{
cWarning() << "No rootMountPoint in global storage, while RunInTarget is specified";
return ProcessResult::Code::NoWorkingDirectory;
}
QString program;
QStringList arguments( args );
if ( location == System::RunLocation::RunInTarget )
{
QString destDir = gs->value( "rootMountPoint" ).toString();
if ( !QDir( destDir ).exists() )
{
cWarning() << "rootMountPoint points to a dir which does not exist";
return ProcessResult::Code::NoWorkingDirectory;
}
program = "chroot";
arguments.prepend( destDir );
}
else
{
program = "env";
}
QProcess process;
process.setProgram( program );
process.setArguments( arguments );
process.setProcessChannelMode( QProcess::MergedChannels );
if ( !workingPath.isEmpty() )
{
if ( QDir( workingPath ).exists() )
{
process.setWorkingDirectory( QDir( workingPath ).absolutePath() );
}
else
{
cWarning() << "Invalid working directory:" << workingPath;
return ProcessResult::Code::NoWorkingDirectory;
}
}
cDebug() << Logger::SubEntry << "Running" << program << Logger::Redacted( arguments );
process.start();
if ( !process.waitForStarted() )
{
cWarning() << "Process" << args.first() << "failed to start" << process.error();
return ProcessResult::Code::FailedToStart;
}
if ( !stdInput.isEmpty() )
{
process.write( stdInput.toLocal8Bit() );
}
process.closeWriteChannel();
if ( !process.waitForFinished( timeoutSec > std::chrono::seconds::zero()
? ( static_cast< int >( std::chrono::milliseconds( timeoutSec ).count() ) )
: -1 ) )
{
cWarning() << "Process" << args.first() << "timed out after" << timeoutSec.count() << "s. Output so far:\n"
<< Logger::NoQuote << process.readAllStandardOutput();
return ProcessResult::Code::TimedOut;
}
QString output = QString::fromLocal8Bit( process.readAllStandardOutput() ).trimmed();
if ( process.exitStatus() == QProcess::CrashExit )
{
cWarning() << "Process" << args.first() << "crashed. Output so far:\n" << Logger::NoQuote << output;
return ProcessResult::Code::Crashed;
}
auto r = process.exitCode();
bool showDebug = ( !Calamares::Settings::instance() ) || ( Calamares::Settings::instance()->debugMode() );
if ( r == 0 )
{
if ( showDebug && !output.isEmpty() )
{
cDebug() << Logger::SubEntry << "Finished. Exit code:" << r << "output:\n" << Logger::NoQuote << output;
}
}
else // if ( r != 0 )
{
if ( !output.isEmpty() )
{
cDebug() << Logger::SubEntry << "Target cmd:" << Logger::Redacted( args ) << "Exit code:" << r
<< "output:\n"
<< Logger::NoQuote << output;
}
else
{
cDebug() << Logger::SubEntry << "Target cmd:" << Logger::Redacted( args ) << "Exit code:" << r
<< "(no output)";
}
}
return ProcessResult( r, output );
} }
/// @brief Cheap check if a path is absolute. /// @brief Cheap check if a path is absolute.