[libcalamares] Add mode to createTargetFile()

- Unconditionally **not** overwriting the target file isn't an option:
  writing hostname, for instance, expects that to be done even
  if `/etc/hostname` already exists on the target filesystem.
This commit is contained in:
Adriaan de Groot 2020-02-24 12:14:50 +01:00
parent d903a50bd8
commit f89951716e
2 changed files with 16 additions and 6 deletions

View File

@ -247,7 +247,7 @@ System::targetPath( const QString& path ) const
} }
CreationResult 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 ); QString completePath = targetPath( path );
if ( completePath.isEmpty() ) if ( completePath.isEmpty() )
@ -256,7 +256,7 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons
} }
QFile f( completePath ); QFile f( completePath );
if ( f.exists() ) if ( ( mode == WriteMode::KeepExisting ) && f.exists() )
{ {
return CreationResult( CreationResult::Code::AlreadyExists ); return CreationResult( CreationResult::Code::AlreadyExists );
} }
@ -264,7 +264,7 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons
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 | ( mode == WriteMode::KeepExisting ? QIODevice::NewOnly : QIODevice::WriteOnly ) |
#endif #endif
QIODevice::WriteOnly | QIODevice::Truncate; QIODevice::WriteOnly | QIODevice::Truncate;

View File

@ -258,6 +258,12 @@ public:
*/ */
DLLEXPORT QString targetPath( const QString& path ) const; DLLEXPORT QString targetPath( const QString& path ) const;
enum class WriteMode
{
KeepExisting,
Overwrite
};
/** @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
@ -265,14 +271,18 @@ public:
* 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 * If the target already exists:
* target file 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 * @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 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. /** @brief Remove a file from the target system.
* *