Reduce code duplication in handling mountPoint/mount/diskSpace.

This commit is contained in:
Chantara Tith 2016-09-12 17:23:19 +07:00
parent 123369f78b
commit bcd6156628
6 changed files with 77 additions and 104 deletions

View File

@ -32,7 +32,6 @@
#include <KDiskFreeSpaceInfo>
#include <KLocalizedString>
#include <KMountPoint>
/** 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::luks*>(fs);
QString mapperNode = luksFs->mapperName();
bool isCryptOpen = !mapperNode.isEmpty();
luksFs->setCryptOpen(isCryptOpen);
luksFs->setLogicalSectorSize(logicalSize());
initLuks(fs, this);
QString mapperNode = static_cast<FS::luks*>(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<float>(logicalSize())));
}
}
}
}
if (fs->supportGetLabel() != FileSystem::cmdSupportNone) {
fs->setLabel(fs->readLabel(lvPath));

View File

@ -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 <blkid/blkid.h>
#include <KMountPoint>
#include <KLocalizedString>
#include <QDebug>
@ -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

View File

@ -186,6 +186,8 @@ public:
static QList<FileSystem::Type> 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;

View File

@ -50,7 +50,6 @@
#include <KAuth>
#include <KLocalizedString>
#include <KMountPoint>
#include <KDiskFreeSpaceInfo>
#include <KPluginFactory>
@ -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<QVariant> partitionPath = job->data()[QLatin1String("partitionPath")].toList();
QList<QVariant> partitionType = job->data()[QLatin1String("partitionType")].toList();
QList<QVariant> 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::luks*>(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::luks*>(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<QVariant> availableFlags = job->data()[QLatin1String("availableFlags")].toList();

View File

@ -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::luks*>(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"),

View File

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