Allow to get the partition nr from the device node

This commit is contained in:
Tomaz Canabrava 2022-01-07 16:49:47 +00:00 committed by Andrius Štikonas
parent 6f28900350
commit afcc0c6c47
1 changed files with 40 additions and 0 deletions

View File

@ -25,6 +25,45 @@
#include <KLocalizedString>
#include <config.h>
#include <utility>
std::pair<QString, QString> parseDevice(const QString& selectedDevice)
{
QString device;
QString partitionNr;
if (selectedDevice.length()) {
const bool isNvme = selectedDevice.startsWith(QStringLiteral("nvme"));
// calculate the number of numbers on the last part of the info.
int numSize = 0;
for (int i = selectedDevice.length() - 1; i > 0; i--) {
if (selectedDevice[i].isNumber()) {
numSize += 1;
} else {
if (isNvme) {
// the entry is just an nvme without a partition.
if (selectedDevice[i] != QLatin1Char('p')) {
numSize = 0;
break;
}
}
break;
}
}
partitionNr = selectedDevice.mid(selectedDevice.length() - numSize);
if (numSize != 0) {
// nvme has a `p` that also needs to be removed.
if (isNvme) {
numSize += 1;
}
device = selectedDevice.mid(0, selectedDevice.length() - numSize);
} else {
device = selectedDevice;
}
}
return {device, partitionNr};
}
int Q_DECL_IMPORT main(int argc, char* argv[])
{
@ -75,6 +114,7 @@ int Q_DECL_IMPORT main(int argc, char* argv[])
return 0;
const QString selectedDevice = parser.value(deviceOption);
auto [device, partitionNr] = parseDevice(selectedDevice);
MainWindow* mainWindow = new MainWindow();
QObject::connect(mainWindow, &MainWindow::scanFinished, mainWindow, [mainWindow, selectedDevice] {