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

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
0005     SPDX-FileCopyrightText: 2015-2016 Teo Mrnjavac <teo@kde.org>
0006     SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
0007     SPDX-FileCopyrightText: 2017 Pali Rohár <pali.rohar@gmail.com>
0008     SPDX-FileCopyrightText: 2017 Adriaan de Groot <groot@kde.org>
0009     SPDX-FileCopyrightText: 2018 Caio Jordão Carvalho <caiojcarvalho@gmail.com>
0010     SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>
0011     SPDX-FileCopyrightText: 2020 Arnaud Ferraris <arnaud.ferraris@collabora.com>
0012     SPDX-FileCopyrightText: 2020 Gaël PORTAY <gael.portay@collabora.com>
0013 
0014     SPDX-License-Identifier: GPL-3.0-or-later
0015 */
0016 
0017 #ifndef KPMCORE_FILESYSTEM_H
0018 #define KPMCORE_FILESYSTEM_H
0019 
0020 #include "util/libpartitionmanagerexport.h"
0021 
0022 #include <QVariant>
0023 #include <QList>
0024 #include <QStringList>
0025 #include <QString>
0026 #include <QtGlobal>
0027 #include <QUrl>
0028 
0029 #include <memory>
0030 #include <vector>
0031 
0032 class QColor;
0033 class QValidator;
0034 class Device;
0035 class Report;
0036 struct FileSystemPrivate;
0037 
0038 /** Base class for all FileSystems.
0039 
0040     Represents a file system and handles support for various types of operations that can
0041     be performed on those.
0042 
0043     @author Volker Lanz <vl@fidra.de>
0044  */
0045 class LIBKPMCORE_EXPORT FileSystem
0046 {
0047     Q_DISABLE_COPY(FileSystem)
0048 
0049 public:
0050     class SupportTool
0051     {
0052     public:
0053         explicit SupportTool(const QString& n = QString(), const QUrl& u = QUrl()) : name(n), url(u) {}
0054 
0055         const QString name;
0056         const QUrl url;
0057     };
0058 
0059     /** Supported FileSystem types */
0060     enum Type : int {
0061         Unknown,
0062         Extended,
0063 
0064         Ext2,
0065         Ext3,
0066         Ext4,
0067         LinuxSwap,
0068         Fat16,
0069         Fat32,
0070         Ntfs,
0071         ReiserFS,
0072         Reiser4,
0073         Xfs,
0074         Jfs,
0075         Hfs,
0076         HfsPlus,
0077         Ufs,
0078         Unformatted,
0079         Btrfs,
0080         Hpfs,
0081         Luks,
0082         Ocfs2,
0083         Zfs,
0084         Exfat,
0085         Nilfs2,
0086         Lvm2_PV,
0087         F2fs,
0088         Udf,
0089         Iso9660,
0090         Luks2,
0091         Fat12,
0092         LinuxRaidMember,
0093         BitLocker,
0094         Apfs,
0095         Minix,
0096 
0097         __lastType
0098     };
0099 
0100     /** The type of support for a given FileSystem action */
0101     enum CommandSupportType {
0102         cmdSupportNone = 0,             /**< no support */
0103         cmdSupportCore = 1,             /**< internal support */
0104         cmdSupportFileSystem = 2,       /**< supported by some external command */
0105         cmdSupportBackend = 4           /**< supported by the backend */
0106     };
0107 
0108     static const std::vector<QColor> defaultColorCode;
0109 
0110     Q_DECLARE_FLAGS(CommandSupportTypes, CommandSupportType)
0111 
0112 protected:
0113     FileSystem(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, FileSystem::Type type);
0114     FileSystem(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, const QVariantMap& features, FileSystem::Type type);
0115 
0116     QString implPosixPermissions() const;
0117     void implSetPosixPermissions(const QString& permissions);
0118 
0119 public:
0120     virtual ~FileSystem();
0121 
0122 public:
0123     virtual void init() {}
0124     virtual void scan(const QString& deviceNode);
0125     virtual qint64 readUsedCapacity(const QString& deviceNode) const;
0126     virtual QString readLabel(const QString& deviceNode) const;
0127     virtual bool create(Report& report, const QString& deviceNode);
0128     virtual bool createWithLabel(Report& report, const QString& deviceNode, const QString& label);
0129     virtual bool resize(Report& report, const QString& deviceNode, qint64 newLength) const;
0130     virtual bool resizeOnline(Report& report, const QString& deviceNode, const QString& mountPoint, qint64 newLength) const;
0131     virtual bool move(Report& report, const QString& deviceNode, qint64 newStartSector) const;
0132     virtual bool writeLabel(Report& report, const QString& deviceNode, const QString& newLabel);
0133     virtual bool writeLabelOnline(Report& report, const QString& deviceNode, const QString& mountPoint, const QString& newLabel);
0134     virtual bool copy(Report& report, const QString& targetDeviceNode, const QString& sourceDeviceNode) const;
0135     virtual bool backup(Report& report, const Device& sourceDevice, const QString& deviceNode, const QString& filename) const;
0136     virtual bool remove(Report& report, const QString& deviceNode) const;
0137     virtual bool check(Report& report, const QString& deviceNode) const;
0138     virtual bool updateUUID(Report& report, const QString& deviceNode) const;
0139     virtual QString readUUID(const QString& deviceNode) const;
0140     virtual bool updateBootSector(Report& report, const QString& deviceNode) const;
0141 
0142     virtual CommandSupportType supportGetUsed() const {
0143         return cmdSupportNone;    /**< @return CommandSupportType for getting used capacity */
0144     }
0145     virtual CommandSupportType supportGetLabel() const {
0146         return cmdSupportNone;    /**< @return CommandSupportType for reading label*/
0147     }
0148     virtual CommandSupportType supportCreate() const {
0149         return cmdSupportNone;    /**< @return CommandSupportType for creating */
0150     }
0151     virtual CommandSupportType supportCreateWithLabel() const {
0152         return cmdSupportNone;    /**< @return CommandSupportType for creating */
0153     }
0154     virtual CommandSupportType supportCreateWithFeatures() const {
0155         return cmdSupportNone;    /**< @return CommandSupportType for creating */
0156     }
0157     virtual CommandSupportType supportGrow() const {
0158         return cmdSupportNone;    /**< @return CommandSupportType for growing */
0159     }
0160     virtual CommandSupportType supportGrowOnline() const {
0161         return cmdSupportNone;    /**< @return CommandSupportType for online growing */
0162     }
0163     virtual CommandSupportType supportShrink() const {
0164         return cmdSupportNone;    /**< @return CommandSupportType for shrinking */
0165     }
0166     virtual CommandSupportType supportShrinkOnline() const {
0167         return cmdSupportNone;    /**< @return CommandSupportType for shrinking */
0168     }
0169     virtual CommandSupportType supportMove() const {
0170         return cmdSupportNone;    /**< @return CommandSupportType for moving */
0171     }
0172     virtual CommandSupportType supportCheck() const {
0173         return cmdSupportNone;    /**< @return CommandSupportType for checking */
0174     }
0175     virtual CommandSupportType supportCheckOnline() const {
0176         return cmdSupportNone;    /**< @return CommandSupportType for checking */
0177     }
0178     virtual CommandSupportType supportCopy() const {
0179         return cmdSupportNone;    /**< @return CommandSupportType for copying */
0180     }
0181     virtual CommandSupportType supportBackup() const {
0182         return cmdSupportNone;    /**< @return CommandSupportType for backing up */
0183     }
0184     virtual CommandSupportType supportSetLabel() const {
0185         return cmdSupportNone;    /**< @return CommandSupportType for setting label */
0186     }
0187     virtual CommandSupportType supportSetLabelOnline() const {
0188         return cmdSupportNone;    /**< @return CommandSupportType for setting label of mounted file systems */
0189     }
0190     virtual CommandSupportType supportUpdateUUID() const {
0191         return cmdSupportNone;    /**< @return CommandSupportType for updating the UUID */
0192     }
0193     virtual CommandSupportType supportGetUUID() const {
0194         return cmdSupportNone;    /**< @return CommandSupportType for reading the UUID */
0195     }
0196 
0197     virtual qint64 minCapacity() const;
0198     virtual qint64 maxCapacity() const;
0199     virtual int maxLabelLength() const;
0200     virtual QValidator* labelValidator(QObject *parent = nullptr) const;
0201 
0202     virtual SupportTool supportToolName() const;
0203     virtual bool supportToolFound() const;
0204 
0205     virtual QString posixPermissions() const { return QString{}; };
0206     virtual void setPosixPermissions(const QString& permissions) { Q_UNUSED(permissions); };
0207 
0208     // Tries to change the posix permission on the filesystem, if the
0209     // filesystem supports it. by supports I mean reimplements `posixPermissions()`
0210     // and setPosixPermissions.
0211     bool execChangePosixPermission(Report& report, const QString& deviceNode);
0212 
0213     /**
0214      * Returns the (possibly translated) name of the type of this filesystem.
0215      * @see nameForType()
0216      */
0217     virtual QString name(const QStringList& languages = {}) const;
0218 
0219     /**
0220      * @return the FileSystem's type
0221      */
0222     virtual FileSystem::Type type() const;
0223 
0224     /**
0225      * Returns the name of the given filesystem type. If @p languages
0226      * is an empty list, uses the translated name of the filesystem,
0227      * in the default locale. If languages is {"C"}, an untranslated
0228      * string is returned. Passing other lists of language identifiers
0229      * may yield unpredicatable results -- see the documentation of
0230      * KLocalizedString() for details on the way toString() is used.
0231      * Returns a single QString with the name.
0232      */
0233     static QString nameForType(FileSystem::Type t, const QStringList& languages = {});
0234     static QList<FileSystem::Type> types();
0235     static FileSystem::Type typeForName(const QString& s, const QStringList& languages = {});
0236     static FileSystem::Type detectFileSystem(const QString& partitionPath);
0237     static QString detectMountPoint(FileSystem* fs, const QString& partitionPath);
0238     static bool detectMountStatus(FileSystem* fs, const QString& partitionPath);
0239 
0240     /**< @return true if this FileSystem can be mounted */
0241     virtual bool canMount(const QString& deviceNode, const QString& mountPoint) const;
0242     virtual bool canUnmount(const QString&) const {
0243         return true;    /**< @return true if this FileSystem can be unmounted */
0244     }
0245 
0246     virtual QString mountTitle() const;
0247     virtual QString unmountTitle() const;
0248 
0249     virtual bool mount(Report& report, const QString& deviceNode, const QString& mountPoint);
0250     virtual bool unmount(Report& report, const QString& deviceNode);
0251 
0252     /**< @return the FileSystem's first sector */
0253     qint64 firstSector() const;
0254 
0255     /**< @return the FileSystem's last sector */
0256     qint64 lastSector() const;
0257 
0258     qint64 length() const {
0259         return lastSector() - firstSector() + 1;    /**< @return the FileSystem's length */
0260     }
0261     qint64 firstByte() const {
0262         return firstSector() * sectorSize();    /**< @return the FileSystem's first byte */
0263     }
0264     qint64 lastByte() const {
0265         return firstByte() + length() * sectorSize() - 1;    /**< @return the FileSystem's last byte */
0266     }
0267 
0268     /**< @param s the new first sector */
0269     void setFirstSector(qint64 s);
0270 
0271     /**< @param s the new last sector */
0272     void setLastSector(qint64 s);
0273 
0274     void move(qint64 newStartSector);
0275 
0276     /**< @return the FileSystem's label */
0277     const QString& label() const;
0278 
0279     /**< @return the FileSystem's available features */
0280     const QStringList& availableFeatures() const;
0281 
0282     /**< @return the FileSystem's features */
0283     const QVariantMap& features() const;
0284 
0285     /**< @param the feature's name to add to the FileSystem */
0286     /**< @param the feature's value to add to the FileSystem */
0287     void addFeature(const QString& name, const QVariant& value);
0288 
0289     /**< @param features the list of features to add to the FileSystem */
0290     void addFeatures(const QVariantMap& features);
0291 
0292     /**< @return the sector size in the underlying Device */
0293     qint64 sectorSize() const;
0294 
0295     /**< @return the sectors in use on the FileSystem */
0296     qint64 sectorsUsed() const;
0297 
0298     /**< @return the FileSystem's UUID */
0299     const QString& uuid() const;
0300 
0301     /**< @param s the new value for sector size */
0302     void setSectorSize(qint64 s);
0303 
0304     /**< @param s the new value for sectors in use */
0305     void setSectorsUsed(qint64 s);
0306 
0307     /**< @param s the new label */
0308     void setLabel(const QString& s);
0309 
0310     /**< @param s the new UUID */
0311     void setUUID(const QString& s);
0312 
0313 protected:
0314     static bool findExternal(const QString& cmdName, const QStringList& args = QStringList(), int exptectedCode = 1);
0315     void addAvailableFeature(const QString& name);
0316 
0317     std::unique_ptr<FileSystemPrivate> d;
0318 };
0319 
0320 Q_DECLARE_OPERATORS_FOR_FLAGS(FileSystem::CommandSupportTypes)
0321 
0322 #endif