diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.cpp b/src/libcalamares/utils/CalamaresUtilsSystem.cpp index a38b28b6d..c464e93f6 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.cpp +++ b/src/libcalamares/utils/CalamaresUtilsSystem.cpp @@ -247,7 +247,7 @@ System::targetPath( const QString& path ) const } CreationResult -System::createTargetFile( const QString& path, const QByteArray& contents ) const +System::createTargetFile( const QString& path, const QByteArray& contents, WriteMode mode ) const { QString completePath = targetPath( path ); if ( completePath.isEmpty() ) @@ -256,7 +256,7 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons } QFile f( completePath ); - if ( f.exists() ) + if ( ( mode == WriteMode::KeepExisting ) && f.exists() ) { return CreationResult( CreationResult::Code::AlreadyExists ); } @@ -264,7 +264,7 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons QIODevice::OpenMode m = #if QT_VERSION >= QT_VERSION_CHECK( 5, 11, 0 ) // New flag from Qt 5.11, implies WriteOnly - QIODevice::NewOnly | + ( mode == WriteMode::KeepExisting ? QIODevice::NewOnly : QIODevice::WriteOnly ) | #endif QIODevice::WriteOnly | QIODevice::Truncate; diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.h b/src/libcalamares/utils/CalamaresUtilsSystem.h index a163a1208..c4db9dc00 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.h +++ b/src/libcalamares/utils/CalamaresUtilsSystem.h @@ -258,6 +258,12 @@ public: */ DLLEXPORT QString targetPath( const QString& path ) const; + enum class WriteMode + { + KeepExisting, + Overwrite + }; + /** @brief Create a (small-ish) file in the target system. * * @param path Path to the file; this is interpreted @@ -265,14 +271,18 @@ public: * but / in the chroot, or / in OEM modes). * @param contents Actual content of the file. * - * Will not overwrite files. Returns an empty string if the - * target file already exists. + * If the target already exists: + * - returns AlreadyExists as a result (and does not overwrite), + * - **unless** @p mode is set to Overwrite, then it tries writing as + * usual and will not return AlreadyExists. * * @return The complete canonical path to the target file from the * root of the host system, or empty on failure. (Here, it is * possible to be canonical because the file exists). */ - DLLEXPORT CreationResult createTargetFile( const QString& path, const QByteArray& contents ) const; + DLLEXPORT CreationResult createTargetFile( const QString& path, + const QByteArray& contents, + WriteMode mode = WriteMode::KeepExisting ) const; /** @brief Remove a file from the target system. *