[libcalamares] Use std::chrono::seconds for timeouts

- Distinguish just-an-int from seconds all across the API
This commit is contained in:
Adriaan de Groot 2019-08-01 22:47:42 +02:00
parent abd7ce2886
commit cac07c1472
7 changed files with 51 additions and 45 deletions

View file

@ -31,7 +31,7 @@ namespace Calamares {
ProcessJob::ProcessJob( const QString& command, ProcessJob::ProcessJob( const QString& command,
const QString& workingPath, const QString& workingPath,
bool runInChroot, bool runInChroot,
int secondsTimeout, std::chrono::seconds secondsTimeout,
QObject* parent ) QObject* parent )
: Job( parent ) : Job( parent )
, m_command( command ) , m_command( command )

View file

@ -22,6 +22,8 @@
#include "Job.h" #include "Job.h"
#include <chrono>
namespace Calamares { namespace Calamares {
class ProcessJob : public Job class ProcessJob : public Job
@ -31,7 +33,7 @@ public:
explicit ProcessJob( const QString& command, explicit ProcessJob( const QString& command,
const QString& workingPath, const QString& workingPath,
bool runInChroot = false, bool runInChroot = false,
int secondsTimeout = 30, std::chrono::seconds secondsTimeout = std::chrono::seconds(30),
QObject* parent = nullptr ); QObject* parent = nullptr );
virtual ~ProcessJob() override; virtual ~ProcessJob() override;
@ -43,7 +45,7 @@ private:
QString m_command; QString m_command;
QString m_workingPath; QString m_workingPath;
bool m_runInChroot; bool m_runInChroot;
int m_timeoutSec; std::chrono::seconds m_timeoutSec;
}; };
} // namespace Calamares } // namespace Calamares

View file

@ -89,11 +89,13 @@ _target_env_command(
const std::string& stdin, const std::string& stdin,
int timeout ) int timeout )
{ {
// Since Python doesn't give us the type system for distinguishing
// seconds from other integral types, massage to seconds here.
return CalamaresUtils::System::instance()-> return CalamaresUtils::System::instance()->
targetEnvCommand( args, targetEnvCommand( args,
QString(), QString(),
QString::fromStdString( stdin ), QString::fromStdString( stdin ),
timeout ); std::chrono::seconds( timeout ) );
} }
int int

View file

@ -219,7 +219,7 @@ System::runCommand(
} }
process.closeWriteChannel(); process.closeWriteChannel();
if ( !process.waitForFinished( timeoutSec ? ( timeoutSec * 1000 ) : -1 ) ) if ( !process.waitForFinished( timeoutSec > std::chrono::seconds::zero() ? ( std::chrono::milliseconds( timeoutSec ).count() ) : -1 ) )
{ {
cWarning().noquote().nospace() << "Timed out. Output so far:\n" << cWarning().noquote().nospace() << "Timed out. Output so far:\n" <<
process.readAllStandardOutput(); process.readAllStandardOutput();
@ -249,7 +249,7 @@ QString
System::targetPath( const QString& path ) const System::targetPath( const QString& path ) const
{ {
QString completePath; QString completePath;
if ( doChroot() ) if ( doChroot() )
{ {
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr; Calamares::GlobalStorage* gs = Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr;
@ -259,14 +259,14 @@ System::targetPath( const QString& path ) const
cWarning() << "No rootMountPoint in global storage, cannot create target file" << path; cWarning() << "No rootMountPoint in global storage, cannot create target file" << path;
return QString(); return QString();
} }
completePath = gs->value( "rootMountPoint" ).toString() + '/' + path; completePath = gs->value( "rootMountPoint" ).toString() + '/' + path;
} }
else else
{ {
completePath = QStringLiteral( "/" ) + path; completePath = QStringLiteral( "/" ) + path;
} }
return completePath; return completePath;
} }
@ -278,32 +278,32 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons
{ {
return QString(); return QString();
} }
QFile f( completePath ); QFile f( completePath );
if ( f.exists() ) if ( f.exists() )
{ {
return QString(); return QString();
} }
QIODevice::OpenMode m = QIODevice::OpenMode m =
#if QT_VERSION >= QT_VERSION_CHECK( 5, 11, 0 ) #if QT_VERSION >= QT_VERSION_CHECK( 5, 11, 0 )
// New flag from Qt 5.11, implies WriteOnly // New flag from Qt 5.11, implies WriteOnly
QIODevice::NewOnly | QIODevice::NewOnly |
#endif #endif
QIODevice::WriteOnly | QIODevice::Truncate; QIODevice::WriteOnly | QIODevice::Truncate;
if ( !f.open( m ) ) if ( !f.open( m ) )
{ {
return QString(); return QString();
} }
if ( f.write( contents ) != contents.size() ) if ( f.write( contents ) != contents.size() )
{ {
f.close(); f.close();
f.remove(); f.remove();
return QString(); return QString();
} }
f.close(); f.close();
return QFileInfo( f ).canonicalFilePath(); return QFileInfo( f ).canonicalFilePath();
} }
@ -371,7 +371,7 @@ System::doChroot() const
} }
Calamares::JobResult Calamares::JobResult
ProcessResult::explainProcess( int ec, const QString& command, const QString& output, int timeout ) ProcessResult::explainProcess( int ec, const QString& command, const QString& output, std::chrono::seconds timeout )
{ {
using Calamares::JobResult; using Calamares::JobResult;
@ -401,7 +401,7 @@ ProcessResult::explainProcess( int ec, const QString& command, const QString& ou
return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command failed to finish." ), return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command failed to finish." ),
QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> failed to finish in %2 seconds." ) QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> failed to finish in %2 seconds." )
.arg( command ) .arg( command )
.arg( timeout ) .arg( timeout.count() )
+ outputMessage ); + outputMessage );
//Any other exit code //Any other exit code

