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