From fe2be46d3f66647097ac187a714bcf02e581e0b3 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 29 Jan 2018 17:01:28 +0100 Subject: [PATCH] [libcalamares] Extend command-list with timeouts - Replace plain StringList with a list of pairs, and run that instead. All code paths still use the default 10sec timeout and there's no way to change that. --- src/libcalamares/utils/CommandList.cpp | 31 ++++++++------ src/libcalamares/utils/CommandList.h | 57 ++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/src/libcalamares/utils/CommandList.cpp b/src/libcalamares/utils/CommandList.cpp index ae42a13bd..543c6a5ad 100644 --- a/src/libcalamares/utils/CommandList.cpp +++ b/src/libcalamares/utils/CommandList.cpp @@ -26,14 +26,17 @@ #include -static QStringList get_variant_stringlist( const QVariantList& l ) +namespace CalamaresUtils { - QStringList retl; + +static CommandList_t get_variant_stringlist( const QVariantList& l, int timeout ) +{ + CommandList_t retl; unsigned int c = 0; for ( const auto& v : l ) { if ( v.type() == QVariant::String ) - retl.append( v.toString() ); + retl.append( CommandLine( v.toString(), timeout ) ); else cDebug() << "WARNING Bad CommandList element" << c << v.type() << v; ++c; @@ -41,22 +44,20 @@ static QStringList get_variant_stringlist( const QVariantList& l ) return retl; } -namespace CalamaresUtils -{ - -CommandList::CommandList( bool doChroot ) +CommandList::CommandList( bool doChroot, int timeout ) : m_doChroot( doChroot ) + , m_timeout( timeout ) { } -CommandList::CommandList::CommandList( const QVariant& v, bool doChroot ) - : CommandList( doChroot ) +CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, int timeout ) + : CommandList( doChroot, timeout ) { if ( v.type() == QVariant::List ) { const auto v_list = v.toList(); if ( v_list.count() ) - append( get_variant_stringlist( v_list ) ); + append( get_variant_stringlist( v_list, m_timeout ) ); else cDebug() << "WARNING: Empty CommandList"; } @@ -90,7 +91,7 @@ Calamares::JobResult CommandList::run( const QObject* parent ) for ( CommandList::const_iterator i = cbegin(); i != cend(); ++i ) { - QString processed_cmd = *i; + QString processed_cmd = i->command(); processed_cmd.replace( "@@ROOT@@", root ); // FIXME? bool suppress_result = false; if ( processed_cmd.startsWith( '-' ) ) @@ -103,7 +104,7 @@ Calamares::JobResult CommandList::run( const QObject* parent ) shell_cmd << processed_cmd; ProcessResult r = System::runCommand( - location, shell_cmd, QString(), QString(), 10 ); + location, shell_cmd, QString(), QString(), i->timeout() ); if ( r.getExitCode() != 0 ) { @@ -117,4 +118,10 @@ Calamares::JobResult CommandList::run( const QObject* parent ) return Calamares::JobResult::ok(); } +void +CommandList::append( const QString& s ) +{ + append( CommandLine( s, m_timeout ) ); +} + } // namespace diff --git a/src/libcalamares/utils/CommandList.h b/src/libcalamares/utils/CommandList.h index 0137fe41e..e80a1b742 100644 --- a/src/libcalamares/utils/CommandList.h +++ b/src/libcalamares/utils/CommandList.h @@ -27,11 +27,47 @@ namespace CalamaresUtils { -class CommandList : protected QStringList +/** + * Each command can have an associated timeout in seconds. The timeout + * defaults to 10 seconds. Provide some convenience naming and construction. + */ +struct CommandLine : public QPair< QString, int > +{ + CommandLine( const QString& s ) + : QPair< QString, int >( s, 10 ) + { + } + + CommandLine( const QString& s, int t ) + : QPair< QString, int >( s, t) + { + } + + QString command() const + { + return first; + } + + int timeout() const + { + return second; + } +} ; + +/** @brief Abbreviation, used internally. */ +using CommandList_t = QList< CommandLine >; + +/** + * A list of commands; the list may have its own default timeout + * for commands (which is then applied to each individual command + * that doesn't have one of its own). + */ +class CommandList : protected CommandList_t { public: - CommandList( bool doChroot = true ); - CommandList( const QVariant& v, bool doChroot = true ); + /** @brief empty command-list with timeout to apply to entries. */ + CommandList( bool doChroot = true, int timeout = 10 ); + CommandList( const QVariant& v, bool doChroot = true, int timeout = 10 ); ~CommandList(); bool doChroot() const @@ -41,14 +77,19 @@ public: Calamares::JobResult run( const QObject* parent ); - using QStringList::isEmpty; - using QStringList::count; - using QStringList::cbegin; - using QStringList::cend; - using QStringList::const_iterator; + using CommandList_t::isEmpty; + using CommandList_t::count; + using CommandList_t::cbegin; + using CommandList_t::cend; + using CommandList_t::const_iterator; + +protected: + using CommandList_t::append; + void append( const QString& ); private: bool m_doChroot; + int m_timeout; } ; } // namespace