From 139f5a9e86e7afcce1d9faf51b42655117051170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Wed, 6 Dec 2017 14:47:54 +0000 Subject: [PATCH 1/4] Use KPMcore SetPartFlags operation. --- .../partition/jobs/SetPartitionFlagsJob.cpp | 89 +++---------------- 1 file changed, 13 insertions(+), 76 deletions(-) diff --git a/src/modules/partition/jobs/SetPartitionFlagsJob.cpp b/src/modules/partition/jobs/SetPartitionFlagsJob.cpp index 8c562450f..9a5ef67bc 100644 --- a/src/modules/partition/jobs/SetPartitionFlagsJob.cpp +++ b/src/modules/partition/jobs/SetPartitionFlagsJob.cpp @@ -23,14 +23,12 @@ #include "utils/Logger.h" -#include -#include -#include -#include -#include -#include -#include -#include +// KPMcore +#include +#include +#include +#include +#include SetPartFlagsJob::SetPartFlagsJob( Device* device, Partition* partition, @@ -131,76 +129,15 @@ SetPartFlagsJob::prettyStatusMessage() const Calamares::JobResult SetPartFlagsJob::exec() { - PartitionTable::Flags oldFlags = partition()->availableFlags(); - if ( oldFlags == m_flags ) - return Calamares::JobResult::ok(); - - CoreBackend* backend = CoreBackendManager::self()->backend(); + Report report (nullptr); + SetPartFlagsOperation op(*m_device, *partition(), m_flags); + op.setStatus(Operation::StatusRunning); + connect(&op, &Operation::progress, [&](int percent) { emit progress(percent / 100.0); } ); QString errorMessage = tr( "The installer failed to set flags on partition %1." ) .arg( m_partition->partitionPath() ); + if (op.execute(report)) + return Calamares::JobResult::ok(); - QScopedPointer< CoreBackendDevice > backendDevice( backend->openDevice( m_device->deviceNode() ) ); - if ( !backendDevice.data() ) - { - return Calamares::JobResult::error( - errorMessage, - tr( "Could not open device '%1'." ).arg( m_device->deviceNode() ) - ); - } - - QScopedPointer< CoreBackendPartitionTable > backendPartitionTable( backendDevice->openPartitionTable() ); - if ( !backendPartitionTable.data() ) - { - return Calamares::JobResult::error( - errorMessage, - tr( "Could not open partition table on device '%1'." ).arg( m_device->deviceNode() ) - ); - } - - QScopedPointer< CoreBackendPartition > backendPartition( - ( partition()->roles().has( PartitionRole::Extended ) ) - ? backendPartitionTable->getExtendedPartition() - : backendPartitionTable->getPartitionBySector( partition()->firstSector() ) - ); - if ( !backendPartition.data() ) { - return Calamares::JobResult::error( - errorMessage, - tr( "Could not find partition '%1'." ).arg( partition()->partitionPath() ) - ); - } - - quint32 count = 0; - - foreach( const PartitionTable::Flag& f, PartitionTable::flagList() ) - { - emit progress(++count); - - const bool state = ( m_flags & f ) ? true : false; - - Report report( nullptr ); - if ( !backendPartition->setFlag( report, f, state ) ) - { - cDebug() << QStringLiteral( "WARNING: Could not set flag %2 on " - "partition '%1'." ) - .arg( partition()->partitionPath() ) - .arg( PartitionTable::flagName( f ) ); - } - } - - // HACK: Partition (in KPMcore) declares SetPartFlagsJob as friend, but this actually - // refers to an unrelated class SetPartFlagsJob which is in KPMcore but is not - // exported. - // Obviously here we are relying on having a class in Calamares with the same - // name as a private one in KPMcore, which is awful, but it's the least evil - // way to call Partition::setFlags (KPMcore's SetPartFlagsJob needs its friend - // status for the very same reason). - m_partition->setFlags( m_flags ); - - backendPartitionTable->commit(); - - return Calamares::JobResult::ok(); + return Calamares::JobResult::error(errorMessage, report.toText()); } - - -#include "SetPartitionFlagsJob.moc" From 38688bab47953b0edc857e8a38a9be05c936aeb4 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 14 Dec 2017 15:55:23 -0500 Subject: [PATCH 2/4] [partition] code style (minor followup to #872) --- .../partition/jobs/SetPartitionFlagsJob.cpp | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/modules/partition/jobs/SetPartitionFlagsJob.cpp b/src/modules/partition/jobs/SetPartitionFlagsJob.cpp index 9a5ef67bc..938c3db89 100644 --- a/src/modules/partition/jobs/SetPartitionFlagsJob.cpp +++ b/src/modules/partition/jobs/SetPartitionFlagsJob.cpp @@ -31,8 +31,8 @@ #include SetPartFlagsJob::SetPartFlagsJob( Device* device, - Partition* partition, - PartitionTable::Flags flags ) + Partition* partition, + PartitionTable::Flags flags ) : PartitionJob( partition ) , m_device( device ) , m_flags( flags ) @@ -47,8 +47,8 @@ SetPartFlagsJob::prettyName() const if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Set flags on %1MB %2 partition." ) - .arg( partition()->capacity() /1024 /1024) - .arg( partition()->fileSystem().name() ); + .arg( partition()->capacity() /1024 /1024 ) + .arg( partition()->fileSystem().name() ); return tr( "Set flags on new partition." ); } @@ -62,12 +62,12 @@ SetPartFlagsJob::prettyDescription() const { if ( !partition()->partitionPath().isEmpty() ) return tr( "Clear flags on partition %1." ) - .arg( partition()->partitionPath() ); + .arg( partition()->partitionPath() ); if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Clear flags on %1MB %2 partition." ) - .arg( partition()->capacity() /1024 /1024) - .arg( partition()->fileSystem().name() ); + .arg( partition()->capacity() /1024 /1024 ) + .arg( partition()->fileSystem().name() ); return tr( "Clear flags on new partition." ); } @@ -75,18 +75,18 @@ SetPartFlagsJob::prettyDescription() const if ( !partition()->partitionPath().isEmpty() ) return tr( "Flag partition %1 as " "%2." ) - .arg( partition()->partitionPath() ) - .arg( flagsList.join( ", " ) ); + .arg( partition()->partitionPath() ) + .arg( flagsList.join( ", " ) ); if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Flag %1MB %2 partition as " "%3." ) - .arg( partition()->capacity() /1024 /1024) - .arg( partition()->fileSystem().name() ) - .arg( flagsList.join( ", " ) ); + .arg( partition()->capacity() /1024 /1024 ) + .arg( partition()->fileSystem().name() ) + .arg( flagsList.join( ", " ) ); return tr( "Flag new partition as %1." ) - .arg( flagsList.join( ", " ) ); + .arg( flagsList.join( ", " ) ); } @@ -98,12 +98,12 @@ SetPartFlagsJob::prettyStatusMessage() const { if ( !partition()->partitionPath().isEmpty() ) return tr( "Clearing flags on partition %1." ) - .arg( partition()->partitionPath() ); + .arg( partition()->partitionPath() ); if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Clearing flags on %1MB %2 partition." ) - .arg( partition()->capacity() /1024 /1024) - .arg( partition()->fileSystem().name() ); + .arg( partition()->capacity() /1024 /1024 ) + .arg( partition()->fileSystem().name() ); return tr( "Clearing flags on new partition." ); } @@ -111,33 +111,36 @@ SetPartFlagsJob::prettyStatusMessage() const if ( !partition()->partitionPath().isEmpty() ) return tr( "Setting flags %2 on partition " "%1." ) - .arg( partition()->partitionPath() ) - .arg( flagsList.join( ", " ) ); + .arg( partition()->partitionPath() ) + .arg( flagsList.join( ", " ) ); if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Setting flags %3 on " "%1MB %2 partition." ) - .arg( partition()->capacity() /1024 /1024) - .arg( partition()->fileSystem().name() ) - .arg( flagsList.join( ", " ) ); + .arg( partition()->capacity() /1024 /1024 ) + .arg( partition()->fileSystem().name() ) + .arg( flagsList.join( ", " ) ); return tr( "Setting flags %1 on new partition." ) - .arg( flagsList.join( ", " ) ); + .arg( flagsList.join( ", " ) ); } Calamares::JobResult SetPartFlagsJob::exec() { - Report report (nullptr); - SetPartFlagsOperation op(*m_device, *partition(), m_flags); - op.setStatus(Operation::StatusRunning); - connect(&op, &Operation::progress, [&](int percent) { emit progress(percent / 100.0); } ); + Report report ( nullptr ); + SetPartFlagsOperation op( *m_device, *partition(), m_flags ); + op.setStatus( Operation::StatusRunning ); + connect( &op, &Operation::progress, [&]( int percent ) + { + emit progress( percent / 100.0 ); + } ); QString errorMessage = tr( "The installer failed to set flags on partition %1." ) .arg( m_partition->partitionPath() ); - if (op.execute(report)) + if ( op.execute( report ) ) return Calamares::JobResult::ok(); - return Calamares::JobResult::error(errorMessage, report.toText()); + return Calamares::JobResult::error( errorMessage, report.toText() ); } From 5c4286eebfd0dd33b91d8a7971b90b50fa3642a0 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 14 Dec 2017 16:00:46 -0500 Subject: [PATCH 3/4] [partition] Switch to convenience function for MiB --- src/modules/partition/jobs/ResizePartitionJob.cpp | 12 ++++++++---- src/modules/partition/jobs/SetPartitionFlagsJob.cpp | 13 ++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/modules/partition/jobs/ResizePartitionJob.cpp b/src/modules/partition/jobs/ResizePartitionJob.cpp index 41950d4df..d5a0e28e8 100644 --- a/src/modules/partition/jobs/ResizePartitionJob.cpp +++ b/src/modules/partition/jobs/ResizePartitionJob.cpp @@ -20,11 +20,15 @@ #include "jobs/ResizePartitionJob.h" +#include "utils/Units.h" + // KPMcore #include #include #include +using CalamaresUtils::BytesToMiB; + //- ResizePartitionJob --------------------------------------------------------- ResizePartitionJob::ResizePartitionJob( Device* device, Partition* partition, qint64 firstSector, qint64 lastSector ) : PartitionJob( partition ) @@ -51,8 +55,8 @@ ResizePartitionJob::prettyDescription() const return tr( "Resize %2MB partition %1 to " "%3MB." ) .arg( partition()->partitionPath() ) - .arg( ( m_oldLastSector - m_oldFirstSector + 1 ) * partition()->sectorSize() / 1024 / 1024 ) - .arg( ( m_newLastSector - m_newFirstSector + 1 ) * partition()->sectorSize() / 1024 / 1024 ); + .arg( ( BytesToMiB( m_oldLastSector - m_oldFirstSector + 1 ) * partition()->sectorSize() ) ) + .arg( ( BytesToMiB( m_newLastSector - m_newFirstSector + 1 ) * partition()->sectorSize() ) ); } @@ -62,8 +66,8 @@ ResizePartitionJob::prettyStatusMessage() const return tr( "Resizing %2MB partition %1 to " "%3MB." ) .arg( partition()->partitionPath() ) - .arg( ( m_oldLastSector - m_oldFirstSector + 1 ) * partition()->sectorSize() / 1024 / 1024 ) - .arg( ( m_newLastSector - m_newFirstSector + 1 ) * partition()->sectorSize() / 1024 / 1024 ); + .arg( ( BytesToMiB( m_oldLastSector - m_oldFirstSector + 1 ) * partition()->sectorSize() ) ) + .arg( ( BytesToMiB( m_newLastSector - m_newFirstSector + 1 ) * partition()->sectorSize() ) ); } diff --git a/src/modules/partition/jobs/SetPartitionFlagsJob.cpp b/src/modules/partition/jobs/SetPartitionFlagsJob.cpp index 938c3db89..cadfd0261 100644 --- a/src/modules/partition/jobs/SetPartitionFlagsJob.cpp +++ b/src/modules/partition/jobs/SetPartitionFlagsJob.cpp @@ -22,6 +22,7 @@ #include "SetPartitionFlagsJob.h" #include "utils/Logger.h" +#include "utils/Units.h" // KPMcore #include @@ -30,6 +31,8 @@ #include #include +using CalamaresUtils::BytesToMiB; + SetPartFlagsJob::SetPartFlagsJob( Device* device, Partition* partition, PartitionTable::Flags flags ) @@ -47,7 +50,7 @@ SetPartFlagsJob::prettyName() const if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Set flags on %1MB %2 partition." ) - .arg( partition()->capacity() /1024 /1024 ) + .arg( BytesToMiB( partition()->capacity() ) ) .arg( partition()->fileSystem().name() ); return tr( "Set flags on new partition." ); @@ -66,7 +69,7 @@ SetPartFlagsJob::prettyDescription() const if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Clear flags on %1MB %2 partition." ) - .arg( partition()->capacity() /1024 /1024 ) + .arg( BytesToMiB( partition()->capacity() ) ) .arg( partition()->fileSystem().name() ); return tr( "Clear flags on new partition." ); @@ -81,7 +84,7 @@ SetPartFlagsJob::prettyDescription() const if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Flag %1MB %2 partition as " "%3." ) - .arg( partition()->capacity() /1024 /1024 ) + .arg( BytesToMiB( partition()->capacity() ) ) .arg( partition()->fileSystem().name() ) .arg( flagsList.join( ", " ) ); @@ -102,7 +105,7 @@ SetPartFlagsJob::prettyStatusMessage() const if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Clearing flags on %1MB %2 partition." ) - .arg( partition()->capacity() /1024 /1024 ) + .arg( BytesToMiB( partition()->capacity() ) ) .arg( partition()->fileSystem().name() ); return tr( "Clearing flags on new partition." ); @@ -117,7 +120,7 @@ SetPartFlagsJob::prettyStatusMessage() const if ( !partition()->fileSystem().name().isEmpty() ) return tr( "Setting flags %3 on " "%1MB %2 partition." ) - .arg( partition()->capacity() /1024 /1024 ) + .arg( BytesToMiB( partition()->capacity() ) ) .arg( partition()->fileSystem().name() ) .arg( flagsList.join( ", " ) ); From 55ed2ba4c078f2ab4700d6efdfc9fdc84ba5a13b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 14 Dec 2017 16:09:54 -0500 Subject: [PATCH 4/4] [partition] Introduce convenience translation for progress. --- src/modules/partition/jobs/PartitionJob.cpp | 9 +++++++++ src/modules/partition/jobs/PartitionJob.h | 8 ++++++++ src/modules/partition/jobs/ResizePartitionJob.cpp | 2 +- src/modules/partition/jobs/SetPartitionFlagsJob.cpp | 5 +---- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/modules/partition/jobs/PartitionJob.cpp b/src/modules/partition/jobs/PartitionJob.cpp index a85540704..2de93ac47 100644 --- a/src/modules/partition/jobs/PartitionJob.cpp +++ b/src/modules/partition/jobs/PartitionJob.cpp @@ -21,3 +21,12 @@ PartitionJob::PartitionJob( Partition* partition ) : m_partition( partition ) {} + +void PartitionJob::progress(int percent) +{ + if ( percent < 0 ) + percent = 0; + if ( percent > 100 ) + percent = 100; + emit progress( qreal( percent / 100.0 ) ); +} diff --git a/src/modules/partition/jobs/PartitionJob.h b/src/modules/partition/jobs/PartitionJob.h index fc27e14f2..cf773d60e 100644 --- a/src/modules/partition/jobs/PartitionJob.h +++ b/src/modules/partition/jobs/PartitionJob.h @@ -37,6 +37,14 @@ public: return m_partition; } +public slots: + /** @brief Translate from KPMCore to Calamares progress. + * + * KPMCore presents progress as an integer percent from 0 .. 100, + * while Calamares uses a qreal from 0 .. 1.00 . + */ + void progress( int percent ); + protected: Partition* m_partition; }; diff --git a/src/modules/partition/jobs/ResizePartitionJob.cpp b/src/modules/partition/jobs/ResizePartitionJob.cpp index d5a0e28e8..340a19acd 100644 --- a/src/modules/partition/jobs/ResizePartitionJob.cpp +++ b/src/modules/partition/jobs/ResizePartitionJob.cpp @@ -80,7 +80,7 @@ ResizePartitionJob::exec() m_partition->setLastSector( m_oldLastSector ); ResizeOperation op(*m_device, *m_partition, m_newFirstSector, m_newLastSector); op.setStatus(Operation::StatusRunning); - connect(&op, &Operation::progress, [&](int percent) { emit progress(percent / 100.0); } ); + connect(&op, &Operation::progress, this, &ResizePartitionJob::progress ); QString errorMessage = tr( "The installer failed to resize partition %1 on disk '%2'." ) .arg( m_partition->partitionPath() ) diff --git a/src/modules/partition/jobs/SetPartitionFlagsJob.cpp b/src/modules/partition/jobs/SetPartitionFlagsJob.cpp index cadfd0261..ecc8b2142 100644 --- a/src/modules/partition/jobs/SetPartitionFlagsJob.cpp +++ b/src/modules/partition/jobs/SetPartitionFlagsJob.cpp @@ -135,10 +135,7 @@ SetPartFlagsJob::exec() Report report ( nullptr ); SetPartFlagsOperation op( *m_device, *partition(), m_flags ); op.setStatus( Operation::StatusRunning ); - connect( &op, &Operation::progress, [&]( int percent ) - { - emit progress( percent / 100.0 ); - } ); + connect( &op, &Operation::progress, this, &SetPartFlagsJob::progress ); QString errorMessage = tr( "The installer failed to set flags on partition %1." ) .arg( m_partition->partitionPath() );