[partition] Apply coding style

This commit is contained in:
Adriaan de Groot 2022-05-18 12:07:20 +02:00
parent 56071c4016
commit 8fea6f71ab
6 changed files with 233 additions and 237 deletions

View File

@ -20,9 +20,9 @@
#include <kpmcore/backend/corebackendmanager.h>
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>
#include <kpmcore/fs/filesystem.h>
#include <kpmcore/fs/filesystemfactory.h>
#include <kpmcore/fs/luks.h>
#include <kpmcore/fs/filesystem.h>
#include <kpmcore/util/externalcommand.h>
using CalamaresUtils::Partition::PartitionIterator;
@ -30,202 +30,198 @@ using CalamaresUtils::Partition::PartitionIterator;
namespace KPMHelpers
{
Partition*
findPartitionByMountPoint( const QList< Device* >& devices, const QString& mountPoint )
Partition*
findPartitionByMountPoint( const QList< Device* >& devices, const QString& mountPoint )
{
for ( auto device : devices )
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( PartitionInfo::mountPoint( *it ) == mountPoint )
{
return *it;
}
return nullptr;
}
Partition*
createNewPartition( PartitionNode* parent,
const Device& device,
const PartitionRole& role,
FileSystem::Type fsType,
const QString& fsLabel,
qint64 firstSector,
qint64 lastSector,
PartitionTable::Flags flags )
{
FileSystem* fs = FileSystemFactory::create( fsType, firstSector, lastSector, device.logicalSize() );
fs->setLabel( fsLabel );
return new Partition( parent,
device,
role,
fs,
fs->firstSector(),
fs->lastSector(),
QString() /* path */,
KPM_PARTITION_FLAG( None ) /* availableFlags */,
QString() /* mountPoint */,
false /* mounted */,
flags /* activeFlags */,
KPM_PARTITION_STATE( New ) );
}
Partition*
createNewEncryptedPartition( PartitionNode* parent,
const Device& device,
const PartitionRole& role,
FileSystem::Type fsType,
const QString& fsLabel,
qint64 firstSector,
qint64 lastSector,
const QString& passphrase,
PartitionTable::Flags flags )
{
PartitionRole::Roles newRoles = role.roles();
if ( !role.has( PartitionRole::Luks ) )
{
for ( auto device : devices )
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( PartitionInfo::mountPoint( *it ) == mountPoint )
{
return *it;
}
newRoles |= PartitionRole::Luks;
}
FS::luks* fs = dynamic_cast< FS::luks* >(
FileSystemFactory::create( FileSystem::Luks, firstSector, lastSector, device.logicalSize() ) );
if ( !fs )
{
cError() << "cannot create LUKS filesystem. Giving up.";
return nullptr;
}
fs->createInnerFileSystem( fsType );
fs->setPassphrase( passphrase );
fs->setLabel( fsLabel );
Partition* p = new Partition( parent,
device,
PartitionRole( newRoles ),
fs,
fs->firstSector(),
fs->lastSector(),
QString() /* path */,
KPM_PARTITION_FLAG( None ) /* availableFlags */,
QString() /* mountPoint */,
false /* mounted */,
flags /* activeFlags */,
KPM_PARTITION_STATE( New ) );
return p;
}
Partition*
createNewPartition( PartitionNode* parent,
const Device& device,
const PartitionRole& role,
FileSystem::Type fsType,
const QString& fsLabel,
qint64 firstSector,
qint64 lastSector,
PartitionTable::Flags flags )
Partition*
clonePartition( Device* device, Partition* partition )
{
FileSystem* fs = FileSystemFactory::create(
partition->fileSystem().type(), partition->firstSector(), partition->lastSector(), device->logicalSize() );
return new Partition( partition->parent(),
*device,
partition->roles(),
fs,
fs->firstSector(),
fs->lastSector(),
partition->partitionPath(),
partition->activeFlags() );
}
// Adapted from luks cryptOpen which always opens a dialog to ask for a passphrase
int
updateLuksDevice( Partition* partition, const QString& passphrase )
{
const QString deviceNode = partition->partitionPath();
cDebug() << "Update Luks device: " << deviceNode;
if ( passphrase.isEmpty() )
{
FileSystem* fs = FileSystemFactory::create( fsType, firstSector, lastSector, device.logicalSize() );
fs->setLabel( fsLabel );
return new Partition( parent,
device,
role,
fs,
fs->firstSector(),
fs->lastSector(),
QString() /* path */,
KPM_PARTITION_FLAG( None ) /* availableFlags */,
QString() /* mountPoint */,
false /* mounted */,
flags /* activeFlags */,
KPM_PARTITION_STATE( New ) );
cWarning() << Logger::SubEntry << "#1: Passphrase is empty";
return 1;
}
Partition*
createNewEncryptedPartition( PartitionNode* parent,
const Device& device,
const PartitionRole& role,
FileSystem::Type fsType,
const QString& fsLabel,
qint64 firstSector,
qint64 lastSector,
const QString& passphrase,
PartitionTable::Flags flags )
if ( partition->fileSystem().type() != FileSystem::Luks )
{
PartitionRole::Roles newRoles = role.roles();
if ( !role.has( PartitionRole::Luks ) )
{
newRoles |= PartitionRole::Luks;
}
FS::luks* fs = dynamic_cast< FS::luks* >(
FileSystemFactory::create( FileSystem::Luks, firstSector, lastSector, device.logicalSize() ) );
if ( !fs )
{
cError() << "cannot create LUKS filesystem. Giving up.";
return nullptr;
}
fs->createInnerFileSystem( fsType );
fs->setPassphrase( passphrase );
fs->setLabel( fsLabel );
Partition* p = new Partition( parent,
device,
PartitionRole( newRoles ),
fs,
fs->firstSector(),
fs->lastSector(),
QString() /* path */,
KPM_PARTITION_FLAG( None ) /* availableFlags */,
QString() /* mountPoint */,
false /* mounted */,
flags /* activeFlags */,
KPM_PARTITION_STATE( New ) );
return p;
cWarning() << Logger::SubEntry << "#2: Not a luks encrypted device";
return 2;
}
// Cast partition fs to luks fs
FS::luks* luksFs = dynamic_cast< FS::luks* >( &partition->fileSystem() );
Partition*
clonePartition( Device* device, Partition* partition )
// Test the given passphrase
if ( !luksFs->testPassphrase( deviceNode, passphrase ) )
{
FileSystem* fs = FileSystemFactory::create(
partition->fileSystem().type(), partition->firstSector(), partition->lastSector(), device->logicalSize() );
return new Partition( partition->parent(),
*device,
partition->roles(),
fs,
fs->firstSector(),
fs->lastSector(),
partition->partitionPath(),
partition->activeFlags() );
cWarning() << Logger::SubEntry << "#3: Passphrase incorrect";
return 3;
}
// Adapted from luks cryptOpen which always opens a dialog to ask for a passphrase
int
updateLuksDevice( Partition* partition, const QString& passphrase )
if ( luksFs->isCryptOpen() )
{
const QString deviceNode = partition->partitionPath();
cDebug() << "Update Luks device: " << deviceNode;
if ( passphrase.isEmpty() )
if ( !luksFs->mapperName().isEmpty() )
{
cWarning() << Logger::SubEntry << "#1: Passphrase is empty";
return 1;
cWarning() << Logger::SubEntry << "#4: Device already decrypted";
return 4;
}
if ( partition->fileSystem().type() != FileSystem::Luks )
else
{
cWarning() << Logger::SubEntry << "#2: Not a luks encrypted device";
return 2;
cDebug() << Logger::SubEntry << "No mapper node found";
luksFs->setCryptOpen( false );
}
// Cast partition fs to luks fs
FS::luks* luksFs = dynamic_cast< FS::luks* >( &partition->fileSystem() );
// Test the given passphrase
if ( !luksFs->testPassphrase( deviceNode, passphrase ) )
{
cWarning() << Logger::SubEntry << "#3: Passphrase incorrect";
return 3;
}
if ( luksFs->isCryptOpen() )
{
if ( !luksFs->mapperName().isEmpty())
{
cWarning() << Logger::SubEntry << "#4: Device already decrypted";
return 4;
}
else
{
cDebug() << Logger::SubEntry << "No mapper node found";
luksFs->setCryptOpen( false );
}
}
ExternalCommand openCmd( QStringLiteral( "cryptsetup" ),
{ QStringLiteral( "open" ),
deviceNode,
luksFs->suggestedMapperName( deviceNode ) } );
if ( !( openCmd.write( passphrase.toLocal8Bit() + '\n' ) &&
openCmd.start( -1 ) &&
openCmd.exitCode() == 0 ) )
{
cWarning() << Logger::SubEntry << openCmd.exitCode() << ": cryptsetup command failed";
return openCmd.exitCode();
}
// Save the existing passphrase
luksFs->setPassphrase( passphrase );
luksFs->scan( deviceNode );
if ( luksFs->mapperName().isEmpty() )
{
cWarning() << Logger::SubEntry << "#5: No mapper node found";
return 5;
}
luksFs->loadInnerFileSystem( luksFs->mapperName() );
luksFs->setCryptOpen( luksFs->innerFS() != nullptr );
if ( !luksFs->isCryptOpen() )
{
cWarning() << Logger::SubEntry << "#6: Device could not be decrypted";
return 6;
}
return 0;
}
Calamares::JobResult
execute( Operation& operation, const QString& failureMessage )
ExternalCommand openCmd( QStringLiteral( "cryptsetup" ),
{ QStringLiteral( "open" ), deviceNode, luksFs->suggestedMapperName( deviceNode ) } );
if ( !( openCmd.write( passphrase.toLocal8Bit() + '\n' ) && openCmd.start( -1 ) && openCmd.exitCode() == 0 ) )
{
operation.setStatus( Operation::StatusRunning );
Report report( nullptr );
if ( operation.execute( report ) )
{
return Calamares::JobResult::ok();
}
// Remove the === lines from the report by trimming them to empty
QStringList l = report.toText().split( '\n' );
std::for_each( l.begin(), l.end(), []( QString& s ) { CalamaresUtils::removeLeading( s, '=' ); } );
return Calamares::JobResult::error( failureMessage, l.join( '\n' ) );
cWarning() << Logger::SubEntry << openCmd.exitCode() << ": cryptsetup command failed";
return openCmd.exitCode();
}
// Save the existing passphrase
luksFs->setPassphrase( passphrase );
luksFs->scan( deviceNode );
if ( luksFs->mapperName().isEmpty() )
{
cWarning() << Logger::SubEntry << "#5: No mapper node found";
return 5;
}
luksFs->loadInnerFileSystem( luksFs->mapperName() );
luksFs->setCryptOpen( luksFs->innerFS() != nullptr );
if ( !luksFs->isCryptOpen() )
{
cWarning() << Logger::SubEntry << "#6: Device could not be decrypted";
return 6;
}
return 0;
}
Calamares::JobResult
execute( Operation& operation, const QString& failureMessage )
{
operation.setStatus( Operation::StatusRunning );
Report report( nullptr );
if ( operation.execute( report ) )
{
return Calamares::JobResult::ok();
}
// Remove the === lines from the report by trimming them to empty
QStringList l = report.toText().split( '\n' );
std::for_each( l.begin(), l.end(), []( QString& s ) { CalamaresUtils::removeLeading( s, '=' ); } );
return Calamares::JobResult::error( failureMessage, l.join( '\n' ) );
}
} // namespace KPMHelpers

