From 42e465aa10a38b6c37a43fb9be25e676c65d9e91 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Thu, 6 Aug 2015 19:10:04 +0200 Subject: [PATCH] CalamaresUtils::System is now a class. --- src/calamares/CalamaresApplication.cpp | 2 + src/libcalamares/ProcessJob.cpp | 11 +- src/libcalamares/PythonJobApi.cpp | 49 ++++---- .../utils/CalamaresUtilsSystem.cpp | 40 +++++-- src/libcalamares/utils/CalamaresUtilsSystem.h | 110 ++++++++++-------- src/libcalamaresui/Settings.cpp | 10 +- src/libcalamaresui/Settings.h | 3 + src/modules/locale/SetTimezoneJob.cpp | 6 +- src/modules/partition/gui/EraseDiskPage.cpp | 4 +- src/modules/users/CreateUserJob.cpp | 29 ++--- src/modules/users/SetPasswordJob.cpp | 3 +- .../welcome/checker/RequirementsChecker.cpp | 4 +- 12 files changed, 165 insertions(+), 106 deletions(-) diff --git a/src/calamares/CalamaresApplication.cpp b/src/calamares/CalamaresApplication.cpp index 69d628faf..ce2ec6b7c 100644 --- a/src/calamares/CalamaresApplication.cpp +++ b/src/calamares/CalamaresApplication.cpp @@ -26,6 +26,7 @@ #include "modulesystem/ModuleManager.h" #include "utils/CalamaresUtilsGui.h" +#include "utils/CalamaresUtilsSystem.h" #include "utils/Logger.h" #include "JobQueue.h" #include "Branding.h" @@ -384,5 +385,6 @@ void CalamaresApplication::initJobQueue() { Calamares::JobQueue* jobQueue = new Calamares::JobQueue( this ); + new CalamaresUtils::System( Calamares::Settings::instance()->doChroot(), this ); Calamares::Branding::instance()->setGlobals( jobQueue->globalStorage() ); } diff --git a/src/libcalamares/ProcessJob.cpp b/src/libcalamares/ProcessJob.cpp index 373f2018f..84e02f1dd 100644 --- a/src/libcalamares/ProcessJob.cpp +++ b/src/libcalamares/ProcessJob.cpp @@ -69,11 +69,12 @@ ProcessJob::exec() int ec = 0; QString output; if ( m_runInChroot ) - ec = CalamaresUtils::targetEnvOutput( m_command, - output, - m_workingPath, - QString(), - m_timeoutSec ); + ec = CalamaresUtils::System::instance()-> + targetEnvOutput( m_command, + output, + m_workingPath, + QString(), + m_timeoutSec ); else ec = callOutput( m_command, output, diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index a8becf938..9203330ec 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -38,10 +38,11 @@ mount( const std::string& device_path, const std::string& filesystem_name, const std::string& options ) { - return CalamaresUtils::mount( QString::fromStdString( device_path ), - QString::fromStdString( mount_point ), - QString::fromStdString( filesystem_name ), - QString::fromStdString( options ) ); + return CalamaresUtils::System::instance()-> + mount( QString::fromStdString( device_path ), + QString::fromStdString( mount_point ), + QString::fromStdString( filesystem_name ), + QString::fromStdString( options ) ); } @@ -50,10 +51,11 @@ target_env_call( const std::string& command, const std::string& stdin, int timeout ) { - return CalamaresUtils::targetEnvCall( QString::fromStdString( command ), - QString(), - QString::fromStdString( stdin ), - timeout ); + return CalamaresUtils::System::instance()-> + targetEnvCall( QString::fromStdString( command ), + QString(), + QString::fromStdString( stdin ), + timeout ); } @@ -69,10 +71,11 @@ target_env_call( const bp::list& args, bp::extract< std::string >( args[ i ] ) ) ); } - return CalamaresUtils::targetEnvCall( list, - QString(), - QString::fromStdString( stdin ), - timeout ); + return CalamaresUtils::System::instance()-> + targetEnvCall( list, + QString(), + QString::fromStdString( stdin ), + timeout ); } @@ -112,11 +115,12 @@ check_target_env_output( const std::string& command, int timeout ) { QString output; - int ec = CalamaresUtils::targetEnvOutput( QString::fromStdString( command ), - output, - QString(), - QString::fromStdString( stdin ), - timeout ); + int ec = CalamaresUtils::System::instance()-> + targetEnvOutput( QString::fromStdString( command ), + output, + QString(), + QString::fromStdString( stdin ), + timeout ); _handle_check_target_env_call_error( ec, QString::fromStdString( command ) ); return output.toStdString(); } @@ -135,11 +139,12 @@ check_target_env_output( const bp::list& args, bp::extract< std::string >( args[ i ] ) ) ); } - int ec = CalamaresUtils::targetEnvOutput( list, - output, - QString(), - QString::fromStdString( stdin ), - timeout ); + int ec = CalamaresUtils::System::instance()-> + targetEnvOutput( list, + output, + QString(), + QString::fromStdString( stdin ), + timeout ); _handle_check_target_env_call_error( ec, list.join( ' ' ) ); return output.toStdString(); } diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.cpp b/src/libcalamares/utils/CalamaresUtilsSystem.cpp index 6fe38b6d2..235023406 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.cpp +++ b/src/libcalamares/utils/CalamaresUtilsSystem.cpp @@ -29,8 +29,30 @@ namespace CalamaresUtils { +System* System::s_instance = nullptr; + + +System::System( bool doChroot, QObject* parent ) + : QObject( parent ) + , m_doChroot( doChroot ) +{ + Q_ASSERT( !s_instance ); + s_instance = this; +} + + +System::~System() +{} + + +System*System::instance() +{ + return s_instance; +} + + int -mount( const QString& devicePath, +System::mount( const QString& devicePath, const QString& mountPoint, const QString& filesystemName, const QString& options ) @@ -59,7 +81,7 @@ mount( const QString& devicePath, } int -targetEnvCall( const QStringList& args, +System::targetEnvCall( const QStringList& args, const QString& workingPath, const QString& stdInput, int timeoutSec ) @@ -74,7 +96,7 @@ targetEnvCall( const QStringList& args, int -targetEnvCall( const QString& command, +System::targetEnvCall( const QString& command, const QString& workingPath, const QString& stdInput, int timeoutSec ) @@ -87,7 +109,7 @@ targetEnvCall( const QString& command, int -targetEnvOutput( const QStringList& args, +System::targetEnvOutput( const QStringList& args, QString& output, const QString& workingPath, const QString& stdInput, @@ -100,7 +122,7 @@ targetEnvOutput( const QStringList& args, Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage(); if ( !gs || - ( doChroot && !gs->contains( "rootMountPoint" ) ) ) + ( m_doChroot && !gs->contains( "rootMountPoint" ) ) ) { cLog() << "No rootMountPoint in global storage"; return -3; @@ -110,7 +132,7 @@ targetEnvOutput( const QStringList& args, QString program; QStringList arguments; - if ( doChroot ) + if ( m_doChroot ) { QString destDir = gs->value( "rootMountPoint" ).toString(); if ( !QDir( destDir ).exists() ) @@ -177,7 +199,7 @@ targetEnvOutput( const QStringList& args, int -targetEnvOutput( const QString& command, +System::targetEnvOutput( const QString& command, QString& output, const QString& workingPath, const QString& stdInput, @@ -192,7 +214,7 @@ targetEnvOutput( const QString& command, qint64 -getPhysicalMemoryB() +System::getPhysicalMemoryB() { QProcess p; p.start( "dmidecode", { "-t", "17" } ); @@ -219,7 +241,7 @@ getPhysicalMemoryB() qint64 -getTotalMemoryB() +System::getTotalMemoryB() { // A line in meminfo looks like this, with {print $2} we grab the second column. // MemTotal: 8133432 kB diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.h b/src/libcalamares/utils/CalamaresUtilsSystem.h index 6648d3818..c5e99744b 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.h +++ b/src/libcalamares/utils/CalamaresUtilsSystem.h @@ -20,57 +20,73 @@ #include "DllMacro.h" +#include #include namespace CalamaresUtils { -static bool doChroot = true; - -/** - * Runs the mount utility with the specified parameters. - * @returns the program's exit code, or: - * -1 = QProcess crash - * -2 = QProcess cannot start - * -3 = bad arguments - */ -DLLEXPORT int mount( const QString& devicePath, - const QString& mountPoint, - const QString& filesystemName = QString(), - const QString& options = QString() ); - -/** - * Runs the specified command in the chroot of the target system. - * @returns the program's exit code, or: - * -1 = QProcess crash - * -2 = QProcess cannot start - * -3 = bad arguments - * -4 = QProcess timeout - */ -DLLEXPORT int targetEnvCall( const QStringList& args, - const QString& workingPath = QString(), - const QString& stdInput = QString(), - int timeoutSec = 0 ); - -DLLEXPORT int targetEnvCall( const QString& command, - const QString& workingPath = QString(), - const QString& stdInput = QString(), - int timeoutSec = 0 ); - -DLLEXPORT int targetEnvOutput( const QStringList& args, - QString& output, - const QString& workingPath = QString(), - const QString& stdInput = QString(), - int timeoutSec = 0 ); - -DLLEXPORT int targetEnvOutput( const QString& command, - QString& output, - const QString& workingPath = QString(), - const QString& stdInput = QString(), - int timeoutSec = 0 ); - -DLLEXPORT qint64 getPhysicalMemoryB(); //Better guess, doesn't work in VirualBox - -DLLEXPORT qint64 getTotalMemoryB(); //Always underguessed, but always works on Linux + +class DLLEXPORT System : public QObject +{ + Q_OBJECT +public: + explicit System( bool doChroot, QObject* parent = nullptr ); + virtual ~System(); + + static System* instance(); + + /** + * Runs the mount utility with the specified parameters. + * @returns the program's exit code, or: + * -1 = QProcess crash + * -2 = QProcess cannot start + * -3 = bad arguments + */ + DLLEXPORT int mount( const QString& devicePath, + const QString& mountPoint, + const QString& filesystemName = QString(), + const QString& options = QString() ); + + /** + * Runs the specified command in the chroot of the target system. + * @returns the program's exit code, or: + * -1 = QProcess crash + * -2 = QProcess cannot start + * -3 = bad arguments + * -4 = QProcess timeout + */ + DLLEXPORT int targetEnvCall( const QStringList& args, + const QString& workingPath = QString(), + const QString& stdInput = QString(), + int timeoutSec = 0 ); + + DLLEXPORT int targetEnvCall( const QString& command, + const QString& workingPath = QString(), + const QString& stdInput = QString(), + int timeoutSec = 0 ); + + DLLEXPORT int targetEnvOutput( const QStringList& args, + QString& output, + const QString& workingPath = QString(), + const QString& stdInput = QString(), + int timeoutSec = 0 ); + + DLLEXPORT int targetEnvOutput( const QString& command, + QString& output, + const QString& workingPath = QString(), + const QString& stdInput = QString(), + int timeoutSec = 0 ); + + DLLEXPORT qint64 getPhysicalMemoryB(); //Better guess, doesn't work in VirualBox + + DLLEXPORT qint64 getTotalMemoryB(); //Always underguessed, but always works on Linux + +private: + static System* s_instance; + + bool m_doChroot; +}; + } #endif // CALAMARESUTILSSYSTEM_H diff --git a/src/libcalamaresui/Settings.cpp b/src/libcalamaresui/Settings.cpp index 2519984ff..494a2288a 100644 --- a/src/libcalamaresui/Settings.cpp +++ b/src/libcalamaresui/Settings.cpp @@ -19,7 +19,6 @@ #include "Settings.h" #include "utils/CalamaresUtils.h" -#include "utils/CalamaresUtilsSystem.h" #include "utils/Logger.h" #include "utils/YamlUtils.h" @@ -97,8 +96,7 @@ Settings::Settings( const QString& settingsFilePath, .as< std::string >() ); m_promptInstall = config[ "prompt-install" ].as< bool >(); - bool doChroot = !config[ "dont-chroot" ].as< bool >(); - CalamaresUtils::doChroot = doChroot; + m_doChroot = !config[ "dont-chroot" ].as< bool >(); } catch ( YAML::Exception& e ) { @@ -158,5 +156,11 @@ Settings::debugMode() const return m_debug; } +bool +Settings::doChroot() const +{ + return m_doChroot; +} + } diff --git a/src/libcalamaresui/Settings.h b/src/libcalamaresui/Settings.h index 2511b0734..f237507e3 100644 --- a/src/libcalamaresui/Settings.h +++ b/src/libcalamaresui/Settings.h @@ -50,10 +50,13 @@ public: bool debugMode() const; + bool doChroot() const; + private: static Settings* s_instance; bool m_debug; + bool m_doChroot; QStringList m_modulesSearchPaths; diff --git a/src/modules/locale/SetTimezoneJob.cpp b/src/modules/locale/SetTimezoneJob.cpp index f1f918781..1602946a3 100644 --- a/src/modules/locale/SetTimezoneJob.cpp +++ b/src/modules/locale/SetTimezoneJob.cpp @@ -57,11 +57,13 @@ SetTimezoneJob::exec() tr( "Bad path: %1" ).arg( zoneFile.absolutePath() ) ); // Make sure /etc/localtime doesn't exist, otherwise symlinking will fail - CalamaresUtils::targetEnvCall( { "rm", + CalamaresUtils::System::instance()-> + targetEnvCall( { "rm", "-f", localtimeSlink } ); - int ec = CalamaresUtils::targetEnvCall( { "ln", + int ec = CalamaresUtils::System::instance()-> + targetEnvCall( { "ln", "-s", zoneinfoPath, localtimeSlink } ); diff --git a/src/modules/partition/gui/EraseDiskPage.cpp b/src/modules/partition/gui/EraseDiskPage.cpp index 0046a47f1..a6106dbd8 100644 --- a/src/modules/partition/gui/EraseDiskPage.cpp +++ b/src/modules/partition/gui/EraseDiskPage.cpp @@ -286,11 +286,11 @@ EraseDiskPage::swapSuggestion( const qint64 availableSpaceB ) const { // = 4 GiB, if mem >= 64 GiB qint64 suggestedSwapSizeB = 0; - qint64 availableRamB = CalamaresUtils::getPhysicalMemoryB(); + qint64 availableRamB = CalamaresUtils::System::instance()->getPhysicalMemoryB(); qreal overestimationFactor = 1.01; if ( !availableRamB ) { - availableRamB = CalamaresUtils::getTotalMemoryB(); + availableRamB = CalamaresUtils::System::instance()->getTotalMemoryB(); overestimationFactor = 1.10; } diff --git a/src/modules/users/CreateUserJob.cpp b/src/modules/users/CreateUserJob.cpp index d95d4742a..9bd902ae3 100644 --- a/src/modules/users/CreateUserJob.cpp +++ b/src/modules/users/CreateUserJob.cpp @@ -108,7 +108,8 @@ CreateUserJob::exec() foreach ( const QString& group, m_defaultGroups ) if ( !groupsLines.contains( group ) ) - CalamaresUtils::targetEnvCall( { "groupadd", group } ); + CalamaresUtils::System::instance()-> + targetEnvCall( { "groupadd", group } ); QString defaultGroups = m_defaultGroups.join( ',' ); if ( m_autologin ) @@ -120,33 +121,35 @@ CreateUserJob::exec() else autologinGroup = QStringLiteral( "autologin" ); - CalamaresUtils::targetEnvCall( { "groupadd", autologinGroup } ); + CalamaresUtils::System::instance()->targetEnvCall( { "groupadd", autologinGroup } ); defaultGroups.append( QString( ",%1" ).arg( autologinGroup ) ); } - int ec = CalamaresUtils::targetEnvCall( { "useradd", - "-m", - "-s", - "/bin/bash", - "-g", - "users", - "-G", - defaultGroups, - m_userName } ); + int ec = CalamaresUtils::System::instance()-> + targetEnvCall( { "useradd", + "-m", + "-s", + "/bin/bash", + "-g", + "users", + "-G", + defaultGroups, + m_userName } ); if ( ec ) return Calamares::JobResult::error( tr( "Cannot create user %1." ) .arg( m_userName ), tr( "useradd terminated with error code %1." ) .arg( ec ) ); - ec = CalamaresUtils::targetEnvCall( { "chfn", "-f", m_fullName, m_userName } ); + ec = CalamaresUtils::System::instance()->targetEnvCall( { "chfn", "-f", m_fullName, m_userName } ); if ( ec ) return Calamares::JobResult::error( tr( "Cannot set full name for user %1." ) .arg( m_userName ), tr( "chfn terminated with error code %1." ) .arg( ec ) ); - ec = CalamaresUtils::targetEnvCall( { "chown", + ec = CalamaresUtils::System::instance()-> + targetEnvCall( { "chown", "-R", QString( "%1:%2" ).arg( m_userName ) .arg( m_userGroup ), diff --git a/src/modules/users/SetPasswordJob.cpp b/src/modules/users/SetPasswordJob.cpp index 2d09cb1c1..5631dedae 100644 --- a/src/modules/users/SetPasswordJob.cpp +++ b/src/modules/users/SetPasswordJob.cpp @@ -61,7 +61,8 @@ SetPasswordJob::exec() QByteArray data = crypt( m_newPassword.toLatin1(), QString( "$6$%1$" ).arg( m_userName ).toLatin1() ); - int ec = CalamaresUtils::targetEnvCall( { "usermod", + int ec = CalamaresUtils::System::instance()-> + targetEnvCall( { "usermod", "-p", QString::fromLatin1( data ), m_userName } ); diff --git a/src/modules/welcome/checker/RequirementsChecker.cpp b/src/modules/welcome/checker/RequirementsChecker.cpp index 3f8501bbb..84e684dbd 100644 --- a/src/modules/welcome/checker/RequirementsChecker.cpp +++ b/src/modules/welcome/checker/RequirementsChecker.cpp @@ -246,9 +246,9 @@ RequirementsChecker::checkEnoughStorage( qint64 requiredSpace ) bool RequirementsChecker::checkEnoughRam( qint64 requiredRam ) { - qint64 availableRam = CalamaresUtils::getPhysicalMemoryB(); + qint64 availableRam = CalamaresUtils::System::instance()->getPhysicalMemoryB(); if ( !availableRam ) - availableRam = CalamaresUtils::getTotalMemoryB(); + availableRam = CalamaresUtils::System::instance()->getTotalMemoryB(); return availableRam >= requiredRam * 0.95; // because MemTotal is variable }