Move file system label and UUID read code to backends.

This allows to use different implementations on different platforms.
E.g. libblkid is not available on FreeBSD.

libparted backend still uses the same libblkid implementation
sfdisk backend reads label and UUID from udev database
This commit is contained in:
Andrius Štikonas 2018-01-31 15:04:55 +00:00
parent 7e8f64ffd7
commit ba4e443223
9 changed files with 98 additions and 27 deletions

View File

@ -100,11 +100,25 @@ public:
/**
* Scan a single device in the system.
* @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda)
* @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda1)
* @return FileSystem type of the device on deviceNode
*/
virtual FileSystem::Type detectFileSystem(const QString& deviceNode) = 0;
/**
* Read a file system label
* @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda1)
* @return FileSystem label on deviceNode
*/
virtual QString readLabel(const QString& deviceNode) const = 0;
/**
* Read a file system UUID
* @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda1)
* @return FileSystem UUID on deviceNode
*/
virtual QString readUUID(const QString& deviceNode) const = 0;
/**
* Scan a single device in the system.
* @param deviceNode The path to the device that is to be scanned (e.g. /dev/sda)

View File

@ -29,8 +29,6 @@
#include "util/capacity.h"
#include "util/helpers.h"
#include <blkid/blkid.h>
#include <KLocalizedString>
#include <QFileInfo>
@ -101,27 +99,6 @@ qint64 FileSystem::readUsedCapacity(const QString& deviceNode) const
return -1;
}
static QString readBlkIdValue(const QString& deviceNode, const QString& tag)
{
blkid_cache cache;
QString rval;
if (blkid_get_cache(&cache, nullptr) == 0) {
blkid_dev dev;
char* label = nullptr;
if ((dev = blkid_get_dev(cache, deviceNode.toLocal8Bit().constData(), BLKID_DEV_NORMAL)) != nullptr &&
(label = blkid_get_tag_value(cache, tag.toLocal8Bit().constData(), deviceNode.toLocal8Bit().constData()))) {
rval = QString::fromLocal8Bit(label);
free(label);
}
blkid_put_cache(cache);
}
return rval;
}
FileSystem::Type FileSystem::detectFileSystem(const QString& partitionPath)
{
return CoreBackendManager::self()->backend()->detectFileSystem(partitionPath);
@ -168,7 +145,7 @@ bool FileSystem::detectMountStatus(FileSystem* fs, const QString& partitionPath)
*/
QString FileSystem::readLabel(const QString& deviceNode) const
{
return readBlkIdValue(deviceNode, QStringLiteral("LABEL"));
return CoreBackendManager::self()->backend()->readLabel(deviceNode);
}
/** Creates a new FileSystem
@ -363,7 +340,7 @@ bool FileSystem::updateUUID(Report& report, const QString& deviceNode) const
*/
QString FileSystem::readUUID(const QString& deviceNode) const
{
return readBlkIdValue(deviceNode, QStringLiteral("UUID"));
return CoreBackendManager::self()->backend()->readUUID(deviceNode);
}
/** Give implementations of FileSystem a chance to update the boot sector after the

View File

@ -18,8 +18,8 @@
*************************************************************************/
#if !defined(KPMCORE_FILESYSTEM_H)
#define KPMCORE_FILESYSTEM_H
#include "util/libpartitionmanagerexport.h"
#include <QColor>

View File

@ -77,6 +77,20 @@ FileSystem::Type DummyBackend::detectFileSystem(const QString& deviceNode)
return FileSystem::Unknown;
}
QString DummyBackend::readLabel(const QString& deviceNode) const
{
Q_UNUSED(deviceNode)
return QString();
}
QString DummyBackend::readUUID(const QString& deviceNode) const
{
Q_UNUSED(deviceNode)
return QString();
}
CoreBackendDevice* DummyBackend::openDevice(const Device& d)
{
DummyDevice* device = new DummyDevice(d.deviceNode());

View File

@ -50,6 +50,8 @@ public:
bool closeDevice(CoreBackendDevice* coreDevice) override;
Device* scanDevice(const QString& deviceNode) override;
FileSystem::Type detectFileSystem(const QString& deviceNode) override;
QString readLabel(const QString& deviceNode) const override;
QString readUUID(const QString& deviceNode) const override;
};
#endif

