diff --git a/src/modules/partition/CreatePartitionDialog.cpp b/src/modules/partition/CreatePartitionDialog.cpp index d908514a9..13697aa6e 100644 --- a/src/modules/partition/CreatePartitionDialog.cpp +++ b/src/modules/partition/CreatePartitionDialog.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -137,23 +138,14 @@ CreatePartitionDialog::createPartition() } } - FileSystem::Type type = m_role.has( PartitionRole::Extended ) - ? FileSystem::Extended - : FileSystem::typeForName( m_ui->fsComboBox->currentText() ); - FileSystem* fs = FileSystemFactory::create( type, m_minSector, lastSector ); - - auto partition = new Partition( + FileSystem::Type fsType = m_role.has( PartitionRole::Extended ) + ? FileSystem::Extended + : FileSystem::typeForName( m_ui->fsComboBox->currentText() ); + Partition* partition = PMUtils::createNewPartition( m_parent, *m_device, m_role, - fs, m_minSector, lastSector, - QString() /* path */, - PartitionTable::FlagNone /* availableFlags */, - QString() /* mountPoint */, - false /* mounted */, - PartitionTable::FlagNone /* activeFlags */, - Partition::StateNew - ); + fsType, m_minSector, lastSector ); PartitionInfo::setMountPoint( partition, m_ui->mountPointComboBox->currentText() ); PartitionInfo::setFormat( partition, true ); diff --git a/src/modules/partition/EditExistingPartitionDialog.cpp b/src/modules/partition/EditExistingPartitionDialog.cpp index 492f41088..bd1d01d2b 100644 --- a/src/modules/partition/EditExistingPartitionDialog.cpp +++ b/src/modules/partition/EditExistingPartitionDialog.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -62,8 +63,59 @@ void EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core ) { PartitionInfo::setMountPoint( m_partition, m_ui->mountPointComboBox->currentText() ); - if ( m_ui->formatRadioButton->isChecked() ) - core->formatPartition( m_device, m_partition ); + + qint64 oldSize = mbSizeForSectorRange( m_partition->firstSector(), m_partition->lastSector() ); + qint64 newSize = m_ui->sizeSpinBox->value(); + if ( oldSize == newSize ) + { + if ( m_ui->formatRadioButton->isChecked() ) + core->formatPartition( m_device, m_partition ); + else + core->refreshPartition( m_device, m_partition ); + } else - core->refreshPartition( m_device, m_partition ); + { + // FIXME: Duplicated from CreatePartitionDialog + qint64 maxSector = m_partition->lastSector() + m_device->partitionTable()->freeSectorsAfter( *m_partition ); + + qint64 lastSector; + int mbSize = m_ui->sizeSpinBox->value(); + if ( mbSize == m_ui->sizeSpinBox->maximum() ) + { + // If we are at the maximum value, select the last sector to avoid + // potential rounding errors which could leave a few sectors at the end + // unused + lastSector = maxSector; + } + else + { + lastSector = m_partition->firstSector() + qint64( mbSize ) * 1024 * 1024 / m_device->logicalSectorSize(); + Q_ASSERT( lastSector <= maxSector ); + if ( lastSector > maxSector ) + { + cDebug() << "lastSector (" << lastSector << ") > maxSector (" << maxSector << "). This should not happen!"; + lastSector = maxSector; + } + } + + if ( m_ui->formatRadioButton->isChecked() ) + { + Partition* newPartition = PMUtils::createNewPartition( + m_partition->parent(), + *m_device, + m_partition->roles(), + m_partition->fileSystem().type(), + m_partition->firstSector(), + lastSector); + PartitionInfo::setMountPoint( newPartition, PartitionInfo::mountPoint( m_partition ) ); + PartitionInfo::setFormat( newPartition, true ); + + core->deletePartition( m_device, m_partition ); + core->createPartition( m_device, newPartition ); + } + else + { + //core->resizePartition( m_device, m_partition ); + } + } } diff --git a/src/modules/partition/PMUtils.cpp b/src/modules/partition/PMUtils.cpp index 1f0997f14..5cc48ae55 100644 --- a/src/modules/partition/PMUtils.cpp +++ b/src/modules/partition/PMUtils.cpp @@ -23,7 +23,7 @@ // CalaPM #include -#include +#include namespace PMUtils { @@ -48,4 +48,22 @@ findPartitionByMountPoint( const QList< Device* >& devices, const QString& mount return nullptr; } +Partition* +createNewPartition( PartitionNode* parent, const Device& device, const PartitionRole& role, FileSystem::Type fsType, qint64 firstSector, qint64 lastSector ) +{ + FileSystem* fs = FileSystemFactory::create( fsType, firstSector, lastSector ); + return new Partition( + parent, + device, + role, + fs, fs->firstSector(), fs->lastSector(), + QString() /* path */, + PartitionTable::FlagNone /* availableFlags */, + QString() /* mountPoint */, + false /* mounted */, + PartitionTable::FlagNone /* activeFlags */, + Partition::StateNew + ); +} + } // namespace diff --git a/src/modules/partition/PMUtils.h b/src/modules/partition/PMUtils.h index 09112363d..23eed5708 100644 --- a/src/modules/partition/PMUtils.h +++ b/src/modules/partition/PMUtils.h @@ -18,11 +18,16 @@ #ifndef PMUTILS_H #define PMUTILS_H +// CalaPM +#include + // Qt #include class Device; class Partition; +class PartitionNode; +class PartitionRole; namespace PMUtils { @@ -33,6 +38,8 @@ bool isPartitionNew( Partition* ); Partition* findPartitionByMountPoint( const QList< Device* >& devices, const QString& mountPoint ); +Partition* createNewPartition( PartitionNode* parent, const Device& device, const PartitionRole& role, FileSystem::Type fsType, qint64 firstSector, qint64 lastSector ); + } #endif /* PMUTILS_H */