From 71921cf500c5ae0cdde0176cf240e4a9fb282320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Mon, 25 Jul 2016 16:29:02 +0100 Subject: [PATCH] Add KAuth helper to detectFileSystem. --- src/plugins/libparted/helpers/CMakeLists.txt | 2 +- .../helpers/org.kde.kpmcore.scan.actions | 6 ++ src/plugins/libparted/helpers/scan.cpp | 37 +++++++ src/plugins/libparted/helpers/scan.h | 1 + src/plugins/libparted/libpartedbackend.cpp | 100 ++++++++---------- src/plugins/libparted/libpartedbackend.h | 2 +- 6 files changed, 91 insertions(+), 57 deletions(-) diff --git a/src/plugins/libparted/helpers/CMakeLists.txt b/src/plugins/libparted/helpers/CMakeLists.txt index fe13dd7..bc57e0a 100644 --- a/src/plugins/libparted/helpers/CMakeLists.txt +++ b/src/plugins/libparted/helpers/CMakeLists.txt @@ -1,5 +1,5 @@ add_executable(kpmcore_scan scan.cpp) -target_link_libraries(kpmcore_scan ${LIBPARTED_LIBS} KF5::Auth) +target_link_libraries(kpmcore_scan ${LIBPARTED_LIBS} ${BLKID_LIBRARIES} KF5::Auth) install(TARGETS kpmcore_scan DESTINATION ${KAUTH_HELPER_INSTALL_DIR}) kauth_install_helper_files(kpmcore_scan org.kde.kpmcore.scan root) diff --git a/src/plugins/libparted/helpers/org.kde.kpmcore.scan.actions b/src/plugins/libparted/helpers/org.kde.kpmcore.scan.actions index 5b525b4..b911cc9 100644 --- a/src/plugins/libparted/helpers/org.kde.kpmcore.scan.actions +++ b/src/plugins/libparted/helpers/org.kde.kpmcore.scan.actions @@ -37,3 +37,9 @@ Description[sv]=Läs använda sektorer Description[x-test]=xxRead used sectorsxx Policy=yes Persistence=session + +[org.kde.kpmcore.scan.detectfilesystem] +Name=Detect file system type action +Description=Detect file system type +Policy=yes +Persistence=session diff --git a/src/plugins/libparted/helpers/scan.cpp b/src/plugins/libparted/helpers/scan.cpp index d8c3dd6..e2d6349 100644 --- a/src/plugins/libparted/helpers/scan.cpp +++ b/src/plugins/libparted/helpers/scan.cpp @@ -19,6 +19,8 @@ #include "core/partitiontable.h" #include "plugins/libparted/pedflags.h" +#include + ActionReply Scan::scandevice(const QVariantMap& args) { ActionReply reply; @@ -166,4 +168,39 @@ ActionReply Scan::readsectorsused(const QVariantMap& args) return reply; } +ActionReply Scan::detectfilesystem(const QVariantMap& args) +{ + ActionReply reply; + QVariantMap returnArgs; + QString deviceNode = args[QLatin1String("deviceNode")].toString(); + + blkid_cache cache; + if (blkid_get_cache(&cache, nullptr) == 0) { + blkid_dev dev; + + if ((dev = blkid_get_dev(cache, deviceNode.toLocal8Bit().constData(), BLKID_DEV_NORMAL)) != nullptr) { + char *string = blkid_get_tag_value(cache, "TYPE", deviceNode.toLocal8Bit().constData()); + QString s = QString::fromUtf8(string); + free(string); + + if (s == QStringLiteral("vfat")) { + // libblkid uses SEC_TYPE to distinguish between FAT16 and FAT32 + string = blkid_get_tag_value(cache, "SEC_TYPE", deviceNode.toLocal8Bit().constData()); + QString st = QString::fromUtf8(string); + free(string); + if (st == QStringLiteral("msdos")) + returnArgs[QLatin1String("fileSystem")] = QStringLiteral("fat16"); + else + returnArgs[QLatin1String("fileSystem")] = QStringLiteral("fat32"); + } + else + returnArgs[QLatin1String("fileSystem")] = s; + } + blkid_put_cache(cache); + } + + reply.setData(returnArgs); + return reply; +} + KAUTH_HELPER_MAIN("org.kde.kpmcore.scan", Scan) diff --git a/src/plugins/libparted/helpers/scan.h b/src/plugins/libparted/helpers/scan.h index dca38da..9a11177 100644 --- a/src/plugins/libparted/helpers/scan.h +++ b/src/plugins/libparted/helpers/scan.h @@ -31,6 +31,7 @@ class Scan : public QObject public Q_SLOTS: ActionReply scandevice(const QVariantMap& args); ActionReply readsectorsused(const QVariantMap& args); + ActionReply detectfilesystem(const QVariantMap& args); }; // -------------------------------------------------------------------------- diff --git a/src/plugins/libparted/libpartedbackend.cpp b/src/plugins/libparted/libpartedbackend.cpp index e70510e..8412ce5 100644 --- a/src/plugins/libparted/libpartedbackend.cpp +++ b/src/plugins/libparted/libpartedbackend.cpp @@ -41,8 +41,6 @@ #include "util/externalcommand.h" #include "util/helpers.h" -#include - #include #include #include @@ -81,10 +79,10 @@ static qint64 readSectorsUsedLibParted(const Partition& p) args[QLatin1String("deviceNode")] = p.deviceNode(); args[QLatin1String("firstSector")] = p.firstSector(); - KAuth::Action scanAction = QStringLiteral("org.kde.kpmcore.scan.readsectorsused"); - scanAction.setHelperId(QStringLiteral("org.kde.kpmcore.scan")); - scanAction.setArguments(args); - KAuth::ExecuteJob *job = scanAction.execute(); + KAuth::Action action = QStringLiteral("org.kde.kpmcore.scan.readsectorsused"); + action.setHelperId(QStringLiteral("org.kde.kpmcore.scan")); + action.setArguments(args); + KAuth::ExecuteJob *job = action.execute(); if (!job->exec()) { qWarning() << "KAuth returned an error code: " << job->errorString(); return -1; @@ -200,6 +198,9 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) QList partitionEnd = job->data()[QLatin1String("partitionEnd")].toList(); QList partitionBusy = job->data()[QLatin1String("partitionBusy")].toList(); + QList availableFlags = job->data()[QLatin1String("availableFlags")].toList(); + QList activeFlags = job->data()[QLatin1String("activeFlags")].toList(); + quint32 totalPartitions = partitionPath.size(); QList partitions; for (quint32 i = 0; i < totalPartitions; ++i) { @@ -277,9 +278,7 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) mounted = busy; } - QList availableFlags = job->data()[QLatin1String("availableFlags")].toList(); PartitionTable::Flags available = static_cast(availableFlags[i].toInt()); - QList activeFlags = job->data()[QLatin1String("activeFlags")].toList(); PartitionTable::Flags active = static_cast(activeFlags[i].toInt()); Partition* part = new Partition(parent, *d, PartitionRole(r), fs, start, end, partitionNode, available, mountPoint, mounted, active); @@ -348,61 +347,52 @@ QList LibPartedBackend::scanDevices(bool excludeReadOnly) } /** Detects the type of a FileSystem given a PedDevice and a PedPartition - @param partitionPath path to the partition + @param deviceNode path to the partition @return the detected FileSystem type (FileSystem::Unknown if not detected) */ -FileSystem::Type LibPartedBackend::detectFileSystem(const QString& partitionPath) +FileSystem::Type LibPartedBackend::detectFileSystem(const QString& deviceNode) { FileSystem::Type rval = FileSystem::Unknown; - blkid_cache cache; - if (blkid_get_cache(&cache, nullptr) == 0) { - blkid_dev dev; + QVariantMap args; + args[QLatin1String("deviceNode")] = deviceNode; - if ((dev = blkid_get_dev(cache, - partitionPath.toLocal8Bit().constData(), - BLKID_DEV_NORMAL)) != nullptr) { - char *string = blkid_get_tag_value(cache, "TYPE", partitionPath.toLocal8Bit().constData()); - QString s = QString::fromUtf8(string); - free(string); - - 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 - string = blkid_get_tag_value(cache, "SEC_TYPE", partitionPath.toLocal8Bit().constData()); - QString st = QString::fromUtf8(string); - free(string); - 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); + KAuth::Action action = QStringLiteral("org.kde.kpmcore.scan.detectfilesystem"); + action.setHelperId(QStringLiteral("org.kde.kpmcore.scan")); + action.setArguments(args); + KAuth::ExecuteJob *job = action.execute(); + if (!job->exec()) { + qWarning() << "KAuth returned an error code: " << job->errorString(); + return rval; } + QString s = job->data()[QLatin1String("fileSystem")].toString(); + + 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("fat16")) rval = FileSystem::Fat16; + else if (s == QStringLiteral("fat32")) rval = FileSystem::Fat32; + 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 " << deviceNode; + return rval; } diff --git a/src/plugins/libparted/libpartedbackend.h b/src/plugins/libparted/libpartedbackend.h index cae8664..c750a14 100644 --- a/src/plugins/libparted/libpartedbackend.h +++ b/src/plugins/libparted/libpartedbackend.h @@ -66,7 +66,7 @@ public: bool closeDevice(CoreBackendDevice* core_device) override; Device* scanDevice(const QString& deviceNode) override; QList scanDevices(bool excludeReadOnly = true) override; - FileSystem::Type detectFileSystem(const QString& partitionPath) override; + FileSystem::Type detectFileSystem(const QString& deviceNode) override; static QString lastPartedExceptionMessage();