Patchwork [v2,08/12] qemu-img: introduce qemu_img_handle_error

login
register
mail settings
Submitter Pavel Hrdina
Date March 22, 2013, 1:16 p.m.
Message ID <30db14a4083032599fe732b07ff5cde2dc190c67.1363957855.git.phrdina@redhat.com>
Download mbox | patch
Permalink /patch/230017/
State New
Headers show

Comments

Pavel Hrdina - March 22, 2013, 1:16 p.m.
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 qemu-img.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
Eric Blake - March 26, 2013, 9:26 p.m.
On 03/22/2013 07:16 AM, Pavel Hrdina wrote:
> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
> ---
>  qemu-img.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/qemu-img.c b/qemu-img.c
> index 21d02bf..34badad 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -322,6 +322,14 @@ static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
>      return 0;
>  }
>  
> +static void qemu_img_handle_error(Error *err)
> +{
> +    if (error_is_set(&err)) {

Here you say it is permissible to pass in an unset err,...

> +        error_report("%s", error_get_pretty(err));
> +        error_free(err);
> +    }
> +}
> +
>  static int img_create(int argc, char **argv)
>  {
>      int c;
> @@ -401,8 +409,7 @@ static int img_create(int argc, char **argv)
>      bdrv_img_create(filename, fmt, base_filename, base_fmt,
>                      options, img_size, BDRV_O_FLAGS, &local_err, quiet);
>      if (error_is_set(&local_err)) {
> -        error_report("%s", error_get_pretty(local_err));
> -        error_free(local_err);
> +        qemu_img_handle_error(local_err);

...which means you are duplicating the error_is_set() call here.

>          return 1;
>      }

Does it make sense to have qemu_img_handle_error() return a different
value according to whether an error was present, so that you could
instead write:

if (qemu_img_handle_error(local_err) < 0) {
    return 1;
}

Or, if all callers are already checking for an actual error before
calling qemu_img_handle_error (for other reasons, such as img_create's
reason of control flow), should you drop the redundant condition out of
the helper function?

Patch

diff --git a/qemu-img.c b/qemu-img.c
index 21d02bf..34badad 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -322,6 +322,14 @@  static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
     return 0;
 }
 
+static void qemu_img_handle_error(Error *err)
+{
+    if (error_is_set(&err)) {
+        error_report("%s", error_get_pretty(err));
+        error_free(err);
+    }
+}
+
 static int img_create(int argc, char **argv)
 {
     int c;
@@ -401,8 +409,7 @@  static int img_create(int argc, char **argv)
     bdrv_img_create(filename, fmt, base_filename, base_fmt,
                     options, img_size, BDRV_O_FLAGS, &local_err, quiet);
     if (error_is_set(&local_err)) {
-        error_report("%s", error_get_pretty(local_err));
-        error_free(local_err);
+        qemu_img_handle_error(local_err);
         return 1;
     }