From 4a0b8f8efbb59f9fa46788184a6ec8ace13c6b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Fri, 22 Jul 2016 12:56:59 +0100 Subject: [PATCH] Use lsblk to detect devices. --- .../helpers/org.kde.kpmcore.scan.actions | 6 ---- src/plugins/libparted/helpers/scan.cpp | 29 ---------------- src/plugins/libparted/helpers/scan.h | 1 - src/plugins/libparted/libpartedbackend.cpp | 34 ++++++++----------- 4 files changed, 15 insertions(+), 55 deletions(-) diff --git a/src/plugins/libparted/helpers/org.kde.kpmcore.scan.actions b/src/plugins/libparted/helpers/org.kde.kpmcore.scan.actions index 992d285..15f419d 100644 --- a/src/plugins/libparted/helpers/org.kde.kpmcore.scan.actions +++ b/src/plugins/libparted/helpers/org.kde.kpmcore.scan.actions @@ -1,9 +1,3 @@ -[org.kde.kpmcore.scan.scandevices] -Name=Scan devices action -Description=Scan available devices -Policy=yes -Persistence=session - [org.kde.kpmcore.scan.scandevice] Name=Scan device action Description=Scan device diff --git a/src/plugins/libparted/helpers/scan.cpp b/src/plugins/libparted/helpers/scan.cpp index 2a26e96..d8c3dd6 100644 --- a/src/plugins/libparted/helpers/scan.cpp +++ b/src/plugins/libparted/helpers/scan.cpp @@ -19,35 +19,6 @@ #include "core/partitiontable.h" #include "plugins/libparted/pedflags.h" -ActionReply Scan::scandevices(const QVariantMap& args) -{ - ActionReply reply; - - ped_device_probe_all(); - PedDevice* pedDevice = nullptr; - QList paths; - bool excludeReadOnly = args[QLatin1String("excludeReadOnly")].toBool(); - - while (true) { - pedDevice = ped_device_get_next(pedDevice); - if (!pedDevice) - break; - if (pedDevice->type == PED_DEVICE_DM) - continue; - if (excludeReadOnly && ( - pedDevice->type == PED_DEVICE_LOOP || - pedDevice->read_only)) - continue; - - paths.append(QString::fromUtf8(pedDevice->path)); - } - - QVariantMap returnArgs; - returnArgs[QLatin1String("paths")] = paths; - reply.setData(returnArgs); - return reply; -} - ActionReply Scan::scandevice(const QVariantMap& args) { ActionReply reply; diff --git a/src/plugins/libparted/helpers/scan.h b/src/plugins/libparted/helpers/scan.h index cedf455..dca38da 100644 --- a/src/plugins/libparted/helpers/scan.h +++ b/src/plugins/libparted/helpers/scan.h @@ -29,7 +29,6 @@ class Scan : public QObject Q_OBJECT public Q_SLOTS: - ActionReply scandevices(const QVariantMap& args); ActionReply scandevice(const QVariantMap& args); ActionReply readsectorsused(const QVariantMap& args); }; diff --git a/src/plugins/libparted/libpartedbackend.cpp b/src/plugins/libparted/libpartedbackend.cpp index 4574f63..0dbae60 100644 --- a/src/plugins/libparted/libpartedbackend.cpp +++ b/src/plugins/libparted/libpartedbackend.cpp @@ -38,6 +38,7 @@ #include "fs/luks.h" #include "util/globallog.h" +#include "util/externalcommand.h" #include "util/helpers.h" #include @@ -310,26 +311,21 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) QList LibPartedBackend::scanDevices(bool excludeReadOnly) { QList result; - QVariantMap args; - args[QLatin1String("excludeReadOnly")] = excludeReadOnly; - KAuth::Action scanAction = QStringLiteral("org.kde.kpmcore.scan.scandevices"); - scanAction.setHelperId(QStringLiteral("org.kde.kpmcore.scan")); - scanAction.setArguments(args); - KAuth::ExecuteJob *job = scanAction.execute(); - if (!job->exec()) { - qWarning() << "KAuth returned an error code: " << job->errorString(); - return result; - } - QList paths = job->data()[QLatin1String("paths")].toList(); - - quint32 totalDevices = paths.size(); - for (quint32 i = 0; i < totalDevices; ++i) { - QString path = paths[i].toString(); - emitScanProgress(path, i * 100 / totalDevices); - Device* d = scanDevice(path); - if (d) - result.append(d); + ExternalCommand cmd(QStringLiteral("lsblk"), { + QStringLiteral("--nodeps"), + QStringLiteral("--noheadings"), + QStringLiteral("--output"), QString::fromLatin1("name"), + QStringLiteral("--paths"), + QStringLiteral("--include"), excludeReadOnly ? QStringLiteral("3,8") : QStringLiteral("3,7,8") }); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QStringList devices = cmd.output().split(QString::fromLatin1("\n")); + devices.removeLast(); + quint32 totalDevices = devices.length(); + for (quint32 i = 0; i < totalDevices; ++i) { + emitScanProgress(devices[i], i * 100 / totalDevices); + result.append(scanDevice(devices[i])); + } } return result;