diff --git a/src/backend/corebackend.h b/src/backend/corebackend.h index 9ad2cbd..35d7367 100644 --- a/src/backend/corebackend.h +++ b/src/backend/corebackend.h @@ -20,7 +20,8 @@ #define COREBACKEND__H -#include "../util/libpartitionmanagerexport.h" +#include "util/libpartitionmanagerexport.h" +#include "fs/filesystem.h" #include #include @@ -91,6 +92,13 @@ public: */ virtual QList scanDevices(bool excludeReadOnly = false) = 0; + /** + * Scan a single device in the system. + * @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda) + * @return FileSystem type of the device on deviceNode + */ + virtual FileSystem::Type detectFileSystem(const QString& deviceNode) = 0; + /** * Scan a single device in the system. * @param device_node The path to the device that is to be scanned (e.g. /dev/sda) diff --git a/src/fs/filesystem.cpp b/src/fs/filesystem.cpp index 8f87611..9fe2fcc 100644 --- a/src/fs/filesystem.cpp +++ b/src/fs/filesystem.cpp @@ -19,6 +19,9 @@ #include "fs/filesystem.h" +#include "backend/corebackend.h" +#include "backend/corebackendmanager.h" + #include "util/externalcommand.h" #include "util/capacity.h" @@ -110,57 +113,7 @@ static QString readBlkIdValue(const QString& deviceNode, const QString& tag) FileSystem::Type FileSystem::detectFileSystem(const QString& partitionPath) { - FileSystem::Type rval = FileSystem::Unknown; - - blkid_cache cache; - if (blkid_get_cache(&cache, nullptr) == 0) { - blkid_dev dev; - - if ((dev = blkid_get_dev(cache, - partitionPath.toLocal8Bit().constData(), - BLKID_DEV_NORMAL)) != nullptr) { - QString s = QString::fromUtf8(blkid_get_tag_value(cache, - "TYPE", - partitionPath.toLocal8Bit().constData())); - - if (s == QStringLiteral("ext2")) rval = FileSystem::Ext2; - else if (s == QStringLiteral("ext3")) rval = FileSystem::Ext3; - else if (s.startsWith(QStringLiteral("ext4"))) rval = FileSystem::Ext4; - else if (s == QStringLiteral("swap")) rval = FileSystem::LinuxSwap; - else if (s == QStringLiteral("ntfs")) rval = FileSystem::Ntfs; - else if (s == QStringLiteral("reiserfs")) rval = FileSystem::ReiserFS; - else if (s == QStringLiteral("reiser4")) rval = FileSystem::Reiser4; - else if (s == QStringLiteral("xfs")) rval = FileSystem::Xfs; - else if (s == QStringLiteral("jfs")) rval = FileSystem::Jfs; - else if (s == QStringLiteral("hfs")) rval = FileSystem::Hfs; - else if (s == QStringLiteral("hfsplus")) rval = FileSystem::HfsPlus; - else if (s == QStringLiteral("ufs")) rval = FileSystem::Ufs; - else if (s == QStringLiteral("vfat")) { - // libblkid uses SEC_TYPE to distinguish between FAT16 and FAT32 - QString st = QString::fromUtf8(blkid_get_tag_value(cache, - "SEC_TYPE", - partitionPath.toLocal8Bit().constData())); - if (st == QStringLiteral("msdos")) - rval = FileSystem::Fat16; - else - rval = FileSystem::Fat32; - } else if (s == QStringLiteral("btrfs")) rval = FileSystem::Btrfs; - else if (s == QStringLiteral("ocfs2")) rval = FileSystem::Ocfs2; - else if (s == QStringLiteral("zfs_member")) rval = FileSystem::Zfs; - else if (s == QStringLiteral("hpfs")) rval = FileSystem::Hpfs; - else if (s == QStringLiteral("crypto_LUKS")) rval = FileSystem::Luks; - else if (s == QStringLiteral("exfat")) rval = FileSystem::Exfat; - else if (s == QStringLiteral("nilfs2")) rval = FileSystem::Nilfs2; - else if (s == QStringLiteral("LVM2_member")) rval = FileSystem::Lvm2_PV; - else if (s == QStringLiteral("f2fs")) rval = FileSystem::F2fs; - else - qWarning() << "blkid: unknown file system type " << s << " on " << partitionPath; - } - - blkid_put_cache(cache); - } - - return rval; + return CoreBackendManager::self()->backend()->detectFileSystem(partitionPath); } /** Reads the label for this FileSystem diff --git a/src/plugins/dummy/dummybackend.h b/src/plugins/dummy/dummybackend.h index 9fa5be7..20e63e4 100644 --- a/src/plugins/dummy/dummybackend.h +++ b/src/plugins/dummy/dummybackend.h @@ -49,6 +49,7 @@ public: virtual CoreBackendDevice* openDeviceExclusive(const QString& device_node) override; virtual bool closeDevice(CoreBackendDevice* core_device) override; virtual Device* scanDevice(const QString& device_node) override; + virtual FileSystem::Type detectFileSystem(const QString& deviceNode) override {return FileSystem::Unknown;} }; #endif diff --git a/src/plugins/libparted/libpartedbackend.cpp b/src/plugins/libparted/libpartedbackend.cpp index eab12c7..f430f10 100644 --- a/src/plugins/libparted/libpartedbackend.cpp +++ b/src/plugins/libparted/libpartedbackend.cpp @@ -39,6 +39,8 @@ #include "util/globallog.h" #include "util/helpers.h" +#include + #include #include #include @@ -318,7 +320,12 @@ void LibPartedBackend::scanDevicePartitions(Device& d, PedDisk* pedDisk) continue; PartitionRole::Roles r = PartitionRole::None; - FileSystem::Type type = detectFileSystem(pedPartition); + + char* pedPath = ped_partition_get_path(pedPartition); + FileSystem::Type type = FileSystem::Unknown; + if (pedPath) + type = detectFileSystem(QString::fromUtf8(pedPath)); + free(pedPath); switch (pedPartition->type) { case PED_PARTITION_NORMAL: @@ -470,6 +477,65 @@ QList LibPartedBackend::scanDevices(bool excludeReadOnly) return result; } +/** Detects the type of a FileSystem given a PedDevice and a PedPartition + @param pedPartition pointer to the pedPartition. Must not be nullptr + @return the detected FileSystem type (FileSystem::Unknown if not detected) +*/ +FileSystem::Type LibPartedBackend::detectFileSystem(const QString& partitionPath) +{ + FileSystem::Type rval = FileSystem::Unknown; + + blkid_cache cache; + if (blkid_get_cache(&cache, nullptr) == 0) { + blkid_dev dev; + + if ((dev = blkid_get_dev(cache, + partitionPath.toLocal8Bit().constData(), + BLKID_DEV_NORMAL)) != nullptr) { + QString s = QString::fromUtf8(blkid_get_tag_value(cache, + "TYPE", + partitionPath.toLocal8Bit().constData())); + + if (s == QStringLiteral("ext2")) rval = FileSystem::Ext2; + else if (s == QStringLiteral("ext3")) rval = FileSystem::Ext3; + else if (s.startsWith(QStringLiteral("ext4"))) rval = FileSystem::Ext4; + else if (s == QStringLiteral("swap")) rval = FileSystem::LinuxSwap; + else if (s == QStringLiteral("ntfs")) rval = FileSystem::Ntfs; + else if (s == QStringLiteral("reiserfs")) rval = FileSystem::ReiserFS; + else if (s == QStringLiteral("reiser4")) rval = FileSystem::Reiser4; + else if (s == QStringLiteral("xfs")) rval = FileSystem::Xfs; + else if (s == QStringLiteral("jfs")) rval = FileSystem::Jfs; + else if (s == QStringLiteral("hfs")) rval = FileSystem::Hfs; + else if (s == QStringLiteral("hfsplus")) rval = FileSystem::HfsPlus; + else if (s == QStringLiteral("ufs")) rval = FileSystem::Ufs; + else if (s == QStringLiteral("vfat")) { + // libblkid uses SEC_TYPE to distinguish between FAT16 and FAT32 + QString st = QString::fromUtf8(blkid_get_tag_value(cache, + "SEC_TYPE", + partitionPath.toLocal8Bit().constData())); + if (st == QStringLiteral("msdos")) + rval = FileSystem::Fat16; + else + rval = FileSystem::Fat32; + } else if (s == QStringLiteral("btrfs")) rval = FileSystem::Btrfs; + else if (s == QStringLiteral("ocfs2")) rval = FileSystem::Ocfs2; + else if (s == QStringLiteral("zfs_member")) rval = FileSystem::Zfs; + else if (s == QStringLiteral("hpfs")) rval = FileSystem::Hpfs; + else if (s == QStringLiteral("crypto_LUKS")) rval = FileSystem::Luks; + else if (s == QStringLiteral("exfat")) rval = FileSystem::Exfat; + else if (s == QStringLiteral("nilfs2")) rval = FileSystem::Nilfs2; + else if (s == QStringLiteral("LVM2_member")) rval = FileSystem::Lvm2_PV; + else if (s == QStringLiteral("f2fs")) rval = FileSystem::F2fs; + else + qWarning() << "blkid: unknown file system type " << s << " on " << partitionPath; + } + + blkid_put_cache(cache); + } + + return rval; +} + CoreBackendDevice* LibPartedBackend::openDevice(const QString& device_node) { LibPartedDevice* device = new LibPartedDevice(device_node); @@ -499,24 +565,6 @@ bool LibPartedBackend::closeDevice(CoreBackendDevice* core_device) return core_device->close(); } -/** Detects the type of a FileSystem given a PedDevice and a PedPartition - @param pedPartition pointer to the pedPartition. Must not be nullptr - @return the detected FileSystem type (FileSystem::Unknown if not detected) -*/ -FileSystem::Type LibPartedBackend::detectFileSystem(PedPartition* pedPartition) -{ - FileSystem::Type rval = FileSystem::Unknown; - - char* pedPath = ped_partition_get_path(pedPartition); - - if (pedPath) - rval = FileSystem::detectFileSystem(QString::fromUtf8(pedPath)); - - free(pedPath); - - return rval; -} - PedPartitionFlag LibPartedBackend::getPedFlag(PartitionTable::Flag flag) { for (quint32 i = 0; i < sizeof(flagmap) / sizeof(flagmap[0]); i++) diff --git a/src/plugins/libparted/libpartedbackend.h b/src/plugins/libparted/libpartedbackend.h index 39f832b..97a4da8 100644 --- a/src/plugins/libparted/libpartedbackend.h +++ b/src/plugins/libparted/libpartedbackend.h @@ -66,13 +66,13 @@ public: virtual bool closeDevice(CoreBackendDevice* core_device) override; virtual Device* scanDevice(const QString& device_node) override; virtual QList scanDevices(bool excludeReadOnly = false) override; + virtual FileSystem::Type detectFileSystem(const QString& partitionPath) override; static QString lastPartedExceptionMessage(); private: - static FileSystem::Type detectFileSystem(PedPartition* pedPartition); static PedPartitionFlag getPedFlag(PartitionTable::Flag flag); - static void scanDevicePartitions(Device& d, PedDisk* pedDisk); + void scanDevicePartitions(Device& d, PedDisk* pedDisk); }; #endif diff --git a/src/plugins/libparted/libpartedpartitiontable.cpp b/src/plugins/libparted/libpartedpartitiontable.cpp index d75f185..a369e43 100644 --- a/src/plugins/libparted/libpartedpartitiontable.cpp +++ b/src/plugins/libparted/libpartedpartitiontable.cpp @@ -299,14 +299,15 @@ FileSystem::Type LibPartedPartitionTable::detectFileSystemBySector(Report& repor { PedPartition* pedPartition = ped_disk_get_partition_by_sector(pedDisk(), sector); - FileSystem::Type rval = FileSystem::Unknown; - - if (pedPartition) - rval = LibPartedBackend::detectFileSystem(pedPartition); + char* pedPath = ped_partition_get_path(pedPartition); + FileSystem::Type type = FileSystem::Unknown; + if (pedPartition && pedPath) + type = CoreBackendManager::self()->backend()->detectFileSystem(QString::fromUtf8(pedPath)); else report.line() << xi18nc("@info/plain", "Could not determine file system of partition at sector %1 on device %2.", sector, device.deviceNode()); + free(pedPath); - return rval; + return type; } bool LibPartedPartitionTable::setPartitionSystemType(Report& report, const Partition& partition)