File indexing completed on 2024-04-21 05:46:11

0001 /*
0002     SPDX-FileCopyrightText: 2017 Adriaan de Groot <groot@kde.org>
0003     SPDX-FileCopyrightText: 2017 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/devicescanner.h"
0017 #include "core/operationstack.h"
0018 #include "core/partition.h"
0019 #include "util/capacity.h"
0020 
0021 #include <QCoreApplication>
0022 #include <QDebug>
0023 #include <QList>
0024 
0025 #include <memory>
0026 
0027 using PartitionList = QList<Partition *>;
0028 
0029 // Recursive helper for flatten(), adds partitions that
0030 // are children of @p n to the list @p l.
0031 void _flatten(PartitionList& l, PartitionNode *n)
0032 {
0033     for (const auto &p : n->children()) {
0034         l.append(p);
0035 
0036         if (p->roles().has(PartitionRole::Extended)) {
0037             _flatten(l, p);
0038         }
0039     }
0040 }
0041 
0042 /**
0043  * Recursively walk the partition table and collect all the partitions
0044  * in it (also in extended partitions). Produces a sorted list.
0045  */
0046 PartitionList flatten(PartitionTable *table)
0047 {
0048     PartitionList l;
0049     _flatten(l, table);
0050     std::sort(l.begin(), l.end(),
0051                 [](const Partition* p1, const Partition* p2) { return p1->number() < p2->number(); });
0052     return l;
0053 }
0054 
0055 int main( int argc, char **argv )
0056 {
0057     QCoreApplication app(argc, argv);
0058     std::unique_ptr<KPMCoreInitializer> i;
0059 
0060     if (argc != 2) {
0061         i = std::make_unique<KPMCoreInitializer>();
0062         if (!i->isValid())
0063             return 1;
0064     } else {
0065         i = std::make_unique<KPMCoreInitializer>( argv[1] );
0066         if (!i->isValid())
0067             return 1;
0068     }
0069 
0070     auto backend = CoreBackendManager::self()->backend();
0071 
0072     if (!backend) {
0073         qWarning() << "Could not get backend.";
0074         return 1;
0075     }
0076 
0077      // First create operationStack with another QObject as parent.
0078     OperationStack *operationStack = new OperationStack();
0079     DeviceScanner *deviceScanner = new DeviceScanner(nullptr, *operationStack);
0080     deviceScanner->scan();
0081 
0082     const auto devices = operationStack->previewDevices();
0083     qDebug() << "Found" << devices.length() << "devices.";
0084 
0085     for (const auto &pdev : devices) {
0086         qDebug() << "Device @" << (void *)pdev;
0087         qDebug() << "  " << pdev->prettyName();
0088 
0089         const auto partitiontable = pdev->partitionTable();
0090         qDebug() << "    Partition Table @" << (void *)partitiontable << '('
0091             << (partitiontable ? partitiontable->typeName() : QLatin1String("null")) << ')';
0092 
0093         const auto partitionlist = flatten(partitiontable);
0094         for (const auto &p : partitionlist)
0095             qDebug() << "      "
0096             << p->number()
0097             << p->partitionPath()
0098             << p->label()
0099             << Capacity::formatByteSize(p->capacity())
0100             << p->fileSystem().name();
0101     }
0102 
0103     return 0;
0104 }
0105