From: Karel Zak Date: Fri, 18 Apr 2014 12:00:39 +0000 (+0200) Subject: libfdisk: (gpt) implement 'fix order' commnad X-Git-Tag: v2.25-rc1~260 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9348ef251102eefdf9e352616393778f0950720f;p=thirdparty%2Futil-linux.git libfdisk: (gpt) implement 'fix order' commnad Signed-off-by: Karel Zak --- diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index aba33cd37c..2026c538fd 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -1371,6 +1371,7 @@ done: return rc; } + /* * List label partitions. */ @@ -2272,6 +2273,47 @@ static int gpt_toggle_partition_flag( return 0; } +static int gpt_entry_cmp_start(const void *a, const void *b) +{ + struct gpt_entry *ae = (struct gpt_entry *) a, + *be = (struct gpt_entry *) b; + int au = partition_unused(ae), + bu = partition_unused(be); + + if (au && bu) + return 0; + if (au) + return 1; + if (bu) + return -1; + + return gpt_partition_start(ae) - gpt_partition_start(be); +} + +/* sort partition by start sector */ +static int gpt_reorder(struct fdisk_context *cxt) +{ + struct fdisk_gpt_label *gpt; + size_t nparts; + + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + gpt = self_label(cxt); + nparts = le32_to_cpu(gpt->pheader->npartition_entries); + + qsort(gpt->ents, nparts, sizeof(struct gpt_entry), + gpt_entry_cmp_start); + + gpt_recompute_crc(gpt->pheader, gpt->ents); + gpt_recompute_crc(gpt->bheader, gpt->ents); + fdisk_label_set_changed(cxt->label, 1); + + fdisk_sinfo(cxt, FDISK_INFO_SUCCESS, _("Done.")); + return 0; +} + static int gpt_reset_alignment(struct fdisk_context *cxt) { struct fdisk_gpt_label *gpt; @@ -2329,6 +2371,7 @@ static const struct fdisk_label_operations gpt_operations = .create = gpt_create_disklabel, .list = gpt_list_disklabel, .locate = gpt_locate_disklabel, + .reorder = gpt_reorder, .get_id = gpt_get_disklabel_id, .set_id = gpt_set_disklabel_id,