Move FS detection code back to backends.

This commit is contained in:
Andrius Štikonas 2016-05-06 19:14:01 +01:00
parent befdf9d799
commit dc421db5c3
6 changed files with 89 additions and 78 deletions

View File

@ -20,7 +20,8 @@
#define COREBACKEND__H
#include "../util/libpartitionmanagerexport.h"
#include "util/libpartitionmanagerexport.h"
#include "fs/filesystem.h"
#include <QObject>
#include <QList>
@ -91,6 +92,13 @@ public:
*/
virtual QList<Device*> 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)

View File

@ -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

View File

@ -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

View File

@ -39,6 +39,8 @@
#include "util/globallog.h"
#include "util/helpers.h"
#include <blkid/blkid.h>
#include <QString>
#include <QStringList>
#include <QDebug>
@ -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<Device*> 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++)

View File

@ -66,13 +66,13 @@ public:
virtual bool closeDevice(CoreBackendDevice* core_device) override;
virtual Device* scanDevice(const QString& device_node) override;
virtual QList<Device*> 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

View File

@ -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 <filename>%2</filename>.", sector, device.deviceNode());
free(pedPath);
return rval;
return type;
}
bool LibPartedPartitionTable::setPartitionSystemType(Report& report, const Partition& partition)