Add KAuth helper to detectFileSystem.

This commit is contained in:
Andrius Štikonas 2016-07-25 16:29:02 +01:00
parent cb7a3d26c5
commit 71921cf500
6 changed files with 91 additions and 57 deletions

View File

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

View File

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

View File

@ -19,6 +19,8 @@
#include "core/partitiontable.h"
#include "plugins/libparted/pedflags.h"
#include <blkid/blkid.h>
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)

View File

@ -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);
};
// --------------------------------------------------------------------------

View File

@ -41,8 +41,6 @@
#include "util/externalcommand.h"
#include "util/helpers.h"
#include <blkid/blkid.h>
#include <QDebug>
#include <QString>
#include <QStringList>
@ -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<QVariant> partitionEnd = job->data()[QLatin1String("partitionEnd")].toList();
QList<QVariant> partitionBusy = job->data()[QLatin1String("partitionBusy")].toList();
QList<QVariant> availableFlags = job->data()[QLatin1String("availableFlags")].toList();
QList<QVariant> activeFlags = job->data()[QLatin1String("activeFlags")].toList();
quint32 totalPartitions = partitionPath.size();
QList<Partition*> partitions;
for (quint32 i = 0; i < totalPartitions; ++i) {
@ -277,9 +278,7 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode)
mounted = busy;
}
QList<QVariant> availableFlags = job->data()[QLatin1String("availableFlags")].toList();
PartitionTable::Flags available = static_cast<PartitionTable::Flag>(availableFlags[i].toInt());
QList<QVariant> activeFlags = job->data()[QLatin1String("activeFlags")].toList();
PartitionTable::Flags active = static_cast<PartitionTable::Flag>(activeFlags[i].toInt());
Partition* part = new Partition(parent, *d, PartitionRole(r), fs, start, end, partitionNode, available, mountPoint, mounted, active);
@ -348,61 +347,52 @@ QList<Device*> 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;
}

View File

@ -66,7 +66,7 @@ public:
bool closeDevice(CoreBackendDevice* core_device) override;
Device* scanDevice(const QString& deviceNode) override;
QList<Device*> scanDevices(bool excludeReadOnly = true) override;
FileSystem::Type detectFileSystem(const QString& partitionPath) override;
FileSystem::Type detectFileSystem(const QString& deviceNode) override;
static QString lastPartedExceptionMessage();