[machineid] Migrate removeFile() to libcalamares

- Becomes removeTargetFile()
This commit is contained in:
Adriaan de Groot 2020-02-07 11:14:57 +01:00
parent 9ef04192db
commit bf882cec1d
5 changed files with 35 additions and 20 deletions

View File

@ -1,7 +1,7 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === This file is part of Calamares - <https://github.com/calamares> ===
* *
* Copyright 2014, Teo Mrnjavac <teo@kde.org> * Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org> * Copyright 2017-2018, 2020, Adriaan de Groot <groot@kde.org>
* *
* Calamares is free software: you can redistribute it and/or modify * Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -285,6 +285,13 @@ System::targetPath( const QString& path ) const
} }
} }
/// @brief Cheap check if a path is absolute.
static inline bool
isAbsolutePath( const QString& path )
{
return path.startsWith( '/' );
}
QString QString
System::createTargetFile( const QString& path, const QByteArray& contents ) const System::createTargetFile( const QString& path, const QByteArray& contents ) const
{ {
@ -323,6 +330,17 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) cons
return QFileInfo( f ).canonicalFilePath(); return QFileInfo( f ).canonicalFilePath();
} }
void
System::removeTargetFile( const QString& path ) const
{
if ( !isAbsolutePath( path ) )
{
cWarning() << "Will not remove non-absolute path" << path;
return;
}
QFile::remove( targetPath( path ) );
}
QPair< quint64, float > QPair< quint64, float >
System::getTotalMemoryB() const System::getTotalMemoryB() const

View File

@ -1,7 +1,7 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === This file is part of Calamares - <https://github.com/calamares> ===
* *
* Copyright 2014, Teo Mrnjavac <teo@kde.org> * Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org> * Copyright 2017-2018, 2020, Adriaan de Groot <groot@kde.org>
* *
* Calamares is free software: you can redistribute it and/or modify * Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -246,6 +246,15 @@ public:
*/ */
DLLEXPORT QString createTargetFile( const QString& path, const QByteArray& contents ) const; DLLEXPORT QString createTargetFile( const QString& path, const QByteArray& contents ) const;
/** @brief Remove a file from the target system.
*
* @param path Path to the file; this is interpreted from the root
* of the target system (@see targetPath()).
*
* Does no error checking to see if the target file was really removed.
*/
DLLEXPORT void removeTargetFile( const QString& path ) const;
/** /**
* @brief getTotalMemoryB returns the total main memory, in bytes. * @brief getTotalMemoryB returns the total main memory, in bytes.
* *

View File

@ -3,7 +3,7 @@
* Copyright 2014, Kevin Kofler <kevin.kofler@chello.at> * Copyright 2014, Kevin Kofler <kevin.kofler@chello.at>
* Copyright 2016, Philip Müller <philm@manjaro.org> * Copyright 2016, Philip Müller <philm@manjaro.org>
* Copyright 2017, Alf Gaida <agaida@siduction.org> * Copyright 2017, Alf Gaida <agaida@siduction.org>
* Copyright 2019, Adriaan de Groot <groot@kde.org> * Copyright 2019-2020, Adriaan de Groot <groot@kde.org>
* *
* Calamares is free software: you can redistribute it and/or modify * Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -68,18 +68,20 @@ MachineIdJob::exec()
QString target_dbus_machineid_file = QStringLiteral( "/var/lib/dbus/machine-id" ); QString target_dbus_machineid_file = QStringLiteral( "/var/lib/dbus/machine-id" );
QString target_entropy_file = QStringLiteral( "/var/lib/urandom/random-seed" ); QString target_entropy_file = QStringLiteral( "/var/lib/urandom/random-seed" );
const auto* system = CalamaresUtils::System::instance();
// Clear existing files // Clear existing files
if ( m_entropy ) if ( m_entropy )
{ {
MachineId::removeFile( root, target_entropy_file ); system->removeTargetFile( target_entropy_file );
} }
if ( m_dbus ) if ( m_dbus )
{ {
MachineId::removeFile( root, target_dbus_machineid_file ); system->removeTargetFile( target_dbus_machineid_file );
} }
if ( m_systemd ) if ( m_systemd )
{ {
MachineId::removeFile( root, target_systemd_machineid_file ); system->removeTargetFile( target_systemd_machineid_file );
} }
//Create new files //Create new files

View File

@ -36,17 +36,6 @@ isAbsolutePath( const QString& fileName )
return fileName.startsWith( '/' ); return fileName.startsWith( '/' );
} }
// might need to use a helper to remove the file
void
removeFile( const QString& rootMountPoint, const QString& fileName )
{
if ( isAbsolutePath( fileName ) )
{
QFile::remove( rootMountPoint + fileName );
}
// Otherwise, do nothing
}
Calamares::JobResult Calamares::JobResult
copyFile( const QString& rootMountPoint, const QString& fileName ) copyFile( const QString& rootMountPoint, const QString& fileName )
{ {

View File

@ -30,9 +30,6 @@ namespace MachineId
* for moving files around in the target system. * for moving files around in the target system.
*/ */
/// @brief Remove @p fileName from the target system at @p rootMountPoint
void removeFile( const QString& rootMountPoint, const QString& fileName );
/// @brief Copy @p fileName from host into target system at @p rootMountPoint /// @brief Copy @p fileName from host into target system at @p rootMountPoint
Calamares::JobResult copyFile( const QString& rootMountPoint, const QString& fileName ); Calamares::JobResult copyFile( const QString& rootMountPoint, const QString& fileName );