View file

@ -62,16 +62,16 @@ public:
* @param timeout Timeout passed to the process runner, for explaining * @param timeout Timeout passed to the process runner, for explaining
* error code -4 (timeout). * error code -4 (timeout).
*/ */
static Calamares::JobResult explainProcess( int errorCode, const QString& command, const QString& output, int timeout ); static Calamares::JobResult explainProcess( int errorCode, const QString& command, const QString& output, std::chrono::seconds timeout );
/// @brief Convenience wrapper for explainProcess() /// @brief Convenience wrapper for explainProcess()
inline Calamares::JobResult explainProcess( const QString& command, int timeout ) const inline Calamares::JobResult explainProcess( const QString& command, std::chrono::seconds timeout ) const
{ {
return explainProcess( getExitCode(), command, getOutput(), timeout ); return explainProcess( getExitCode(), command, getOutput(), timeout );
} }
/// @brief Convenience wrapper for explainProcess() /// @brief Convenience wrapper for explainProcess()
inline Calamares::JobResult explainProcess( const QStringList& command, int timeout ) const inline Calamares::JobResult explainProcess( const QStringList& command, std::chrono::seconds timeout ) const
{ {
return explainProcess( getExitCode(), command.join( ' ' ), getOutput(), timeout ); return explainProcess( getExitCode(), command.join( ' ' ), getOutput(), timeout );
} }
@ -207,40 +207,40 @@ public:
return targetEnvOutput( QStringList{ command }, output, workingPath, stdInput, timeoutSec ); return targetEnvOutput( QStringList{ command }, output, workingPath, stdInput, timeoutSec );
} }
/** @brief Gets a path to a file in the target system, from the host. /** @brief Gets a path to a file in the target system, from the host.
* *
* @param path Path to the file; this is interpreted * @param path Path to the file; this is interpreted
* from the root of the target system (whatever that may be, * from the root of the target system (whatever that may be,
* but / in the chroot, or / in OEM modes). * but / in the chroot, or / in OEM modes).
* *
* @return The complete path to the target file, from * @return The complete path to the target file, from
* the root of the host system, or empty on failure. * the root of the host system, or empty on failure.
* *
* For instance, during installation where the target root is * For instance, during installation where the target root is
* mounted on /tmp/calamares-something, asking for targetPath("/etc/passwd") * mounted on /tmp/calamares-something, asking for targetPath("/etc/passwd")
* will give you /tmp/calamares-something/etc/passwd. * will give you /tmp/calamares-something/etc/passwd.
* *
* No attempt is made to canonicalize anything, since paths might not exist. * No attempt is made to canonicalize anything, since paths might not exist.
*/ */
DLLEXPORT QString targetPath( const QString& path ) const; DLLEXPORT QString targetPath( const QString& path ) const;
/** @brief Create a (small-ish) file in the target system. /** @brief Create a (small-ish) file in the target system.
* *
* @param path Path to the file; this is interpreted * @param path Path to the file; this is interpreted
* from the root of the target system (whatever that may be, * from the root of the target system (whatever that may be,
* but / in the chroot, or / in OEM modes). * but / in the chroot, or / in OEM modes).
* @param contents Actual content of the file. * @param contents Actual content of the file.
* *
* Will not overwrite files. Returns an empty string if the * Will not overwrite files. Returns an empty string if the
* target file already exists. * target file already exists.
* *
* @return The complete canonical path to the target file from the * @return The complete canonical path to the target file from the
* root of the host system, or empty on failure. (Here, it is * root of the host system, or empty on failure. (Here, it is
* possible to be canonical because the file exists). * possible to be canonical because the file exists).
*/ */
DLLEXPORT QString createTargetFile( const QString& path, const QByteArray& contents ) const; DLLEXPORT QString createTargetFile( const QString& path, const QByteArray& contents ) const;
/** /**
* @brief getTotalMemoryB returns the total main memory, in bytes. * @brief getTotalMemoryB returns the total main memory, in bytes.
* *

View file

@ -35,10 +35,10 @@ namespace CalamaresUtils
static CommandLine get_variant_object( const QVariantMap& m ) static CommandLine get_variant_object( const QVariantMap& m )
{ {
QString command = CalamaresUtils::getString( m, "command" ); QString command = CalamaresUtils::getString( m, "command" );
int timeout = CalamaresUtils::getInteger( m, "timeout", CommandLine::TimeoutNotSet ); int timeout = CalamaresUtils::getInteger( m, "timeout", -1 );
if ( !command.isEmpty() ) if ( !command.isEmpty() )
return CommandLine( command, timeout ); return CommandLine( command, timeout >= 0 ? std::chrono::seconds( timeout ) : CommandLine::TimeoutNotSet() );
cWarning() << "Bad CommandLine element" << m; cWarning() << "Bad CommandLine element" << m;
return CommandLine(); return CommandLine();
} }
@ -50,7 +50,7 @@ static CommandList_t get_variant_stringlist( const QVariantList& l )
for ( const auto& v : l ) for ( const auto& v : l )
{ {
if ( v.type() == QVariant::String ) if ( v.type() == QVariant::String )
retl.append( CommandLine( v.toString(), CommandLine::TimeoutNotSet ) ); retl.append( CommandLine( v.toString(), CommandLine::TimeoutNotSet() ) );
else if ( v.type() == QVariant::Map ) else if ( v.type() == QVariant::Map )
{ {
auto command( get_variant_object( v.toMap() ) ); auto command( get_variant_object( v.toMap() ) );
@ -65,13 +65,13 @@ static CommandList_t get_variant_stringlist( const QVariantList& l )
return retl; return retl;
} }
CommandList::CommandList( bool doChroot, int timeout ) CommandList::CommandList( bool doChroot, std::chrono::seconds timeout )
: m_doChroot( doChroot ) : m_doChroot( doChroot )
, m_timeout( timeout ) , m_timeout( timeout )
{ {
} }
CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, int timeout ) CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, std::chrono::seconds timeout )
: CommandList( doChroot, timeout ) : CommandList( doChroot, timeout )
{ {
if ( v.type() == QVariant::List ) if ( v.type() == QVariant::List )
@ -155,7 +155,7 @@ Calamares::JobResult CommandList::run()
QStringList shell_cmd { "/bin/sh", "-c" }; QStringList shell_cmd { "/bin/sh", "-c" };
shell_cmd << processed_cmd; shell_cmd << processed_cmd;
int timeout = i->timeout() >= 0 ? i->timeout() : m_timeout; std::chrono::seconds timeout = i->timeout() >= std::chrono::seconds::zero() ? i->timeout() : m_timeout;
ProcessResult r = System::runCommand( ProcessResult r = System::runCommand(
location, shell_cmd, QString(), QString(), timeout ); location, shell_cmd, QString(), QString(), timeout );

View file

@ -24,6 +24,8 @@
#include <QStringList> #include <QStringList>
#include <QVariant> #include <QVariant>
#include <chrono>
namespace CalamaresUtils namespace CalamaresUtils
{ {
@ -31,23 +33,23 @@ namespace CalamaresUtils
* Each command can have an associated timeout in seconds. The timeout * Each command can have an associated timeout in seconds. The timeout
* defaults to 10 seconds. Provide some convenience naming and construction. * defaults to 10 seconds. Provide some convenience naming and construction.
*/ */
struct CommandLine : public QPair< QString, int > struct CommandLine : public QPair< QString, std::chrono::seconds >
{ {
enum { TimeoutNotSet = -1 }; static inline constexpr std::chrono::seconds TimeoutNotSet() { return std::chrono::seconds(-1); }
/// An invalid command line /// An invalid command line
CommandLine() CommandLine()
: QPair< QString, int >( QString(), TimeoutNotSet ) : QPair( QString(), TimeoutNotSet() )
{ {
} }
CommandLine( const QString& s ) CommandLine( const QString& s )
: QPair< QString, int >( s, TimeoutNotSet ) : QPair( s, TimeoutNotSet() )
{ {
} }
CommandLine( const QString& s, int t ) CommandLine( const QString& s, std::chrono::seconds t )
: QPair< QString, int >( s, t) : QPair( s, t)
{ {
} }
@ -56,7 +58,7 @@ struct CommandLine : public QPair< QString, int >
return first; return first;
} }
int timeout() const std::chrono::seconds timeout() const
{ {
return second; return second;
} }
@ -82,8 +84,8 @@ class CommandList : protected CommandList_t
{ {
public: public:
/** @brief empty command-list with timeout to apply to entries. */ /** @brief empty command-list with timeout to apply to entries. */
CommandList( bool doChroot = true, int timeout = 10 ); CommandList( bool doChroot = true, std::chrono::seconds timeout = std::chrono::seconds( 10 ) );
CommandList( const QVariant& v, bool doChroot = true, int timeout = 10 ); CommandList( const QVariant& v, bool doChroot = true, std::chrono::seconds timeout = std::chrono::seconds( 10 ) );
~CommandList(); ~CommandList();
bool doChroot() const bool doChroot() const
@ -106,7 +108,7 @@ protected:
private: private:
bool m_doChroot; bool m_doChroot;
int m_timeout; std::chrono::seconds m_timeout;
} ; } ;
} // namespace } // namespace