From ecdd09edcd1a46a8cd4190038e5b211877d87280 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 21 Jun 2019 21:17:43 +0200 Subject: [PATCH 1/5] [partition] Refactor getting partitions from device - Split out into own function - Prepare for implementation that doesn't use external processes --- src/modules/partition/jobs/ClearMountsJob.cpp | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/modules/partition/jobs/ClearMountsJob.cpp b/src/modules/partition/jobs/ClearMountsJob.cpp index 51ecd5d09..3c06fc04d 100644 --- a/src/modules/partition/jobs/ClearMountsJob.cpp +++ b/src/modules/partition/jobs/ClearMountsJob.cpp @@ -60,14 +60,9 @@ ClearMountsJob::prettyStatusMessage() const } -Calamares::JobResult -ClearMountsJob::exec() +QStringList +getPartitionsForDevice( const QString& deviceName ) { - CalamaresUtils::Partition::Syncer s; - QStringList goodNews; - - QString deviceName = m_device->deviceNode().split( '/' ).last(); - QProcess process; process.setProgram( "sh" ); process.setArguments( { @@ -81,6 +76,21 @@ ClearMountsJob::exec() const QString partitions = process.readAllStandardOutput(); const QStringList partitionsList = partitions.simplified().split( ' ' ); + return partitionsList; +} + +Calamares::JobResult +ClearMountsJob::exec() +{ + CalamaresUtils::Partition::Syncer s; + + QString deviceName = m_device->deviceNode().split( '/' ).last(); + + QStringList goodNews; + QProcess process; + + QStringList partitionsList = getPartitionsForDevice( deviceName ); + // Build a list of partitions of type 82 (Linux swap / Solaris). // We then need to clear them just in case they contain something resumable from a // previous suspend-to-disk. From b1ccd573f8aea145f8db02012c7f4ea0f1a4fc2e Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 24 Jun 2019 12:37:00 +0200 Subject: [PATCH 2/5] [partition] Code-formatting for tests --- .../partition/tests/PartitionJobTests.cpp | 117 ++++++++++-------- .../partition/tests/PartitionJobTests.h | 3 +- 2 files changed, 66 insertions(+), 54 deletions(-) diff --git a/src/modules/partition/tests/PartitionJobTests.cpp b/src/modules/partition/tests/PartitionJobTests.cpp index 2f30f0d8e..7684ad7a8 100644 --- a/src/modules/partition/tests/PartitionJobTests.cpp +++ b/src/modules/partition/tests/PartitionJobTests.cpp @@ -24,10 +24,10 @@ #include "utils/Logger.h" #include "utils/Units.h" +#include #include #include #include -#include // CalaPM #include @@ -59,15 +59,14 @@ public: ~PartitionMounter() { if ( !m_mounted ) + { return; + } int ret = QProcess::execute( "umount", QStringList() << m_mountPointDir.path() ); QCOMPARE( ret, 0 ); } - QString mountPoint() const - { - return m_mounted ? m_mountPointDir.path() : QString(); - } + QString mountPoint() const { return m_mounted ? m_mountPointDir.path() : QString(); } private: QString m_devicePath; @@ -80,9 +79,9 @@ static QByteArray generateTestData( qint64 size ) { QByteArray ba; - ba.resize( static_cast( size ) ); + ba.resize( static_cast< int >( size ) ); // Fill the array explicitly to keep Valgrind happy - for ( auto it = ba.data() ; it < ba.data() + size ; ++it ) + for ( auto it = ba.data(); it < ba.data() + size; ++it ) { *it = char( rand() & 0xff ); } @@ -105,9 +104,9 @@ writeFile( const QString& path, const QByteArray data ) if ( count < 0 ) { QString msg = QString( "Writing file failed. Only %1 bytes written out of %2. Error: '%3'." ) - .arg( ptr - data.constData() ) - .arg( data.size() ) - .arg( file.errorString() ); + .arg( ptr - data.constData() ) + .arg( data.size() ) + .arg( file.errorString() ); QFAIL( qPrintable( msg ) ); } ptr += count; @@ -117,9 +116,11 @@ writeFile( const QString& path, const QByteArray data ) static Partition* firstFreePartition( PartitionNode* parent ) { - for( auto child : parent->children() ) + for ( auto child : parent->children() ) if ( isPartitionFreeSpace( child ) ) + { return child; + } return nullptr; } @@ -146,7 +147,9 @@ QueueRunner::run() m_queue->start(); QEventLoop loop; while ( !m_finished ) + { loop.processEvents(); + } return m_success; } @@ -169,7 +172,8 @@ CalamaresUtils::Partition::KPMManager* kpmcore = nullptr; //- PartitionJobTests ------------------------------------------------------------------ PartitionJobTests::PartitionJobTests() : m_runner( &m_queue ) -{} +{ +} void PartitionJobTests::initTestCase() @@ -178,7 +182,8 @@ PartitionJobTests::initTestCase() if ( devicePath.isEmpty() ) { // The 0 is to keep the macro parameters happy - QSKIP( "Skipping test, CALAMARES_TEST_DISK is not set. It should point to a disk which can be safely formatted", 0 ); + QSKIP( "Skipping test, CALAMARES_TEST_DISK is not set. It should point to a disk which can be safely formatted", + 0 ); } kpmcore = new CalamaresUtils::Partition::KPMManager(); @@ -216,7 +221,7 @@ PartitionJobTests::testPartitionTable() } void -PartitionJobTests::queuePartitionTableCreation( PartitionTable::TableType type) +PartitionJobTests::queuePartitionTableCreation( PartitionTable::TableType type ) { auto job = new CreatePartitionTableJob( m_device.data(), type ); job->updatePreview(); @@ -224,7 +229,10 @@ PartitionJobTests::queuePartitionTableCreation( PartitionTable::TableType type) } CreatePartitionJob* -PartitionJobTests::newCreatePartitionJob( Partition* freeSpacePartition, PartitionRole role, FileSystem::Type type, qint64 size ) +PartitionJobTests::newCreatePartitionJob( Partition* freeSpacePartition, + PartitionRole role, + FileSystem::Type type, + qint64 size ) { Q_ASSERT( freeSpacePartition ); @@ -232,25 +240,27 @@ PartitionJobTests::newCreatePartitionJob( Partition* freeSpacePartition, Partiti qint64 lastSector; if ( size > 0 ) + { lastSector = firstSector + size / m_device->logicalSize(); + } else + { lastSector = freeSpacePartition->lastSector(); - FileSystem* fs = FileSystemFactory::create( type, firstSector, lastSector - ,m_device->logicalSize() - ); + } + FileSystem* fs = FileSystemFactory::create( type, firstSector, lastSector, m_device->logicalSize() ); - Partition* partition = new Partition( - freeSpacePartition->parent(), - *m_device, - role, - fs, firstSector, lastSector, - QString() /* path */, - KPM_PARTITION_FLAG(None) /* availableFlags */, - QString() /* mountPoint */, - false /* mounted */, - KPM_PARTITION_FLAG(None) /* activeFlags */, - KPM_PARTITION_STATE(New) - ); + Partition* partition = new Partition( freeSpacePartition->parent(), + *m_device, + role, + fs, + firstSector, + lastSector, + QString() /* path */, + KPM_PARTITION_FLAG( None ) /* availableFlags */, + QString() /* mountPoint */, + false /* mounted */, + KPM_PARTITION_FLAG( None ) /* activeFlags */, + KPM_PARTITION_STATE( New ) ); return new CreatePartitionJob( m_device.data(), partition ); } @@ -263,7 +273,7 @@ PartitionJobTests::testCreatePartition() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 1_MiB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 1_MiB ); Partition* partition1 = job->partition(); QVERIFY( partition1 ); job->updatePreview(); @@ -271,7 +281,7 @@ PartitionJobTests::testCreatePartition() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::LinuxSwap, 1_MiB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::LinuxSwap, 1_MiB ); Partition* partition2 = job->partition(); QVERIFY( partition2 ); job->updatePreview(); @@ -279,7 +289,7 @@ PartitionJobTests::testCreatePartition() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Fat32, 1_MiB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Fat32, 1_MiB ); Partition* partition3 = job->partition(); QVERIFY( partition3 ); job->updatePreview(); @@ -304,7 +314,7 @@ PartitionJobTests::testCreatePartitionExtended() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 10_MiB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 10_MiB ); Partition* partition1 = job->partition(); QVERIFY( partition1 ); job->updatePreview(); @@ -312,14 +322,15 @@ PartitionJobTests::testCreatePartitionExtended() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Extended ), FileSystem::Extended, 10_MiB); + job = newCreatePartitionJob( + freePartition, PartitionRole( PartitionRole::Extended ), FileSystem::Extended, 10_MiB ); job->updatePreview(); m_queue.enqueue( job_ptr( job ) ); Partition* extendedPartition = job->partition(); freePartition = firstFreePartition( extendedPartition ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Logical ), FileSystem::Ext4, 0); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Logical ), FileSystem::Ext4, 0 ); Partition* partition2 = job->partition(); QVERIFY( partition2 ); job->updatePreview(); @@ -343,10 +354,10 @@ PartitionJobTests::testResizePartition_data() QTest::addColumn< unsigned int >( "newStartMiB" ); QTest::addColumn< unsigned int >( "newSizeMiB" ); - QTest::newRow("grow") << 10 << 50 << 10 << 70; - QTest::newRow("shrink") << 10 << 70 << 10 << 50; - QTest::newRow("moveLeft") << 10 << 50 << 8 << 50; - QTest::newRow("moveRight") << 10 << 50 << 12 << 50; + QTest::newRow( "grow" ) << 10 << 50 << 10 << 70; + QTest::newRow( "shrink" ) << 10 << 70 << 10 << 50; + QTest::newRow( "moveLeft" ) << 10 << 50 << 8 << 50; + QTest::newRow( "moveRight" ) << 10 << 50 << 12 << 50; } void @@ -360,9 +371,9 @@ PartitionJobTests::testResizePartition() const qint64 sectorsPerMiB = 1_MiB / m_device->logicalSize(); qint64 oldFirst = sectorsPerMiB * oldStartMiB; - qint64 oldLast = oldFirst + sectorsPerMiB * oldSizeMiB - 1; + qint64 oldLast = oldFirst + sectorsPerMiB * oldSizeMiB - 1; qint64 newFirst = sectorsPerMiB * newStartMiB; - qint64 newLast = newFirst + sectorsPerMiB * newSizeMiB - 1; + qint64 newLast = newFirst + sectorsPerMiB * newSizeMiB - 1; // Make the test data file smaller than the full size of the partition to // accomodate for the file system overhead @@ -376,15 +387,13 @@ PartitionJobTests::testResizePartition() Partition* freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - Partition* partition = KPMHelpers::createNewPartition( - freePartition->parent(), - *m_device, - PartitionRole( PartitionRole::Primary ), - FileSystem::Ext4, - oldFirst, - oldLast, - KPM_PARTITION_FLAG(None) - ); + Partition* partition = KPMHelpers::createNewPartition( freePartition->parent(), + *m_device, + PartitionRole( PartitionRole::Primary ), + FileSystem::Ext4, + oldFirst, + oldLast, + KPM_PARTITION_FLAG( None ) ); CreatePartitionJob* job = new CreatePartitionJob( m_device.data(), partition ); job->updatePreview(); m_queue.enqueue( job_ptr( job ) ); @@ -396,7 +405,8 @@ PartitionJobTests::testResizePartition() // Write a test file in the partition refreshDevice(); QVERIFY( m_device->partitionTable() ); - Partition* partition = m_device->partitionTable()->findPartitionBySector( oldFirst, PartitionRole( PartitionRole::Primary ) ); + Partition* partition + = m_device->partitionTable()->findPartitionBySector( oldFirst, PartitionRole( PartitionRole::Primary ) ); QVERIFY( partition ); QCOMPARE( partition->firstSector(), oldFirst ); QCOMPARE( partition->lastSector(), oldLast ); @@ -421,7 +431,8 @@ PartitionJobTests::testResizePartition() { refreshDevice(); QVERIFY( m_device->partitionTable() ); - Partition* partition = m_device->partitionTable()->findPartitionBySector( newFirst, PartitionRole( PartitionRole::Primary ) ); + Partition* partition + = m_device->partitionTable()->findPartitionBySector( newFirst, PartitionRole( PartitionRole::Primary ) ); QVERIFY( partition ); QCOMPARE( partition->firstSector(), newFirst ); QCOMPARE( partition->lastSector(), newLast ); diff --git a/src/modules/partition/tests/PartitionJobTests.h b/src/modules/partition/tests/PartitionJobTests.h index bfa1d6707..1aad945e5 100644 --- a/src/modules/partition/tests/PartitionJobTests.h +++ b/src/modules/partition/tests/PartitionJobTests.h @@ -72,7 +72,8 @@ private: QueueRunner m_runner; void queuePartitionTableCreation( PartitionTable::TableType type ); - CreatePartitionJob* newCreatePartitionJob( Partition* freeSpacePartition, PartitionRole, FileSystem::Type type, qint64 size ); + CreatePartitionJob* + newCreatePartitionJob( Partition* freeSpacePartition, PartitionRole, FileSystem::Type type, qint64 size ); void refreshDevice(); }; From d16c75b15cce35684eb19e2d5ef5d157a8810259 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 24 Jun 2019 12:47:14 +0200 Subject: [PATCH 3/5] [partition] Expand tests to ClearMountsJob - This is a tiny bit of TDD to replace the existing implementation (a shell pipeline) with processing inside Calamares. - The test fails right now, since the implementations are not the same. --- src/modules/partition/tests/CMakeLists.txt | 41 ++++++++++---- .../partition/tests/ClearMountsJobTests.cpp | 55 +++++++++++++++++++ .../partition/tests/ClearMountsJobTests.h | 34 ++++++++++++ 3 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 src/modules/partition/tests/ClearMountsJobTests.cpp create mode 100644 src/modules/partition/tests/ClearMountsJobTests.h diff --git a/src/modules/partition/tests/CMakeLists.txt b/src/modules/partition/tests/CMakeLists.txt index 8ebc14754..804dd9277 100644 --- a/src/modules/partition/tests/CMakeLists.txt +++ b/src/modules/partition/tests/CMakeLists.txt @@ -1,17 +1,9 @@ find_package( Qt5 COMPONENTS Gui REQUIRED ) +# Roundabout way of saying .. set( PartitionModule_SOURCE_DIR .. ) - -set( partitionjobtests_SRCS - ${PartitionModule_SOURCE_DIR}/core/KPMHelpers.cpp - ${PartitionModule_SOURCE_DIR}/core/PartitionInfo.cpp - ${PartitionModule_SOURCE_DIR}/jobs/CreatePartitionJob.cpp - ${PartitionModule_SOURCE_DIR}/jobs/CreatePartitionTableJob.cpp - ${PartitionModule_SOURCE_DIR}/jobs/DeletePartitionJob.cpp - ${PartitionModule_SOURCE_DIR}/jobs/PartitionJob.cpp - ${PartitionModule_SOURCE_DIR}/jobs/ResizePartitionJob.cpp - PartitionJobTests.cpp -) +# This is set by parent CMakeLists.txt +# set( _partition_defs ) include_directories( ${Qt5Gui_INCLUDE_DIRS} @@ -21,7 +13,16 @@ include_directories( ) if( ECM_FOUND AND BUILD_TESTING ) - ecm_add_test( ${partitionjobtests_SRCS} + ecm_add_test( + ${PartitionModule_SOURCE_DIR}/core/KPMHelpers.cpp + ${PartitionModule_SOURCE_DIR}/core/PartitionInfo.cpp + ${PartitionModule_SOURCE_DIR}/jobs/CreatePartitionJob.cpp + ${PartitionModule_SOURCE_DIR}/jobs/CreatePartitionTableJob.cpp + ${PartitionModule_SOURCE_DIR}/jobs/DeletePartitionJob.cpp + ${PartitionModule_SOURCE_DIR}/jobs/PartitionJob.cpp + ${PartitionModule_SOURCE_DIR}/jobs/ResizePartitionJob.cpp + PartitionJobTests.cpp + TEST_NAME partitionjobtests LINK_LIBRARIES ${CALAMARES_LIBRARIES} @@ -32,4 +33,20 @@ if( ECM_FOUND AND BUILD_TESTING ) set_target_properties( partitionjobtests PROPERTIES AUTOMOC TRUE ) target_compile_definitions( partitionjobtests PRIVATE ${_partition_defs} ) + + ecm_add_test( + ${PartitionModule_SOURCE_DIR}/jobs/ClearMountsJob.cpp + ClearMountsJobTests.cpp + + TEST_NAME clearmountsjobtests + LINK_LIBRARIES + ${CALAMARES_LIBRARIES} + kpmcore + Qt5::Core + Qt5::Test + ) + + set_target_properties( clearmountsjobtests PROPERTIES AUTOMOC TRUE ) + target_compile_definitions( clearmountsjobtests PRIVATE ${_partition_defs} ) + endif() diff --git a/src/modules/partition/tests/ClearMountsJobTests.cpp b/src/modules/partition/tests/ClearMountsJobTests.cpp new file mode 100644 index 000000000..4382d09d8 --- /dev/null +++ b/src/modules/partition/tests/ClearMountsJobTests.cpp @@ -0,0 +1,55 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "ClearMountsJobTests.h" + +#include "utils/Logger.h" + +#include + +QTEST_GUILESS_MAIN( ClearMountsJobTests ) + + +/* Not exactly public API */ +QStringList +getPartitionsForDevice( const QString& deviceName ); + +QStringList +getPartitionsForDevice_other(const QString& deviceName) +{ + QStringList partitions; + + return partitions; +} + + +ClearMountsJobTests::ClearMountsJobTests() +{ + Logger::setupLogLevel(6); +} + +void ClearMountsJobTests::testFindPartitions() +{ + QStringList partitions = getPartitionsForDevice( "sda" ); + QStringList other_part = getPartitionsForDevice_other( "sda" ); + + cDebug() << "Initial implementation:" << Logger::DebugList( partitions ); + cDebug() << "Other implementation:" << Logger::DebugList( other_part ); + + QCOMPARE( partitions, other_part ); +} diff --git a/src/modules/partition/tests/ClearMountsJobTests.h b/src/modules/partition/tests/ClearMountsJobTests.h new file mode 100644 index 000000000..0cc2b5c78 --- /dev/null +++ b/src/modules/partition/tests/ClearMountsJobTests.h @@ -0,0 +1,34 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef CLEARMOUNTSJOBTESTS_H +#define CLEARMOUNTSJOBTESTS_H + +#include + +class ClearMountsJobTests : public QObject +{ + Q_OBJECT +public: + ClearMountsJobTests(); + +private Q_SLOTS: + void testFindPartitions(); +}; + +#endif From f2438a5bf458d85d47df1857773be4c31bdebbbe Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 24 Jun 2019 13:07:19 +0200 Subject: [PATCH 4/5] [partition] Implement other way of getting partitions - Just read /proc/partitions and process it; split into columns, add relevant bits. - This implementation supports devices named "name", which the other didn't (but that would be really weird). The tests now pass. --- .../partition/tests/ClearMountsJobTests.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/modules/partition/tests/ClearMountsJobTests.cpp b/src/modules/partition/tests/ClearMountsJobTests.cpp index 4382d09d8..c74c1f25b 100644 --- a/src/modules/partition/tests/ClearMountsJobTests.cpp +++ b/src/modules/partition/tests/ClearMountsJobTests.cpp @@ -34,6 +34,28 @@ getPartitionsForDevice_other(const QString& deviceName) { QStringList partitions; + QFile dev_partitions( "/proc/partitions" ); + if ( dev_partitions.open( QFile::ReadOnly ) ) + { + cDebug() << "Reading from" << dev_partitions.fileName(); + QTextStream in( &dev_partitions ); + (void) in.readLine(); // That's the header line, skip it + while ( !in.atEnd() ) + { + // The fourth column (index from 0, so index 3) is the name of the device; + // keep it if it is followed by something. + QStringList columns = in.readLine().split( ' ', QString::SkipEmptyParts ); + if ( ( columns.count() >= 4 ) && ( columns[3].startsWith( deviceName ) ) && ( columns[3] != deviceName ) ) + { + partitions.append( columns[3] ); + } + } + } + else + { + cDebug() << "Could not open" << dev_partitions.fileName(); + } + return partitions; } From 399919c49a6e515b3e243c5d71d9650608f6e0e9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 24 Jun 2019 13:10:45 +0200 Subject: [PATCH 5/5] [partition] Swap the two implementations of getting partitions --- src/modules/partition/jobs/ClearMountsJob.cpp | 35 ++++++++++++------- .../partition/tests/ClearMountsJobTests.cpp | 35 +++++++------------ 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/modules/partition/jobs/ClearMountsJob.cpp b/src/modules/partition/jobs/ClearMountsJob.cpp index 3c06fc04d..2678a70a3 100644 --- a/src/modules/partition/jobs/ClearMountsJob.cpp +++ b/src/modules/partition/jobs/ClearMountsJob.cpp @@ -63,20 +63,31 @@ ClearMountsJob::prettyStatusMessage() const QStringList getPartitionsForDevice( const QString& deviceName ) { - QProcess process; - process.setProgram( "sh" ); - process.setArguments( { - "-c", - QString( "echo $(awk '{print $4}' /proc/partitions | sed -e '/name/d' -e '/^$/d' -e '/[1-9]/!d' | grep %1)" ) - .arg( deviceName ) - } ); - process.start(); - process.waitForFinished(); + QStringList partitions; - const QString partitions = process.readAllStandardOutput(); - const QStringList partitionsList = partitions.simplified().split( ' ' ); + QFile dev_partitions( "/proc/partitions" ); + if ( dev_partitions.open( QFile::ReadOnly ) ) + { + cDebug() << "Reading from" << dev_partitions.fileName(); + QTextStream in( &dev_partitions ); + (void) in.readLine(); // That's the header line, skip it + while ( !in.atEnd() ) + { + // The fourth column (index from 0, so index 3) is the name of the device; + // keep it if it is followed by something. + QStringList columns = in.readLine().split( ' ', QString::SkipEmptyParts ); + if ( ( columns.count() >= 4 ) && ( columns[3].startsWith( deviceName ) ) && ( columns[3] != deviceName ) ) + { + partitions.append( columns[3] ); + } + } + } + else + { + cDebug() << "Could not open" << dev_partitions.fileName(); + } - return partitionsList; + return partitions; } Calamares::JobResult diff --git a/src/modules/partition/tests/ClearMountsJobTests.cpp b/src/modules/partition/tests/ClearMountsJobTests.cpp index c74c1f25b..1f01c4638 100644 --- a/src/modules/partition/tests/ClearMountsJobTests.cpp +++ b/src/modules/partition/tests/ClearMountsJobTests.cpp @@ -32,31 +32,20 @@ getPartitionsForDevice( const QString& deviceName ); QStringList getPartitionsForDevice_other(const QString& deviceName) { - QStringList partitions; + QProcess process; + process.setProgram( "sh" ); + process.setArguments( { + "-c", + QString( "echo $(awk '{print $4}' /proc/partitions | sed -e '/name/d' -e '/^$/d' -e '/[1-9]/!d' | grep %1)" ) + .arg( deviceName ) + } ); + process.start(); + process.waitForFinished(); - QFile dev_partitions( "/proc/partitions" ); - if ( dev_partitions.open( QFile::ReadOnly ) ) - { - cDebug() << "Reading from" << dev_partitions.fileName(); - QTextStream in( &dev_partitions ); - (void) in.readLine(); // That's the header line, skip it - while ( !in.atEnd() ) - { - // The fourth column (index from 0, so index 3) is the name of the device; - // keep it if it is followed by something. - QStringList columns = in.readLine().split( ' ', QString::SkipEmptyParts ); - if ( ( columns.count() >= 4 ) && ( columns[3].startsWith( deviceName ) ) && ( columns[3] != deviceName ) ) - { - partitions.append( columns[3] ); - } - } - } - else - { - cDebug() << "Could not open" << dev_partitions.fileName(); - } + const QString partitions = process.readAllStandardOutput(); + const QStringList partitionsList = partitions.simplified().split( ' ' ); - return partitions; + return partitionsList; }