View File

@ -43,56 +43,56 @@ class PartitionRole;
namespace KPMHelpers
{
/**
* Iterates on all devices and return the first partition which is associated
* with mountPoint. This uses PartitionInfo::mountPoint(), not Partition::mountPoint()
*/
Partition* findPartitionByMountPoint( const QList< Device* >& devices, const QString& mountPoint );
/**
* Iterates on all devices and return the first partition which is associated
* with mountPoint. This uses PartitionInfo::mountPoint(), not Partition::mountPoint()
*/
Partition* findPartitionByMountPoint( const QList< Device* >& devices, const QString& mountPoint );
/**
* Helper function to create a new Partition object (does not create anything
* on the disk) associated with a FileSystem.
*/
Partition* createNewPartition( PartitionNode* parent,
const Device& device,
const PartitionRole& role,
FileSystem::Type fsType,
const QString& fsLabel,
qint64 firstSector,
qint64 lastSector,
PartitionTable::Flags flags );
/**
* Helper function to create a new Partition object (does not create anything
* on the disk) associated with a FileSystem.
*/
Partition* createNewPartition( PartitionNode* parent,
const Device& device,
const PartitionRole& role,
FileSystem::Type fsType,
const QString& fsLabel,
qint64 firstSector,
qint64 lastSector,
PartitionTable::Flags flags );
Partition* createNewEncryptedPartition( PartitionNode* parent,
const Device& device,
const PartitionRole& role,
FileSystem::Type fsType,
const QString& fsLabel,
qint64 firstSector,
qint64 lastSector,
const QString& passphrase,
PartitionTable::Flags flags );
Partition* createNewEncryptedPartition( PartitionNode* parent,
const Device& device,
const PartitionRole& role,
FileSystem::Type fsType,
const QString& fsLabel,
qint64 firstSector,
qint64 lastSector,
const QString& passphrase,
PartitionTable::Flags flags );
Partition* clonePartition( Device* device, Partition* partition );
Partition* clonePartition( Device* device, Partition* partition );
int updateLuksDevice( Partition* partition, const QString& passphrase );
int updateLuksDevice( Partition* partition, const QString& passphrase );
/** @brief Return a result for an @p operation
*
* Executes the operation, and if successful, returns a success result.
* Otherwise returns an error using @p failureMessage as the primary part
* of the error, and details obtained from the operation.
*/
Calamares::JobResult execute( Operation& operation, const QString& failureMessage );
/** @brief Return a result for an @p operation
*
* It's acceptable to use an rvalue: the operation-running is the effect
* you're interested in, rather than keeping the temporary around.
*/
static inline Calamares::JobResult
execute( Operation&& operation, const QString& failureMessage )
{
return execute( operation, failureMessage );
}
/** @brief Return a result for an @p operation
*
* Executes the operation, and if successful, returns a success result.
* Otherwise returns an error using @p failureMessage as the primary part
* of the error, and details obtained from the operation.
*/
Calamares::JobResult execute( Operation& operation, const QString& failureMessage );
/** @brief Return a result for an @p operation
*
* It's acceptable to use an rvalue: the operation-running is the effect
* you're interested in, rather than keeping the temporary around.
*/
static inline Calamares::JobResult
execute( Operation&& operation, const QString& failureMessage )
{
return execute( operation, failureMessage );
}
} // namespace KPMHelpers

