Add scanDevices function that uses flags instead of bools.

Add and option to scan for loopback devices.
parent bfa6a327
......@@ -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 <vl@fidra.de>
......@@ -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<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.
......
......@@ -63,7 +63,7 @@ void DeviceScanner::scan()
clear();
const QList<Device*> deviceList = CoreBackendManager::self()->backend()->scanDevices();
const QList<Device*> deviceList = CoreBackendManager::self()->backend()->scanDevices(ScanFlag::includeLoopback);
for (const auto &d : deviceList)
operationStack().addDevice(d);
......
......@@ -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;
result.append(scanDevice(QStringLiteral("/dev/sda")));
emitScanProgress(QStringLiteral("/dev/sda"), 100);
return result;
return scanDevices(false);
}
Device* DummyBackend::scanDevice(const QString& deviceNode)
......
......@@ -45,6 +45,7 @@ public:
void initFSSupport() 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> openDeviceExclusive(const Device& d) override;
bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) override;
......
......@@ -64,7 +64,14 @@ void SfdiskBackend::initFSSupport()
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;
QStringList deviceNodes;
......@@ -82,11 +89,14 @@ QList<Device*> 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<Device*> 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.
......
......@@ -47,6 +47,7 @@ public:
void initFSSupport() 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> openDeviceExclusive(const Device& d) override;
bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) override;
......
......@@ -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)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment