Add scanDevices function that uses flags instead of bools.

Add and option to scan for loopback devices.
This commit is contained in:
Andrius Štikonas 2019-02-09 15:40:06 +00:00
parent bfa6a327af
commit 3df5acb9e3
Signed by: andrius
GPG Key ID: E2E5CD054CB9CD3E
7 changed files with 46 additions and 11 deletions

View File

@ -36,6 +36,13 @@ class PartitionTable;
class QString; 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. * Interface class for backend plugins.
* @author Volker Lanz <vl@fidra.de> * @author Volker Lanz <vl@fidra.de>
@ -86,8 +93,7 @@ public:
/** /**
* Scan for devices in the system. * Scan for devices in the system.
* @param excludeReadOnly when true, devices that are read-only * @param excludeReadOnly when true, are left out of the list.
* according to the kernel are left out of the list.
* When false (the default) all devices, writable or * When false (the default) all devices, writable or
* not, including CD ROM devices, are returned. * not, including CD ROM devices, are returned.
* @return a QList of pointers to Device instances. The caller is responsible * @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 * @note A Device object is a description of the device, not
* an object to operate on. See openDevice(). * an object to operate on. See openDevice().
*/ */
virtual QList<Device*> scanDevices(bool excludeReadOnly = false) = 0; [[deprecated("port to scanDevices(ScanFlags)")]] virtual QList<Device*> 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<Device*> scanDevices(const ScanFlags scanFlags) = 0;
/** /**
* Scan a single device in the system. * Scan a single device in the system.

View File

@ -63,7 +63,7 @@ void DeviceScanner::scan()
clear(); clear();
const QList<Device*> deviceList = CoreBackendManager::self()->backend()->scanDevices(); const QList<Device*> deviceList = CoreBackendManager::self()->backend()->scanDevices(ScanFlag::includeLoopback);
for (const auto &d : deviceList) for (const auto &d : deviceList)
operationStack().addDevice(d); operationStack().addDevice(d);

View File

@ -46,15 +46,21 @@ void DummyBackend::initFSSupport()
{ {
} }
QList<Device*> DummyBackend::scanDevices(bool excludeLoop) QList<Device*> DummyBackend::scanDevices(bool excludeReadOnly)
{ {
Q_UNUSED(excludeLoop) Q_UNUSED(excludeReadOnly)
return scanDevices(ScanFlags());
}
QList<Device*> DummyBackend::scanDevices(const ScanFlags scanFlags)
{
Q_UNUSED(scanFlags)
QList<Device*> result; QList<Device*> result;
result.append(scanDevice(QStringLiteral("/dev/sda"))); result.append(scanDevice(QStringLiteral("/dev/sda")));
emitScanProgress(QStringLiteral("/dev/sda"), 100); emitScanProgress(QStringLiteral("/dev/sda"), 100);
return result; return scanDevices(false);
} }
Device* DummyBackend::scanDevice(const QString& deviceNode) Device* DummyBackend::scanDevice(const QString& deviceNode)

View File

@ -45,6 +45,7 @@ public:
void initFSSupport() override; void initFSSupport() override;
QList<Device*> scanDevices(bool excludeReadOnly = false) override; QList<Device*> scanDevices(bool excludeReadOnly = false) override;
QList<Device*> scanDevices(const ScanFlags scanFlags) override;
std::unique_ptr<CoreBackendDevice> openDevice(const Device& d) override; std::unique_ptr<CoreBackendDevice> openDevice(const Device& d) override;
std::unique_ptr<CoreBackendDevice> openDeviceExclusive(const Device& d) override; std::unique_ptr<CoreBackendDevice> openDeviceExclusive(const Device& d) override;
bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) override; bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) override;

View File

@ -64,7 +64,14 @@ void SfdiskBackend::initFSSupport()
QList<Device*> SfdiskBackend::scanDevices(bool excludeReadOnly) QList<Device*> SfdiskBackend::scanDevices(bool excludeReadOnly)
{ {
// TODO: add another bool option for loopDevices return scanDevices(excludeReadOnly ? ScanFlags() : ScanFlag::includeReadOnly);
}
QList<Device*> SfdiskBackend::scanDevices(const ScanFlags scanFlags)
{
const bool includeReadOnly = scanFlags.testFlag(ScanFlag::includeReadOnly);
const bool includeLoopback = scanFlags.testFlag(ScanFlag::includeLoopback);
QList<Device*> result; QList<Device*> result;
QStringList deviceNodes; QStringList deviceNodes;
@ -82,11 +89,14 @@ QList<Device*> SfdiskBackend::scanDevices(bool excludeReadOnly)
const QJsonArray jsonArray = jsonObject[QLatin1String("blockdevices")].toArray(); const QJsonArray jsonArray = jsonObject[QLatin1String("blockdevices")].toArray();
for (const auto &deviceLine : jsonArray) { for (const auto &deviceLine : jsonArray) {
QJsonObject deviceObject = deviceLine.toObject(); 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; continue;
}
const QString deviceNode = deviceObject[QLatin1String("name")].toString(); const QString deviceNode = deviceObject[QLatin1String("name")].toString();
if (excludeReadOnly) { if (!includeReadOnly) {
QString deviceName = deviceNode; QString deviceName = deviceNode;
deviceName.remove(QStringLiteral("/dev/")); deviceName.remove(QStringLiteral("/dev/"));
QFile f(QStringLiteral("/sys/block/%1/ro").arg(deviceName)); QFile f(QStringLiteral("/sys/block/%1/ro").arg(deviceName));
@ -115,6 +125,7 @@ QList<Device*> SfdiskBackend::scanDevices(bool excludeReadOnly)
return result; return result;
} }
/** Create a Device for the given device_node and scan it for partitions. /** Create a Device for the given device_node and scan it for partitions.
@param deviceNode the device node (e.g. "/dev/sda") @param deviceNode the device node (e.g. "/dev/sda")
@return the created Device object. callers need to free this. @return the created Device object. callers need to free this.

View File

@ -47,6 +47,7 @@ public:
void initFSSupport() override; void initFSSupport() override;
QList<Device*> scanDevices(bool excludeReadOnly = false) override; QList<Device*> scanDevices(bool excludeReadOnly = false) override;
QList<Device*> scanDevices(const ScanFlags scanFlags) override;
std::unique_ptr<CoreBackendDevice> openDevice(const Device& d) override; std::unique_ptr<CoreBackendDevice> openDevice(const Device& d) override;
std::unique_ptr<CoreBackendDevice> openDeviceExclusive(const Device& d) override; std::unique_ptr<CoreBackendDevice> openDeviceExclusive(const Device& d) override;
bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) override; bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) override;

View File

@ -86,7 +86,7 @@ int main( int argc, char **argv )
return 1; return 1;
} }
const auto devices = backend->scanDevices(); const auto devices = backend->scanDevices(ScanFlag::includeLoopback);
qDebug() << "Found" << devices.length() << "devices."; qDebug() << "Found" << devices.length() << "devices.";
for (const auto pdev : devices) for (const auto pdev : devices)
{ {