diff mbox series

[v2,1/1] diskpart: improve partition diff logging

Message ID 20210516221805.29310-1-james.hilliard1@gmail.com
State Changes Requested
Headers show
Series [v2,1/1] diskpart: improve partition diff logging | expand

Commit Message

James Hilliard May 16, 2021, 10:18 p.m. UTC
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 handlers/diskpart_handler.c | 47 +++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
index b10ffc6..9261d65 100644
--- a/handlers/diskpart_handler.c
+++ b/handlers/diskpart_handler.c
@@ -131,10 +131,29 @@  static int diskpart_set_partition(struct fdisk_partition *pa,
 	return ret;
 }
 
+static void diskpart_partition_info(const char *name, struct fdisk_partition *pa, struct fdisk_context *cxt,
+		size_t nids, int *ids, struct fdisk_label *lb)
+{
+	size_t i;
+	TRACE("%s:", name);
+	for (i = 0; i < nids; i++) {
+		const struct fdisk_field *field =
+				fdisk_label_get_field(lb, ids[i]);
+		char *data = NULL;
+		if (!field)
+			continue;
+		if (fdisk_partition_to_string(pa, cxt, ids[i], &data))
+			continue;
+		TRACE("\t%s: %s", fdisk_field_get_name(field), data);
+		free(data);
+	}
+}
+
 /*
  * Return true if partition differs
  */
-static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *firstpa, struct fdisk_partition *secondpa)
+static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_context *cxt,
+		struct fdisk_partition *firstpa, struct fdisk_partition *secondpa)
 {
 	if (!firstpa || !secondpa)
 		return true;
@@ -151,11 +170,23 @@  static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *f
 			fdisk_parttype_get_code(fdisk_partition_get_type(firstpa)) !=
 			fdisk_parttype_get_code(fdisk_partition_get_type(secondpa))) ||
 		fdisk_partition_get_size(firstpa) != fdisk_partition_get_size(secondpa))) {
-		TRACE("Partition differ : %s(%llu) <--> %s(%llu)",
-			fdisk_partition_get_name (firstpa) ? fdisk_partition_get_name(firstpa) : "",
-			(long long unsigned)fdisk_partition_get_size(firstpa),
-			fdisk_partition_get_name(secondpa) ? fdisk_partition_get_name(secondpa) : "",
-			(long long unsigned)fdisk_partition_get_size(secondpa));
+		struct fdisk_label *lb;
+		int *ids = NULL;
+		size_t nids = 0;
+		lb = fdisk_get_label(cxt, NULL);
+		fdisk_label_get_fields_ids_all(lb, cxt, &ids, &nids);
+		TRACE("Partition differ:");
+		if (ids && lb) {
+			diskpart_partition_info("Original", firstpa, cxt, nids, ids, lb);
+			diskpart_partition_info("New", secondpa, cxt, nids, ids, lb);
+		} else {
+			if (!ids)
+				ERROR("Failed to load field ids");
+			if (!lb)
+				ERROR("Failed to load label");
+		}
+		if (ids)
+			free(ids);
 		return true;
 	}
 	return false;
@@ -394,7 +425,7 @@  static int diskpart(struct img_type *img,
 					ret = -EFAULT;
 					goto handler_exit;
 				}
-				if (diskpart_partition_cmp(lbtype, pa, newpa)) {
+				if (diskpart_partition_cmp(lbtype, cxt, pa, newpa)) {
 					createtable = true;
 				}
 
@@ -402,6 +433,8 @@  static int diskpart(struct img_type *img,
 				fdisk_unref_partition(pa);
 				i++;
 			}
+			fdisk_free_iter(itr);
+			fdisk_free_iter(olditr);
 		}
 	}