More conversion from QStringList to QList<const Partition*>.

This commit is contained in:
Andrius Štikonas 2016-09-18 01:53:42 +01:00
parent c41b008dc1
commit 28478ee0ad
13 changed files with 117 additions and 74 deletions

View File

@ -29,8 +29,8 @@
#include "fs/lvm2_pv.h"
#include "util/externalcommand.h"
#include <QRegularExpression>
#include <QDebug>
/** Constructs a DeviceScanner
@param ostack the OperationStack where the devices will be created
@ -78,5 +78,10 @@ void DeviceScanner::scan()
operationStack().physicalVolumes().append(FS::lvm2_pv::getPVinNode(d->partitionTable()));
}
// Store list of physical volumes in LvmDevice
for (const auto &d : lvmList)
for (const auto &p : operationStack().physicalVolumes())
if (p.first == d->name())
d->physicalVolumes().append(p.second);
}

View File

@ -61,7 +61,7 @@ LvmDevice::LvmDevice(const QString& vgName, const QString& iconName)
* shared list of PV's paths that will be added to any VGs.
* (have been added to an operation, but not yet applied)
*/
QStringList LvmDevice::s_DirtyPVs;
QList<const Partition*> LvmDevice::s_DirtyPVs;
LvmDevice::~LvmDevice()
{
@ -409,13 +409,13 @@ bool LvmDevice::movePV(Report& report, const QString& pvPath, const QStringList&
return (cmd.run(-1) && cmd.exitCode() == 0);
}
bool LvmDevice::createVG(Report& report, const QString vgName, const QStringList pvList, const qint32 peSize)
bool LvmDevice::createVG(Report& report, const QString vgName, const QList<const Partition*>& pvList, const qint32 peSize)
{
QStringList args = QStringList();
args << QStringLiteral("vgcreate") << QStringLiteral("--physicalextentsize") << QString::number(peSize);
args << vgName;
for (const auto &pvNode : pvList)
args << pvNode.trimmed();
for (const auto &p : pvList)
args << p->partitionPath();
ExternalCommand cmd(report, QStringLiteral("lvm"), args);

View File

@ -52,7 +52,7 @@ public:
const QStringList partitionNodes() const override;
qint64 partitionSize(QString& partitionPath) const override;
static QStringList s_DirtyPVs;
static QList<const Partition*> s_DirtyPVs;
public:
static QList<LvmDevice*> scanSystemLVM();
@ -81,7 +81,7 @@ public:
static bool movePV(Report& report, const QString& pvPath, const QStringList& destinations = QStringList());
static bool removeVG(Report& report, LvmDevice& d);
static bool createVG(Report& report, const QString vgName, const QStringList pvList, const qint32 peSize = 4); // peSize in megabytes
static bool createVG(Report& report, const QString vgName, const QList<const Partition*>& pvList, const qint32 peSize = 4); // peSize in megabytes
static bool deactivateVG(Report& report, const LvmDevice& d);
static bool activateVG(Report& report, const LvmDevice& d);
@ -108,12 +108,13 @@ public:
QString UUID() const {
return m_UUID;
}
QStringList* LVPathList() const {
return m_LVPathList;
}
const QList <const Partition*> physicalVolumes() const {
QList <const Partition*>& physicalVolumes() {
return m_PVs;
}
const QList <const Partition*>& physicalVolumes() const {
return m_PVs;
}
@ -130,7 +131,7 @@ private:
QString m_UUID;
mutable QStringList* m_LVPathList;
mutable QList <const Partition*> m_PVs;
QList <const Partition*> m_PVs;
mutable QMap<QString, qint64>* m_LVSizeMap;
};

View File

@ -28,7 +28,7 @@
* @param pvList List of LVM Physical Volumes used to create Volume Group
* @param peSize LVM Physical Extent size in MiB
*/
CreateVolumeGroupJob::CreateVolumeGroupJob(const QString& vgName, const QStringList& pvList, const qint32 peSize) :
CreateVolumeGroupJob::CreateVolumeGroupJob(const QString& vgName, const QList<const Partition*>& pvList, const qint32 peSize) :
Job(),
m_vgName(vgName),
m_pvList(pvList),
@ -52,8 +52,9 @@ bool CreateVolumeGroupJob::run(Report& parent)
QString CreateVolumeGroupJob::description() const
{
QString tmp = QString();
for (const auto &name : pvList()) {
tmp += QStringLiteral("\n") + name;
for (const auto &p : pvList()) {
tmp += QStringLiteral(", ") + p->deviceNode();
}
tmp.chop(2);
return xi18nc("@info/plain", "Create a new Volume Group: <filename>%1</filename> with PV: %2", vgName(), tmp);
}

View File

@ -19,10 +19,11 @@
#define CREATEVOLUMEGROUPJOB_H
#include "core/partition.h"
#include "jobs/job.h"
class LvmDevice;
class Partition;
class Report;
class QString;
@ -30,7 +31,7 @@ class QString;
class CreateVolumeGroupJob : public Job
{
public:
CreateVolumeGroupJob(const QString& vgName, const QStringList& pvList, const qint32 peSize);
CreateVolumeGroupJob(const QString& vgName, const QList<const Partition*>& pvList, const qint32 peSize);
public:
bool run(Report& parent) override;
@ -43,10 +44,10 @@ protected:
const QString vgName() const {
return m_vgName;
}
QStringList pvList() {
QList<const Partition*>& pvList() {
return m_pvList;
}
const QStringList pvList() const {
const QList<const Partition*>& pvList() const {
return m_pvList;
}
@ -56,7 +57,7 @@ protected:
private:
QString m_vgName;
QStringList m_pvList;
QList<const Partition*> m_pvList;
qint32 m_PESize;
};

View File

@ -26,7 +26,7 @@
/** Creates a new MovePhysicalVolumeJob
* @param d Device representing LVM Volume Group
*/
MovePhysicalVolumeJob::MovePhysicalVolumeJob(LvmDevice& d, const QStringList partList) :
MovePhysicalVolumeJob::MovePhysicalVolumeJob(LvmDevice& d, const QList <const Partition*>& partList) :
Job(),
m_Device(d),
m_PartList(partList)
@ -40,14 +40,14 @@ bool MovePhysicalVolumeJob::run(Report& parent)
Report* report = jobStarted(parent);
QStringList destinations = device().deviceNodes();
for (const auto &partPath : partList()) {
if (destinations.contains(partPath)) {
destinations.removeAll(partPath);
for (const auto &p : partList()) {
if (destinations.contains(p->partitionPath())) {
destinations.removeAll(p->partitionPath());
}
}
for (const auto &partPath : partList()) {
rval = LvmDevice::movePV(*report, partPath, destinations);
for (const auto &p : partList()) {
rval = LvmDevice::movePV(*report, p->partitionPath(), destinations);
if (rval == false) {
break;
}
@ -60,5 +60,9 @@ bool MovePhysicalVolumeJob::run(Report& parent)
QString MovePhysicalVolumeJob::description() const
{
return xi18nc("@info/plain", "Move used PE in %1 on %2 to other available Physical Volumes", partList().join(QStringLiteral(", ")), device().name());
QString movedPartitions = QString();
for (const auto &p : partList())
movedPartitions += QStringLiteral(", ") + p->deviceNode();
movedPartitions.chop(2);
return xi18nc("@info/plain", "Move used PE in %1 on %2 to other available Physical Volumes", movedPartitions, device().name());
}

View File

@ -19,6 +19,7 @@
#define MOVEPHYSICALVOLUMEJOB_H
#include "core/partition.h"
#include "jobs/job.h"
class LvmDevice;
@ -29,7 +30,7 @@ class QString;
class MovePhysicalVolumeJob : public Job
{
public:
MovePhysicalVolumeJob(LvmDevice& dev, const QStringList partlist);
MovePhysicalVolumeJob(LvmDevice& dev, const QList <const Partition*>& partlist);
public:
bool run(Report& parent) override;
@ -43,13 +44,13 @@ protected:
const LvmDevice& device() const {
return m_Device;
}
const QStringList partList() const {
const QList <const Partition*>& partList() const {
return m_PartList;
}
private:
LvmDevice& m_Device;
const QStringList m_PartList;
const QList <const Partition*> m_PartList;
};
#endif

View File

@ -25,7 +25,7 @@
/** Creates a new ResizeVolumeGroupJob
*/
ResizeVolumeGroupJob::ResizeVolumeGroupJob(LvmDevice& dev, const QStringList partlist, const Type type) :
ResizeVolumeGroupJob::ResizeVolumeGroupJob(LvmDevice& dev, const QList <const Partition*>& partlist, const Type type) :
Job(),
m_Device(dev),
m_PartList(partlist),
@ -39,15 +39,14 @@ bool ResizeVolumeGroupJob::run(Report& parent)
Report* report = jobStarted(parent);
for (const auto &pvpath : partList()) {
if (type() == ResizeVolumeGroupJob::Grow) {
rval = LvmDevice::insertPV(*report, device(), pvpath);
} else if (type() == ResizeVolumeGroupJob::Shrink) {
rval = LvmDevice::removePV(*report, device(), pvpath);
}
if (rval == false) {
for (const auto &p : partList()) {
if (type() == ResizeVolumeGroupJob::Grow)
rval = LvmDevice::insertPV(*report, device(), p->partitionPath());
else if (type() == ResizeVolumeGroupJob::Shrink)
rval = LvmDevice::removePV(*report, device(), p->partitionPath());
if (rval == false)
break;
}
}
jobFinished(*report, rval);
@ -57,7 +56,11 @@ bool ResizeVolumeGroupJob::run(Report& parent)
QString ResizeVolumeGroupJob::description() const
{
const QString partitionList = partList().join(QStringLiteral(", "));
QString partitionList = QString();
for (const auto &p : partList()) {
partitionList += QStringLiteral(", ") + p->deviceNode();
}
partitionList.chop(2);
const qint32 count = partList().count();
if (type() == ResizeVolumeGroupJob::Grow) {

View File

@ -19,6 +19,7 @@
#define RESIZEVOLUMEGROUPJOB_H
#include "core/partition.h"
#include "jobs/job.h"
class LvmDevice;
@ -36,7 +37,7 @@ public:
};
public:
ResizeVolumeGroupJob(LvmDevice& dev, const QStringList partlist, const Type type);
ResizeVolumeGroupJob(LvmDevice& dev, const QList <const Partition*>& partlist, const Type type);
public:
bool run(Report& parent) override;
@ -50,7 +51,7 @@ protected:
return m_Device;
}
const QStringList partList() const {
const QList <const Partition*>& partList() const {
return m_PartList;
}
@ -60,7 +61,7 @@ protected:
private:
LvmDevice& m_Device;
const QStringList m_PartList;
const QList <const Partition*> m_PartList;
ResizeVolumeGroupJob::Type m_Type;
};

View File

@ -31,7 +31,7 @@
* @param pvList List of LVM Physical Volumes used to create Volume Group
* @param peSize LVM Physical Extent size in MiB
*/
CreateVolumeGroupOperation::CreateVolumeGroupOperation(const QString& vgName, const QStringList& pvList, const qint32 peSize) :
CreateVolumeGroupOperation::CreateVolumeGroupOperation(const QString& vgName, const QList<const Partition*>& pvList, const qint32 peSize) :
Operation(),
m_CreateVolumeGroupJob(new CreateVolumeGroupJob(vgName, pvList, peSize)),
m_PVList(pvList)

View File

@ -37,7 +37,7 @@ class LIBKPMCORE_EXPORT CreateVolumeGroupOperation : public Operation
friend class OperationStack;
public:
CreateVolumeGroupOperation(const QString& vgName, const QStringList& pvList, const qint32 peSize = 4);
CreateVolumeGroupOperation(const QString& vgName, const QList<const Partition*>& pvList, const qint32 peSize = 4);
public:
QString iconName() const override {
@ -61,13 +61,13 @@ protected:
return m_CreateVolumeGroupJob;
}
const QStringList PVList() {
const QList<const Partition*>& PVList() {
return m_PVList;
}
private:
CreateVolumeGroupJob* m_CreateVolumeGroupJob;
QStringList m_PVList;
const QList<const Partition*> m_PVList;
};
#endif

View File

@ -19,6 +19,7 @@
#include "core/lvmdevice.h"
#include "fs/lvm2_pv.h"
#include "fs/luks.h"
#include "core/partition.h"
#include "jobs/resizevolumegroupjob.h"
@ -30,38 +31,55 @@
/** Creates a new ResizeVolumeGroupOperation.
@param d the Device to create the new PartitionTable on
@param partlist list of LVM Physical Volumes that should be in LVM Volume Group
@param partList list of LVM Physical Volumes that should be in LVM Volume Group
*/
ResizeVolumeGroupOperation::ResizeVolumeGroupOperation(LvmDevice& d, const QStringList partlist)
ResizeVolumeGroupOperation::ResizeVolumeGroupOperation(LvmDevice& d, const QList<const Partition*>& partList)
: Operation()
, m_Device(d)
, m_TargetList(partlist)
, m_CurrentList(d.deviceNodes())
, m_TargetList(partList)
, m_CurrentList(d.physicalVolumes())
, m_TargetSize(0)
, m_CurrentSize(0)
, m_GrowVolumeGroupJob(nullptr)
, m_ShrinkVolumeGroupJob(nullptr)
, m_MovePhysicalVolumeJob(nullptr)
{
const QStringList curList = currentList();
m_TargetSize = FS::lvm2_pv::getPVSize(targetList());
m_CurrentSize = FS::lvm2_pv::getPVSize(currentList());
for (const auto &p : targetList())
m_TargetSize += p->capacity();
for (const auto &p : currentList())
m_CurrentSize += p->capacity();
QStringList toRemoveList = curList;
for (const QString &path : partlist)
if (toRemoveList.contains(path))
toRemoveList.removeAll(path);
QList<const Partition*> toRemoveList;
for (const auto &p : currentList())
if (!targetList().contains(p))
toRemoveList.append(p);
QStringList toInsertList = partlist;
for (const QString &path : curList)
if (toInsertList.contains(path))
toInsertList.removeAll(path);
QList<const Partition*> toInsertList;
for (const auto &p : targetList())
if (!currentList().contains(p))
toInsertList.append(p);
qint64 freePE = FS::lvm2_pv::getFreePE(curList) - FS::lvm2_pv::getFreePE(toRemoveList);
qint64 movePE = FS::lvm2_pv::getAllocatedPE(toRemoveList);
qint64 growPE = FS::lvm2_pv::getPVSize(toInsertList) / LvmDevice::getPeSize(d.name());
qint64 currentFreePE = 0;
for (const auto &p : currentList())
currentFreePE += FS::lvm2_pv::getFreePE(p->partitionPath());
qint64 removedFreePE = 0;
for (const auto &p : toRemoveList) // FIXME: qAsConst
removedFreePE += FS::lvm2_pv::getFreePE(p->partitionPath());
qint64 freePE = currentFreePE - removedFreePE;
qint64 movePE = 0;
for (const auto &p : toRemoveList) { // FIXME: qAsConst
const FS::lvm2_pv* lvm2PVFs = p->roles().has(PartitionRole::Luks) ?
static_cast<const FS::lvm2_pv*>(static_cast<const FS::luks*>(&p->fileSystem())->innerFS()) : // LVM inside LUKS partition
static_cast<const FS::lvm2_pv*>(&p->fileSystem()); // simple LVM
movePE += lvm2PVFs->allocatedPE();
}
qint64 growPE = 0;
for (const auto &p : toInsertList) // FIXME: qAsConst
growPE += FS::lvm2_pv::getPVSize(p->partitionPath()) / LvmDevice::getPeSize(d.name());
if ( movePE > (freePE + growPE)) {
// *ABORT* can't move
} else if (partlist == curList) {
} else if (partList == currentList()) {
// *DO NOTHING*
} else {
if (!toInsertList.isEmpty()) {
@ -79,8 +97,16 @@ ResizeVolumeGroupOperation::ResizeVolumeGroupOperation(LvmDevice& d, const QStri
QString ResizeVolumeGroupOperation::description() const
{
QString tList = targetList().join(QStringLiteral(", "));
QString curList = currentList().join(QStringLiteral(", "));
QString tList = QString();
for (const auto &p : targetList()) {
tList += p->deviceNode() + QStringLiteral(", ");
}
tList.chop(2);
QString curList = QString();
for (const auto &p : currentList()) {
curList += p->deviceNode() + QStringLiteral(", ");
}
curList.chop(2);
return xi18nc("@info/plain", "Resize volume %1 from %2 to %3", device().name(), curList, tList);
}
@ -92,8 +118,8 @@ bool ResizeVolumeGroupOperation::targets(const Device& d) const
bool ResizeVolumeGroupOperation::targets(const Partition& p) const
{
for (const QString &partPath : targetList()) {
if (partPath == p.partitionPath()) {
for (const auto &partition : targetList()) {
if (partition->partitionPath() == p.partitionPath()) {
return true;
}
}

View File

@ -39,7 +39,7 @@ class LIBKPMCORE_EXPORT ResizeVolumeGroupOperation : public Operation
friend class OperationStack;
public:
ResizeVolumeGroupOperation(LvmDevice& dev, const QStringList partlist);
ResizeVolumeGroupOperation(LvmDevice& dev, const QList<const Partition*>& partlist);
public:
QString iconName() const override {
@ -64,11 +64,11 @@ protected:
const LvmDevice& device() const {
return m_Device;
}
const QStringList targetList() const {
const QList<const Partition*>& targetList() const {
return m_TargetList;
}
const QStringList currentList() const {
const QList<const Partition*>& currentList() const {
return m_CurrentList;
}
@ -95,8 +95,8 @@ protected:
private:
LvmDevice& m_Device;
const QStringList m_TargetList;
const QStringList m_CurrentList;
QList<const Partition*> m_TargetList;
QList<const Partition*> m_CurrentList;
qint64 m_TargetSize;
qint64 m_CurrentSize;