Add a test for KPMcore device scanner.
This commit is contained in:
parent
ffca7c7854
commit
2d7a27e066
|
@ -42,3 +42,6 @@ set(BACKEND $<TARGET_FILE:pmlibpartedbackendplugin>)
|
||||||
# Listing devices, partitions
|
# Listing devices, partitions
|
||||||
kpm_test(testlist testlist.cpp)
|
kpm_test(testlist testlist.cpp)
|
||||||
add_test(NAME testlist COMMAND testlist ${BACKEND})
|
add_test(NAME testlist COMMAND testlist ${BACKEND})
|
||||||
|
|
||||||
|
kpm_test(testdevicescanner testdevicescanner.cpp)
|
||||||
|
add_test(NAME testdevicescanner COMMAND testdevicescanner ${BACKEND})
|
||||||
|
|
|
@ -0,0 +1,115 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* Copyright 2017 by Adriaan de Groot <groot@kde.org> *
|
||||||
|
* Copyright 2017 by Andrius Štikonas <andrius@stikonas.eu> *
|
||||||
|
* *
|
||||||
|
* This program 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. *
|
||||||
|
* *
|
||||||
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.*
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
// SPDX-License-Identifier: GPL-3.0+
|
||||||
|
|
||||||
|
// Lists devices
|
||||||
|
|
||||||
|
#include "helpers.h"
|
||||||
|
|
||||||
|
#include "backend/corebackend.h"
|
||||||
|
#include "backend/corebackendmanager.h"
|
||||||
|
#include "core/device.h"
|
||||||
|
#include "core/devicescanner.h"
|
||||||
|
#include "core/operationstack.h"
|
||||||
|
#include "core/partition.h"
|
||||||
|
#include "util/capacity.h"
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QList>
|
||||||
|
|
||||||
|
using PartitionList = QList<Partition *>;
|
||||||
|
|
||||||
|
// Recursive helper for flatten(), adds partitions that
|
||||||
|
// are children of @p n to the list @p l.
|
||||||
|
void _flatten(PartitionList& l, PartitionNode *n)
|
||||||
|
{
|
||||||
|
for (const auto &p : n->children()) {
|
||||||
|
l.append(p);
|
||||||
|
|
||||||
|
if (p->roles().has(PartitionRole::Extended)) {
|
||||||
|
_flatten(l, p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively walk the partition table and collect all the partitions
|
||||||
|
* in it (also in extended partitions). Produces a sorted list.
|
||||||
|
*/
|
||||||
|
PartitionList flatten(PartitionTable *table)
|
||||||
|
{
|
||||||
|
PartitionList l;
|
||||||
|
_flatten(l, table);
|
||||||
|
std::sort(l.begin(), l.end(),
|
||||||
|
[](const Partition* p1, const Partition* p2) { return p1->number() < p2->number(); });
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main( int argc, char **argv )
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
KPMCoreInitializer i;
|
||||||
|
if (!i.isValid())
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
KPMCoreInitializer i( argv[1] );
|
||||||
|
if (!i.isValid())
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto backend = CoreBackendManager::self()->backend();
|
||||||
|
if (!backend)
|
||||||
|
{
|
||||||
|
qWarning() << "Could not get backend.";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// First create operationStack with another QObject as parent.
|
||||||
|
OperationStack *operationStack = new OperationStack();
|
||||||
|
DeviceScanner *deviceScanner = new DeviceScanner(nullptr, *operationStack);
|
||||||
|
deviceScanner->scan();
|
||||||
|
|
||||||
|
const auto devices = operationStack->previewDevices();
|
||||||
|
qDebug() << "Found" << devices.length() << "devices.";
|
||||||
|
for (const auto pdev : devices)
|
||||||
|
{
|
||||||
|
qDebug() << "Device @" << (void *)pdev;
|
||||||
|
qDebug() << " " << pdev->prettyName();
|
||||||
|
|
||||||
|
const auto partitiontable = pdev->partitionTable();
|
||||||
|
qDebug() << " Partition Table @" << (void *)partitiontable << '('
|
||||||
|
<< (partitiontable ? partitiontable->typeName() : QLatin1String("null")) << ')';
|
||||||
|
|
||||||
|
const auto partitionlist = flatten(partitiontable);
|
||||||
|
for (const auto &p : partitionlist)
|
||||||
|
qDebug() << " "
|
||||||
|
<< p->number()
|
||||||
|
<< p->partitionPath()
|
||||||
|
<< p->label()
|
||||||
|
<< Capacity::formatByteSize(p->capacity())
|
||||||
|
<< p->fileSystem().name();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -80,9 +80,9 @@ int main( int argc, char **argv )
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto devices = backend->scanDevices();
|
const auto devices = backend->scanDevices();
|
||||||
qDebug() << "Found" << devices.length() << "devices.";
|
qDebug() << "Found" << devices.length() << "devices.";
|
||||||
for (const auto pdev : qAsConst(devices))
|
for (const auto pdev : devices)
|
||||||
{
|
{
|
||||||
qDebug() << "Device @" << (void *)pdev;
|
qDebug() << "Device @" << (void *)pdev;
|
||||||
qDebug() << " " << pdev->prettyName();
|
qDebug() << " " << pdev->prettyName();
|
||||||
|
@ -92,7 +92,7 @@ int main( int argc, char **argv )
|
||||||
<< (partitiontable ? partitiontable->typeName() : QLatin1String("null")) << ')';
|
<< (partitiontable ? partitiontable->typeName() : QLatin1String("null")) << ')';
|
||||||
|
|
||||||
const auto partitionlist = flatten(partitiontable);
|
const auto partitionlist = flatten(partitiontable);
|
||||||
for (const auto &p : qAsConst(partitionlist))
|
for (const auto &p : partitionlist)
|
||||||
qDebug() << " "
|
qDebug() << " "
|
||||||
<< p->number()
|
<< p->number()
|
||||||
<< p->partitionPath()
|
<< p->partitionPath()
|
||||||
|
|
Loading…
Reference in New Issue