Warning, file /system/kpmcore/src/core/partition.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2008 Laurent Montel <montel@kde.org>
0004     SPDX-FileCopyrightText: 2013-2020 Andrius Štikonas <andrius@stikonas.eu>
0005     SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
0006     SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
0007     SPDX-FileCopyrightText: 2020 Gaël PORTAY <gael.portay@collabora.com>
0008 
0009     SPDX-License-Identifier: GPL-3.0-or-later
0010 */
0011 
0012 #ifndef KPMCORE_PARTITION_H
0013 #define KPMCORE_PARTITION_H
0014 
0015 #include "core/partitionnode.h"
0016 #include "core/partitionrole.h"
0017 #include "core/partitiontable.h"
0018 
0019 #include "util/libpartitionmanagerexport.h"
0020 
0021 #include <QtGlobal>
0022 #include <QPointer>
0023 
0024 class Device;
0025 class OperationStack;
0026 class CoreBackendPartitionTable;
0027 class PartitionAlignment;
0028 
0029 class PartResizerWidget;
0030 class ResizeDialog;
0031 class InsertDialog;
0032 class NewDialog;
0033 class EditMountPointDialog;
0034 class PartPropsDialog;
0035 class SizeDialogBase;
0036 
0037 class CreateFileSystemOperation;
0038 class RestoreOperation;
0039 class SetPartFlagsOperation;
0040 class CopyOperation;
0041 class NewOperation;
0042 class ResizeOperation;
0043 
0044 class SetPartGeometryJob;
0045 class CreatePartitionJob;
0046 class SetPartFlagsJob;
0047 class RestoreFileSystemJob;
0048 
0049 class FileSystem;
0050 
0051 class Report;
0052 
0053 class QString;
0054 class QTextStream;
0055 
0056 /** A partition or some unallocated space on a Device.
0057 
0058     Represent partitions in a PartitionTable on a Device. Partitions can be unallocated, thus not all
0059     instances really are partitions in the way the user would see them.
0060 
0061     Extended partitions have child objects that represent the logicals inside them.
0062 
0063     @see PartitionTable, Device, FileSystem
0064     @author Volker Lanz <vl@fidra.de>
0065 */
0066 class LIBKPMCORE_EXPORT Partition : public PartitionNode
0067 {
0068 
0069 public:
0070     /** A Partition state -- where did it come from? */
0071     enum State {
0072         None,      /**< exists on disk */
0073         New,       /**< from a NewOperation */
0074         Copy,      /**< from a CopyOperation */
0075         Restore,   /**< from a RestoreOperation */
0076         StateNone [[deprecated("Use Partition::State::None")]] = None,
0077         StateNew [[deprecated("Use Partition::State::New")]] = New,
0078         StateCopy [[deprecated("Use Partition::State::Copy")]] = Copy,
0079         StateRestore [[deprecated("Use Partition::State::Restore")]] = Restore
0080     };
0081 
0082     Partition(PartitionNode* parent, const Device& device, const PartitionRole& role, FileSystem* fs, qint64 sectorStart, qint64 sectorEnd, QString partitionPath, PartitionTable::Flags availableFlags = PartitionTable::Flag::None, const QString& mountPoint = QString(), bool mounted = false, PartitionTable::Flags activeFlags = PartitionTable::Flag::None, State state = State::None);
0083     ~Partition() override;
0084 
0085     Partition(const Partition& other, PartitionNode* parent = nullptr);
0086     Partition& operator=(const Partition&);
0087 
0088     bool operator==(const Partition& other) const;
0089     bool operator!=(const Partition& other) const;
0090 
0091     qint32 number() const {
0092         return m_Number;    /**< @return the Partition's device number, e.g. 7 for /dev/sdd7 */
0093     }
0094 
0095     bool isRoot() const override {
0096         return false;    /**< @return always false for Partition */
0097     }
0098 
0099     const PartitionTable* partitionTable() const;
0100 
0101     PartitionNode* parent() override {
0102         return m_Parent;    /**< @return the Partition's parent PartitionNode */
0103     }
0104     const PartitionNode* parent() const override {
0105         return m_Parent;    /**< @return the Partition's parent PartitionNode */
0106     }
0107 
0108     Partitions& children() override {
0109         return m_Children;    /**< @return the Partition's children. empty for non-extended. */
0110     }
0111     const Partitions& children() const override {
0112         return m_Children;    /**< @return the Partition's children. empty for non-extended. */
0113     }
0114     const QString& devicePath() const {
0115         return m_DevicePath;    /**< @return the Partition's device path, e.g. /dev/sdd */
0116     }
0117     const QString& partitionPath() const {
0118         return m_PartitionPath;    /**< @return the Partition's path, e.g. /dev/sdd1 */
0119     }
0120     const QString& label() const {
0121         return m_Label;    /**< @return the GPT Partition label */
0122     }
0123     const QString& type() const {
0124         return m_Type;    /**< @return the GPT Partition type */
0125     }
0126     const QString& uuid() const {
0127         return m_UUID;    /**< @return the GPT Partition UUID */
0128     }
0129     quint64 attributes() const {
0130         return m_Attributes;    /**< @return the GPT Partition attributes */
0131     }
0132     qint64 firstSector() const {
0133         return m_FirstSector;    /**< @return the Partition's first sector on the Device */
0134     }
0135     qint64 lastSector() const {
0136         return m_LastSector;    /**< @return the Partition's last sector on the Device */
0137     }
0138     qint64 firstByte() const {
0139         return firstSector() * sectorSize();    /**< @return the Partition's first byte on the Device */
0140     }
0141     qint64 lastByte() const {
0142         return firstByte() + length() * sectorSize() - 1;    /**< @return the Partition's last byte on the Device */
0143     }
0144     qint64 sectorsUsed() const;
0145     qint64 sectorSize() const {
0146         return m_SectorSize;    /**< @return the sector size on the Partition's Device */
0147     }
0148     qint64 length() const {
0149         return lastSector() - firstSector() + 1;    /**< @return the length of the Partition */
0150     }
0151     qint64 capacity() const {
0152         return length() * sectorSize();    /**< @return the capacity of the Partition in bytes */
0153     }
0154     qint64 used() const {
0155         return sectorsUsed() < 0 ? -1 : sectorsUsed() * sectorSize();    /**< @return the number of used sectors in the Partition's FileSystem */
0156     }
0157     qint64 available() const {
0158         return sectorsUsed() < 0 ? -1 : capacity() - used();    /**< @return the number of free sectors in the Partition's FileSystem */
0159     }
0160     qint64 minimumSectors() const;
0161     qint64 maximumSectors() const;
0162     qint64 maxFirstSector() const;
0163     qint64 minLastSector() const;
0164 
0165     QString deviceNode() const;
0166 
0167     const PartitionRole& roles() const {
0168         return m_Roles;    /**< @return the Partition's role(s) */
0169     }
0170 
0171     const QString& mountPoint() const {
0172         return m_MountPoint;    /**< @return the Partition's mount point */
0173     }
0174 
0175     PartitionTable::Flags activeFlags() const {
0176         return m_ActiveFlags;    /**< @return the flags currently set for this Partition */
0177     }
0178     PartitionTable::Flags availableFlags() const {
0179         return m_AvailableFlags;    /**< @return the flags available for this Partition */
0180     }
0181     bool isMounted() const {
0182         return m_IsMounted;    /**< @return true if Partition is mounted */
0183     }
0184     FileSystem& fileSystem() {
0185         return *m_FileSystem;    /**< @return the Partition's FileSystem */
0186     }
0187     const FileSystem& fileSystem() const {
0188         return *m_FileSystem;    /**< @return the Partition's FileSystem */
0189     }
0190     State state() const {
0191         return m_State;    /**< @return the Partition's state */
0192     }
0193     bool hasChildren() const;
0194 
0195     bool mount(Report& report);
0196     bool unmount(Report& report);
0197 
0198     bool canMount() const;
0199     bool canUnmount() const;
0200 
0201     void adjustLogicalNumbers(qint32 deletedNumber, qint32 insertedNumber) const;
0202     void checkChildrenMounted();
0203 
0204     void setFirstSector(qint64 s) {
0205         m_FirstSector = s;
0206     }
0207     void setLastSector(qint64 s) {
0208         m_LastSector = s;
0209     }
0210 
0211     void setLabel(const QString& s) {
0212         m_Label = s;    /**< @param s the new label */
0213     }
0214     void setType(const QString& s) {
0215         m_Type = s;    /**< @param s the new type */
0216     }
0217     void setUUID(const QString& s) {
0218         m_UUID = s;    /**< @param s the new UUID */
0219     }
0220     void setAttributes(quint64 f) {
0221         m_Attributes = f;    /**< @param f the new attributes */
0222     }
0223 
0224     void append(Partition* p) override {
0225         m_Children.append(p);
0226         std::sort(m_Children.begin(), m_Children.end(), [] (const Partition *a, const Partition *b) -> bool {return a->firstSector() < b->firstSector();});
0227     }
0228     void setDevicePath(const QString& s) {
0229         m_DevicePath = s;
0230     }
0231     void setPartitionPath(const QString& s);
0232     void setRoles(const PartitionRole& r) {
0233         m_Roles = r;
0234     }
0235     void setMountPoint(const QString& s) {
0236         m_MountPoint = s;
0237     }
0238     void setFlags(PartitionTable::Flags f) {
0239         m_ActiveFlags = f;
0240     }
0241     void setSectorSize(qint32 s) {
0242         m_SectorSize = s;
0243     }
0244     void move(qint64 newStartSector);
0245     void setMounted(bool b);
0246 
0247     void setFlag(PartitionTable::Flag f) {
0248         m_ActiveFlags = m_ActiveFlags.setFlag(f);
0249     }
0250     void unsetFlag(PartitionTable::Flag f) {
0251         m_ActiveFlags = m_ActiveFlags.setFlag(f, false);
0252     }
0253     void setParent(PartitionNode* p) {
0254         m_Parent = p;
0255     }
0256     void setFileSystem(FileSystem* fs);
0257     void setState(State s) {
0258         m_State = s;
0259     }
0260     void deleteFileSystem();
0261 
0262 private:
0263     void setNumber(qint32 n) {
0264         m_Number = n;
0265     }
0266 
0267     qint32 m_Number = 0;
0268     Partitions m_Children;
0269     QPointer< PartitionNode > m_Parent = nullptr;
0270     FileSystem* m_FileSystem = nullptr;
0271     PartitionRole m_Roles;
0272     qint64 m_FirstSector = 0;
0273     qint64 m_LastSector = 0;
0274     QString m_DevicePath;
0275     QString m_Label;
0276     QString m_Type;
0277     QString m_UUID;
0278     quint64 m_Attributes = 0;
0279     QString m_PartitionPath;
0280     QString m_MountPoint;
0281     PartitionTable::Flags m_AvailableFlags;
0282     PartitionTable::Flags m_ActiveFlags;
0283     bool m_IsMounted = false;
0284     qint64 m_SectorSize = 0;
0285     State m_State = None;
0286 };
0287 
0288 QTextStream& operator<<(QTextStream& stream, const Partition& p);
0289 
0290 #endif