From 3df5acb9e334bb57b000ca5ea46faded2375832e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Sat, 9 Feb 2019 15:40:06 +0000 Subject: [PATCH] Add scanDevices function that uses flags instead of bools. Add and option to scan for loopback devices. --- src/backend/corebackend.h | 22 +++++++++++++++++++--- src/core/devicescanner.cpp | 2 +- src/plugins/dummy/dummybackend.cpp | 12 +++++++++--- src/plugins/dummy/dummybackend.h | 1 + src/plugins/sfdisk/sfdiskbackend.cpp | 17 ++++++++++++++--- src/plugins/sfdisk/sfdiskbackend.h | 1 + test/testlist.cpp | 2 +- 7 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/backend/corebackend.h b/src/backend/corebackend.h index 386d67f..f9bc32f 100644 --- a/src/backend/corebackend.h +++ b/src/backend/corebackend.h @@ -36,6 +36,13 @@ class PartitionTable; class QString; +enum class ScanFlag : uint8_t { + includeReadOnly = 0x1, /**< devices that are read-only according to the kernel */ + includeLoopback = 0x2, +}; +Q_DECLARE_FLAGS(ScanFlags, ScanFlag) +Q_DECLARE_OPERATORS_FOR_FLAGS(ScanFlags) + /** * Interface class for backend plugins. * @author Volker Lanz @@ -86,8 +93,7 @@ public: /** * Scan for devices in the system. - * @param excludeReadOnly when true, devices that are read-only - * according to the kernel are left out of the list. + * @param excludeReadOnly when true, are left out of the list. * When false (the default) all devices, writable or * not, including CD ROM devices, are returned. * @return a QList of pointers to Device instances. The caller is responsible @@ -95,7 +101,17 @@ public: * @note A Device object is a description of the device, not * an object to operate on. See openDevice(). */ - virtual QList scanDevices(bool excludeReadOnly = false) = 0; + [[deprecated("port to scanDevices(ScanFlags)")]] virtual QList scanDevices(bool excludeReadOnly = false) = 0; + + /** + * Scan for devices in the system. + * @param scanFlags can be used to expand the list of scanned devices. + * @return a QList of pointers to Device instances. The caller is responsible + * for deleting these objects. + * @note A Device object is a description of the device, not + * an object to operate on. See openDevice(). + */ + virtual QList scanDevices(const ScanFlags scanFlags) = 0; /** * Scan a single device in the system. diff --git a/src/core/devicescanner.cpp b/src/core/devicescanner.cpp index 7024290..bff979b 100644 --- a/src/core/devicescanner.cpp +++ b/src/core/devicescanner.cpp @@ -63,7 +63,7 @@ void DeviceScanner::scan() clear(); - const QList deviceList = CoreBackendManager::self()->backend()->scanDevices(); + const QList deviceList = CoreBackendManager::self()->backend()->scanDevices(ScanFlag::includeLoopback); for (const auto &d : deviceList) operationStack().addDevice(d); diff --git a/src/plugins/dummy/dummybackend.cpp b/src/plugins/dummy/dummybackend.cpp index ae6f938..cd527cb 100644 --- a/src/plugins/dummy/dummybackend.cpp +++ b/src/plugins/dummy/dummybackend.cpp @@ -46,15 +46,21 @@ void DummyBackend::initFSSupport() { } -QList DummyBackend::scanDevices(bool excludeLoop) +QList DummyBackend::scanDevices(bool excludeReadOnly) { - Q_UNUSED(excludeLoop) + Q_UNUSED(excludeReadOnly) + return scanDevices(ScanFlags()); +} + +QList DummyBackend::scanDevices(const ScanFlags scanFlags) +{ + Q_UNUSED(scanFlags) QList result; result.append(scanDevice(QStringLiteral("/dev/sda"))); emitScanProgress(QStringLiteral("/dev/sda"), 100); - return result; + return scanDevices(false); } Device* DummyBackend::scanDevice(const QString& deviceNode) diff --git a/src/plugins/dummy/dummybackend.h b/src/plugins/dummy/dummybackend.h index a9f8fb3..aa73d7f 100644 --- a/src/plugins/dummy/dummybackend.h +++ b/src/plugins/dummy/dummybackend.h @@ -45,6 +45,7 @@ public: void initFSSupport() override; QList scanDevices(bool excludeReadOnly = false) override; + QList scanDevices(const ScanFlags scanFlags) override; std::unique_ptr openDevice(const Device& d) override; std::unique_ptr openDeviceExclusive(const Device& d) override; bool closeDevice(std::unique_ptr coreDevice) override; diff --git a/src/plugins/sfdisk/sfdiskbackend.cpp b/src/plugins/sfdisk/sfdiskbackend.cpp index 80c7d92..e812c31 100644 --- a/src/plugins/sfdisk/sfdiskbackend.cpp +++ b/src/plugins/sfdisk/sfdiskbackend.cpp @@ -64,7 +64,14 @@ void SfdiskBackend::initFSSupport() QList SfdiskBackend::scanDevices(bool excludeReadOnly) { -// TODO: add another bool option for loopDevices + return scanDevices(excludeReadOnly ? ScanFlags() : ScanFlag::includeReadOnly); +} + +QList SfdiskBackend::scanDevices(const ScanFlags scanFlags) +{ + const bool includeReadOnly = scanFlags.testFlag(ScanFlag::includeReadOnly); + const bool includeLoopback = scanFlags.testFlag(ScanFlag::includeLoopback); + QList result; QStringList deviceNodes; @@ -82,11 +89,14 @@ QList SfdiskBackend::scanDevices(bool excludeReadOnly) const QJsonArray jsonArray = jsonObject[QLatin1String("blockdevices")].toArray(); for (const auto &deviceLine : jsonArray) { QJsonObject deviceObject = deviceLine.toObject(); - if (deviceObject[QLatin1String("type")].toString() != QLatin1String("disk")) + if (! (deviceObject[QLatin1String("type")].toString() == QLatin1String("disk") + || (includeLoopback && deviceObject[QLatin1String("type")].toString() == QLatin1String("loop")) )) + { continue; + } const QString deviceNode = deviceObject[QLatin1String("name")].toString(); - if (excludeReadOnly) { + if (!includeReadOnly) { QString deviceName = deviceNode; deviceName.remove(QStringLiteral("/dev/")); QFile f(QStringLiteral("/sys/block/%1/ro").arg(deviceName)); @@ -115,6 +125,7 @@ QList SfdiskBackend::scanDevices(bool excludeReadOnly) return result; } + /** Create a Device for the given device_node and scan it for partitions. @param deviceNode the device node (e.g. "/dev/sda") @return the created Device object. callers need to free this. diff --git a/src/plugins/sfdisk/sfdiskbackend.h b/src/plugins/sfdisk/sfdiskbackend.h index dcc7952..9a659c8 100644 --- a/src/plugins/sfdisk/sfdiskbackend.h +++ b/src/plugins/sfdisk/sfdiskbackend.h @@ -47,6 +47,7 @@ public: void initFSSupport() override; QList scanDevices(bool excludeReadOnly = false) override; + QList scanDevices(const ScanFlags scanFlags) override; std::unique_ptr openDevice(const Device& d) override; std::unique_ptr openDeviceExclusive(const Device& d) override; bool closeDevice(std::unique_ptr coreDevice) override; diff --git a/test/testlist.cpp b/test/testlist.cpp index 1960e00..703283b 100644 --- a/test/testlist.cpp +++ b/test/testlist.cpp @@ -86,7 +86,7 @@ int main( int argc, char **argv ) return 1; } - const auto devices = backend->scanDevices(); + const auto devices = backend->scanDevices(ScanFlag::includeLoopback); qDebug() << "Found" << devices.length() << "devices."; for (const auto pdev : devices) {