File indexing completed on 2024-12-08 05:10:25

0001 /*
0002     SPDX-FileCopyrightText: 2017 Adriaan de Groot <groot@kde.org>
0003     SPDX-FileCopyrightText: 2017-2019 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-3.0-or-later
0007 */
0008 
0009 // Lists devices
0010 
0011 #include "helpers.h"
0012 
0013 #include "backend/corebackend.h"
0014 #include "backend/corebackendmanager.h"
0015 #include "core/device.h"
0016 #include "core/partition.h"
0017 #include "util/capacity.h"
0018 
0019 #include <QCoreApplication>
0020 #include <QDebug>
0021 #include <QList>
0022 
0023 #include <memory>
0024 
0025 using PartitionList = QList<Partition *>;
0026 
0027 // Recursive helper for flatten(), adds partitions that
0028 // are children of @p n to the list @p l.
0029 void _flatten(PartitionList& l, PartitionNode *n)
0030 {
0031     for (const auto &p : n->children()) {
0032         l.append(p);
0033 
0034         if (p->roles().has(PartitionRole::Extended)) {
0035             _flatten(l, p);
0036         }
0037     }
0038 }
0039 
0040 /**
0041  * Recursively walk the partition table and collect all the partitions
0042  * in it (also in extended partitions). Produces a sorted list.
0043  */
0044 PartitionList flatten(PartitionTable *table)
0045 {
0046     PartitionList l;
0047     _flatten(l, table);
0048     std::sort(l.begin(), l.end(),
0049                 [](const Partition* p1, const Partition* p2) { return p1->number() < p2->number(); });
0050     return l;
0051 }
0052 
0053 int main( int argc, char **argv )
0054 {
0055     QCoreApplication app(argc, argv);
0056     std::unique_ptr<KPMCoreInitializer> i;
0057 
0058     if (argc != 2) {
0059         i = std::make_unique<KPMCoreInitializer>();
0060         if (!i->isValid())
0061             return 1;
0062     } else {
0063         i = std::make_unique<KPMCoreInitializer>( argv[1] );
0064         if (!i->isValid())
0065             return 1;
0066     }
0067 
0068     auto backend = CoreBackendManager::self()->backend();
0069 
0070     if (!backend) {
0071         qWarning() << "Could not get backend.";
0072         return 1;
0073     }
0074 
0075     const auto devices = backend->scanDevices(ScanFlag::includeLoopback);
0076     qDebug() << "Found" << devices.length() << "devices.";
0077 
0078     for (const auto &pdev : devices) {
0079         qDebug() << "Device @" << (void *)pdev;
0080         qDebug() << "  " << pdev->prettyName();
0081 
0082         const auto partitiontable = pdev->partitionTable();
0083         qDebug() << "    Partition Table @" << (void *)partitiontable << '('
0084             << (partitiontable ? partitiontable->typeName() : QLatin1String("null")) << ')';
0085 
0086         const auto partitionlist = flatten(partitiontable);
0087         for (const auto &p : partitionlist)
0088             qDebug() << "      "
0089             << p->number()
0090             << p->partitionPath()
0091             << p->label()
0092             << Capacity::formatByteSize(p->capacity())
0093             << p->fileSystem().name();
0094     }
0095 
0096     return 0;
0097 }
0098