Don't assume a device always has a valid partition table. This is the correct

fix for what I initially tried to fix with commit 898001.

svn path=/trunk/extragear/sysadmin/partitionmanager/; revision=908045
This commit is contained in:
Volker Lanz 2009-01-09 07:10:59 +00:00
parent edb74962fe
commit 306667e09e
17 changed files with 160 additions and 112 deletions

View File

@ -36,7 +36,7 @@ Device::Device(const QString& name, const QString& devicenode, qint32 heads, qin
QObject(),
m_Name(name),
m_DeviceNode(devicenode),
m_PartitionTable(new PartitionTable()),
m_PartitionTable(NULL),
m_Heads(heads),
m_SectorsPerTrack(numSectors),
m_Cylinders(cylinders),

View File

@ -27,6 +27,7 @@
class PartitionTable;
class CreatePartitionTableOperation;
class LibParted;
/** @brief A device.
@ -43,6 +44,7 @@ class Device : public QObject
Q_DISABLE_COPY(Device)
friend class CreatePartitionTableOperation;
friend class LibParted;
public:
Device(const QString& name, const QString& devicenode, qint32 heads, qint32 numSectors, qint32 cylinders, qint64 sectorSize);
@ -51,8 +53,8 @@ class Device : public QObject
public:
const QString& name() const { return m_Name; } /**< @return the Device's name, usually some manufacturer string */
const QString& deviceNode() const { return m_DeviceNode; } /**< @return the Device's node, for example "/dev/sda" */
PartitionTable& partitionTable() { return *m_PartitionTable; } /**< @return the Device's PartitionTable */
const PartitionTable& partitionTable() const { return *m_PartitionTable; } /**< @return the Device's PartitionTable */
PartitionTable* partitionTable() { return m_PartitionTable; } /**< @return the Device's PartitionTable */
const PartitionTable* partitionTable() const { return m_PartitionTable; } /**< @return the Device's PartitionTable */
qint32 heads() const { return m_Heads; } /**< @return the number of heads on the Device in CHS notation */
qint32 cylinders() const { return m_Cylinders; } /**< @return the number of cylinders on the Device in CHS notation */
qint32 sectorsPerTrack() const { return m_SectorsPerTrack; } /**< @return the number of sectors on the Device in CHS notation */

View File

@ -173,6 +173,7 @@ static void scanDevicePartitions(PedDevice* pedDevice, Device& d, PedDisk* pedDi
{
Q_ASSERT(pedDevice);
Q_ASSERT(pedDisk);
Q_ASSERT(d.partitionTable());
PedPartition* pedPartition = NULL;
@ -204,11 +205,11 @@ static void scanDevicePartitions(PedDevice* pedDevice, Device& d, PedDisk* pedDi
}
// Find an extended partition this partition is in.
PartitionNode* parent = d.partitionTable().findPartitionBySector(pedPartition->geom.start, PartitionRole(PartitionRole::Extended));
PartitionNode* parent = d.partitionTable()->findPartitionBySector(pedPartition->geom.start, PartitionRole(PartitionRole::Extended));
// None found, so it's a primary in the device's partition table.
if (parent == NULL)
parent = &d.partitionTable();
parent = d.partitionTable();
const QString node = pedDisk->dev->path + QString::number(pedPartition->num);
FileSystem* fs = FileSystemFactory::create(type, pedPartition->geom.start, pedPartition->geom.end);
@ -227,7 +228,7 @@ static void scanDevicePartitions(PedDevice* pedDevice, Device& d, PedDisk* pedDi
PartitionTable::isSnapped(d, *part);
}
d.partitionTable().updateUnallocated(d);
d.partitionTable()->updateUnallocated(d);
ped_disk_destroy(pedDisk);
}
@ -271,8 +272,9 @@ void LibParted::scanDevices(OperationStack& ostack)
if (pedDisk)
{
d->partitionTable().setMaxPrimaries(ped_disk_get_max_primary_partition_count(pedDisk));
d->partitionTable().setTypeName(pedDisk->type->name);
d->setPartitionTable(new PartitionTable());
d->partitionTable()->setMaxPrimaries(ped_disk_get_max_primary_partition_count(pedDisk));
d->partitionTable()->setTypeName(pedDisk->type->name);
scanDevicePartitions(pedDevice, *d, pedDisk, mountInfo);
}

View File

@ -431,7 +431,11 @@ void OperationStack::clearDevices()
Device* OperationStack::findDeviceForPartition(const Partition* p)
{
foreach (Device* d, previewDevices())
foreach(const Partition* part, d->partitionTable().children())
{
if (d->partitionTable() == NULL)
continue;
foreach(const Partition* part, d->partitionTable()->children())
{
if (part == p)
return d;
@ -440,6 +444,7 @@ Device* OperationStack::findDeviceForPartition(const Partition* p)
if (child == p)
return d;
}
}
return NULL;
}

View File

@ -271,10 +271,12 @@ bool PartitionTable::isSnapped(const Device& d, const Partition& p)
*/
static bool canSnapToSector(const Device& d, const Partition& p, qint64 s, const Partition* originalPartition)
{
Q_ASSERT(d.partitionTable());
if (s < d.sectorsPerTrack() || s >= d.totalSectors())
return false;
const Partition* other = d.partitionTable().findPartitionBySector(s, PartitionRole(PartitionRole::Logical | PartitionRole::Primary | PartitionRole::Extended | PartitionRole::Unallocated));
const Partition* other = d.partitionTable()->findPartitionBySector(s, PartitionRole(PartitionRole::Logical | PartitionRole::Primary | PartitionRole::Extended | PartitionRole::Unallocated));
if (other && other->roles().has(PartitionRole::Unallocated))
other = NULL;

View File

@ -118,9 +118,16 @@ void InfoPane::showDevice(const Device& d)
int y = createHeader(d.name());
createLabels(i18nc("@label device", "Path:"), d.deviceNode(), y++);
const QString type = (d.partitionTable().isReadOnly())
? i18nc("@label device", "%1 (read only)", d.partitionTable().typeName())
: d.partitionTable().typeName();
QString type = "---";
QString maxPrimaries = "---";
if (d.partitionTable() != NULL)
{
type = (d.partitionTable()->isReadOnly())
? i18nc("@label device", "%1 (read only)", d.partitionTable()->typeName())
: d.partitionTable()->typeName();
maxPrimaries = QString("%1/%2").arg(d.partitionTable()->numPrimaries()).arg(d.partitionTable()->maxPrimaries());
}
createLabels(i18nc("@label device", "Type:"), type, y++);
createLabels(i18nc("@label device", "Capacity:"), Capacity(d).toString(), y++);
@ -130,5 +137,5 @@ void InfoPane::showDevice(const Device& d)
createLabels(i18nc("@label device", "Sectors:"), KGlobal::locale()->formatNumber(d.sectorsPerTrack(), 0), y++);
createLabels(i18nc("@label device", "Sector size:"), Capacity(d.sectorSize()).toString(Capacity::Byte, Capacity::AppendUnit), y++);
createLabels(i18nc("@label device", "Cylinder size:"), i18ncp("@label", "1 Sector", "%1 Sectors", d.cylinderSize()), y++);
createLabels(i18nc("@label device", "Primaries/Max:"), QString("%1/%2").arg(d.partitionTable().numPrimaries()).arg(d.partitionTable().maxPrimaries()), y++);
createLabels(i18nc("@label device", "Primaries/Max:"), maxPrimaries, y++);
}

View File

@ -397,7 +397,7 @@ void MainWindow::enableActions()
const Partition* part = selectedPartition();
const bool readOnly = selectedDevice() == NULL || selectedDevice()->partitionTable().isReadOnly();
const bool readOnly = selectedDevice() == NULL || selectedDevice()->partitionTable() == NULL || selectedDevice()->partitionTable()->isReadOnly();
actionCollection()->action("newPartition")->setEnabled(!readOnly && NewOperation::canCreateNew(part));
const bool canResize = ResizeOperation::canGrow(part) || ResizeOperation::canShrink(part) || ResizeOperation::canMove(part);
@ -497,25 +497,28 @@ void MainWindow::updatePartitions()
if (selectedDevice() == NULL)
return;
partTableWidget().setPartitionTable(&selectedDevice()->partitionTable());
partTableWidget().setPartitionTable(selectedDevice()->partitionTable());
QTreeWidgetItem* deviceItem = new QTreeWidgetItem();
deviceItem->setText(0, selectedDevice()->name());
deviceItem->setIcon(0, SmallIcon("drive-harddisk"));
treePartitions().addTopLevelItem(deviceItem);
foreach(const Partition* p, selectedDevice()->partitionTable().children())
if (selectedDevice()->partitionTable() != NULL)
{
QTreeWidgetItem* item = createTreeWidgetItem(*p);
foreach(const Partition* child, p->children())
foreach(const Partition* p, selectedDevice()->partitionTable()->children())
{
QTreeWidgetItem* childItem = createTreeWidgetItem(*child);
item->addChild(childItem);
}
QTreeWidgetItem* item = createTreeWidgetItem(*p);
deviceItem->addChild(item);
item->setExpanded(true);
foreach(const Partition* child, p->children())
{
QTreeWidgetItem* childItem = createTreeWidgetItem(*child);
item->addChild(childItem);
}
deviceItem->addChild(item);
item->setExpanded(true);
}
}
treePartitions().setFirstItemColumnSpanned(deviceItem, true);
@ -549,16 +552,14 @@ void MainWindow::on_m_TreePartitions_itemDoubleClicked(QTreeWidgetItem* item, in
Partition* MainWindow::selectedPartition()
{
if (selectedDevice() == NULL || partTableWidget().activeWidget() == NULL || partTableWidget().activeWidget()->partition() == NULL)
if (selectedDevice() == NULL || selectedDevice()->partitionTable() == NULL || partTableWidget().activeWidget() == NULL || partTableWidget().activeWidget()->partition() == NULL)
return NULL;
// The active partition we get from PartTableWidget is const; we need non-const.
// So take the first sector and find the partition in the selected device's
// partition table.
const Partition* activePartition = partTableWidget().activeWidget()->partition();
PartitionTable& ptable = selectedDevice()->partitionTable();
return ptable.findPartitionBySector(activePartition->firstSector(), PartitionRole(PartitionRole::Any));
return selectedDevice()->partitionTable()->findPartitionBySector(activePartition->firstSector(), PartitionRole(PartitionRole::Any));
}
Device* MainWindow::selectedDevice()
@ -729,12 +730,14 @@ void MainWindow::onFinished()
static bool checkTooManyPartitions(QWidget* parent, const Device& d, const Partition& p)
{
if (p.roles().has(PartitionRole::Unallocated) && d.partitionTable().numPrimaries() >= d.partitionTable().maxPrimaries() && !p.roles().has(PartitionRole::Logical))
Q_ASSERT(d.partitionTable());
if (p.roles().has(PartitionRole::Unallocated) && d.partitionTable()->numPrimaries() >= d.partitionTable()->maxPrimaries() && !p.roles().has(PartitionRole::Logical))
{
KMessageBox::sorry(parent, i18nc("@info",
"<para>There are already %1 primary partitions on this device. This is the maximum number its partition table can handle.</para>"
"<para>You cannot create, paste or restore a primary partition on it before you delete an existing one.</para>",
d.partitionTable().numPrimaries()), i18nc("@title:window", "Too Many Primary Partitions."));
d.partitionTable()->numPrimaries()), i18nc("@title:window", "Too Many Primary Partitions."));
return true;
}
@ -752,12 +755,20 @@ void MainWindow::onNewPartition()
return;
}
Q_ASSERT(selectedDevice()->partitionTable());
if (selectedDevice()->partitionTable() == NULL)
{
kWarning() << "partition table on selected device is null";
return;
}
if (checkTooManyPartitions(this, *selectedDevice(), *selectedPartition()))
return;
Partition* newPartition = NewOperation::createNew(*selectedPartition());
NewDialog dlg(this, *selectedDevice(), *newPartition, selectedDevice()->partitionTable().childRoles(*selectedPartition()));
NewDialog dlg(this, *selectedDevice(), *newPartition, selectedDevice()->partitionTable()->childRoles(*selectedPartition()));
if (dlg.exec() == KDialog::Accepted)
{
PartitionTable::snap(*selectedDevice(), *newPartition);
@ -835,8 +846,16 @@ void MainWindow::onResizePartition()
return;
}
const qint64 freeBefore = selectedDevice()->partitionTable().freeSectorsBefore(*selectedPartition());
const qint64 freeAfter = selectedDevice()->partitionTable().freeSectorsAfter(*selectedPartition());
Q_ASSERT(selectedDevice()->partitionTable());
if (selectedDevice()->partitionTable() == NULL)
{
kWarning() << "partition table on selected device is null";
return;
}
const qint64 freeBefore = selectedDevice()->partitionTable()->freeSectorsBefore(*selectedPartition());
const qint64 freeAfter = selectedDevice()->partitionTable()->freeSectorsAfter(*selectedPartition());
Partition resizedPartition(*selectedPartition());
ResizeDialog dlg(this, *selectedDevice(), resizedPartition, freeBefore, freeAfter);

