diff mbox series

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

Message ID 20210517093228.36987-1-james.hilliard1@gmail.com
State New
Headers show
Series [v3,1/1] diskpart: improve partition diff logging | expand

Commit Message

James Hilliard May 17, 2021, 9:32 a.m. UTC
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 handlers/diskpart_handler.c | 44 +++++++++++++++++++++++++++++++------
 1 file changed, 37 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
index b10ffc6..d990ed9 100644
--- a/handlers/diskpart_handler.c
+++ b/handlers/diskpart_handler.c
@@ -131,10 +131,42 @@  static int diskpart_set_partition(struct fdisk_partition *pa,
 	return ret;
 }
 
+static void diskpart_partition_info(struct fdisk_context *cxt, const char *name, struct fdisk_partition *pa)
+{
+	struct fdisk_label *lb;
+	int *ids = NULL;
+	size_t nids = 0;
+	size_t i;
+	lb = fdisk_get_label(cxt, NULL);
+	fdisk_label_get_fields_ids_all(lb, cxt, &ids, &nids);
+	if (ids && lb) {
+		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);
+		}
+	} else {
+		if (!ids)
+			ERROR("Failed to load field ids");
+		if (!lb)
+			ERROR("Failed to load label");
+	}
+	if (ids)
+		free(ids);
+}
+
 /*
  * 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 +183,9 @@  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));
+		TRACE("Partition differ:");
+		diskpart_partition_info(cxt, "Original", firstpa);
+		diskpart_partition_info(cxt, "New", secondpa);
 		return true;
 	}
 	return false;
@@ -394,7 +424,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;
 				}