diff --git a/src/core/lvmdevice.cpp b/src/core/lvmdevice.cpp index 4bff050..1ae5f31 100644 --- a/src/core/lvmdevice.cpp +++ b/src/core/lvmdevice.cpp @@ -32,7 +32,6 @@ #include #include -#include /** Constructs a representation of LVM device with initialized LV as Partitions * @@ -124,69 +123,30 @@ Partition* LvmDevice::scanPartition(const QString& lvPath, PartitionTable* pTabl FileSystem* fs = FileSystemFactory::create(type, 0, lvSize - 1); fs->scan(lvPath); - bool mounted = isMounted(lvPath); - QString mountPoint = QString(); - - KMountPoint::List mountPointList = KMountPoint::currentMountPoints(KMountPoint::NeedRealDeviceName); - mountPointList.append(KMountPoint::possibleMountPoints(KMountPoint::NeedRealDeviceName)); - PartitionRole::Roles r = PartitionRole::Lvm_Lv; + QString mountPoint; + bool mounted; - if (type == FileSystem::Luks) { + // Handle LUKS partition + if (fs->type() == FileSystem::Luks) { r |= PartitionRole::Luks; - FS::luks* luksFs = static_cast(fs); - QString mapperNode = luksFs->mapperName(); - bool isCryptOpen = !mapperNode.isEmpty(); - luksFs->setCryptOpen(isCryptOpen); - luksFs->setLogicalSectorSize(logicalSize()); + initLuks(fs, this); + QString mapperNode = static_cast(fs)->mapperName(); + mountPoint = FileSystem::detectMountPoint(fs, mapperNode); + mounted = FileSystem::detectMountStatus(fs, mapperNode); + } else { + mountPoint = FileSystem::detectMountPoint(fs, lvPath); + mounted = FileSystem::detectMountStatus(fs, lvPath); - if (isCryptOpen) { - luksFs->loadInnerFileSystem(mapperNode); - - if (luksFs->type() == FileSystem::Lvm2_PV) { - mountPoint = FS::lvm2_pv::getVGName(mapperNode); - mounted = false; - } - else { - mountPoint = mountPointList.findByDevice(mapperNode) ? - mountPointList.findByDevice(mapperNode)->mountPoint() : - QString(); - if (mountPoint == QStringLiteral("none")) - mountPoint = QString(); - mounted = isMounted(mapperNode); - if (mounted) { - const KDiskFreeSpaceInfo freeSpaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(mountPoint); - if (freeSpaceInfo.isValid() && mountPoint != QString()) - luksFs->setSectorsUsed((freeSpaceInfo.used() + luksFs->payloadOffset()) / logicalSize()); - } - } - } else { - mounted = false; - } - luksFs->setMounted(mounted); - } - else if (type == FileSystem::Lvm2_PV) { - r |= PartitionRole::Lvm_Lv; - mountPoint = FS::lvm2_pv::getVGName(lvPath); - mounted = false; - } - else { - mountPoint = mountPointList.findByDevice(lvPath) ? - mountPointList.findByDevice(lvPath)->mountPoint() : - QString(); const KDiskFreeSpaceInfo freeSpaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(mountPoint); - - if (mountPoint == QStringLiteral("none")) - mountPoint = QString(); - - if (logicalSize() > 0) { + if (logicalSize() > 0 && fs->type() != FileSystem::Luks) { if (mounted && freeSpaceInfo.isValid() && mountPoint != QString()) { fs->setSectorsUsed(freeSpaceInfo.used() / logicalSize()); } else if (fs->supportGetUsed() == FileSystem::cmdSupportFileSystem) { fs->setSectorsUsed(qCeil(fs->readUsedCapacity(lvPath) / static_cast(logicalSize()))); } } - } + } if (fs->supportGetLabel() != FileSystem::cmdSupportNone) { fs->setLabel(fs->readLabel(lvPath)); diff --git a/src/fs/filesystem.cpp b/src/fs/filesystem.cpp index 15a9b0c..d824b4d 100644 --- a/src/fs/filesystem.cpp +++ b/src/fs/filesystem.cpp @@ -18,15 +18,18 @@ *************************************************************************/ #include "fs/filesystem.h" +#include "fs/lvm2_pv.h" #include "backend/corebackend.h" #include "backend/corebackendmanager.h" #include "util/externalcommand.h" #include "util/capacity.h" +#include "util/helpers.h" #include +#include #include #include @@ -118,6 +121,37 @@ FileSystem::Type FileSystem::detectFileSystem(const QString& partitionPath) return CoreBackendManager::self()->backend()->detectFileSystem(partitionPath); } +QString FileSystem::detectMountPoint(FileSystem* fs, const QString& partitionPath) +{ + QString mountPoint = QString(); + + KMountPoint::List mountPoints = KMountPoint::currentMountPoints(KMountPoint::NeedRealDeviceName); + mountPoints.append(KMountPoint::possibleMountPoints(KMountPoint::NeedRealDeviceName)); + + if (fs->type() == FileSystem::Lvm2_PV) { + mountPoint = FS::lvm2_pv::getVGName(partitionPath); + } else { + mountPoint = mountPoints.findByDevice(partitionPath) ? + mountPoints.findByDevice(partitionPath)->mountPoint() : + QString(); + if (mountPoint == QStringLiteral("none")) + mountPoint = QString(); + } + return mountPoint; +} + +bool FileSystem::detectMountStatus(FileSystem* fs, const QString& partitionPath) +{ + bool mounted = false; + + if (fs->type() == FileSystem::Lvm2_PV) { + mounted = false; + } else { + mounted = isMounted(partitionPath); + } + return mounted; +} + /** Reads the label for this FileSystem @param deviceNode the device node for the Partition the FileSystem is on @return the FileSystem label or an empty string in case of error diff --git a/src/fs/filesystem.h b/src/fs/filesystem.h index 7d1457a..c886ead 100644 --- a/src/fs/filesystem.h +++ b/src/fs/filesystem.h @@ -186,6 +186,8 @@ public: static QList types(); static FileSystem::Type typeForName(const QString& s); static FileSystem::Type detectFileSystem(const QString& partitionPath); + static QString detectMountPoint(FileSystem* fs, const QString& partitionPath); + static bool detectMountStatus(FileSystem* fs, const QString& partitionPath); /**< @return true if this FileSystem can be mounted */ virtual bool canMount(const QString& deviceNode, const QString& mountPoint) const; diff --git a/src/plugins/libparted/libpartedbackend.cpp b/src/plugins/libparted/libpartedbackend.cpp index e5def75..e73bfaa 100644 --- a/src/plugins/libparted/libpartedbackend.cpp +++ b/src/plugins/libparted/libpartedbackend.cpp @@ -50,7 +50,6 @@ #include #include -#include #include #include @@ -190,9 +189,6 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) CoreBackend::setPartitionTableForDevice(*d, new PartitionTable(type, firstUsableSector, lastUsableSector)); CoreBackend::setPartitionTableMaxPrimaries(*d->partitionTable(), maxPrimaryPartitionCount); - KMountPoint::List mountPoints = KMountPoint::currentMountPoints(KMountPoint::NeedRealDeviceName); - mountPoints.append(KMountPoint::possibleMountPoints(KMountPoint::NeedRealDeviceName)); - QList partitionPath = job->data()[QLatin1String("partitionPath")].toList(); QList partitionType = job->data()[QLatin1String("partitionType")].toList(); QList partitionStart = job->data()[QLatin1String("partitionStart")].toList(); @@ -206,7 +202,6 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) int type = partitionType[i].toInt(); qint64 start = partitionStart[i].toLongLong(); qint64 end = partitionEnd[i].toLongLong(); - bool busy = partitionBusy[i].toBool(); PartitionRole::Roles r = PartitionRole::None; @@ -239,56 +234,19 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) FileSystem* fs = FileSystemFactory::create(fsType, start, end); fs->scan(partitionNode); + QString mountPoint; + bool mounted; // libparted does not handle LUKS partitions - QString mountPoint; - bool mounted = false; - if (fsType == FileSystem::Luks) { + if (fs->type() == FileSystem::Luks) { r |= PartitionRole::Luks; - FS::luks* luksFs = static_cast(fs); - QString mapperNode = luksFs->mapperName(); - bool isCryptOpen = !mapperNode.isEmpty(); - luksFs->setCryptOpen(isCryptOpen); - luksFs->setLogicalSectorSize(d->logicalSectorSize()); - - if (isCryptOpen) { - luksFs->loadInnerFileSystem(mapperNode); - - if (luksFs->type() == FileSystem::Lvm2_PV) { - mountPoint = FS::lvm2_pv::getVGName(mapperNode); - mounted = false; - } else { - - mountPoint = mountPoints.findByDevice(mapperNode) ? - mountPoints.findByDevice(mapperNode)->mountPoint() : - QString(); - if (mountPoint == QStringLiteral("none")) - mountPoint = QString(); - // We cannot use libparted to check the mounted status because - // we don't have a PedPartition for the mapper device, so we use lsblk - mounted = isMounted(mapperNode); - } - if (mounted) { - const KDiskFreeSpaceInfo freeSpaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(mountPoint); - if (freeSpaceInfo.isValid() && mountPoint != QString()) - luksFs->setSectorsUsed((freeSpaceInfo.used() + luksFs->payloadOffset()) / d->logicalSectorSize()); - } - } else { - mounted = false; - } - - luksFs->setMounted(mounted); - } else if (fsType == FileSystem::Lvm2_PV) { - r |= PartitionRole::Lvm_Lv; - mountPoint = FS::lvm2_pv::getVGName(partitionNode); - mounted = false; + initLuks(fs, d); + QString mapperNode = static_cast(fs)->mapperName(); + mountPoint = FileSystem::detectMountPoint(fs, mapperNode); + mounted = FileSystem::detectMountStatus(fs, mapperNode); } else { - mountPoint = mountPoints.findByDevice(partitionNode) ? - mountPoints.findByDevice(partitionNode)->mountPoint() : - QString(); - if (mountPoint == QStringLiteral("none")) - mountPoint = QString(); - mounted = busy; + mountPoint = FileSystem::detectMountPoint(fs, partitionNode); + mounted = FileSystem::detectMountStatus(fs, partitionNode); } QList availableFlags = job->data()[QLatin1String("availableFlags")].toList(); diff --git a/src/util/helpers.cpp b/src/util/helpers.cpp index d175edb..1fac96d 100644 --- a/src/util/helpers.cpp +++ b/src/util/helpers.cpp @@ -19,6 +19,8 @@ #include "util/helpers.h" #include "util/externalcommand.h" #include "util/globallog.h" +#include "fs/luks.h" +#include "core/device.h" #include "ops/operation.h" @@ -84,6 +86,21 @@ bool isMounted(const QString& deviceNode) return false; } +void initLuks(FileSystem* fs, const Device* dev) +{ + if (fs->type() == FileSystem::Luks) { + FS::luks* luksFS = static_cast(fs); + QString mapperNode = luksFS->mapperName(); + bool isCryptOpen = !mapperNode.isEmpty(); + luksFS->setCryptOpen(isCryptOpen); + luksFS->setLogicalSectorSize(dev->logicalSize()); + if (isCryptOpen) { + luksFS->loadInnerFileSystem(mapperNode); + } + luksFS->setMounted(isMounted(mapperNode)); + } +} + KAboutData aboutKPMcore() { KAboutData aboutData( QStringLiteral("kpmcore"), diff --git a/src/util/helpers.h b/src/util/helpers.h index 39b95e8..b4b2769 100644 --- a/src/util/helpers.h +++ b/src/util/helpers.h @@ -39,6 +39,8 @@ LIBKPMCORE_EXPORT bool checkAccessibleDevices(); LIBKPMCORE_EXPORT bool isMounted(const QString& deviceNode); +LIBKPMCORE_EXPORT void initLuks(FileSystem* fs, const Device* dev); + LIBKPMCORE_EXPORT KAboutData aboutKPMcore(); #endif