mirror of
https://github.com/parchlinux/calamares.git
synced 2025-03-01 05:15:44 -05:00
Merge branch 'refactor-getpartitions' into kpmcore-manager
This commit is contained in:
commit
a991577779
6 changed files with 227 additions and 77 deletions
|
@ -60,26 +60,47 @@ ClearMountsJob::prettyStatusMessage() const
|
|||
}
|
||||
|
||||
|
||||
QStringList
|
||||
getPartitionsForDevice( 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;
|
||||
}
|
||||
|
||||
Calamares::JobResult
|
||||
ClearMountsJob::exec()
|
||||
{
|
||||
CalamaresUtils::Partition::Syncer s;
|
||||
QStringList goodNews;
|
||||
|
||||
QString deviceName = m_device->deviceNode().split( '/' ).last();
|
||||
|
||||
QStringList goodNews;
|
||||
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();
|
||||
|
||||
const QString partitions = process.readAllStandardOutput();
|
||||
const QStringList partitionsList = partitions.simplified().split( ' ' );
|
||||
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
|
||||
|
|
|
@ -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()
|
||||
|
|
66
src/modules/partition/tests/ClearMountsJobTests.cpp
Normal file
66
src/modules/partition/tests/ClearMountsJobTests.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ClearMountsJobTests.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
QTEST_GUILESS_MAIN( ClearMountsJobTests )
|
||||
|
||||
|
||||
/* Not exactly public API */
|
||||
QStringList
|
||||
getPartitionsForDevice( const QString& deviceName );
|
||||
|
||||
QStringList
|
||||
getPartitionsForDevice_other(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();
|
||||
|
||||
const QString partitions = process.readAllStandardOutput();
|
||||
const QStringList partitionsList = partitions.simplified().split( ' ' );
|
||||
|
||||
return partitionsList;
|
||||
}
|
||||
|
||||
|
||||
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 );
|
||||
}
|
34
src/modules/partition/tests/ClearMountsJobTests.h
Normal file
34
src/modules/partition/tests/ClearMountsJobTests.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CLEARMOUNTSJOBTESTS_H
|
||||
#define CLEARMOUNTSJOBTESTS_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class ClearMountsJobTests : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ClearMountsJobTests();
|
||||
|
||||
private Q_SLOTS:
|
||||
void testFindPartitions();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -24,10 +24,10 @@
|
|||
#include "utils/Logger.h"
|
||||
#include "utils/Units.h"
|
||||
|
||||
#include <core/KPMHelpers.h>
|
||||
#include <jobs/CreatePartitionJob.h>
|
||||
#include <jobs/CreatePartitionTableJob.h>
|
||||
#include <jobs/ResizePartitionJob.h>
|
||||
#include <core/KPMHelpers.h>
|
||||
|
||||
// CalaPM
|
||||
#include <backend/corebackend.h>
|
||||
|
@ -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;
|
||||
|
@ -119,7 +118,9 @@ firstFreePartition( PartitionNode* parent )
|
|||
{
|
||||
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();
|
||||
|
@ -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(),
|
||||
Partition* partition = new Partition( freeSpacePartition->parent(),
|
||||
*m_device,
|
||||
role,
|
||||
fs, firstSector, lastSector,
|
||||
fs,
|
||||
firstSector,
|
||||
lastSector,
|
||||
QString() /* path */,
|
||||
KPM_PARTITION_FLAG( None ) /* availableFlags */,
|
||||
QString() /* mountPoint */,
|
||||
false /* mounted */,
|
||||
KPM_PARTITION_FLAG( None ) /* activeFlags */,
|
||||
KPM_PARTITION_STATE(New)
|
||||
);
|
||||
KPM_PARTITION_STATE( New ) );
|
||||
return new CreatePartitionJob( m_device.data(), partition );
|
||||
}
|
||||
|
||||
|
@ -312,7 +322,8 @@ 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();
|
||||
|
@ -376,15 +387,13 @@ PartitionJobTests::testResizePartition()
|
|||
|
||||
Partition* freePartition = firstFreePartition( m_device->partitionTable() );
|
||||
QVERIFY( freePartition );
|
||||
Partition* partition = KPMHelpers::createNewPartition(
|
||||
freePartition->parent(),
|
||||
Partition* partition = KPMHelpers::createNewPartition( freePartition->parent(),
|
||||
*m_device,
|
||||
PartitionRole( PartitionRole::Primary ),
|
||||
FileSystem::Ext4,
|
||||
oldFirst,
|
||||
oldLast,
|
||||
KPM_PARTITION_FLAG(None)
|
||||
);
|
||||
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 );
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue