[libcalamares] More detail for createTargetFile()

- Return a result-object with statrus information and the path
   which was previously used (empty for "failures").
This commit is contained in:
Adriaan de Groot 2020-02-14 12:52:37 +01:00
parent 695b88b8a7
commit 2d7398161d
2 changed files with 42 additions and 7 deletions

View File

@ -293,19 +293,19 @@ System::targetPath( const QString& path ) const
} }
} }
QString CreationResult
System::createTargetFile( const QString& path, const QByteArray& contents ) const System::createTargetFile( const QString& path, const QByteArray& contents ) const
{ {
QString completePath = targetPath( path ); QString completePath = targetPath( path );
if ( completePath.isEmpty() ) if ( completePath.isEmpty() )
{ {
return QString(); return CreationResult( CreationResult::Code::Invalid );
} }
QFile f( completePath ); QFile f( completePath );
if ( f.exists() ) if ( f.exists() )
{ {
return QString(); return CreationResult( CreationResult::Code::AlreadyExists );
} }
QIODevice::OpenMode m = QIODevice::OpenMode m =
@ -317,18 +317,18 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons
if ( !f.open( m ) ) if ( !f.open( m ) )
{ {
return QString(); return CreationResult( CreationResult::Code::Failed );
} }
if ( f.write( contents ) != contents.size() ) if ( f.write( contents ) != contents.size() )
{ {
f.close(); f.close();
f.remove(); f.remove();
return QString(); return CreationResult( CreationResult::Code::Failed );
} }
f.close(); f.close();
return QFileInfo( f ).canonicalFilePath(); return CreationResult( QFileInfo( f ).canonicalFilePath() );
} }
void void

View File

@ -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 * @brief The System class is a singleton with utility functions that perform
* system-specific operations. * system-specific operations.
@ -244,7 +279,7 @@ public:
* 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 CreationResult createTargetFile( const QString& path, const QByteArray& contents ) const;
/** @brief Remove a file from the target system. /** @brief Remove a file from the target system.
* *