diff mbox

[v2,5/6] block: sort formats alphabetically in bdrv_iterate_format()

Message ID 1409137736-827-6-git-send-email-stefanha@redhat.com
State New
Headers show

Commit Message

Stefan Hajnoczi Aug. 27, 2014, 11:08 a.m. UTC
Format names are best consumed in alphabetical order.  This makes
human-readable output easy to produce.

bdrv_iterate_format() already has an array of format strings.  Sort them
before invoking the iteration callback.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 block.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

Comments

Benoît Canet Aug. 27, 2014, 2:23 p.m. UTC | #1
The Wednesday 27 Aug 2014 à 12:08:55 (+0100), Stefan Hajnoczi wrote :
> Format names are best consumed in alphabetical order.  This makes
> human-readable output easy to produce.
> 
> bdrv_iterate_format() already has an array of format strings.  Sort them
> before invoking the iteration callback.
> 
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>  block.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/block.c b/block.c
> index e9380f6..1df13ac 100644
> --- a/block.c
> +++ b/block.c
> @@ -3744,11 +3744,17 @@ const char *bdrv_get_format_name(BlockDriverState *bs)
>      return bs->drv ? bs->drv->format_name : NULL;
>  }
>  
> +static int qsort_strcmp(const void *a, const void *b)
> +{
> +    return strcmp(a, b);
> +}
> +
>  void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
>                           void *opaque)
>  {
>      BlockDriver *drv;
>      int count = 0;
> +    int i;
>      const char **formats = NULL;
>  
>      QLIST_FOREACH(drv, &bdrv_drivers, list) {
> @@ -3762,10 +3768,16 @@ void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
>              if (!found) {
>                  formats = g_renew(const char *, formats, count + 1);
>                  formats[count++] = drv->format_name;
> -                it(opaque, drv->format_name);
>              }
>          }
>      }
> +
> +    qsort(formats, count, sizeof(formats[0]), qsort_strcmp);

We are lucky this is not using qsort_r which was added in 2008 after RHEL 5 release :)

Reviewed-by: Benoît Canet <benoit.canet@nodalink.com>

> +
> +    for (i = 0; i < count; i++) {
> +        it(opaque, formats[i]);
> +    }
> +
>      g_free(formats);
>  }
>  
> -- 
> 1.9.3
> 
>
diff mbox

Patch

diff --git a/block.c b/block.c
index e9380f6..1df13ac 100644
--- a/block.c
+++ b/block.c
@@ -3744,11 +3744,17 @@  const char *bdrv_get_format_name(BlockDriverState *bs)
     return bs->drv ? bs->drv->format_name : NULL;
 }
 
+static int qsort_strcmp(const void *a, const void *b)
+{
+    return strcmp(a, b);
+}
+
 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
                          void *opaque)
 {
     BlockDriver *drv;
     int count = 0;
+    int i;
     const char **formats = NULL;
 
     QLIST_FOREACH(drv, &bdrv_drivers, list) {
@@ -3762,10 +3768,16 @@  void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
             if (!found) {
                 formats = g_renew(const char *, formats, count + 1);
                 formats[count++] = drv->format_name;
-                it(opaque, drv->format_name);
             }
         }
     }
+
+    qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
+
+    for (i = 0; i < count; i++) {
+        it(opaque, formats[i]);
+    }
+
     g_free(formats);
 }