From 6f4b8831305fb1f98461bfa9b8aae5d555d4b799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20PORTAY?= Date: Tue, 16 Jun 2020 17:02:01 -0400 Subject: [PATCH] sfdisk: Move GPT Attributes functions to new sfdiskgptattributes This centralizes the two get functions related to GPT Attributes. --- src/plugins/sfdisk/CMakeLists.txt | 1 + src/plugins/sfdisk/sfdiskbackend.cpp | 14 ++--- src/plugins/sfdisk/sfdiskgptattributes.cpp | 58 +++++++++++++++++++++ src/plugins/sfdisk/sfdiskgptattributes.h | 36 +++++++++++++ src/plugins/sfdisk/sfdiskpartitiontable.cpp | 19 +------ 5 files changed, 100 insertions(+), 28 deletions(-) create mode 100644 src/plugins/sfdisk/sfdiskgptattributes.cpp create mode 100644 src/plugins/sfdisk/sfdiskgptattributes.h diff --git a/src/plugins/sfdisk/CMakeLists.txt b/src/plugins/sfdisk/CMakeLists.txt index c7b137d..ad51de5 100644 --- a/src/plugins/sfdisk/CMakeLists.txt +++ b/src/plugins/sfdisk/CMakeLists.txt @@ -16,6 +16,7 @@ set (pmsfdiskbackendplugin_SRCS sfdiskbackend.cpp sfdiskdevice.cpp + sfdiskgptattributes.cpp sfdiskpartitiontable.cpp ${CMAKE_SOURCE_DIR}/src/backend/corebackenddevice.cpp ${CMAKE_SOURCE_DIR}/src/core/copysourcedevice.cpp diff --git a/src/plugins/sfdisk/sfdiskbackend.cpp b/src/plugins/sfdisk/sfdiskbackend.cpp index 25eae0b..7e6156e 100644 --- a/src/plugins/sfdisk/sfdiskbackend.cpp +++ b/src/plugins/sfdisk/sfdiskbackend.cpp @@ -20,6 +20,7 @@ #include "plugins/sfdisk/sfdiskbackend.h" #include "plugins/sfdisk/sfdiskdevice.h" +#include "plugins/sfdisk/sfdiskgptattributes.h" #include "core/copysourcedevice.h" #include "core/copytargetbytearray.h" @@ -305,17 +306,8 @@ void SfdiskBackend::scanDevicePartitions(Device& d, const QJsonArray& jsonPartit part->setLabel(partitionObject[QLatin1String("name")].toString()); part->setUUID(partitionObject[QLatin1String("uuid")].toString()); part->setType(partitionObject[QLatin1String("type")].toString()); - quint64 attrs = 0; - for (auto& attr: QStringList(partitionObject[QLatin1String("attrs")].toString().split(QLatin1Char(' ')))) - if (attr.compare(QStringLiteral("RequiredPartition")) == 0) - attrs |= 0x0000000000000001; - else if (attr.compare(QStringLiteral("NoBlockIOProtocol")) == 0) - attrs |= 0x0000000000000002; - else if (attr.compare(QStringLiteral("LegacyBIOSBootable")) == 0) - attrs |= 0x0000000000000004; - else if (attr.startsWith(QStringLiteral("GUID:"))) - attrs |= 1ULL << QStringRef(&attr, 5, attr.length() - 5).toULongLong(); - part->setAttributes(attrs); + QString attrs = partitionObject[QLatin1String("attrs")].toString(); + part->setAttributes(SfdiskGptAttributes::toULongLong(attrs.split(QLatin1Char(' ')))); } if (fs->supportGetUUID() != FileSystem::cmdSupportNone) diff --git a/src/plugins/sfdisk/sfdiskgptattributes.cpp b/src/plugins/sfdisk/sfdiskgptattributes.cpp new file mode 100644 index 0000000..0541f48 --- /dev/null +++ b/src/plugins/sfdisk/sfdiskgptattributes.cpp @@ -0,0 +1,58 @@ +/************************************************************************* + * Copyright (C) 2020 by Gaël PORTAY * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#include "plugins/sfdisk/sfdiskgptattributes.h" + +#include +#include + +const static QString requiredPartition = QStringLiteral("RequiredPartition"); +const static QString noBlockIoProtocol = QStringLiteral("NoBlockIOProtocol"); +const static QString legacyBiosBootable = QStringLiteral("LegacyBIOSBootable"); +const static QString guid = QStringLiteral("GUID:"); + +quint64 SfdiskGptAttributes::toULongLong(const QStringList& attrs) +{ + quint64 attributes = 0; + for (auto& attr: attrs) + if (attr.compare(requiredPartition) == 0) + attributes |= 0x1ULL; + else if (attr.compare(noBlockIoProtocol) == 0) + attributes |= 0x2ULL; + else if (attr.compare(legacyBiosBootable) == 0) + attributes |= 0x4ULL; + else if (attr.startsWith(guid)) + attributes |= 1ULL << QStringRef(&attr, guid.length(), attr.length() - guid.length()).toULongLong(); + + return attributes; +} + +QStringList SfdiskGptAttributes::toStringList(quint64 attrs) +{ + QStringList list; + if (attrs & 0x1) + list += requiredPartition; + if (attrs & 0x2) + list += noBlockIoProtocol; + if (attrs & 0x4) + list += legacyBiosBootable; + for (int bit = 48; bit < 64; bit++) + if (attrs & (1 << bit)) + list += guid + QString::number(bit); + + return list; +} diff --git a/src/plugins/sfdisk/sfdiskgptattributes.h b/src/plugins/sfdisk/sfdiskgptattributes.h new file mode 100644 index 0000000..19db26e --- /dev/null +++ b/src/plugins/sfdisk/sfdiskgptattributes.h @@ -0,0 +1,36 @@ +/************************************************************************* + * Copyright (C) 2020 by Gaël PORTAY * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#if !defined(SFDISKGPTATTRIBUTES__H) + +#define SFDISKGPTATTRIBUTES__H + +#include + +class QStringList; + +/** Sfdisk GPT Attributes helpers. + @author Gaël PORTAY +*/ +class SfdiskGptAttributes +{ +public: + static quint64 toULongLong(const QStringList& attrs); + static QStringList toStringList(quint64 attrs); +}; + +#endif diff --git a/src/plugins/sfdisk/sfdiskpartitiontable.cpp b/src/plugins/sfdisk/sfdiskpartitiontable.cpp index 702542c..5d27b35 100644 --- a/src/plugins/sfdisk/sfdiskpartitiontable.cpp +++ b/src/plugins/sfdisk/sfdiskpartitiontable.cpp @@ -16,6 +16,7 @@ *************************************************************************/ #include "plugins/sfdisk/sfdiskpartitiontable.h" +#include "plugins/sfdisk/sfdiskgptattributes.h" #include "backend/corebackend.h" #include "backend/corebackendmanager.h" @@ -213,22 +214,6 @@ static QLatin1String getPartitionType(FileSystem::Type t, PartitionTable::TableT return QLatin1String(); } -static QStringList getAttributeList(quint64 attrs) -{ - QStringList list; - if (attrs & 0x01) - list += QStringLiteral("RequiredPartition"); - if (attrs & 0x02) - list += QStringLiteral("NoBlockIOProtocol"); - if (attrs & 0x04) - list += QStringLiteral("LegacyBIOSBootable"); - for (int bit = 48; bit < 64; bit++) - if (attrs & (1 << bit)) - list += QString::number(bit); - - return list; -} - bool SfdiskPartitionTable::setPartitionLabel(Report& report, const Partition& partition, const QString& label) { if (label.isEmpty()) @@ -264,7 +249,7 @@ bool SfdiskPartitionTable::setPartitionUUID(Report& report, const Partition& par bool SfdiskPartitionTable::setPartitionAttributes(Report& report, const Partition& partition, quint64 attrs) { - QStringList attributes = getAttributeList(attrs); + QStringList attributes = SfdiskGptAttributes::toStringList(attrs); if (attributes.isEmpty()) return true; ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--part-attrs"), m_device->deviceNode(), QString::number(partition.number()),