View File

@ -467,10 +467,14 @@ ChoicePage::onActionChanged()
}
// Whole disk encryption isn't implemented for zfs so disable the option for now
if ( m_eraseFsTypesChoiceComboBox != nullptr && m_enableEncryptionWidget ) {
if ( m_eraseFsTypesChoiceComboBox->currentText() == "zfs" ) {
if ( m_eraseFsTypesChoiceComboBox != nullptr && m_enableEncryptionWidget )
{
if ( m_eraseFsTypesChoiceComboBox->currentText() == "zfs" )
{
m_encryptWidget->hide();
} else {
}
else
{
m_encryptWidget->show();
}
}

View File

@ -17,10 +17,10 @@
#include "ui_EditExistingPartitionDialog.h"
#include "core/ColorUtils.h"
#include "core/KPMHelpers.h"
#include "core/PartUtils.h"
#include "core/PartitionCoreModule.h"
#include "core/PartitionInfo.h"
#include "core/KPMHelpers.h"
#include "gui/PartitionDialogHelpers.h"
#include "gui/PartitionSizeController.h"
@ -38,9 +38,9 @@
#include <QComboBox>
#include <QDir>
#include <QPushButton>
#include <QProcess>
#include <QMessageBox>
#include <QProcess>
#include <QPushButton>
using CalamaresUtils::Partition::untranslatedFS;
using CalamaresUtils::Partition::userVisibleFS;
@ -265,8 +265,7 @@ EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
"or delete and create a new encrypted partition." )
.arg( m_partition->partitionPath() );
QMessageBox mb( QMessageBox::Information, message, description,
QMessageBox::Ok, this->parentWidget() );
QMessageBox mb( QMessageBox::Information, message, description, QMessageBox::Ok, this->parentWidget() );
Calamares::fixButtonLabels( &mb );
mb.exec();
}
@ -321,16 +320,15 @@ EditExistingPartitionDialog::updateMountPointPicker()
}
toggleEncryptWidget();
}
void
EditExistingPartitionDialog::checkMountPointSelection()
{
if ( validateMountPoint( selectedMountPoint( m_ui->mountPointComboBox ),
m_usedMountPoints,
m_ui->mountPointExplanation,
m_ui->buttonBox->button( QDialogButtonBox::Ok ) ) )
m_usedMountPoints,
m_ui->mountPointExplanation,
m_ui->buttonBox->button( QDialogButtonBox::Ok ) ) )
{
toggleEncryptWidget();
}
@ -344,17 +342,15 @@ EditExistingPartitionDialog::toggleEncryptWidget()
// and not currently formatted
// and its mount point not a standard mount point except when it's /home
QString mp = selectedMountPoint( m_ui->mountPointComboBox );
if ( !mp.isEmpty()
&& m_partition->fileSystem().type() == FileSystem::Luks
&& !m_ui->formatRadioButton->isChecked()
&& ( !standardMountPoints().contains(mp) || mp == "/home" ) )
if ( !mp.isEmpty() && m_partition->fileSystem().type() == FileSystem::Luks && !m_ui->formatRadioButton->isChecked()
&& ( !standardMountPoints().contains( mp ) || mp == "/home" ) )
{
m_ui->encryptWidget->show();
m_ui->encryptWidget->reset( false );
}
// TODO: When formatting a partition user must be able to encrypt that partition
// Probably need to delete this partition and create a new one
// else if ( m_ui->formatRadioButton->isChecked()
// else if ( m_ui->formatRadioButton->isChecked()
// && !mp.isEmpty())
// {
// m_ui->encryptWidget->show();

View File

@ -36,7 +36,7 @@ public:
{
Partition* p;
};
EditExistingPartitionDialog( Device* device,
Partition* partition,
const QStringList& usedMountPoints,

View File

@ -149,7 +149,7 @@ EncryptWidget::updateState()
}
Encryption newState;
if ( m_ui->m_encryptCheckBox->isChecked() || !m_ui->m_encryptCheckBox->isVisible())
if ( m_ui->m_encryptCheckBox->isChecked() || !m_ui->m_encryptCheckBox->isVisible() )
{
if ( !m_ui->m_passphraseLineEdit->text().isEmpty()
&& m_ui->m_passphraseLineEdit->text() == m_ui->m_confirmLineEdit->text() )