diff mbox

[v2] block: Ignore duplicate or NULL format_name in bdrv_iterate_format

Message ID 1db5ce318669b35ce822b2aa6b8e4f5eb691f678.1398710311.git.jcody@redhat.com
State New
Headers show

Commit Message

Jeff Cody April 28, 2014, 10:29 p.m. UTC
Some block drivers have multiple BlockDriver instances with identical
format_name fields (e.g. gluster, nbd).

Both qemu-img and qemu will use bdrv_iterate_format() to list the
supported formats when a help option is invoked.  As protocols and
formats may register multiple drivers, redundant listings of formats
occur (e.g., "Supported formats: ... gluster gluster gluster gluster ...
").

Since the list of driver formats will be small, this performs a simple
linear search on format_name, and ignores any duplicates.

The end result change is that the iterator will no longer receive
duplicate string names, nor will it receive NULL pointers.

Signed-off-by: Jeff Cody <jcody@redhat.com>
---

Note: This is v2 of: 
      'block: prefer protocol_name over format_name in bdrv_iterate_format'

 block.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Comments

Kevin Wolf April 29, 2014, 9:55 a.m. UTC | #1
Am 29.04.2014 um 00:29 hat Jeff Cody geschrieben:
> Some block drivers have multiple BlockDriver instances with identical
> format_name fields (e.g. gluster, nbd).
> 
> Both qemu-img and qemu will use bdrv_iterate_format() to list the
> supported formats when a help option is invoked.  As protocols and
> formats may register multiple drivers, redundant listings of formats
> occur (e.g., "Supported formats: ... gluster gluster gluster gluster ...
> ").
> 
> Since the list of driver formats will be small, this performs a simple
> linear search on format_name, and ignores any duplicates.
> 
> The end result change is that the iterator will no longer receive
> duplicate string names, nor will it receive NULL pointers.
> 
> Signed-off-by: Jeff Cody <jcody@redhat.com>

Thanks, applied to the block branch.

Kevin
diff mbox

Patch

diff --git a/block.c b/block.c
index 4745712..100fa86 100644
--- a/block.c
+++ b/block.c
@@ -3601,10 +3601,25 @@  void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
                          void *opaque)
 {
     BlockDriver *drv;
+    int count = 0;
+    const char **formats = NULL;
 
     QLIST_FOREACH(drv, &bdrv_drivers, list) {
-        it(opaque, drv->format_name);
+        if (drv->format_name) {
+            bool found = false;
+            int i = count;
+            while (formats && i && !found) {
+                found = !strcmp(formats[--i], drv->format_name);
+            }
+
+            if (!found) {
+                formats = g_realloc(formats, (count + 1) * sizeof(char *));
+                formats[count++] = drv->format_name;
+                it(opaque, drv->format_name);
+            }
+        }
     }
+    g_free(formats);
 }
 
 /* This function is to find block backend bs */