File: testlist.cpp

package info (click to toggle)
kpmcore 24.12.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,696 kB
  • sloc: cpp: 20,700; sh: 22; makefile: 7
file content (98 lines) | stat: -rw-r--r-- 2,663 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
    SPDX-FileCopyrightText: 2017 Adriaan de Groot <groot@kde.org>
    SPDX-FileCopyrightText: 2017-2019 Andrius Štikonas <andrius@stikonas.eu>
    SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>

    SPDX-License-Identifier: GPL-3.0-or-later
*/

// 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 <QCoreApplication>
#include <QDebug>
#include <QList>

#include <memory>

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 )
{
    QCoreApplication app(argc, argv);
    std::unique_ptr<KPMCoreInitializer> i;

    if (argc != 2) {
        i = std::make_unique<KPMCoreInitializer>();
        if (!i->isValid())
            return 1;
    } else {
        i = std::make_unique<KPMCoreInitializer>( argv[1] );
        if (!i->isValid())
            return 1;
    }

    auto backend = CoreBackendManager::self()->backend();

    if (!backend) {
        qWarning() << "Could not get backend.";
        return 1;
    }

    const auto devices = backend->scanDevices(ScanFlag::includeLoopback);
    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;
}