[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/backend/corebackendmanager.h>
#include <kpmcore/core/device.h> #include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h> #include <kpmcore/core/partition.h>
#include <kpmcore/fs/filesystem.h>
#include <kpmcore/fs/filesystemfactory.h> #include <kpmcore/fs/filesystemfactory.h>
#include <kpmcore/fs/luks.h> #include <kpmcore/fs/luks.h>
#include <kpmcore/fs/filesystem.h>
#include <kpmcore/util/externalcommand.h> #include <kpmcore/util/externalcommand.h>
using CalamaresUtils::Partition::PartitionIterator; using CalamaresUtils::Partition::PartitionIterator;
@ -30,202 +30,198 @@ using CalamaresUtils::Partition::PartitionIterator;
namespace KPMHelpers namespace KPMHelpers
{ {
Partition* Partition*
findPartitionByMountPoint( const QList< Device* >& devices, const QString& mountPoint ) 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 ) newRoles |= PartitionRole::Luks;
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it ) }
if ( PartitionInfo::mountPoint( *it ) == mountPoint )
{ FS::luks* fs = dynamic_cast< FS::luks* >(
return *it; FileSystemFactory::create( FileSystem::Luks, firstSector, lastSector, device.logicalSize() ) );
} if ( !fs )
{
cError() << "cannot create LUKS filesystem. Giving up.";
return nullptr; 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, Partition*
const Device& device, clonePartition( Device* device, Partition* partition )
const PartitionRole& role, {
FileSystem::Type fsType, FileSystem* fs = FileSystemFactory::create(
const QString& fsLabel, partition->fileSystem().type(), partition->firstSector(), partition->lastSector(), device->logicalSize() );
qint64 firstSector, return new Partition( partition->parent(),
qint64 lastSector, *device,
PartitionTable::Flags flags ) 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() ); cWarning() << Logger::SubEntry << "#1: Passphrase is empty";
fs->setLabel( fsLabel ); return 1;
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 ) );
} }
if ( partition->fileSystem().type() != FileSystem::Luks )
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(); cWarning() << Logger::SubEntry << "#2: Not a luks encrypted device";
if ( !role.has( PartitionRole::Luks ) ) return 2;
{
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;
} }
// Cast partition fs to luks fs
FS::luks* luksFs = dynamic_cast< FS::luks* >( &partition->fileSystem() );
Partition* // Test the given passphrase
clonePartition( Device* device, Partition* partition ) if ( !luksFs->testPassphrase( deviceNode, passphrase ) )
{ {
FileSystem* fs = FileSystemFactory::create( cWarning() << Logger::SubEntry << "#3: Passphrase incorrect";
partition->fileSystem().type(), partition->firstSector(), partition->lastSector(), device->logicalSize() ); return 3;
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 if ( luksFs->isCryptOpen() )
int
updateLuksDevice( Partition* partition, const QString& passphrase )
{ {
const QString deviceNode = partition->partitionPath(); if ( !luksFs->mapperName().isEmpty() )
cDebug() << "Update Luks device: " << deviceNode;
if ( passphrase.isEmpty() )
{ {
cWarning() << Logger::SubEntry << "#1: Passphrase is empty"; cWarning() << Logger::SubEntry << "#4: Device already decrypted";
return 1; return 4;
} }
else
if ( partition->fileSystem().type() != FileSystem::Luks )
{ {
cWarning() << Logger::SubEntry << "#2: Not a luks encrypted device"; cDebug() << Logger::SubEntry << "No mapper node found";
return 2; 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 ExternalCommand openCmd( QStringLiteral( "cryptsetup" ),
execute( Operation& operation, const QString& failureMessage ) { QStringLiteral( "open" ), deviceNode, luksFs->suggestedMapperName( deviceNode ) } );
if ( !( openCmd.write( passphrase.toLocal8Bit() + '\n' ) && openCmd.start( -1 ) && openCmd.exitCode() == 0 ) )
{ {
operation.setStatus( Operation::StatusRunning ); cWarning() << Logger::SubEntry << openCmd.exitCode() << ": cryptsetup command failed";
return openCmd.exitCode();
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' ) );
} }
// 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 } // namespace KPMHelpers

View File

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

View File

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

View File

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

View File

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

View File

@ -149,7 +149,7 @@ EncryptWidget::updateState()
} }
Encryption newState; 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() if ( !m_ui->m_passphraseLineEdit->text().isEmpty()
&& m_ui->m_passphraseLineEdit->text() == m_ui->m_confirmLineEdit->text() ) && m_ui->m_passphraseLineEdit->text() == m_ui->m_confirmLineEdit->text() )