From 2d7398161d3eb6fc20ac25de607cc22a321ddc8b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 14 Feb 2020 12:52:37 +0100 Subject: [PATCH] [libcalamares] More detail for createTargetFile() - Return a result-object with statrus information and the path which was previously used (empty for "failures"). --- .../utils/CalamaresUtilsSystem.cpp | 12 +++--- src/libcalamares/utils/CalamaresUtilsSystem.h | 37 ++++++++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.cpp b/src/libcalamares/utils/CalamaresUtilsSystem.cpp index 61e05976a..8bd696bf0 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.cpp +++ b/src/libcalamares/utils/CalamaresUtilsSystem.cpp @@ -293,19 +293,19 @@ System::targetPath( const QString& path ) const } } -QString +CreationResult System::createTargetFile( const QString& path, const QByteArray& contents ) const { QString completePath = targetPath( path ); if ( completePath.isEmpty() ) { - return QString(); + return CreationResult( CreationResult::Code::Invalid ); } QFile f( completePath ); if ( f.exists() ) { - return QString(); + return CreationResult( CreationResult::Code::AlreadyExists ); } QIODevice::OpenMode m = @@ -317,18 +317,18 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons if ( !f.open( m ) ) { - return QString(); + return CreationResult( CreationResult::Code::Failed ); } if ( f.write( contents ) != contents.size() ) { f.close(); f.remove(); - return QString(); + return CreationResult( CreationResult::Code::Failed ); } f.close(); - return QFileInfo( f ).canonicalFilePath(); + return CreationResult( QFileInfo( f ).canonicalFilePath() ); } void diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.h b/src/libcalamares/utils/CalamaresUtilsSystem.h index ca8e0d797..900634a74 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.h +++ b/src/libcalamares/utils/CalamaresUtilsSystem.h @@ -84,6 +84,41 @@ public: } }; +/** @brief The result of a create*() action, for status + * + * A CreationResult has a status field, can be converted to bool + * (true only on success) and can report the full pathname of + * the thing created if it was successful. + */ +class CreationResult : public QPair< int, QString > +{ +public: + enum class Code : int + { + // These are "not failed", but only OK is a success + OK = 0, + AlreadyExists = 1, + // These are "failed" + Invalid = -1, + Failed = -2 + }; + + CreationResult( Code r ) + : QPair< int, QString >( static_cast< int >( r ), QString() ) + { + } + explicit CreationResult( const QString& path ) + : QPair< int, QString >( 0, path ) + { + } + + Code code() const { return static_cast< Code >( first ); } + QString path() const { return second; } + + bool failed() const { return first < 0; } + operator bool() const { return first == 0; } +}; + /** * @brief The System class is a singleton with utility functions that perform * system-specific operations. @@ -244,7 +279,7 @@ public: * root of the host system, or empty on failure. (Here, it is * possible to be canonical because the file exists). */ - DLLEXPORT QString createTargetFile( const QString& path, const QByteArray& contents ) const; + DLLEXPORT CreationResult createTargetFile( const QString& path, const QByteArray& contents ) const; /** @brief Remove a file from the target system. *