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

0001 /*
0002     SPDX-FileCopyrightText: 2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2014-2020 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
0005     SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
0006 
0007     SPDX-License-Identifier: GPL-3.0-or-later
0008 */
0009 
0010 #ifndef KPMCORE_COREBACKEND_H
0011 #define KPMCORE_COREBACKEND_H
0012 
0013 #include "util/libpartitionmanagerexport.h"
0014 #include "fs/filesystem.h"
0015 
0016 #include <memory>
0017 
0018 #include <QObject>
0019 #include <QList>
0020 
0021 class CoreBackendManager;
0022 class CoreBackendDevice;
0023 struct CoreBackendPrivate;
0024 class Device;
0025 class PartitionTable;
0026 
0027 class QString;
0028 
0029 enum class ScanFlag : uint8_t {
0030     includeReadOnly = 0x1, /**< devices that are read-only according to the kernel */
0031     includeLoopback = 0x2,
0032 };
0033 Q_DECLARE_FLAGS(ScanFlags, ScanFlag)
0034 Q_DECLARE_OPERATORS_FOR_FLAGS(ScanFlags)
0035 
0036 /**
0037   * Interface class for backend plugins.
0038   * @author Volker Lanz <vl@fidra.de>
0039   */
0040 
0041 class LIBKPMCORE_EXPORT CoreBackend : public QObject
0042 {
0043     Q_OBJECT
0044     Q_DISABLE_COPY(CoreBackend)
0045 
0046     friend class CoreBackendManager;
0047 
0048 protected:
0049     CoreBackend();
0050     ~CoreBackend() override;
0051 
0052 Q_SIGNALS:
0053     /**
0054      * Emitted to inform about progress of any kind.
0055       * @param i the progress in percent (from 0 to 100)
0056      */
0057     void progress(int i);
0058 
0059     /**
0060       * Emitted to inform about scan progress.
0061       * @param deviceNode the device being scanned just now (e.g. "/dev/sda")
0062       * @param i the progress in percent (from 0 to 100)
0063       */
0064     void scanProgress(const QString& deviceNode, int i);
0065 
0066 public:
0067     /**
0068       * Return the plugin's unique Id from JSON metadata
0069       * @return the plugin's unique Id from JSON metadata
0070       */
0071     QString id();
0072 
0073     /**
0074       * Return the plugin's version from JSON metadata
0075       * @return the plugin's version from JSON metadata
0076       */
0077     QString version();
0078 
0079     /**
0080       * Initialize the plugin's FileSystem support
0081       */
0082     virtual void initFSSupport() = 0;
0083 
0084     /**
0085       * Scan for devices in the system.
0086       * @param excludeReadOnly when true,  are left out of the list.
0087       *         When false (the default) all devices, writable or
0088       *         not, including CD ROM devices, are returned.
0089       * @return a QList of pointers to Device instances. The caller is responsible
0090       *         for deleting these objects.
0091       * @note A Device object is a description of the device, not
0092       *         an object to operate on. See openDevice().
0093       */
0094     [[deprecated("port to scanDevices(ScanFlags)")]] virtual QList<Device*> scanDevices(bool excludeReadOnly = false) = 0;
0095 
0096     /**
0097       * Scan for devices in the system.
0098       * @param scanFlags can be used to expand the list of scanned devices.
0099       * @return a QList of pointers to Device instances. The caller is responsible
0100       *         for deleting these objects.
0101       * @note A Device object is a description of the device, not
0102       *         an object to operate on. See openDevice().
0103       */
0104     virtual QList<Device*> scanDevices(const ScanFlags scanFlags) = 0;
0105 
0106     /**
0107       * Scan a single device in the system.
0108       * @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda1)
0109       * @return FileSystem type of the device on deviceNode
0110       */
0111     virtual FileSystem::Type detectFileSystem(const QString& deviceNode) = 0;
0112 
0113     /**
0114       * Read a file system label
0115       * @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda1)
0116       * @return FileSystem label on deviceNode
0117       */
0118     virtual QString readLabel(const QString& deviceNode) const = 0;
0119 
0120     /**
0121       * Read a file system UUID
0122       * @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda1)
0123       * @return FileSystem UUID on deviceNode
0124       */
0125     virtual QString readUUID(const QString& deviceNode) const = 0;
0126 
0127     /**
0128       * Scan a single device in the system.
0129       * @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda)
0130       * @return a pointer to a Device instance. The caller is responsible for deleting
0131       *         this object.
0132       */
0133     virtual Device* scanDevice(const QString& deviceNode) = 0;
0134 
0135     /**
0136       * Open a device for reading.
0137       * @param deviceNode The path of the device that is to be opened (e.g. /dev/sda)
0138       * @return a pointer to a CoreBackendDevice or nullptr if the open failed.
0139       */
0140     virtual std::unique_ptr<CoreBackendDevice> openDevice(const Device& d) = 0;
0141 
0142     /**
0143       * Open a device in exclusive mode for writing.
0144       * @param deviceNode The path of the device that is to be opened (e.g. /dev/sda)
0145       * @return a pointer to a CoreBackendDevice or nullptr if the open failed.
0146       */
0147     virtual std::unique_ptr<CoreBackendDevice> openDeviceExclusive(const Device& d) = 0;
0148 
0149     /**
0150       * Close a CoreBackendDevice that has previously been opened.
0151       * @param core_device Pointer to the CoreBackendDevice to be closed. Must not be nullptr.
0152       * @return true if closing the CoreBackendDevice succeeded, otherwise false.
0153       */
0154     virtual bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) = 0;
0155 
0156     /**
0157       * Emit progress.
0158       * @param i the progress in percent (from 0 to 100)
0159       * This is used to emit a progress() signal from somewhere deep inside the plugin
0160       * backend code if that is ever necessary.
0161       */
0162     virtual void emitProgress(int i);
0163 
0164     /**
0165       * Emit scan progress.
0166       * @param deviceNode the path to the device just being scanned (e.g. /dev/sda)
0167       * @param i the progress in percent (from 0 to 100)
0168       * This is used to emit a scanProgress() signal from the backend device scanning
0169       * code.
0170       */
0171     virtual void emitScanProgress(const QString& deviceNode, int i);
0172 
0173     static bool isPolkitInstalledCorrectly();
0174 
0175 protected:
0176     static void setPartitionTableForDevice(Device& d, PartitionTable* p);
0177     static void setPartitionTableMaxPrimaries(PartitionTable& p, qint32 max_primaries);
0178 
0179 private:
0180     void setId(const QString& id);
0181     void setVersion(const QString& version);
0182 
0183 private:
0184     std::unique_ptr<CoreBackendPrivate> d;
0185 };
0186 
0187 #endif