1 /*
2     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
3     SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu>
4     SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
5     SPDX-FileCopyrightText: 2015-2016 Teo Mrnjavac <teo@kde.org>
6     SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
7     SPDX-FileCopyrightText: 2017 Pali Rohár <pali.rohar@gmail.com>
8     SPDX-FileCopyrightText: 2017 Adriaan de Groot <groot@kde.org>
9     SPDX-FileCopyrightText: 2018 Caio Jordão Carvalho <caiojcarvalho@gmail.com>
10     SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>
11     SPDX-FileCopyrightText: 2020 Arnaud Ferraris <arnaud.ferraris@collabora.com>
12     SPDX-FileCopyrightText: 2020 Gaël PORTAY <gael.portay@collabora.com>
13 
14     SPDX-License-Identifier: GPL-3.0-or-later
15 */
16 
17 #ifndef KPMCORE_FILESYSTEM_H
18 #define KPMCORE_FILESYSTEM_H
19 
20 #include "util/libpartitionmanagerexport.h"
21 
22 #include <QVariant>
23 #include <QList>
24 #include <QStringList>
25 #include <QString>
26 #include <QtGlobal>
27 #include <QUrl>
28 
29 #include <memory>
30 #include <vector>
31 
32 class QColor;
33 class QValidator;
34 class Device;
35 class Report;
36 struct FileSystemPrivate;
37 
38 /** Base class for all FileSystems.
39 
40     Represents a file system and handles support for various types of operations that can
41     be performed on those.
42 
43     @author Volker Lanz <vl@fidra.de>
44  */
45 class LIBKPMCORE_EXPORT FileSystem
46 {
Q_DISABLE_COPY(FileSystem)47     Q_DISABLE_COPY(FileSystem)
48 
49 public:
50     class SupportTool
51     {
52     public:
53         explicit SupportTool(const QString& n = QString(), const QUrl& u = QUrl()) : name(n), url(u) {}
54 
55         const QString name;
56         const QUrl url;
57     };
58 
59     /** Supported FileSystem types */
60     enum Type : int {
61         Unknown,
62         Extended,
63 
64         Ext2,
65         Ext3,
66         Ext4,
67         LinuxSwap,
68         Fat16,
69         Fat32,
70         Ntfs,
71         ReiserFS,
72         Reiser4,
73         Xfs,
74         Jfs,
75         Hfs,
76         HfsPlus,
77         Ufs,
78         Unformatted,
79         Btrfs,
80         Hpfs,
81         Luks,
82         Ocfs2,
83         Zfs,
84         Exfat,
85         Nilfs2,
86         Lvm2_PV,
87         F2fs,
88         Udf,
89         Iso9660,
90         Luks2,
91         Fat12,
92         LinuxRaidMember,
93         BitLocker,
94         Apfs,
95         Minix,
96 
97         __lastType
98     };
99 
100     /** The type of support for a given FileSystem action */
101     enum CommandSupportType {
102         cmdSupportNone = 0,             /**< no support */
103         cmdSupportCore = 1,             /**< internal support */
104         cmdSupportFileSystem = 2,       /**< supported by some external command */
105         cmdSupportBackend = 4           /**< supported by the backend */
106     };
107 
108     static const std::vector<QColor> defaultColorCode;
109 
110     Q_DECLARE_FLAGS(CommandSupportTypes, CommandSupportType)
111 
112 protected:
113     FileSystem(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, FileSystem::Type type);
114     FileSystem(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, const QVariantMap& features, FileSystem::Type type);
115 
116 public:
117     virtual ~FileSystem();
118 
119 public:
init()120     virtual void init() {}
121     virtual void scan(const QString& deviceNode);
122     virtual qint64 readUsedCapacity(const QString& deviceNode) const;
123     virtual QString readLabel(const QString& deviceNode) const;
124     virtual bool create(Report& report, const QString& deviceNode);
125     virtual bool createWithLabel(Report& report, const QString& deviceNode, const QString& label);
126     virtual bool resize(Report& report, const QString& deviceNode, qint64 newLength) const;
127     virtual bool resizeOnline(Report& report, const QString& deviceNode, const QString& mountPoint, qint64 newLength) const;
128     virtual bool move(Report& report, const QString& deviceNode, qint64 newStartSector) const;
129     virtual bool writeLabel(Report& report, const QString& deviceNode, const QString& newLabel);
130     virtual bool writeLabelOnline(Report& report, const QString& deviceNode, const QString& mountPoint, const QString& newLabel);
131     virtual bool copy(Report& report, const QString& targetDeviceNode, const QString& sourceDeviceNode) const;
132     virtual bool backup(Report& report, const Device& sourceDevice, const QString& deviceNode, const QString& filename) const;
133     virtual bool remove(Report& report, const QString& deviceNode) const;
134     virtual bool check(Report& report, const QString& deviceNode) const;
135     virtual bool updateUUID(Report& report, const QString& deviceNode) const;
136     virtual QString readUUID(const QString& deviceNode) const;
137     virtual bool updateBootSector(Report& report, const QString& deviceNode) const;
138 
supportGetUsed()139     virtual CommandSupportType supportGetUsed() const {
140         return cmdSupportNone;    /**< @return CommandSupportType for getting used capacity */
141     }
supportGetLabel()142     virtual CommandSupportType supportGetLabel() const {
143         return cmdSupportNone;    /**< @return CommandSupportType for reading label*/
144     }
supportCreate()145     virtual CommandSupportType supportCreate() const {
146         return cmdSupportNone;    /**< @return CommandSupportType for creating */
147     }
supportCreateWithLabel()148     virtual CommandSupportType supportCreateWithLabel() const {
149         return cmdSupportNone;    /**< @return CommandSupportType for creating */
150     }
supportCreateWithFeatures()151     virtual CommandSupportType supportCreateWithFeatures() const {
152         return cmdSupportNone;    /**< @return CommandSupportType for creating */
153     }
supportGrow()154     virtual CommandSupportType supportGrow() const {
155         return cmdSupportNone;    /**< @return CommandSupportType for growing */
156     }
supportGrowOnline()157     virtual CommandSupportType supportGrowOnline() const {
158         return cmdSupportNone;    /**< @return CommandSupportType for online growing */
159     }
supportShrink()160     virtual CommandSupportType supportShrink() const {
161         return cmdSupportNone;    /**< @return CommandSupportType for shrinking */
162     }
supportShrinkOnline()163     virtual CommandSupportType supportShrinkOnline() const {
164         return cmdSupportNone;    /**< @return CommandSupportType for shrinking */
165     }
supportMove()166     virtual CommandSupportType supportMove() const {
167         return cmdSupportNone;    /**< @return CommandSupportType for moving */
168     }
supportCheck()169     virtual CommandSupportType supportCheck() const {
170         return cmdSupportNone;    /**< @return CommandSupportType for checking */
171     }
supportCheckOnline()172     virtual CommandSupportType supportCheckOnline() const {
173         return cmdSupportNone;    /**< @return CommandSupportType for checking */
174     }
supportCopy()175     virtual CommandSupportType supportCopy() const {
176         return cmdSupportNone;    /**< @return CommandSupportType for copying */
177     }
supportBackup()178     virtual CommandSupportType supportBackup() const {
179         return cmdSupportNone;    /**< @return CommandSupportType for backing up */
180     }
supportSetLabel()181     virtual CommandSupportType supportSetLabel() const {
182         return cmdSupportNone;    /**< @return CommandSupportType for setting label */
183     }
supportSetLabelOnline()184     virtual CommandSupportType supportSetLabelOnline() const {
185         return cmdSupportNone;    /**< @return CommandSupportType for setting label of mounted file systems */
186     }
supportUpdateUUID()187     virtual CommandSupportType supportUpdateUUID() const {
188         return cmdSupportNone;    /**< @return CommandSupportType for updating the UUID */
189     }
supportGetUUID()190     virtual CommandSupportType supportGetUUID() const {
191         return cmdSupportNone;    /**< @return CommandSupportType for reading the UUID */
192     }
193 
194     virtual qint64 minCapacity() const;
195     virtual qint64 maxCapacity() const;
196     virtual int maxLabelLength() const;
197     virtual QValidator* labelValidator(QObject *parent = nullptr) const;
198 
199     virtual SupportTool supportToolName() const;
200     virtual bool supportToolFound() const;
201 
202     /**
203      * Returns the (possibly translated) name of the type of this filesystem.
204      * @see nameForType()
205      */
206     virtual QString name(const QStringList& languages = {}) const;
207 
208     /**
209      * @return the FileSystem's type
210      */
211     virtual FileSystem::Type type() const;
212 
213     /**
214      * Returns the name of the given filesystem type. If @p languages
215      * is an empty list, uses the translated name of the filesystem,
216      * in the default locale. If languages is {"C"}, an untranslated
217      * string is returned. Passing other lists of language identifiers
218      * may yield unpredicatable results -- see the documentation of
219      * KLocalizedString() for details on the way toString() is used.
220      * Returns a single QString with the name.
221      */
222     static QString nameForType(FileSystem::Type t, const QStringList& languages = {});
223     static QList<FileSystem::Type> types();
224     static FileSystem::Type typeForName(const QString& s, const QStringList& languages = {});
225     static FileSystem::Type detectFileSystem(const QString& partitionPath);
226     static QString detectMountPoint(FileSystem* fs, const QString& partitionPath);
227     static bool detectMountStatus(FileSystem* fs, const QString& partitionPath);
228 
229     /**< @return true if this FileSystem can be mounted */
230     virtual bool canMount(const QString& deviceNode, const QString& mountPoint) const;
canUnmount(const QString &)231     virtual bool canUnmount(const QString&) const {
232         return true;    /**< @return true if this FileSystem can be unmounted */
233     }
234 
235     virtual QString mountTitle() const;
236     virtual QString unmountTitle() const;
237 
238     virtual bool mount(Report& report, const QString& deviceNode, const QString& mountPoint);
239     virtual bool unmount(Report& report, const QString& deviceNode);
240 
241     /**< @return the FileSystem's first sector */
242     qint64 firstSector() const;
243 
244     /**< @return the FileSystem's last sector */
245     qint64 lastSector() const;
246 
length()247     qint64 length() const {
248         return lastSector() - firstSector() + 1;    /**< @return the FileSystem's length */
249     }
firstByte()250     qint64 firstByte() const {
251         return firstSector() * sectorSize();    /**< @return the FileSystem's first byte */
252     }
lastByte()253     qint64 lastByte() const {
254         return firstByte() + length() * sectorSize() - 1;    /**< @return the FileSystem's last byte */
255     }
256 
257     /**< @param s the new first sector */
258     void setFirstSector(qint64 s);
259 
260     /**< @param s the new last sector */
261     void setLastSector(qint64 s);
262 
263     void move(qint64 newStartSector);
264 
265     /**< @return the FileSystem's label */
266     const QString& label() const;
267 
268     /**< @return the FileSystem's available features */
269     const QStringList& availableFeatures() const;
270 
271     /**< @return the FileSystem's features */
272     const QVariantMap& features() const;
273 
274     /**< @param the feature's name to add to the FileSystem */
275     /**< @param the feature's value to add to the FileSystem */
276     void addFeature(const QString& name, const QVariant& value);
277 
278     /**< @param features the list of features to add to the FileSystem */
279     void addFeatures(const QVariantMap& features);
280 
281     /**< @return the sector size in the underlying Device */
282     qint64 sectorSize() const;
283 
284     /**< @return the sectors in use on the FileSystem */
285     qint64 sectorsUsed() const;
286 
287     /**< @return the FileSystem's UUID */
288     const QString& uuid() const;
289 
290     /**< @param s the new value for sector size */
291     void setSectorSize(qint64 s);
292 
293     /**< @param s the new value for sectors in use */
294     void setSectorsUsed(qint64 s);
295 
296     /**< @param s the new label */
297     void setLabel(const QString& s);
298 
299     /**< @param s the new UUID */
300     void setUUID(const QString& s);
301 
302 protected:
303     static bool findExternal(const QString& cmdName, const QStringList& args = QStringList(), int exptectedCode = 1);
304     void addAvailableFeature(const QString& name);
305 
306     std::unique_ptr<FileSystemPrivate> d;
307 };
308 
309 Q_DECLARE_OPERATORS_FOR_FLAGS(FileSystem::CommandSupportTypes)
310 
311 #endif
312