958e1b
From 2e9c1d07ca7609d66fa0433c0eebe8ab0d2508ed Mon Sep 17 00:00:00 2001
958e1b
From: Jeffrey Cody <jcody@redhat.com>
958e1b
Date: Tue, 27 May 2014 18:34:03 +0200
958e1b
Subject: [PATCH 06/13] block: Ignore duplicate or NULL format_name in bdrv_iterate_format
958e1b
958e1b
RH-Author: Jeffrey Cody <jcody@redhat.com>
958e1b
Message-id: <d4929261531a1ad759bbaed14cec6968e554f7c6.1401215469.git.jcody@redhat.com>
958e1b
Patchwork-id: 59038
958e1b
O-Subject: [PATCH qemu-kvm RHEL7.1] block: Ignore duplicate or NULL format_name in bdrv_iterate_format
958e1b
Bugzilla: 1088695 1093983
958e1b
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
958e1b
RH-Acked-by: Fam Zheng <famz@redhat.com>
958e1b
RH-Acked-by: Max Reitz <mreitz@redhat.com>
958e1b
958e1b
Some block drivers have multiple BlockDriver instances with identical
958e1b
format_name fields (e.g. gluster, nbd).
958e1b
958e1b
Both qemu-img and qemu will use bdrv_iterate_format() to list the
958e1b
supported formats when a help option is invoked.  As protocols and
958e1b
formats may register multiple drivers, redundant listings of formats
958e1b
occur (e.g., "Supported formats: ... gluster gluster gluster gluster ...
958e1b
").
958e1b
958e1b
Since the list of driver formats will be small, this performs a simple
958e1b
linear search on format_name, and ignores any duplicates.
958e1b
958e1b
The end result change is that the iterator will no longer receive
958e1b
duplicate string names, nor will it receive NULL pointers.
958e1b
958e1b
Signed-off-by: Jeff Cody <jcody@redhat.com>
958e1b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
958e1b
(cherry picked from commit e855e4fb7b97f7f605e1f44427b98022e39e6f8f)
958e1b
---
958e1b
958e1b
RHEL7 Notes:
958e1b
Brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=7505698
958e1b
BZ: 1093983
958e1b
BZ: 1088695
958e1b
958e1b
 block.c | 17 ++++++++++++++++-
958e1b
 1 file changed, 16 insertions(+), 1 deletion(-)
958e1b
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
---
958e1b
 block.c |   17 ++++++++++++++++-
958e1b
 1 files changed, 16 insertions(+), 1 deletions(-)
958e1b
958e1b
diff --git a/block.c b/block.c
958e1b
index 4906f6b..43e325e 100644
958e1b
--- a/block.c
958e1b
+++ b/block.c
958e1b
@@ -3402,10 +3402,25 @@ void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
958e1b
                          void *opaque)
958e1b
 {
958e1b
     BlockDriver *drv;
958e1b
+    int count = 0;
958e1b
+    const char **formats = NULL;
958e1b
 
958e1b
     QLIST_FOREACH(drv, &bdrv_drivers, list) {
958e1b
-        it(opaque, drv->format_name);
958e1b
+        if (drv->format_name) {
958e1b
+            bool found = false;
958e1b
+            int i = count;
958e1b
+            while (formats && i && !found) {
958e1b
+                found = !strcmp(formats[--i], drv->format_name);
958e1b
+            }
958e1b
+
958e1b
+            if (!found) {
958e1b
+                formats = g_realloc(formats, (count + 1) * sizeof(char *));
958e1b
+                formats[count++] = drv->format_name;
958e1b
+                it(opaque, drv->format_name);
958e1b
+            }
958e1b
+        }
958e1b
     }
958e1b
+    g_free(formats);
958e1b
 }
958e1b
 
958e1b
 BlockDriverState *bdrv_find(const char *name)
958e1b
-- 
958e1b
1.7.1
958e1b