View File

@ -45,7 +45,7 @@ PartPropsDialog::PartPropsDialog(QWidget* parent, Device& d, Partition& p) :
m_Partition(p),
m_WarnFileSystemChange(false),
m_DialogWidget(new PartPropsWidget(this)),
m_ReadOnly(partition().isMounted() || partition().state() == Partition::StateCopy || partition().state() == Partition::StateRestore || d.partitionTable().isReadOnly()),
m_ReadOnly(partition().isMounted() || partition().state() == Partition::StateCopy || partition().state() == Partition::StateRestore || d.partitionTable()->isReadOnly()),
m_ForceRecreate(false)
{
setMainWidget(&dialogWidget());

View File

@ -345,8 +345,10 @@ void PartResizerWidget::resizeLogicals()
if (!partition().roles().has(PartitionRole::Extended) || partition().children().size() == 0)
return;
device().partitionTable().removeUnallocated(&partition());
device().partitionTable().insertUnallocated(device(), &partition(), partition().firstSector());
Q_ASSERT(device().partitionTable());
device().partitionTable()->removeUnallocated(&partition());
device().partitionTable()->insertUnallocated(device(), &partition(), partition().firstSector());
partWidget().updateChildren();
}

View File

@ -45,22 +45,17 @@ PartTableWidget::PartTableWidget(QWidget* parent) :
*/
void PartTableWidget::setPartitionTable(const PartitionTable* ptable)
{
Q_ASSERT(ptable);
clear();
if (ptable == NULL)
{
kWarning() << "ptable is null.";
return;
}
m_PartitionTable = ptable;
foreach(const Partition* p, partitionTable()->children())
if (partitionTable() != NULL)
{
widgets().append(new PartWidget(this, this, p));
widgets().last()->show();
foreach(const Partition* p, partitionTable()->children())
{
widgets().append(new PartWidget(this, this, p));
widgets().last()->show();
}
}
if (widgets().isEmpty())

View File

@ -181,5 +181,7 @@ void SizeDialogBase::onFreeSpaceAfterChanged(int newAfter)
const PartitionTable& SizeDialogBase::partitionTable() const
{
return device().partitionTable();
Q_ASSERT(device().partitionTable());
return *device().partitionTable();
}

View File

@ -45,7 +45,9 @@ bool CreatePartitionTableJob::run(Report& parent)
if (openPed(device().deviceNode(), true))
{
PedDiskType* pedDiskType = ped_disk_type_get(device().partitionTable().typeName().toAscii());
Q_ASSERT(device().partitionTable());
PedDiskType* pedDiskType = ped_disk_type_get(device().partitionTable()->typeName().toAscii());
if (pedDiskType)
{
@ -54,7 +56,7 @@ bool CreatePartitionTableJob::run(Report& parent)
ped_disk_destroy(pedDisk);
}
else
report->line() << i18nc("@info/plain", "Creating partition table failed: Could not retrieve partition table type \"%1\" for <filename>%2</filename>.", device().partitionTable().typeName(), device().deviceNode());
report->line() << i18nc("@info/plain", "Creating partition table failed: Could not retrieve partition table type \"%1\" for <filename>%2</filename>.", device().partitionTable()->typeName(), device().deviceNode());
closePed();
}

View File

@ -58,7 +58,9 @@ CopyOperation::CopyOperation(Device& targetdevice, Partition* copiedpartition, D
m_CheckTargetJob(NULL),
m_MaximizeJob(NULL)
{
Partition* dest = targetDevice().partitionTable().findPartitionBySector(copiedPartition().firstSector(), PartitionRole(PartitionRole::Primary | PartitionRole::Logical | PartitionRole::Unallocated));
Q_ASSERT(targetDevice().partitionTable());
Partition* dest = targetDevice().partitionTable()->findPartitionBySector(copiedPartition().firstSector(), PartitionRole(PartitionRole::Primary | PartitionRole::Logical | PartitionRole::Unallocated));
Q_ASSERT(dest);

View File

@ -36,12 +36,12 @@
CreatePartitionTableOperation::CreatePartitionTableOperation(Device& d) :
Operation(),
m_TargetDevice(d),
m_OldPartitionTable(&targetDevice().partitionTable()),
m_OldPartitionTable(targetDevice().partitionTable()),
m_PartitionTable(new PartitionTable()),
m_CreatePartitionTableJob(new CreatePartitionTableJob(targetDevice()))
{
addJob(createPartitionTableJob());
partitionTable().insertUnallocated(targetDevice(), &partitionTable(), targetDevice().sectorsPerTrack());
partitionTable()->insertUnallocated(targetDevice(), partitionTable(), targetDevice().sectorsPerTrack());
}
CreatePartitionTableOperation::~CreatePartitionTableOperation()
@ -52,19 +52,21 @@ CreatePartitionTableOperation::~CreatePartitionTableOperation()
void CreatePartitionTableOperation::preview()
{
targetDevice().setPartitionTable(&partitionTable());
targetDevice().partitionTable().updateUnallocated(targetDevice());
targetDevice().setPartitionTable(partitionTable());
targetDevice().partitionTable()->updateUnallocated(targetDevice());
}
void CreatePartitionTableOperation::undo()
{
targetDevice().setPartitionTable(&oldPartitionTable());
targetDevice().partitionTable().updateUnallocated(targetDevice());
targetDevice().setPartitionTable(oldPartitionTable());
if (targetDevice().partitionTable())
targetDevice().partitionTable()->updateUnallocated(targetDevice());
}
bool CreatePartitionTableOperation::execute(Report& parent)
{
targetDevice().setPartitionTable(&partitionTable());
targetDevice().setPartitionTable(partitionTable());
return Operation::execute(parent);
}
@ -74,7 +76,7 @@ bool CreatePartitionTableOperation::execute(Report& parent)
*/
bool CreatePartitionTableOperation::canCreate(const Device* device)
{
return device != NULL && !device->partitionTable().isChildMounted();
return device != NULL && (device->partitionTable() == NULL || !device->partitionTable()->isChildMounted());
}
QString CreatePartitionTableOperation::description() const

View File

@ -53,8 +53,8 @@ class CreatePartitionTableOperation : public Operation
Device& targetDevice() { return m_TargetDevice; }
const Device& targetDevice() const { return m_TargetDevice; }
PartitionTable& partitionTable() { return *m_PartitionTable; }
PartitionTable& oldPartitionTable() { return *m_OldPartitionTable; }
PartitionTable* partitionTable() { return m_PartitionTable; }
PartitionTable* oldPartitionTable() { return m_OldPartitionTable; }
CreatePartitionTableJob* createPartitionTableJob() { return m_CreatePartitionTableJob; }

View File

@ -47,17 +47,21 @@ Operation::~Operation()
void Operation::insertPreviewPartition(Device& device, Partition& p)
{
device.partitionTable().removeUnallocated();
Q_ASSERT(device.partitionTable());
device.partitionTable()->removeUnallocated();
p.parent()->insert(&p);
device.partitionTable().updateUnallocated(device);
device.partitionTable()->updateUnallocated(device);
}
void Operation::removePreviewPartition(Device& device, Partition& p)
{
Q_ASSERT(device.partitionTable());
if (p.parent()->remove(&p))
device.partitionTable().updateUnallocated(device);
device.partitionTable()->updateUnallocated(device);
else
kWarning() << "failed to remove partition " << p.deviceNode() << " at " << &p << " from preview.";
}

View File

@ -62,7 +62,9 @@ RestoreOperation::RestoreOperation(Device& d, Partition* p, const QString& filen
{
restorePartition().setState(Partition::StateRestore);
Partition* dest = targetDevice().partitionTable().findPartitionBySector(restorePartition().firstSector(), PartitionRole(PartitionRole::Primary | PartitionRole::Logical | PartitionRole::Unallocated));
Q_ASSERT(targetDevice().partitionTable());
Partition* dest = targetDevice().partitionTable()->findPartitionBySector(restorePartition().firstSector(), PartitionRole(PartitionRole::Primary | PartitionRole::Logical | PartitionRole::Unallocated));
Q_ASSERT(dest);