View File

@ -524,6 +524,37 @@ FileSystem::Type LibPartedBackend::detectFileSystem(const QString& partitionPath
return rval;
}
static QString readBlkIdValue(const QString& deviceNode, const QString& tag)
{
blkid_cache cache;
QString rval;
if (blkid_get_cache(&cache, nullptr) == 0) {
blkid_dev dev;
char* label = nullptr;
if ((dev = blkid_get_dev(cache, deviceNode.toLocal8Bit().constData(), BLKID_DEV_NORMAL)) != nullptr &&
(label = blkid_get_tag_value(cache, tag.toLocal8Bit().constData(), deviceNode.toLocal8Bit().constData()))) {
rval = QString::fromLocal8Bit(label);
free(label);
}
blkid_put_cache(cache);
}
return rval;
}
QString LibPartedBackend::readLabel(const QString& deviceNode) const
{
return readBlkIdValue(deviceNode, QStringLiteral("LABEL"));
}
QString LibPartedBackend::readUUID(const QString& deviceNode) const
{
return readBlkIdValue(deviceNode, QStringLiteral("UUID"));
}
CoreBackendDevice* LibPartedBackend::openDevice(const Device& d)
{
LibPartedDevice* device = new LibPartedDevice(d.deviceNode());

View File

@ -67,6 +67,8 @@ public:
DiskDevice* scanDevice(const QString& deviceNode) override;
QList<Device*> scanDevices(bool excludeReadOnly = false) override;
FileSystem::Type detectFileSystem(const QString& partitionPath) override;
QString readLabel(const QString& deviceNode) const override;
QString readUUID(const QString& deviceNode) const override;
static QString lastPartedExceptionMessage();

View File

@ -358,6 +358,35 @@ FileSystem::Type SfdiskBackend::detectFileSystem(const QString& partitionPath)
return rval;
}
QString SfdiskBackend::readLabel(const QString& deviceNode) const
{
ExternalCommand udevCommand(QStringLiteral("udevadm"), {
QStringLiteral("info"),
QStringLiteral("--query=property"),
deviceNode });
QRegularExpression re(QStringLiteral("ID_FS_LABEL=(\\w+)"));
QRegularExpressionMatch reFileSystemLabel = re.match(udevCommand.output());
if (reFileSystemLabel.hasMatch())
return reFileSystemLabel.captured(1);
return QString();
}
QString SfdiskBackend::readUUID(const QString& deviceNode) const
{
ExternalCommand udevCommand(QStringLiteral("udevadm"), {
QStringLiteral("info"),
QStringLiteral("--query=property"),
deviceNode });
QRegularExpression re(QStringLiteral("ID_FS_UUID=(\\w+)"));
QRegularExpressionMatch reFileSystemUUID = re.match(udevCommand.output());
if (reFileSystemUUID.hasMatch())
return reFileSystemUUID.captured(1);
return QString();
}
PartitionTable::Flags SfdiskBackend::availableFlags(PartitionTable::TableType type)
{
PartitionTable::Flags flags;

View File

@ -52,6 +52,8 @@ public:
bool closeDevice(CoreBackendDevice* coreDevice) override;
Device* scanDevice(const QString& deviceNode) override;
FileSystem::Type detectFileSystem(const QString& partitionPath) override;
QString readLabel(const QString& deviceNode) const override;
QString readUUID(const QString& deviceNode) const override;
private:
static void readSectorsUsed(const Device& d, Partition& p, const QString& mountPoint);