Use lsblk to detect devices.

This commit is contained in:
Andrius Štikonas 2016-07-22 12:56:59 +01:00
parent e64a82ff19
commit 4a0b8f8efb
4 changed files with 15 additions and 55 deletions

View File

@ -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] [org.kde.kpmcore.scan.scandevice]
Name=Scan device action Name=Scan device action
Description=Scan device Description=Scan device

View File

@ -19,35 +19,6 @@
#include "core/partitiontable.h" #include "core/partitiontable.h"
#include "plugins/libparted/pedflags.h" #include "plugins/libparted/pedflags.h"
ActionReply Scan::scandevices(const QVariantMap& args)
{
ActionReply reply;
ped_device_probe_all();
PedDevice* pedDevice = nullptr;
QList<QVariant> 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 Scan::scandevice(const QVariantMap& args)
{ {
ActionReply reply; ActionReply reply;

View File

@ -29,7 +29,6 @@ class Scan : public QObject
Q_OBJECT Q_OBJECT
public Q_SLOTS: public Q_SLOTS:
ActionReply scandevices(const QVariantMap& args);
ActionReply scandevice(const QVariantMap& args); ActionReply scandevice(const QVariantMap& args);
ActionReply readsectorsused(const QVariantMap& args); ActionReply readsectorsused(const QVariantMap& args);
}; };

View File

@ -38,6 +38,7 @@
#include "fs/luks.h" #include "fs/luks.h"
#include "util/globallog.h" #include "util/globallog.h"
#include "util/externalcommand.h"
#include "util/helpers.h" #include "util/helpers.h"
#include <blkid/blkid.h> #include <blkid/blkid.h>
@ -310,26 +311,21 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode)
QList<Device*> LibPartedBackend::scanDevices(bool excludeReadOnly) QList<Device*> LibPartedBackend::scanDevices(bool excludeReadOnly)
{ {
QList<Device*> result; QList<Device*> result;
QVariantMap args;
args[QLatin1String("excludeReadOnly")] = excludeReadOnly;
KAuth::Action scanAction = QStringLiteral("org.kde.kpmcore.scan.scandevices"); ExternalCommand cmd(QStringLiteral("lsblk"), {
scanAction.setHelperId(QStringLiteral("org.kde.kpmcore.scan")); QStringLiteral("--nodeps"),
scanAction.setArguments(args); QStringLiteral("--noheadings"),
KAuth::ExecuteJob *job = scanAction.execute(); QStringLiteral("--output"), QString::fromLatin1("name"),
if (!job->exec()) { QStringLiteral("--paths"),
qWarning() << "KAuth returned an error code: " << job->errorString(); QStringLiteral("--include"), excludeReadOnly ? QStringLiteral("3,8") : QStringLiteral("3,7,8") });
return result; if (cmd.run(-1) && cmd.exitCode() == 0) {
} QStringList devices = cmd.output().split(QString::fromLatin1("\n"));
QList<QVariant> paths = job->data()[QLatin1String("paths")].toList(); devices.removeLast();
quint32 totalDevices = devices.length();
quint32 totalDevices = paths.size(); for (quint32 i = 0; i < totalDevices; ++i) {
for (quint32 i = 0; i < totalDevices; ++i) { emitScanProgress(devices[i], i * 100 / totalDevices);
QString path = paths[i].toString(); result.append(scanDevice(devices[i]));
emitScanProgress(path, i * 100 / totalDevices); }
Device* d = scanDevice(path);
if (d)
result.append(d);
} }
return result; return result;