Switch file system detection code to udevadm.

This commit is contained in:
Andrius Štikonas 2017-12-14 01:14:45 +00:00
parent e7b0192630
commit 8c030baf3e
1 changed files with 55 additions and 48 deletions

View File

@ -39,6 +39,7 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRegularExpression>
#include <QStorageInfo>
#include <QString>
#include <QStringList>
@ -291,20 +292,26 @@ FileSystem::Type SfdiskBackend::detectFileSystem(const QString& partitionPath)
{
FileSystem::Type rval = FileSystem::Unknown;
ExternalCommand lsblkCommand(QStringLiteral("lsblk"), {
QStringLiteral("--json"),
QStringLiteral("--paths"),
QStringLiteral("--output=name,fstype"),
ExternalCommand udevCommand(QStringLiteral("udevadm"), {
QStringLiteral("info"),
QStringLiteral("--query=property"),
partitionPath });
if (lsblkCommand.run(-1) && lsblkCommand.exitCode() == 0) {
const QJsonArray partitionArray = QJsonDocument::fromJson(lsblkCommand.rawOutput()).object()[QLatin1String("blockdevices")].toArray();
if (udevCommand.run(-1) && udevCommand.exitCode() == 0) {
QRegularExpression re(QStringLiteral("ID_FS_TYPE=(\\w+)"));
QRegularExpression re2(QStringLiteral("ID_FS_VERSION=(\\w+)"));
QRegularExpressionMatch reFileSystemType = re.match(udevCommand.output());
QRegularExpressionMatch reFileSystemVersion = re2.match(udevCommand.output());
for (const auto &partition : partitionArray) {
QJsonObject partitionObject = partition.toObject();
QString node = partitionObject[QLatin1String("name")].toString();
QString s;
if (reFileSystemType.hasMatch()) {
s = reFileSystemType.captured(1);
}
QString s = partitionObject[QLatin1String("fstype")].toString();
QString version;
if (reFileSystemVersion.hasMatch()) {
version = reFileSystemVersion.captured(1);
}
if (s == QStringLiteral("ext2")) rval = FileSystem::Ext2;
else if (s == QStringLiteral("ext3")) rval = FileSystem::Ext3;
@ -319,21 +326,22 @@ FileSystem::Type SfdiskBackend::detectFileSystem(const QString& partitionPath)
else if (s == QStringLiteral("hfsplus")) rval = FileSystem::HfsPlus;
else if (s == QStringLiteral("ufs")) rval = FileSystem::Ufs;
else if (s == QStringLiteral("vfat")) {
ExternalCommand blkidCommand(QStringLiteral("blkid"), {
QStringLiteral("--output=value"),
QStringLiteral("--match-tag"),
QStringLiteral("SEC_TYPE"),
partitionPath });
// blkid uses SEC_TYPE to distinguish between FAT16 and FAT32
if (blkidCommand.run(-1) && blkidCommand.exitCode() == 0 && blkidCommand.output().trimmed() == QStringLiteral("msdos"))
if (version == QStringLiteral("FAT12") || version == QStringLiteral("FAT16"))
rval = FileSystem::Fat16;
else
else if (version == QStringLiteral("FAT32"))
rval = FileSystem::Fat32;
} else if (s == QStringLiteral("btrfs")) rval = FileSystem::Btrfs;
}
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("crypto_LUKS")) {
if (version == QStringLiteral("1"))
rval = FileSystem::Luks;
else if (version == QStringLiteral("2")) {
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;
@ -341,8 +349,7 @@ FileSystem::Type SfdiskBackend::detectFileSystem(const QString& partitionPath)
else if (s == QStringLiteral("udf")) rval = FileSystem::Udf;
else if (s == QStringLiteral("iso9660")) rval = FileSystem::Iso9660;
else
qWarning() << "lsblk: unknown file system type " << s << " on " << partitionPath;
}
qWarning() << "unknown file system type " << s << " on " << partitionPath;
}
return rval;