Skip to contentSkip to conversation

qemu-img: sort block formats in help message

PATCH Superseded Mike Day 2 participants 3 messages May 2, 2014 – May 5, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Mike Day

author
#0 lore raw

PATCH 1/1 qemu-img: sort block formats in help message

The help message for qemu-img lists the supported block formats, of which there are 27 as of version 2.0.50. The formats are printed in the order of their driver's position in a linked list, which appears random. This patch prints the formats in sorted order, making it easier to read and to find a specific format in the list.

---
 qemu-img.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/qemu-img.c b/qemu-img.c
index 96f4463..d8b7ef4 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -32,6 +32,7 @@
#include "block/block_int.h"
#include "block/qapi.h"
#include <getopt.h>
+#include <glib.h>

#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \
", Copyright (c) 2004-2008 Fabrice Bellard\n"
@@ -60,6 +61,32 @@ static void format_print(void *opaque, const char *name) printf(" %s", name);
}

+static gint compare_data(gconstpointer a, gconstpointer b, gpointer user) +{
+ return g_strcmp0((const char *)a, (const char *)b);
+}
+
+static void GFunc_print_format(gpointer data, gpointer user)
+{
+ format_print(user, data);
+}
+
+static GSequence *init_sequence(void)
+{
+ return g_sequence_new(NULL);
+}
+
+static void add_format_to_seq(void *opaque, const char *fmt_name)
+{
+ GSequence *seq = (GSequence *)opaque;
+
+ if (!g_sequence_lookup(seq, (gpointer)fmt_name,
+ compare_data, NULL)) {
+ g_sequence_insert_sorted(seq, (gpointer)fmt_name,
+ compare_data, NULL);
+ }
+}
+
static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) {
va_list ap;
@@ -144,8 +171,12 @@ static void QEMU_NORETURN help(void)
" '-s' run in Strict mode - fail on different image size or sector allocation\n";

printf("%s\nSupported formats:", help_msg);

 -    bdrv_iterate_format(format_print, NULL);
 +    GSequence *seq = init_sequence();
 +    bdrv_iterate_format(add_format_to_seq, seq);
 +    g_sequence_foreach(seq, GFunc_print_format, NULL);

printf("\n");
+ g_sequence_free(seq);
+
exit(EXIT_SUCCESS);
}

--
1.9.0

#1 lore raw
Quoted 5 lines · Mike Day

The help message for qemu-img lists the supported block formats, of which there are 27 as of version 2.0.50. The formats are printed in the order of their driver's position in a linked list, which appears random. This patch prints the formats in sorted order, making it easier to read and to find a specific format in the list.

Looks useful, thanks!

Quoted 2 lines

---

This patch is whitespace-damaged so git-am(1) refuses to apply it. Please use git-send-email(1) to send patches so that the right format is used.

$ git am
Applying: qemu-img: sort block formats in help message
fatal: unrecognized input
Quoted 19 lines
 qemu-img.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/qemu-img.c b/qemu-img.c
index 96f4463..d8b7ef4 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -32,6 +32,7 @@
#include "block/block_int.h"
#include "block/qapi.h"
#include <getopt.h>
+#include <glib.h>

#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \
", Copyright (c) 2004-2008 Fabrice Bellard\n"
@@ -60,6 +61,32 @@ static void format_print(void *opaque, const char *name) printf(" %s", name);
}

Quoted 6 lines

+static gint compare_data(gconstpointer a, gconstpointer b, gpointer user) +{
+ return g_strcmp0((const char *)a, (const char *)b);
+}
+
+static void GFunc_print_format(gpointer data, gpointer user)

QEMU coding style is lowercase function and variable names. The scripts/checkpatch.pl script should identify coding style violations, please run it.

Quoted 3 lines

+{
+ format_print(user, data);
+}

format_print() isn't called anywhere else, please inline it here since we no longer need it as its own function.

Quoted 5 lines

+
+static GSequence *init_sequence(void)
+{
+ return g_sequence_new(NULL);
+}

Any reason to hide g_sequence_new(NULL) in it's own helper function?

Quoted 10 lines

+
+static void add_format_to_seq(void *opaque, const char *fmt_name) +{
+ GSequence *seq = (GSequence *)opaque;
+
+ if (!g_sequence_lookup(seq, (gpointer)fmt_name,
+ compare_data, NULL)) {
+ g_sequence_insert_sorted(seq, (gpointer)fmt_name,
+ compare_data, NULL);
+ }

The type casts in this patch aren't necessary. In C void* casts to and from any type without an explicit cast. Only C++ demands explicit casts of void*.

author
#2 lore raw

Thanks for the review! I've been able to shorten the next version quite a bit.

Quoted 5 lines · Stefan Hajnoczi
Quoted 1 line

+static void GFunc_print_format(gpointer data, gpointer user)

QEMU coding style is lowercase function and variable names. The scripts/checkpatch.pl script should identify coding style violations, please run it.

Yeah this is ugly. I've long had checkpatch.pl configured as my pre-commit hook. I've confirmed again this morning that these ugly caps pass through with no error or warning. I'll look at the script.

Quoted 11 lines
Quoted 10 lines

+
+static void add_format_to_seq(void *opaque, const char *fmt_name) +{
+ GSequence *seq = (GSequence *)opaque;
+
+ if (!g_sequence_lookup(seq, (gpointer)fmt_name,
+ compare_data, NULL)) {
+ g_sequence_insert_sorted(seq, (gpointer)fmt_name,
+ compare_data, NULL);
+ }

Quoted 3 lines

The type casts in this patch aren't necessary. In C void* casts to and from any type without an explicit cast. Only C++ demands explicit casts of void*.

The casts are ugly, and I don't know how to get rid of them here beyond a pragma. Due to the block and glib APIs I have to cast away a const in the second parameter. I'm bracketed on both ends:
g_sequence_* needs that second parameter to be void * (pointer), while the the block api (bdrv_iterate_format) defines the this function pointer type as (*)(void *, const char *). Ideas?

Mike