Partitions: split device-listing (and winnowing) into separate source file.

The grab-list-of-writable-devices code is usable for the partition module, but
also useful for welcome module, so reduce its footprint.
This commit is contained in:
Adriaan de Groot 2017-07-11 08:54:56 -04:00
parent e4862512f7
commit 102bed1805
6 changed files with 176 additions and 101 deletions

View file

@ -328,96 +328,4 @@ runOsprober( PartitionCoreModule* core )
return osproberEntries;
}
/**
* 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