From 9e1c95ad056f4794b7e73bd4b9e09d7dad79bd38 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 27 Sep 2018 04:10:25 -0400 Subject: [PATCH] [fsresizer] Refactor finding device - Find device in separate function - If device isn't found, bail out --- src/modules/fsresizer/ResizeFSJob.cpp | 56 +++++++++++++++------------ src/modules/fsresizer/ResizeFSJob.h | 9 +++++ 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/modules/fsresizer/ResizeFSJob.cpp b/src/modules/fsresizer/ResizeFSJob.cpp index cf2ea47f6..68fd166e7 100644 --- a/src/modules/fsresizer/ResizeFSJob.cpp +++ b/src/modules/fsresizer/ResizeFSJob.cpp @@ -88,6 +88,32 @@ ResizeFSJob::prettyName() const return tr( "Resize Filesystem Job" ); } +ResizeFSJob::PartitionMatch +ResizeFSJob::findPartition( CoreBackend* backend ) +{ + using DeviceList = QList< Device* >; + DeviceList devices = backend->scanDevices( false ); + cDebug() << "ResizeFSJob found" << devices.count() << "devices."; + for ( DeviceList::iterator dev_it = devices.begin(); dev_it != devices.end(); ++dev_it ) + { + if ( ! (*dev_it) ) + continue; + cDebug() << "ResizeFSJob found" << ( *dev_it )->deviceNode(); + for ( auto part_it = PartitionIterator::begin( *dev_it); part_it != PartitionIterator::end( *dev_it ); ++part_it ) + { + cDebug() << ".." << ( *part_it )->mountPoint() << "on" << ( *part_it )->deviceNode(); + if ( ( !m_fsname.isEmpty() && ( *part_it )->mountPoint() == m_fsname ) || + ( !m_devicename.isEmpty() && ( *part_it )->deviceNode() == m_devicename ) ) + { + cDebug() << ".. matched configuration dev=" << m_devicename << "fs=" << m_fsname; + return PartitionMatch( *dev_it, *part_it ); + } + } + } + + cDebug() << "No match for configuration dev=" << m_devicename << "fs=" << m_fsname; + return PartitionMatch( nullptr, nullptr ); +} Calamares::JobResult ResizeFSJob::exec() @@ -124,30 +150,12 @@ ResizeFSJob::exec() tr( "Calamares cannot start KPMCore for the file-system resize job." ) ); } - Device* resize_this_device = nullptr; - Partition* resize_this_partition = nullptr; - - using DeviceList = QList< Device* >; - DeviceList devices = backend_p->scanDevices( false ); - cDebug() << "ResizeFSJob found" << devices.count() << "devices."; - for ( DeviceList::iterator dev_it = devices.begin(); dev_it != devices.end(); ++dev_it ) - { - if ( ! (*dev_it) ) - continue; - cDebug() << "ResizeFSJob found" << ( *dev_it )->deviceNode(); - for ( auto part_it = PartitionIterator::begin( *dev_it); part_it != PartitionIterator::end( *dev_it ); ++part_it ) - { - cDebug() << ".." << ( *part_it )->mountPoint() << "on" << ( *part_it )->deviceNode(); - if ( ( !m_fsname.isEmpty() && ( *part_it )->mountPoint() == m_fsname ) || - ( !m_devicename.isEmpty() && ( *part_it )->deviceNode() == m_devicename ) ) - { - resize_this_device = ( *dev_it ); - resize_this_partition = ( *part_it ); - cDebug() << ".. matched configuration dev=" << m_devicename << "fs=" << m_fsname; - break; - } - } - } + PartitionMatch m = findPartition( backend_p ); + if ( !m.first || !m.second ) + return Calamares::JobResult::error( + tr( "Resize Failed" ), + m_fsname.isEmpty() ? tr( "The filesystem %1 could not be found in this system, and can not be resized." ).arg(m_fsname) + : tr( "The device %1 could not be found in this system, and can not be resized." ).arg(m_devicename) ); return Calamares::JobResult::ok(); } diff --git a/src/modules/fsresizer/ResizeFSJob.h b/src/modules/fsresizer/ResizeFSJob.h index 85ed98cdc..72b28e690 100644 --- a/src/modules/fsresizer/ResizeFSJob.h +++ b/src/modules/fsresizer/ResizeFSJob.h @@ -28,6 +28,10 @@ #include +class CoreBackend; // From KPMCore +class Device; // From KPMCore +class Partition; + class PLUGINDLLEXPORT ResizeFSJob : public Calamares::CppJob { Q_OBJECT @@ -74,6 +78,7 @@ public: void setConfigurationMap( const QVariantMap& configurationMap ) override; + /** @brief Is the configuration of this job valid? */ bool isValid() const { return ( !m_fsname.isEmpty() || !m_devicename.isEmpty() ) && @@ -85,6 +90,10 @@ private: RelativeSize m_atleast; QString m_fsname; // Either this, or devicename, is set, not both QString m_devicename; + + using PartitionMatch = QPair; + /** @brief Find the configured FS using KPMCore @p backend */ + PartitionMatch findPartition( CoreBackend* backend ); }; CALAMARES_PLUGIN_FACTORY_DECLARATION( ResizeFSJobFactory )