Partitions: shuffle some device-detection code off to PartUtils.cpp

This commit is contained in:
Adriaan de Groot 2017-07-11 06:07:15 -04:00 committed by Philip
parent 1b98974f07
commit f33aba0d5b
3 changed files with 105 additions and 76 deletions

View File

@ -22,7 +22,11 @@
#include "core/DeviceModel.h" #include "core/DeviceModel.h"
#include "core/KPMHelpers.h" #include "core/KPMHelpers.h"
#include "core/PartitionIterator.h"
#include <kpmcore/backend/corebackend.h>
#include <kpmcore/backend/corebackendmanager.h>
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h> #include <kpmcore/core/partition.h>
#include <utils/Logger.h> #include <utils/Logger.h>
@ -325,4 +329,95 @@ runOsprober( PartitionCoreModule* core )
} }
/**
* Does the given @p device contain the root filesystem? This is true if
* the device contains a partition which is currently mounted at / .
*/
static bool
hasRootPartition( Device* device )
{
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( ( *it )->mountPoint() == "/" )
return true;
return false;
} }
static bool
isMounted( Device* device )
{
cDebug() << "Checking for mounted partitions in" << device->deviceNode();
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
{
cDebug() << " .." << ( *it )->partitionPath() << "mount" << ( *it )->mountPoint();
if ( ! ( *it )->mountPoint().isEmpty() )
return true;
}
return false;
}
static bool
isIso9660( const Device* device )
{
QString path = device->deviceNode();
if ( path.isEmpty() )
return false;
QProcess blkid;
blkid.start( "blkid", { path } );
blkid.waitForFinished();
QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() );
if ( output.contains( "iso9660" ) )
return true;
if ( device->partitionTable() &&
!device->partitionTable()->children().isEmpty() )
{
for ( const Partition* partition : device->partitionTable()->children() )
{
path = partition->partitionPath();
blkid.start( "blkid", { path } );
blkid.waitForFinished();
QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() );
if ( output.contains( "iso9660" ) )
return true;
}
}
return false;
}
QList< Device* > getDevices( bool writableOnly )
{
using DeviceList = QList< Device* >;
CoreBackend* backend = CoreBackendManager::self()->backend();
DeviceList devices = backend->scanDevices( true );
cDebug() << "Winnowing" << devices.count() << "devices.";
// Remove the device which contains / from the list
for ( DeviceList::iterator it = devices.begin(); it != devices.end(); )
if ( ! ( *it ) ||
( *it )->deviceNode().startsWith( "/dev/zram" )
)
{
cDebug() << " .. Winnowing" << ( ( *it ) ? ( *it )->deviceNode() : QString( "<null device>" ) );
it = devices.erase( it );
}
else if ( writableOnly && (
hasRootPartition( *it ) ||
isIso9660( *it ) ||
isMounted( *it ) )
)
{
cDebug() << " .. Winnowing" << ( ( *it ) ? ( *it )->deviceNode() : QString( "<null device>" ) );
it = devices.erase( it );
}
else
++it;
return devices;
}
} // nmamespace PartUtils

View File

@ -23,6 +23,7 @@
#include <QString> #include <QString>
class Device;
class PartitionCoreModule; class PartitionCoreModule;
class Partition; class Partition;
@ -62,6 +63,14 @@ bool canBeResized( PartitionCoreModule* core, const QString& partitionPath );
*/ */
OsproberEntryList runOsprober( PartitionCoreModule* core ); OsproberEntryList runOsprober( PartitionCoreModule* core );
/**
* @brief Gets a list of storage devices.
* @param writableOnly if set to true, only devices which can be overwritten
* safely are returned (e.g. RO-media are ignored, as are mounted partitions).
* @return a list of Devices meeting this criterium.
*/
QList< Device* > getDevices( bool writableOnly = false );
} }
#endif // PARTUTILS_H #endif // PARTUTILS_H

View File

@ -55,62 +55,6 @@
#include <QFutureWatcher> #include <QFutureWatcher>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
/**
* Does the given @p device contain the root filesystem? This is true if
* the device contains a partition which is currently mounted at / .
*/
static bool
hasRootPartition( Device* device )
{
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( ( *it )->mountPoint() == "/" )
return true;
return false;
}
static bool
isMounted( Device* device )
{
cDebug() << "Checking for mounted partitions in" << device->deviceNode();
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
{
cDebug() << " .." << ( *it )->partitionPath() << "mount" << ( *it )->mountPoint();
if ( ! ( *it )->mountPoint().isEmpty() )
return true;
}
return false;
}
static bool
isIso9660( const Device* device )
{
QString path = device->deviceNode();
if ( path.isEmpty() )
return false;
QProcess blkid;
blkid.start( "blkid", { path } );
blkid.waitForFinished();
QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() );
if ( output.contains( "iso9660" ) )
return true;
if ( device->partitionTable() &&
!device->partitionTable()->children().isEmpty() )
{
for ( const Partition* partition : device->partitionTable()->children() )
{
path = partition->partitionPath();
blkid.start( "blkid", { path } );
blkid.waitForFinished();
QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() );
if ( output.contains( "iso9660" ) )
return true;
}
}
return false;
}
//- DeviceInfo --------------------------------------------- //- DeviceInfo ---------------------------------------------
PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device ) PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device )
: device( _device ) : device( _device )
@ -171,26 +115,7 @@ PartitionCoreModule::doInit()
FileSystemFactory::init(); FileSystemFactory::init();
using DeviceList = QList< Device* >; using DeviceList = QList< Device* >;
DeviceList devices = PartUtils::getDevices( true );
CoreBackend* backend = CoreBackendManager::self()->backend();
DeviceList devices = backend->scanDevices( true );
cDebug() << "Winnowing" << devices.count() << "devices.";
// Remove the device which contains / from the list
for ( DeviceList::iterator it = devices.begin(); it != devices.end(); )
if ( ! ( *it ) ||
hasRootPartition( *it ) ||
isIso9660( *it ) ||
isMounted( *it ) ||
( *it )->deviceNode().startsWith( "/dev/zram" )
)
{
cDebug() << " .. Winnowing" << ( ( *it ) ? ( *it )->deviceNode() : QString( "<null device>" ) );
it = devices.erase( it );
}
else
++it;
cDebug() << "LIST OF DETECTED DEVICES:"; cDebug() << "LIST OF DETECTED DEVICES:";
cDebug() << "node\tcapacity\tname\tprettyName"; cDebug() << "node\tcapacity\tname\tprettyName";