bool LibPartedPartitionTable::deletePartition(Report& report, const Partition& partition) { Q_ASSERT(partition.devicePath() == QString::fromUtf8(pedDevice()->path)); bool rval = false; PedPartition* pedPartition = partition.roles().has(PartitionRole::Extended) ? ped_disk_extended_partition(pedDisk()) : ped_disk_get_partition_by_sector(pedDisk(), partition.firstSector()); if (pedPartition) { rval = ped_disk_delete_partition(pedDisk(), pedPartition); if (!rval) report.line() << xi18nc("@info:progress", "Could not delete partition <filename>%1</filename>.", partition.deviceNode()); } else report.line() << xi18nc("@info:progress", "Deleting partition failed: Partition to delete (<filename>%1</filename>) not found on disk.", partition.deviceNode()); return rval; }
bool MParted::MParted_Core::removePartition(MParted::Partition & partition) { if (!openDeviceAndDisk(partition.devicePath)) return false; PedPartition *pedPartition = NULL; if (partition.type == MParted::TYPE_EXTENDED) pedPartition = ped_disk_extended_partition(pedDisk); else pedPartition = ped_disk_get_partition_by_sector(pedDisk, partition.getSector()); bool success = false; if (pedPartition != NULL) success = ped_disk_delete_partition(pedDisk, pedPartition) && commit(); closeDeviceAndDisk(); return success; }
gboolean part_del_partition (char *device_file, guint64 offset) { gboolean ret; PedDevice *device; PedDisk *disk; PedPartition *part; PartitionTable *p; gboolean is_extended; int n; HAL_INFO (("In part_del_partition: device_file=%s, offset=%lld", device_file, offset)); ret = FALSE; /* sigh.. one would think that if you passed the sector of where the * the beginning of the extended partition starts, then _by_sector * would return the same as _extended_partition. * * Sadly it's not so.. * * So, check if the passed offset actually corresponds to a nested * partition table... */ is_extended = FALSE; p = part_table_load_from_disk (device_file); if (p == NULL) { HAL_INFO (("Cannot load partition table from %s", device_file)); goto out; } for (n = 0; n < part_table_get_num_entries (p); n++) { PartitionTable *nested; nested = part_table_entry_get_nested (p, n); if (nested != NULL) { if (part_table_get_offset (nested) == offset) { HAL_INFO (("partition to delete is an extended partition")); is_extended = TRUE; } } } part_table_free (p); device = ped_device_get (device_file); if (device == NULL) { HAL_INFO (("ped_device_get() failed")); goto out; } HAL_INFO (("got it")); disk = ped_disk_new (device); if (disk == NULL) { HAL_INFO (("ped_disk_new() failed")); goto out_ped_device; } HAL_INFO (("got disk")); if (is_extended) { part = ped_disk_extended_partition (disk); } else { part = ped_disk_get_partition_by_sector (disk, offset / 512); } if (part == NULL) { HAL_INFO (("ped_disk_get_partition_by_sector() failed")); goto out_ped_disk; } HAL_INFO (("got partition - part->type=%d", part->type)); /* allow only to delete primary, logical and extended partitions */ if (! ((part->type == PED_PARTITION_NORMAL) || (part->type == PED_PARTITION_LOGICAL) || (part->type == PED_PARTITION_EXTENDED))) { HAL_INFO (("no data partition at given offset %lld for device %s", offset, device_file)); goto out_ped_disk; } if (ped_disk_delete_partition (disk, part) == 0) { HAL_INFO (("ped_disk_delete_partition() failed")); goto out_ped_disk; } /* use commit_to_dev rather than just commit to avoid * libparted sending BLKRRPART to the kernel - we want to do * this ourselves... */ if (ped_disk_commit_to_dev (disk) == 0) { HAL_INFO (("ped_disk_commit_to_dev() failed")); goto out_ped_disk; } HAL_INFO (("committed to disk")); ret = TRUE; ped_disk_destroy (disk); ped_device_destroy (device); goto out; out_ped_disk: ped_disk_destroy (disk); out_ped_device: ped_device_destroy (device); out: return ret; }