File indexing completed on 2024-05-05 05:48:41

0001 /*
0002     SPDX-FileCopyrightText: 2008-2012 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2008 Laurent Montel <montel@kde.org>
0004     SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
0005     SPDX-FileCopyrightText: 2016-2019 Andrius Štikonas <andrius@stikonas.eu>
0006     SPDX-FileCopyrightText: 2015-2016 Teo Mrnjavac <teo@kde.org>
0007     SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
0008 
0009     SPDX-License-Identifier: GPL-3.0-or-later
0010 */
0011 
0012 #ifndef KPMCORE_PARTITIONTABLE_H
0013 #define KPMCORE_PARTITIONTABLE_H
0014 
0015 #include "util/libpartitionmanagerexport.h"
0016 
0017 #include "core/partitionnode.h"
0018 #include "core/partitionrole.h"
0019 
0020 #include <QList>
0021 #include <QtGlobal>
0022 
0023 class Device;
0024 class Partition;
0025 class CoreBackend;
0026 
0027 class QTextStream;
0028 
0029 /** The partition table (a.k.a Disk Label)
0030 
0031     PartitionTable represents a partition table (or disk label).
0032 
0033     PartitionTable has child nodes that represent Partitions.
0034 
0035     @author Volker Lanz <vl@fidra.de>
0036 */
0037 class LIBKPMCORE_EXPORT PartitionTable : public PartitionNode
0038 {
0039     PartitionTable &operator=(const PartitionTable &) = delete;
0040 
0041     friend class CoreBackend;
0042     friend LIBKPMCORE_EXPORT QTextStream& operator<<(QTextStream& stream, const PartitionTable& ptable);
0043 
0044 public:
0045     enum TableType : int8_t {
0046         unknownTableType = -1,
0047 
0048         aix,
0049         bsd,
0050         dasd,
0051         msdos,
0052         msdos_sectorbased,
0053         dvh,
0054         gpt,
0055         loop,
0056         mac,
0057         pc98,
0058         amiga,
0059         sun,
0060         vmd,  /* Volume Manager Device */
0061         none, /* Single FileSystem devices */
0062     };
0063 
0064     /** Partition flags */
0065     enum Flag : uint32_t {
0066         None = 0x0,
0067         Boot = 0x1,
0068         Root = 0x2,
0069         Swap = 0x4,
0070         Hidden = 0x8,
0071         Raid = 0x10,
0072         Lvm = 0x20,
0073         Lba = 0x40,
0074         HpService = 0x80,
0075         Palo = 0x100,
0076         Prep = 0x200,
0077         MsftReserved = 0x400,
0078         BiosGrub = 0x800,
0079         AppleTvRecovery = 0x1000,
0080         Diag = 0x2000,
0081         LegacyBoot = 0x4000,
0082         MsftData = 0x8000,
0083         Irst = 0x100000,
0084         FlagNone [[deprecated("Use PartitionTable::Flag::None")]] = None,
0085         FlagBoot [[deprecated("Use PartitionTable::Flag::Boot")]] = Boot,
0086         FlagRoot [[deprecated("Use PartitionTable::Flag::Root")]] = Root,
0087         FlagSwap [[deprecated("Use PartitionTable::Flag::Swap")]] = Swap,
0088         FlagHidden [[deprecated("Use PartitionTable::Flag::Hidden")]] = Hidden,
0089         FlagRaid [[deprecated("Use PartitionTable::Flag::Raid")]] = Raid,
0090         FlagLvm [[deprecated("Use PartitionTable::Flag::Lvm")]] = Lvm,
0091         FlagLba [[deprecated("Use PartitionTable::Flag::Lba")]] = Lba,
0092         FlagHpService [[deprecated("Use PartitionTable::Flag::HpService")]] = HpService,
0093         FlagPalo [[deprecated("Use PartitionTable::Flag::Palo")]] = Palo,
0094         FlagPrep [[deprecated("Use PartitionTable::Flag::Prep")]] = Prep,
0095         FlagMsftReserved [[deprecated("Use PartitionTable::Flag::MsftReserved")]] = MsftReserved,
0096         FlagBiosGrub [[deprecated("Use PartitionTable::Flag::BiosGrub")]] = BiosGrub,
0097         FlagAppleTvRecovery [[deprecated("Use PartitionTable::Flag::AppleTvRecovery")]] = AppleTvRecovery,
0098         FlagDiag [[deprecated("Use PartitionTable::Flag::Diag")]] = Diag,
0099         FlagLegacyBoot [[deprecated("Use PartitionTable::Flag::LegacyBoot")]] = LegacyBoot,
0100         FlagMsftData [[deprecated("Use PartitionTable::Flag::MsftData")]] = MsftData,
0101         FlagIrst [[deprecated("Use PartitionTable::Flag::Irst")]] = Irst,
0102         FlagEsp [[deprecated("Use PartitionTable::Flag::Boot")]] = Boot
0103     };
0104 
0105     Q_DECLARE_FLAGS(Flags, Flag)
0106 
0107 public:
0108     PartitionTable(TableType type, qint64 firstUsable, qint64 lastUsable);
0109     PartitionTable(const PartitionTable& other);
0110     ~PartitionTable() override;
0111 
0112 public:
0113     PartitionNode* parent() override {
0114         return nullptr;    /**< @return always nullptr for PartitionTable */
0115     }
0116     const PartitionNode* parent() const override {
0117         return nullptr;    /**< @return always nullptr for PartitionTable */
0118     }
0119 
0120     bool isRoot() const override {
0121         return true;    /**< @return always true for PartitionTable */
0122     }
0123     bool isReadOnly() const {
0124         return tableTypeIsReadOnly(type());    /**< @return true if the PartitionTable is read only */
0125     }
0126 
0127     Partitions& children() override {
0128         return m_Children;    /**< @return the children in this PartitionTable */
0129     }
0130     const Partitions& children() const override {
0131         return m_Children;    /**< @return the children in this PartitionTable */
0132     }
0133 
0134     void setType(const Device& d, TableType t);
0135 
0136     void append(Partition* partition) override;
0137 
0138     qint64 freeSectorsBefore(const Partition& p) const;
0139     qint64 freeSectorsAfter(const Partition& p) const;
0140     qint64 freeSectors() const;
0141 
0142     bool hasExtended() const;
0143     Partition* extended() const;
0144 
0145     PartitionRole::Roles childRoles(const Partition& p) const;
0146 
0147     qint32 numPrimaries() const;
0148     qint32 maxPrimaries() const {
0149         return m_MaxPrimaries;    /**< @return max number of primary partitions this PartitionTable can handle */
0150     }
0151 
0152     PartitionTable::TableType type() const {
0153         return m_Type;    /**< @return the PartitionTable's type */
0154     }
0155     const QString typeName() const {
0156         return tableTypeToName(type());    /**< @return the name of this PartitionTable type */
0157     }
0158 
0159     qint64 firstUsable() const {
0160         return m_FirstUsable;
0161     }
0162     qint64 lastUsable() const {
0163         return m_LastUsable;
0164     }
0165 
0166     void setFirstUsableSector(qint64 s) {
0167         m_FirstUsable = s;
0168     }
0169     void setLastUsableSector(qint64 s) {
0170         m_LastUsable = s;
0171     }
0172 
0173     void updateUnallocated(const Device& d);
0174     void insertUnallocated(const Device& d, PartitionNode* p, qint64 start);
0175 
0176     bool isSectorBased(const Device& d) const;
0177 
0178     static const QList<Flag> flagList();
0179     static QString flagName(Flag f);
0180     static QStringList flagNames(Flags f);
0181     static PartitionTable::Flags flagsFromList(const QStringList list);
0182 
0183     static bool getUnallocatedRange(const Device& device, PartitionNode& parent, qint64& start, qint64& end);
0184 
0185     static void removeUnallocated(PartitionNode* p);
0186     void removeUnallocated();
0187 
0188     static qint64 defaultFirstUsable(const Device& d, TableType t);
0189     static qint64 defaultLastUsable(const Device& d, TableType t);
0190 
0191     static PartitionTable::TableType nameToTableType(const QString& n);
0192     static QString tableTypeToName(TableType l);
0193     static qint32 maxPrimariesForTableType(TableType l);
0194     static bool tableTypeSupportsExtended(TableType l);
0195     static bool tableTypeIsReadOnly(TableType l);
0196 
0197 protected:
0198     void setMaxPrimaries(qint32 n) {
0199         m_MaxPrimaries = n;
0200     }
0201 
0202 private:
0203     Partitions m_Children;
0204     qint32 m_MaxPrimaries;
0205     TableType m_Type;
0206     qint64 m_FirstUsable;
0207     qint64 m_LastUsable;
0208 };
0209 
0210 Q_DECLARE_OPERATORS_FOR_FLAGS(PartitionTable::Flags)
0211 
0212 QTextStream& operator<<(QTextStream& stream, const PartitionTable& ptable);
0213 
0214 #endif
0215