From 8b60872ec01c185b003826adabe3aa4f52c44ad0 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 16 Apr 2014 12:51:08 +0200 Subject: [PATCH] libfdisk: add generic function to check partitions order Signed-off-by: Karel Zak --- disk-utils/fdisk.c | 6 ++++++ libfdisk/src/libfdisk.h | 1 + libfdisk/src/table.c | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index 2283c41427..8762a7ac8b 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -589,6 +589,12 @@ void list_disklabel(struct fdisk_context *cxt) free(str); } } + + if (fdisk_table_wrong_order(tb)) { + fputc('\n', stdout); + fdisk_info(cxt, _("Partition table entries are not in disk order.")); + } + fdisk_unref_table(tb); } diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index d9f33d3001..b44451da61 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -238,6 +238,7 @@ extern int fdisk_table_remove_partition(struct fdisk_table *tb, struct fdisk_par extern int fdisk_get_partitions(struct fdisk_context *cxt, struct fdisk_table **tb); extern int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb); +extern int fdisk_table_wrong_order(struct fdisk_table *tb); extern int fdisk_table_sort_partitions(struct fdisk_table *tb, int (*cmp)(struct fdisk_partition *, struct fdisk_partition *)); diff --git a/libfdisk/src/table.c b/libfdisk/src/table.c index add6bd983d..27365c0404 100644 --- a/libfdisk/src/table.c +++ b/libfdisk/src/table.c @@ -525,6 +525,29 @@ done: return rc; } +/** + * fdisk_table_wrong_order: + * @tb: table + * + * Returns: 1 of the table is not in disk order + */ +int fdisk_table_wrong_order(struct fdisk_table *tb) +{ + struct fdisk_partition *pa; + struct fdisk_iter itr; + sector_t last = 0; + + DBG(TAB, ul_debugobj(tb, "wrong older check")); + + fdisk_reset_iter(&itr, FDISK_ITER_FORWARD); + while (tb && fdisk_table_next_partition(tb, &itr, &pa) == 0) { + if (pa->start < last) + return 1; + last = pa->start; + } + return 0; +} + /** * fdisk_table_to_string * @tb: table -- 2.39.5