Reduce code duplication in handling mountPoint/mount/diskSpace.
This commit is contained in:
parent
123369f78b
commit
bcd6156628
|
@ -32,7 +32,6 @@
|
||||||
|
|
||||||
#include <KDiskFreeSpaceInfo>
|
#include <KDiskFreeSpaceInfo>
|
||||||
#include <KLocalizedString>
|
#include <KLocalizedString>
|
||||||
#include <KMountPoint>
|
|
||||||
|
|
||||||
/** Constructs a representation of LVM device with initialized LV as Partitions
|
/** 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);
|
FileSystem* fs = FileSystemFactory::create(type, 0, lvSize - 1);
|
||||||
fs->scan(lvPath);
|
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;
|
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;
|
r |= PartitionRole::Luks;
|
||||||
FS::luks* luksFs = static_cast<FS::luks*>(fs);
|
initLuks(fs, this);
|
||||||
QString mapperNode = luksFs->mapperName();
|
QString mapperNode = static_cast<FS::luks*>(fs)->mapperName();
|
||||||
bool isCryptOpen = !mapperNode.isEmpty();
|
mountPoint = FileSystem::detectMountPoint(fs, mapperNode);
|
||||||
luksFs->setCryptOpen(isCryptOpen);
|
mounted = FileSystem::detectMountStatus(fs, mapperNode);
|
||||||
luksFs->setLogicalSectorSize(logicalSize());
|
} 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);
|
const KDiskFreeSpaceInfo freeSpaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(mountPoint);
|
||||||
|
if (logicalSize() > 0 && fs->type() != FileSystem::Luks) {
|
||||||
if (mountPoint == QStringLiteral("none"))
|
|
||||||
mountPoint = QString();
|
|
||||||
|
|
||||||
if (logicalSize() > 0) {
|
|
||||||
if (mounted && freeSpaceInfo.isValid() && mountPoint != QString()) {
|
if (mounted && freeSpaceInfo.isValid() && mountPoint != QString()) {
|
||||||
fs->setSectorsUsed(freeSpaceInfo.used() / logicalSize());
|
fs->setSectorsUsed(freeSpaceInfo.used() / logicalSize());
|
||||||
} else if (fs->supportGetUsed() == FileSystem::cmdSupportFileSystem) {
|
} else if (fs->supportGetUsed() == FileSystem::cmdSupportFileSystem) {
|
||||||
fs->setSectorsUsed(qCeil(fs->readUsedCapacity(lvPath) / static_cast<float>(logicalSize())));
|
fs->setSectorsUsed(qCeil(fs->readUsedCapacity(lvPath) / static_cast<float>(logicalSize())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fs->supportGetLabel() != FileSystem::cmdSupportNone) {
|
if (fs->supportGetLabel() != FileSystem::cmdSupportNone) {
|
||||||
fs->setLabel(fs->readLabel(lvPath));
|
fs->setLabel(fs->readLabel(lvPath));
|
||||||
|
|
|
@ -18,15 +18,18 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
#include "fs/filesystem.h"
|
#include "fs/filesystem.h"
|
||||||
|
#include "fs/lvm2_pv.h"
|
||||||
|
|
||||||
#include "backend/corebackend.h"
|
#include "backend/corebackend.h"
|
||||||
#include "backend/corebackendmanager.h"
|
#include "backend/corebackendmanager.h"
|
||||||
|
|
||||||
#include "util/externalcommand.h"
|
#include "util/externalcommand.h"
|
||||||
#include "util/capacity.h"
|
#include "util/capacity.h"
|
||||||
|
#include "util/helpers.h"
|
||||||
|
|
||||||
#include <blkid/blkid.h>
|
#include <blkid/blkid.h>
|
||||||
|
|
||||||
|
#include <KMountPoint>
|
||||||
#include <KLocalizedString>
|
#include <KLocalizedString>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
@ -118,6 +121,37 @@ FileSystem::Type FileSystem::detectFileSystem(const QString& partitionPath)
|
||||||
return CoreBackendManager::self()->backend()->detectFileSystem(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
|
/** Reads the label for this FileSystem
|
||||||
@param deviceNode the device node for the Partition the FileSystem is on
|
@param deviceNode the device node for the Partition the FileSystem is on
|
||||||
@return the FileSystem label or an empty string in case of error
|
@return the FileSystem label or an empty string in case of error
|
||||||
|
|
|
@ -186,6 +186,8 @@ public:
|
||||||
static QList<FileSystem::Type> types();
|
static QList<FileSystem::Type> types();
|
||||||
static FileSystem::Type typeForName(const QString& s);
|
static FileSystem::Type typeForName(const QString& s);
|
||||||
static FileSystem::Type detectFileSystem(const QString& partitionPath);
|
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 */
|
/**< @return true if this FileSystem can be mounted */
|
||||||
virtual bool canMount(const QString& deviceNode, const QString& mountPoint) const;
|
virtual bool canMount(const QString& deviceNode, const QString& mountPoint) const;
|
||||||
|
|
|
@ -50,7 +50,6 @@
|
||||||
|
|
||||||
#include <KAuth>
|
#include <KAuth>
|
||||||
#include <KLocalizedString>
|
#include <KLocalizedString>
|
||||||
#include <KMountPoint>
|
|
||||||
#include <KDiskFreeSpaceInfo>
|
#include <KDiskFreeSpaceInfo>
|
||||||
#include <KPluginFactory>
|
#include <KPluginFactory>
|
||||||
|
|
||||||
|
@ -190,9 +189,6 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode)
|
||||||
CoreBackend::setPartitionTableForDevice(*d, new PartitionTable(type, firstUsableSector, lastUsableSector));
|
CoreBackend::setPartitionTableForDevice(*d, new PartitionTable(type, firstUsableSector, lastUsableSector));
|
||||||
CoreBackend::setPartitionTableMaxPrimaries(*d->partitionTable(), maxPrimaryPartitionCount);
|
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> partitionPath = job->data()[QLatin1String("partitionPath")].toList();
|
||||||
QList<QVariant> partitionType = job->data()[QLatin1String("partitionType")].toList();
|
QList<QVariant> partitionType = job->data()[QLatin1String("partitionType")].toList();
|
||||||
QList<QVariant> partitionStart = job->data()[QLatin1String("partitionStart")].toList();
|
QList<QVariant> partitionStart = job->data()[QLatin1String("partitionStart")].toList();
|
||||||
|
@ -206,7 +202,6 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode)
|
||||||
int type = partitionType[i].toInt();
|
int type = partitionType[i].toInt();
|
||||||
qint64 start = partitionStart[i].toLongLong();
|
qint64 start = partitionStart[i].toLongLong();
|
||||||
qint64 end = partitionEnd[i].toLongLong();
|
qint64 end = partitionEnd[i].toLongLong();
|
||||||
bool busy = partitionBusy[i].toBool();
|
|
||||||
|
|
||||||
PartitionRole::Roles r = PartitionRole::None;
|
PartitionRole::Roles r = PartitionRole::None;
|
||||||
|
|
||||||
|
@ -239,56 +234,19 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode)
|
||||||
|
|
||||||
FileSystem* fs = FileSystemFactory::create(fsType, start, end);
|
FileSystem* fs = FileSystemFactory::create(fsType, start, end);
|
||||||
fs->scan(partitionNode);
|
fs->scan(partitionNode);
|
||||||
|
QString mountPoint;
|
||||||
|
bool mounted;
|
||||||
|
|
||||||
// libparted does not handle LUKS partitions
|
// libparted does not handle LUKS partitions
|
||||||
QString mountPoint;
|
if (fs->type() == FileSystem::Luks) {
|
||||||
bool mounted = false;
|
|
||||||
if (fsType == FileSystem::Luks) {
|
|
||||||
r |= PartitionRole::Luks;
|
r |= PartitionRole::Luks;
|
||||||
FS::luks* luksFs = static_cast<FS::luks*>(fs);
|
initLuks(fs, d);
|
||||||
QString mapperNode = luksFs->mapperName();
|
QString mapperNode = static_cast<FS::luks*>(fs)->mapperName();
|
||||||
bool isCryptOpen = !mapperNode.isEmpty();
|
mountPoint = FileSystem::detectMountPoint(fs, mapperNode);
|
||||||
luksFs->setCryptOpen(isCryptOpen);
|
mounted = FileSystem::detectMountStatus(fs, mapperNode);
|
||||||
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;
|
|
||||||
} else {
|
} else {
|
||||||
mountPoint = mountPoints.findByDevice(partitionNode) ?
|
mountPoint = FileSystem::detectMountPoint(fs, partitionNode);
|
||||||
mountPoints.findByDevice(partitionNode)->mountPoint() :
|
mounted = FileSystem::detectMountStatus(fs, partitionNode);
|
||||||
QString();
|
|
||||||
if (mountPoint == QStringLiteral("none"))
|
|
||||||
mountPoint = QString();
|
|
||||||
mounted = busy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QVariant> availableFlags = job->data()[QLatin1String("availableFlags")].toList();
|
QList<QVariant> availableFlags = job->data()[QLatin1String("availableFlags")].toList();
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
#include "util/helpers.h"
|
#include "util/helpers.h"
|
||||||
#include "util/externalcommand.h"
|
#include "util/externalcommand.h"
|
||||||
#include "util/globallog.h"
|
#include "util/globallog.h"
|
||||||
|
#include "fs/luks.h"
|
||||||
|
#include "core/device.h"
|
||||||
|
|
||||||
#include "ops/operation.h"
|
#include "ops/operation.h"
|
||||||
|
|
||||||
|
@ -84,6 +86,21 @@ bool isMounted(const QString& deviceNode)
|
||||||
return false;
|
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 aboutKPMcore()
|
||||||
{
|
{
|
||||||
KAboutData aboutData( QStringLiteral("kpmcore"),
|
KAboutData aboutData( QStringLiteral("kpmcore"),
|
||||||
|
|
|
@ -39,6 +39,8 @@ LIBKPMCORE_EXPORT bool checkAccessibleDevices();
|
||||||
|
|
||||||
LIBKPMCORE_EXPORT bool isMounted(const QString& deviceNode);
|
LIBKPMCORE_EXPORT bool isMounted(const QString& deviceNode);
|
||||||
|
|
||||||
|
LIBKPMCORE_EXPORT void initLuks(FileSystem* fs, const Device* dev);
|
||||||
|
|
||||||
LIBKPMCORE_EXPORT KAboutData aboutKPMcore();
|
LIBKPMCORE_EXPORT KAboutData aboutKPMcore();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue