void change_partition_type(struct fdisk_context *cxt)
{
size_t i;
- struct fdisk_parttype *t = NULL, *org_t = NULL;
+ struct fdisk_parttype *t = NULL;
+ struct fdisk_partition *pa = NULL;
+ const char *old = NULL;
assert(cxt);
assert(cxt->label);
if (fdisk_ask_partnum(cxt, &i, FALSE))
return;
- org_t = t = fdisk_get_partition_type(cxt, i);
- if (!t)
- fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
- else {
- do {
- t = ask_partition_type(cxt);
- } while (!t);
-
- if (fdisk_set_partition_type(cxt, i, t) == 0)
- fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
- _("Changed type of partition '%s' to '%s'."),
- org_t ? org_t->name : _("Unknown"),
- t ? t->name : _("Unknown"));
- else
- fdisk_info(cxt,
- _("Type of partition %zu is unchanged: %s."),
- i + 1,
- org_t ? org_t->name : _("Unknown"));
- }
-
- fdisk_free_parttype(t);
- fdisk_free_parttype(org_t);
+ if (fdisk_get_partition(cxt, i, &pa)) {
+ fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
+ return;
+ }
+
+ t = (struct fdisk_parttype *) fdisk_partition_get_type(pa);
+ old = t ? t->name : _("Unknown");
+
+ do {
+ t = ask_partition_type(cxt);
+ } while (!t);
+
+ fdisk_partition_set_type(pa, t);
+
+ if (fdisk_set_partition_type(cxt, i, t) == 0)
+ fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
+ _("Changed type of partition '%s' to '%s'."),
+ old, t ? t->name : _("Unknown"));
+ else
+ fdisk_info(cxt,
+ _("Type of partition %zu is unchanged: %s."),
+ i + 1, old);
+
+ fdisk_free_partition(pa);
}
void list_disk_geometry(struct fdisk_context *cxt)
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
- struct fdisk_partition *pa)
+ struct fdisk_partition **pa)
{
int rc;
if (!cxt->label->op->get_part)
return -ENOSYS;
- fdisk_reset_partition(pa);
- pa->cxt = cxt;
- pa->partno = partno;
-
- rc = cxt->label->op->get_part(cxt, partno, pa);
- if (rc == 0 && fdisk_partition_is_used(pa))
+ if (!*pa) {
+ *pa = fdisk_new_partition();
+ if (!*pa)
+ return -ENOMEM;
+ } else
+ fdisk_reset_partition(*pa);
+ (*pa)->cxt = cxt;
+ (*pa)->partno = partno;
+
+ rc = cxt->label->op->get_part(cxt, partno, *pa);
+ if (rc == 0 && fdisk_partition_is_used(*pa))
DBG(LABEL, dbgprint("get partition %zu", partno));
return rc;
}
rc = -ENOMEM;
goto done;
}
- pa = fdisk_new_partition();
- if (!pa) {
- rc = -ENOMEM;
- goto done;
- }
/* define table columns */
for (j = 0; j < ncols; j++) {
for (i = 0; i < cxt->label->nparts_max; i++) {
struct tt_line *ln;
- rc = fdisk_get_partition(cxt, i, pa);
+ rc = fdisk_get_partition(cxt, i, &pa);
if (rc)
continue;
if (!fdisk_partition_is_used(pa))
return cxt->label->op->set_id(cxt);
}
-/**
- * fdisk_get_partition_type:
- * @cxt: fdisk context
- * @partnum: partition number
- *
- * Returns partition type or NULL upon failure.
- */
-struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt,
- size_t partnum)
-{
- if (!cxt || !cxt->label || !cxt->label->op->part_get_type)
- return NULL;
-
- DBG(LABEL, dbgprint("partition: %zd: get type", partnum));
- return cxt->label->op->part_get_type(cxt, partnum);
-}
-
/**
* fdisk_set_partition_type:
* @cxt: fdisk context
extern int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id);
extern int fdisk_set_disklabel_id(struct fdisk_context *cxt);
-extern int fdisk_get_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition *pa);
+extern int fdisk_get_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition **pa);
extern int fdisk_add_partition(struct fdisk_context *cxt, struct fdisk_parttype *t);
extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum);
-extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, size_t partnum);
extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum,
struct fdisk_parttype *t);
{
if (!pa)
return -EINVAL;
+ fdisk_free_parttype(pa->type);
pa->type = type;
return 0;
}