From 851515216f0a6b2692a3228c59d78a756a72e665 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 28 Nov 2013 12:56:12 +0100 Subject: libfdisk: remove fdisk_get_partition_type() Let's use more generic: fdisk_get_partition() fdisk_partition_get_parttype() rather than fdisk_get_partition_type(). The patch also improves fdisk_get_partition() semantic to allocate a new partition struct if the argument is NULL. Signed-off-by: Karel Zak --- fdisks/fdisk.c | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'fdisks/fdisk.c') diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index a5c1fe634..db3b29870 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -160,7 +160,9 @@ void toggle_dos_compatibility_flag(struct fdisk_context *cxt) 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); @@ -168,28 +170,30 @@ void change_partition_type(struct fdisk_context *cxt) 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) -- cgit v1.2.3-55-g7522