From 63b501f41ddc6cd64400acadb6919570a172d28d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 2 Oct 2017 14:13:47 -0400 Subject: [PATCH] Tests: simple listing of attached devices --- test/CMakeLists.txt | 11 ++++- test/testlist.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 test/testlist.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7fc76eb..d458413 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,7 +20,6 @@ add_library(testhelpers OBJECT helpers.cpp) macro (kpm_test name) add_executable(${name} ${ARGN} $) target_link_libraries(${name} kpmcore) - add_test(${name} ${name}) endmacro() ### @@ -32,4 +31,14 @@ if(TARGET pmdummybackendplugin) endif() if(TARGET pmlibpartedbackendplugin) add_test(NAME testinit-parted COMMAND testinit $) +else() + return() # All the rest really needs a working backend endif() + +set(BACKEND $) + +### +# +# Listing devices, partitions +kpm_test(testlist testlist.cpp) +add_test(NAME testlist COMMAND testlist ${BACKEND}) diff --git a/test/testlist.cpp b/test/testlist.cpp new file mode 100644 index 0000000..b87260f --- /dev/null +++ b/test/testlist.cpp @@ -0,0 +1,107 @@ +/************************************************************************* + * Copyright 2017 by Adriaan de Groot * + * * + * 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 .* + *************************************************************************/ + +// 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/partition.h" +#include "util/capacity.h" + +#include +#include + +using PartitionList = QList; + +// 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; + } + + auto devices = backend->scanDevices(); + 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 : qAsConst(partitionlist)) + qDebug() << " " + << p->number() + << p->partitionPath() + << p->label() + << Capacity::formatByteSize(p->capacity()) + << p->fileSystem().name(); + } + + return 0; + +} +