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-2020 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2015-2016 Teo Mrnjavac <teo@kde.org>
0005     SPDX-FileCopyrightText: 2017 Pali Rohár <pali.rohar@gmail.com>
0006     SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>
0007     SPDX-FileCopyrightText: 2020 Arnaud Ferraris <arnaud.ferraris@collabora.com>
0008     SPDX-FileCopyrightText: 2020 Gaël PORTAY <gael.portay@collabora.com>
0009 
0010     SPDX-License-Identifier: GPL-3.0-or-later
0011 */
0012 
0013 #include <utility>
0014 
0015 #include "fs/filesystemfactory.h"
0016 #include "fs/filesystem.h"
0017 
0018 #include "fs/apfs.h"
0019 #include "fs/bitlocker.h"
0020 #include "fs/btrfs.h"
0021 #include "fs/exfat.h"
0022 #include "fs/ext2.h"
0023 #include "fs/ext3.h"
0024 #include "fs/ext4.h"
0025 #include "fs/extended.h"
0026 #include "fs/f2fs.h"
0027 #include "fs/fat12.h"
0028 #include "fs/fat16.h"
0029 #include "fs/fat32.h"
0030 #include "fs/hfs.h"
0031 #include "fs/hfsplus.h"
0032 #include "fs/hpfs.h"
0033 #include "fs/iso9660.h"
0034 #include "fs/jfs.h"
0035 #include "fs/linuxraidmember.h"
0036 #include "fs/linuxswap.h"
0037 #include "fs/luks.h"
0038 #include "fs/luks2.h"
0039 #include "fs/lvm2_pv.h"
0040 #include "fs/minix.h"
0041 #include "fs/nilfs2.h"
0042 #include "fs/ntfs.h"
0043 #include "fs/ocfs2.h"
0044 #include "fs/reiser4.h"
0045 #include "fs/reiserfs.h"
0046 #include "fs/udf.h"
0047 #include "fs/ufs.h"
0048 #include "fs/unformatted.h"
0049 #include "fs/unknown.h"
0050 #include "fs/xfs.h"
0051 #include "fs/zfs.h"
0052 
0053 #include "backend/corebackendmanager.h"
0054 #include "backend/corebackend.h"
0055 
0056 FileSystemFactory::FileSystems FileSystemFactory::m_FileSystems;
0057 
0058 /** Initializes the instance. */
0059 void FileSystemFactory::init()
0060 {
0061     FileSystems fileSystems;
0062     fileSystems.insert(FileSystem::Type::Apfs, new FS::apfs(-1, -1, -1, QString()));
0063     fileSystems.insert(FileSystem::Type::BitLocker, new FS::bitlocker(-1, -1, -1, QString()));
0064     fileSystems.insert(FileSystem::Type::Btrfs, new FS::btrfs(-1, -1, -1, QString()));
0065     fileSystems.insert(FileSystem::Type::Exfat, new FS::exfat(-1, -1, -1, QString()));
0066     fileSystems.insert(FileSystem::Type::Ext2, new FS::ext2(-1, -1, -1, QString()));
0067     fileSystems.insert(FileSystem::Type::Ext3, new FS::ext3(-1, -1, -1, QString()));
0068     fileSystems.insert(FileSystem::Type::Ext4, new FS::ext4(-1, -1, -1, QString()));
0069     fileSystems.insert(FileSystem::Type::Extended, new FS::extended(-1, -1, -1, QString()));
0070     fileSystems.insert(FileSystem::Type::F2fs, new FS::f2fs(-1, -1, -1, QString()));
0071     fileSystems.insert(FileSystem::Type::Fat12, new FS::fat12(-1, -1, -1, QString()));
0072     fileSystems.insert(FileSystem::Type::Fat16, new FS::fat16(-1, -1, -1, QString()));
0073     fileSystems.insert(FileSystem::Type::Fat32, new FS::fat32(-1, -1, -1, QString()));
0074     fileSystems.insert(FileSystem::Type::Hfs, new FS::hfs(-1, -1, -1, QString()));
0075     fileSystems.insert(FileSystem::Type::HfsPlus, new FS::hfsplus(-1, -1, -1, QString()));
0076     fileSystems.insert(FileSystem::Type::Hpfs, new FS::hpfs(-1, -1, -1, QString()));
0077     fileSystems.insert(FileSystem::Type::Iso9660, new FS::iso9660(-1, -1, -1, QString()));
0078     fileSystems.insert(FileSystem::Type::Jfs, new FS::jfs(-1, -1, -1, QString()));
0079     fileSystems.insert(FileSystem::Type::LinuxRaidMember, new FS::linuxraidmember(-1, -1, -1, QString()));
0080     fileSystems.insert(FileSystem::Type::LinuxSwap, new FS::linuxswap(-1, -1, -1, QString()));
0081     fileSystems.insert(FileSystem::Type::Luks, new FS::luks(-1, -1, -1, QString()));
0082     fileSystems.insert(FileSystem::Type::Luks2, new FS::luks2(-1, -1, -1, QString()));
0083     fileSystems.insert(FileSystem::Type::Lvm2_PV, new FS::lvm2_pv(-1, -1, -1, QString()));
0084     fileSystems.insert(FileSystem::Type::Minix, new FS::minix(-1, -1, -1, QString()));
0085     fileSystems.insert(FileSystem::Type::Nilfs2, new FS::nilfs2(-1, -1, -1, QString()));
0086     fileSystems.insert(FileSystem::Type::Ntfs, new FS::ntfs(-1, -1, -1, QString()));
0087     fileSystems.insert(FileSystem::Type::Ocfs2, new FS::ocfs2(-1, -1, -1, QString()));
0088     fileSystems.insert(FileSystem::Type::ReiserFS, new FS::reiserfs(-1, -1, -1, QString()));
0089     fileSystems.insert(FileSystem::Type::Reiser4, new FS::reiser4(-1, -1, -1, QString()));
0090     fileSystems.insert(FileSystem::Type::Udf, new FS::udf(-1, -1, -1, QString()));
0091     fileSystems.insert(FileSystem::Type::Ufs, new FS::ufs(-1, -1, -1, QString()));
0092     fileSystems.insert(FileSystem::Type::Unformatted, new FS::unformatted(-1, -1, -1, QString()));
0093     fileSystems.insert(FileSystem::Type::Unknown, new FS::unknown(-1, -1, -1, QString()));
0094     fileSystems.insert(FileSystem::Type::Xfs, new FS::xfs(-1, -1, -1, QString()));
0095     fileSystems.insert(FileSystem::Type::Zfs, new FS::zfs(-1, -1, -1, QString()));
0096 
0097     for (const auto &fs : std::as_const(fileSystems))
0098         fs->init();
0099 
0100     qDeleteAll(m_FileSystems);
0101     m_FileSystems.clear();
0102     m_FileSystems = fileSystems;
0103 
0104     CoreBackendManager::self()->backend()->initFSSupport();
0105 }
0106 
0107 /** Creates a new FileSystem
0108     @param t the FileSystem's type
0109     @param firstsector the FileSystem's first sector relative to the Device
0110     @param lastsector the FileSystem's last sector relative to the Device
0111     @param sectorsused the number of used sectors in the FileSystem
0112     @param label the FileSystem's label
0113     @return pointer to the newly created FileSystem object or nullptr if FileSystem could not be created
0114 */
0115 FileSystem* FileSystemFactory::create(FileSystem::Type t, qint64 firstsector, qint64 lastsector, qint64 sectorSize, qint64 sectorsused, const QString& label, const QVariantMap& features, const QString& uuid)
0116 {
0117     FileSystem* fs = nullptr;
0118 
0119     switch (t) {
0120     case FileSystem::Type::Apfs:            fs = new FS::apfs           (firstsector, lastsector, sectorsused, label, features); break;
0121     case FileSystem::Type::BitLocker:       fs = new FS::bitlocker      (firstsector, lastsector, sectorsused, label, features); break;
0122     case FileSystem::Type::Btrfs:           fs = new FS::btrfs          (firstsector, lastsector, sectorsused, label, features); break;
0123     case FileSystem::Type::Exfat:           fs = new FS::exfat          (firstsector, lastsector, sectorsused, label, features); break;
0124     case FileSystem::Type::Ext2:            fs = new FS::ext2           (firstsector, lastsector, sectorsused, label, features); break;
0125     case FileSystem::Type::Ext3:            fs = new FS::ext3           (firstsector, lastsector, sectorsused, label, features); break;
0126     case FileSystem::Type::Ext4:            fs = new FS::ext4           (firstsector, lastsector, sectorsused, label, features); break;
0127     case FileSystem::Type::Extended:        fs = new FS::extended       (firstsector, lastsector, sectorsused, label, features); break;
0128     case FileSystem::Type::F2fs:            fs = new FS::f2fs           (firstsector, lastsector, sectorsused, label, features); break;
0129     case FileSystem::Type::Fat12:           fs = new FS::fat12          (firstsector, lastsector, sectorsused, label, features); break;
0130     case FileSystem::Type::Fat16:           fs = new FS::fat16          (firstsector, lastsector, sectorsused, label, features); break;
0131     case FileSystem::Type::Fat32:           fs = new FS::fat32          (firstsector, lastsector, sectorsused, label, features); break;
0132     case FileSystem::Type::Hfs:             fs = new FS::hfs            (firstsector, lastsector, sectorsused, label, features); break;
0133     case FileSystem::Type::HfsPlus:         fs = new FS::hfsplus        (firstsector, lastsector, sectorsused, label, features); break;
0134     case FileSystem::Type::Hpfs:            fs = new FS::hpfs           (firstsector, lastsector, sectorsused, label, features); break;
0135     case FileSystem::Type::Iso9660:         fs = new FS::iso9660        (firstsector, lastsector, sectorsused, label, features); break;
0136     case FileSystem::Type::Jfs:             fs = new FS::jfs            (firstsector, lastsector, sectorsused, label, features); break;
0137     case FileSystem::Type::LinuxRaidMember: fs = new FS::linuxraidmember(firstsector, lastsector, sectorsused, label, features); break;
0138     case FileSystem::Type::LinuxSwap:       fs = new FS::linuxswap      (firstsector, lastsector, sectorsused, label, features); break;
0139     case FileSystem::Type::Luks:            fs = new FS::luks           (firstsector, lastsector, sectorsused, label, features); break;
0140     case FileSystem::Type::Luks2:           fs = new FS::luks2          (firstsector, lastsector, sectorsused, label, features); break;
0141     case FileSystem::Type::Lvm2_PV:         fs = new FS::lvm2_pv        (firstsector, lastsector, sectorsused, label, features); break;
0142     case FileSystem::Type::Minix:           fs = new FS::minix          (firstsector, lastsector, sectorsused, label, features); break;
0143     case FileSystem::Type::Nilfs2:          fs = new FS::nilfs2         (firstsector, lastsector, sectorsused, label, features); break;
0144     case FileSystem::Type::Ntfs:            fs = new FS::ntfs           (firstsector, lastsector, sectorsused, label, features); break;
0145     case FileSystem::Type::Ocfs2:           fs = new FS::ocfs2          (firstsector, lastsector, sectorsused, label, features); break;
0146     case FileSystem::Type::ReiserFS:        fs = new FS::reiserfs       (firstsector, lastsector, sectorsused, label, features); break;
0147     case FileSystem::Type::Reiser4:         fs = new FS::reiser4        (firstsector, lastsector, sectorsused, label, features); break;
0148     case FileSystem::Type::Udf:             fs = new FS::udf            (firstsector, lastsector, sectorsused, label, features); break;
0149     case FileSystem::Type::Ufs:             fs = new FS::ufs            (firstsector, lastsector, sectorsused, label, features); break;
0150     case FileSystem::Type::Unformatted:     fs = new FS::unformatted    (firstsector, lastsector, sectorsused, label, features); break;
0151     case FileSystem::Type::Unknown:         fs = new FS::unknown        (firstsector, lastsector, sectorsused, label, features); break;
0152     case FileSystem::Type::Xfs:             fs = new FS::xfs            (firstsector, lastsector, sectorsused, label, features); break;
0153     case FileSystem::Type::Zfs:             fs = new FS::zfs            (firstsector, lastsector, sectorsused, label, features); break;
0154     default:                       break;
0155     }
0156 
0157     if (fs != nullptr) {
0158         fs->setUUID(uuid);
0159         fs->setSectorSize(sectorSize);
0160     }
0161 
0162     return fs;
0163 }
0164 
0165 /**
0166     @overload
0167 */
0168 FileSystem* FileSystemFactory::create(const FileSystem& other)
0169 {
0170     return create(other.type(), other.firstSector(), other.lastSector(), other.sectorSize(), other.sectorsUsed(), other.label(), other.features(), other.uuid());
0171 }
0172 
0173 /** @return the map of FileSystems */
0174 const FileSystemFactory::FileSystems& FileSystemFactory::map()
0175 {
0176     return m_FileSystems;
0177 }
0178 
0179 /** Clones a FileSystem from another one, but with a new type.
0180     @param newType the new FileSystem's type
0181     @param other the old FileSystem to clone
0182     @return pointer to the newly created FileSystem or nullptr in case of errors
0183 */
0184 FileSystem* FileSystemFactory::cloneWithNewType(FileSystem::Type newType, const FileSystem& other)
0185 {
0186     return create(newType, other.firstSector(), other.lastSector(), other.sectorSize(), other.sectorsUsed(), other.label(), other.features());
0187 }