@ -19,10 +19,10 @@
# include "CalamaresUtilsSystem.h"
# include "utils/Logger.h"
# include "GlobalStorage.h"
# include "JobQueue.h"
# include "Settings.h"
# include "utils/Logger.h"
# include <QCoreApplication>
# include <QDir>
@ -34,8 +34,8 @@
# endif
# ifdef Q_OS_FREEBSD
# include <sys/types.h>
# include <sys/sysctl.h>
# include <sys/types.h>
# endif
/** @brief When logging commands, don't log everything.
@ -48,12 +48,12 @@
struct RedactedList
{
RedactedList ( const QStringList & l )
: list ( l )
: list ( l )
{
}
const QStringList & list ;
} ;
} ;
QDebug &
operator < < ( QDebug & s , const RedactedList & l )
@ -63,12 +63,18 @@ operator<<( QDebug& s, const RedactedList& l )
{
for ( const auto & item : l . list )
if ( item . startsWith ( " $6$ " ) )
{
s < < " <password> " ;
}
else
{
s < < item ;
}
}
else
{
s < < l . list ;
}
return s ;
}
@ -92,8 +98,7 @@ System::System( bool doChroot, QObject* parent )
}
System : : ~ System ( )
{ }
System : : ~ System ( ) { }
System *
@ -111,18 +116,22 @@ System::instance()
int
System : : mount ( const QString & devicePath ,
const QString & mountPoint ,
const QString & filesystemName ,
const QString & options )
const QString & mountPoint ,
const QString & filesystemName ,
const QString & options )
{
if ( devicePath . isEmpty ( ) | | mountPoint . isEmpty ( ) )
{
if ( devicePath . isEmpty ( ) )
{
cWarning ( ) < < " Can't mount an empty device. " ;
}
if ( mountPoint . isEmpty ( ) )
{
cWarning ( ) < < " Can't mount on an empty mountpoint. " ;
}
return static_cast < int > ( ProcessResult : : Code : : NoWorkingDirectory ) ;
return static_cast < int > ( ProcessResult : : Code : : NoWorkingDirectory ) ;
}
QDir mountPointDir ( mountPoint ) ;
@ -132,7 +141,7 @@ System::mount( const QString& devicePath,
if ( ! ok )
{
cWarning ( ) < < " Could not create mountpoint " < < mountPoint ;
return static_cast < int > ( ProcessResult : : Code : : NoWorkingDirectory ) ;
return static_cast < int > ( ProcessResult : : Code : : NoWorkingDirectory ) ;
}
}
@ -140,28 +149,31 @@ System::mount( const QString& devicePath,
QStringList args = { devicePath , mountPoint } ;
if ( ! filesystemName . isEmpty ( ) )
{
args < < " -t " < < filesystemName ;
}
if ( ! options . isEmpty ( ) )
{
args < < " -o " < < options ;
}
return QProcess : : execute ( program , args ) ;
}
ProcessResult
System : : runCommand (
System : : RunLocation location ,
const QStringList & args ,
const QString & workingPath ,
const QString & stdInput ,
std : : chrono : : seconds timeoutSec )
System : : runCommand ( System : : RunLocation location ,
const QStringList & args ,
const QString & workingPath ,
const QString & stdInput ,
std : : chrono : : seconds timeoutSec )
{
QString output ;
Calamares : : GlobalStorage * gs = Calamares : : JobQueue : : instance ( ) ? Calamares : : JobQueue : : instance ( ) - > globalStorage ( ) : nullptr ;
Calamares : : GlobalStorage * gs
= Calamares : : JobQueue : : instance ( ) ? Calamares : : JobQueue : : instance ( ) - > globalStorage ( ) : nullptr ;
if ( ( location = = System : : RunLocation : : RunInTarget ) & &
( ! gs | | ! gs - > contains ( " rootMountPoint " ) ) )
if ( ( location = = System : : RunLocation : : RunInTarget ) & & ( ! gs | | ! gs - > contains ( " rootMountPoint " ) ) )
{
cWarning ( ) < < " No rootMountPoint in global storage " ;
return ProcessResult : : Code : : NoWorkingDirectory ;
@ -197,7 +209,9 @@ System::runCommand(
if ( ! workingPath . isEmpty ( ) )
{
if ( QDir ( workingPath ) . exists ( ) )
{
process . setWorkingDirectory ( QDir ( workingPath ) . absolutePath ( ) ) ;
}
else
{
cWarning ( ) < < " Invalid working directory: " < < workingPath ;
@ -219,10 +233,10 @@ System::runCommand(
}
process . closeWriteChannel ( ) ;
if ( ! process . waitForFinished ( timeoutSec > std : : chrono : : seconds : : zero ( ) ? ( std : : chrono : : milliseconds ( timeoutSec ) . count ( ) ) : - 1 ) )
if ( ! process . waitForFinished (
timeoutSec > std : : chrono : : seconds : : zero ( ) ? ( std : : chrono : : milliseconds ( timeoutSec ) . count ( ) ) : - 1 ) )
{
cWarning ( ) . noquote ( ) . nospace ( ) < < " Timed out. Output so far: \n " < <
process . readAllStandardOutput ( ) ;
cWarning ( ) . noquote ( ) . nospace ( ) < < " Timed out. Output so far: \n " < < process . readAllStandardOutput ( ) ;
return ProcessResult : : Code : : TimedOut ;
}
@ -242,7 +256,7 @@ System::runCommand(
cDebug ( ) < < " Target cmd: " < < RedactedList ( args ) ;
cDebug ( ) . noquote ( ) . nospace ( ) < < " Target output: \n " < < output ;
}
return ProcessResult ( r , output ) ;
return ProcessResult ( r , output ) ;
}
QString
@ -252,7 +266,8 @@ System::targetPath( const QString& path ) const
if ( doChroot ( ) )
{
Calamares : : GlobalStorage * gs = Calamares : : JobQueue : : instance ( ) ? Calamares : : JobQueue : : instance ( ) - > globalStorage ( ) : nullptr ;
Calamares : : GlobalStorage * gs
= Calamares : : JobQueue : : instance ( ) ? Calamares : : JobQueue : : instance ( ) - > globalStorage ( ) : nullptr ;
if ( ! gs | | ! gs - > contains ( " rootMountPoint " ) )
{
@ -309,28 +324,32 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons
}
QPair < quint64 , float >
QPair < quint64 , float >
System : : getTotalMemoryB ( ) const
{
# ifdef Q_OS_LINUX
struct sysinfo i ;
int r = sysinfo ( & i ) ;
if ( r )
return qMakePair ( 0 , 0.0 ) ;
if ( r )
{
return qMakePair ( 0 , 0.0 ) ;
}
return qMakePair ( quint64 ( i . mem_unit ) * quint64 ( i . totalram ) , 1.1 ) ;
return qMakePair ( quint64 ( i . mem_unit ) * quint64 ( i . totalram ) , 1.1 ) ;
# elif defined( Q_OS_FREEBSD )
unsigned long memsize ;
size_t s = sizeof ( memsize ) ;
size_t s = sizeof ( memsize ) ;
int r = sysctlbyname ( " vm.kmem_size " , & memsize , & s , NULL , 0 ) ;
if ( r )
return qMakePair ( 0 , 0.0 ) ;
int r = sysctlbyname ( " vm.kmem_size " , & memsize , & s , NULL , 0 ) ;
if ( r )
{
return qMakePair ( 0 , 0.0 ) ;
}
return qMakePair ( memsize , 1.01 ) ;
return qMakePair ( memsize , 1.01 ) ;
# else
return qMakePair ( 0 , 0.0 ) ; // Unsupported
return qMakePair ( 0 , 0.0 ) ; // Unsupported
# endif
}
@ -341,14 +360,14 @@ System::getCpuDescription() const
QString model ;
# ifdef Q_OS_LINUX
QFile file ( " /proc/cpuinfo " ) ;
if ( file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) )
QFile file ( " /proc/cpuinfo " ) ;
if ( file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) )
while ( ! file . atEnd ( ) )
{
QByteArray line = file . readLine ( ) ;
if ( line . startsWith ( " model name " ) & & ( line . indexOf ( ' : ' ) > 0 ) )
if ( line . startsWith ( " model name " ) & & ( line . indexOf ( ' : ' ) > 0 ) )
{
model = QString : : fromLatin1 ( line . right ( line . length ( ) - line . indexOf ( ' : ' ) ) ) ;
model = QString : : fromLatin1 ( line . right ( line . length ( ) - line . indexOf ( ' : ' ) ) ) ;
break ;
}
}
@ -376,40 +395,45 @@ ProcessResult::explainProcess( int ec, const QString& command, const QString& ou
using Calamares : : JobResult ;
if ( ec = = 0 )
{
return JobResult : : ok ( ) ;
}
QString outputMessage = output . isEmpty ( )
? QCoreApplication : : translate ( " ProcessResult " , " \n There was no output from the command. " )
: ( QCoreApplication : : translate ( " ProcessResult " , " \n Output: \n " ) + output ) ;
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 = = 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 = = 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 = = 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 )
. arg ( timeout . count ( ) )
+ outputMessage ) ;
? QCoreApplication : : translate ( " ProcessResult " , " \n There was no output from the command. " )
: ( QCoreApplication : : translate ( " ProcessResult " , " \n Output: \n " ) + output ) ;
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 = = 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 = = 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 = = 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 )
. arg ( timeout . count ( ) )
+ outputMessage ) ;
//Any other exit code
return JobResult : : error ( QCoreApplication : : translate ( " ProcessResult " , " External command finished with errors. " ) ,
QCoreApplication : : translate ( " ProcessResult " , " Command <i>%1</i> finished with exit code %2. " )
. arg ( command )
. arg ( ec )
+ outputMessage ) ;
return JobResult : : error (
QCoreApplication : : translate ( " ProcessResult " , " External command finished with errors. " ) ,
QCoreApplication : : translate ( " ProcessResult " , " Command <i>%1</i> finished with exit code %2. " )
. arg ( command )
. arg ( ec )
+ outputMessage ) ;
}
} // namespace
} // namespace CalamaresUtils