diff mbox

[v2,2/6] qemu-img create: Support multiple -o options

Message ID 1392908243-8835-3-git-send-email-kwolf@redhat.com
State New
Headers show

Commit Message

Kevin Wolf Feb. 20, 2014, 2:57 p.m. UTC
If you specified multiple -o options for qemu-img create, it would
silently ignore all but the last one. This patch fixes the problem.

Now multiple -o options has the same meaning as having a single option
with all settings in the order of their respective -o options.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qemu-img.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

Comments

Eric Blake Feb. 20, 2014, 3:52 p.m. UTC | #1
On 02/20/2014 07:57 AM, Kevin Wolf wrote:
> If you specified multiple -o options for qemu-img create, it would
> silently ignore all but the last one. This patch fixes the problem.
> 
> Now multiple -o options has the same meaning as having a single option
> with all settings in the order of their respective -o options.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  qemu-img.c | 24 ++++++++++++++++++------
>  1 file changed, 18 insertions(+), 6 deletions(-)

Much nicer than v1!

Reviewed-by: Eric Blake <eblake@redhat.com>

>          case 'o':
> -            options = optarg;
> +            if (!options) {
> +                options = g_strdup(optarg);
> +            } else {
> +                char *old_options = options;
> +                options = g_strdup_printf("%s,%s", options, optarg);
> +                g_free(old_options);
> +            }

Corner case:

Previously:

qemu-img create -f qcow2 -o backing_file=/dev/null, xyz 1M

creates xyz with a backing file of /dev/null and unspecified backing
format (and ignores the empty trailing option).

Now:

qemu-img create -f qcow2 -o backing_file=/dev/null, \
  -o backing_fmt=qcow2 xyz 1M

creates xyz with a backing file of '/dev/null,backing_fmt=qcow2' and
with an unspecified backing format (and NOT with a backing format of
qcow2).  Yikes!  What we thought was an empty trailing option instead
got turned into a literal comma in the backing_file option.

Maybe you should add a followup patch that errors out on trailing
commas, so that we don't end up creating ',,' in the concatenated
version.  And maybe patch get_opt_value() to detect and reject empty
trailing options as well.

But as this patch is already a strict improvement, it does not have to
be done in this commit.
Eric Blake Feb. 20, 2014, 4:24 p.m. UTC | #2
On 02/20/2014 08:52 AM, Eric Blake wrote:

> Previously:
> 
> qemu-img create -f qcow2 -o backing_file=/dev/null, xyz 1M
> 
> creates xyz with a backing file of /dev/null and unspecified backing
> format (and ignores the empty trailing option).

>   And maybe patch get_opt_value() to detect and reject empty
> trailing options as well.

Further evidence for my desire to improve get_opt_value() for
consistency: we already reject empty leading options...

$ qemu-img create -f qcow2 -o ,backing_file=/dev/null xyz 1MUnknown
option ''
qemu-img: xyz: Invalid options for file format 'qcow2'.

...although we happily ignore an empty option:

$ qemu-img create -f qcow2 -o '' xyz 1M
Formatting 'xyz', fmt=qcow2 size=1048576 encryption=off
cluster_size=65536 lazy_refcounts=off
Jeff Cody Feb. 20, 2014, 7:14 p.m. UTC | #3
On Thu, Feb 20, 2014 at 03:57:19PM +0100, Kevin Wolf wrote:
> If you specified multiple -o options for qemu-img create, it would
> silently ignore all but the last one. This patch fixes the problem.
> 
> Now multiple -o options has the same meaning as having a single option
> with all settings in the order of their respective -o options.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  qemu-img.c | 24 ++++++++++++++++++------
>  1 file changed, 18 insertions(+), 6 deletions(-)
> 
> diff --git a/qemu-img.c b/qemu-img.c
> index b834d52..770e4e6 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -369,13 +369,19 @@ static int img_create(int argc, char **argv)
>          case 'e':
>              error_report("option -e is deprecated, please use \'-o "
>                    "encryption\' instead!");
> -            return 1;
> +            goto fail;
>          case '6':
>              error_report("option -6 is deprecated, please use \'-o "
>                    "compat6\' instead!");
> -            return 1;
> +            goto fail;
>          case 'o':
> -            options = optarg;
> +            if (!options) {
> +                options = g_strdup(optarg);
> +            } else {
> +                char *old_options = options;
> +                options = g_strdup_printf("%s,%s", options, optarg);
> +                g_free(old_options);
> +            }
>              break;
>          case 'q':
>              quiet = true;
> @@ -403,7 +409,7 @@ static int img_create(int argc, char **argv)
>                  error_report("kilobytes, megabytes, gigabytes, terabytes, "
>                               "petabytes and exabytes.");
>              }
> -            return 1;
> +            goto fail;
>          }
>          img_size = (uint64_t)sval;
>      }
> @@ -411,7 +417,8 @@ static int img_create(int argc, char **argv)
>          help();
>      }
>  
> -    if (options && is_help_option(options)) {
> +    if (options && has_help_option(options)) {
> +        g_free(options);
>          return print_block_option_help(filename, fmt);
>      }
>  
> @@ -420,10 +427,15 @@ static int img_create(int argc, char **argv)
>      if (error_is_set(&local_err)) {
>          error_report("%s: %s", filename, error_get_pretty(local_err));
>          error_free(local_err);
> -        return 1;
> +        goto fail;
>      }
>  
> +    g_free(options);
>      return 0;
> +
> +fail:
> +    g_free(options);
> +    return 1;

One minor preference: there are 3 separate cleanup spots to free
'options'.  If a ret variable was used, the label 'fail:' could become
'out:', and those 3 cleanups could be consolidated to a single
g_free(options).  That would help any future committer not forget a
cleanup spot.

But, as it is correct as-is:

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

>  }
>  
>  static void dump_json_image_check(ImageCheck *check, bool quiet)
> -- 
> 1.8.1.4
> 
>
diff mbox

Patch

diff --git a/qemu-img.c b/qemu-img.c
index b834d52..770e4e6 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -369,13 +369,19 @@  static int img_create(int argc, char **argv)
         case 'e':
             error_report("option -e is deprecated, please use \'-o "
                   "encryption\' instead!");
-            return 1;
+            goto fail;
         case '6':
             error_report("option -6 is deprecated, please use \'-o "
                   "compat6\' instead!");
-            return 1;
+            goto fail;
         case 'o':
-            options = optarg;
+            if (!options) {
+                options = g_strdup(optarg);
+            } else {
+                char *old_options = options;
+                options = g_strdup_printf("%s,%s", options, optarg);
+                g_free(old_options);
+            }
             break;
         case 'q':
             quiet = true;
@@ -403,7 +409,7 @@  static int img_create(int argc, char **argv)
                 error_report("kilobytes, megabytes, gigabytes, terabytes, "
                              "petabytes and exabytes.");
             }
-            return 1;
+            goto fail;
         }
         img_size = (uint64_t)sval;
     }
@@ -411,7 +417,8 @@  static int img_create(int argc, char **argv)
         help();
     }
 
-    if (options && is_help_option(options)) {
+    if (options && has_help_option(options)) {
+        g_free(options);
         return print_block_option_help(filename, fmt);
     }
 
@@ -420,10 +427,15 @@  static int img_create(int argc, char **argv)
     if (error_is_set(&local_err)) {
         error_report("%s: %s", filename, error_get_pretty(local_err));
         error_free(local_err);
-        return 1;
+        goto fail;
     }
 
+    g_free(options);
     return 0;
+
+fail:
+    g_free(options);
+    return 1;
 }
 
 static void dump_json_image_check(ImageCheck *check, bool quiet)