diff mbox

[v20,12/26] qcow2.c: replace QEMUOptionParameter with QemuOpts in amend options

Message ID 1392186806-10418-13-git-send-email-cyliu@suse.com
State New
Headers show

Commit Message

Chunyan Liu Feb. 12, 2014, 6:33 a.m. UTC
qcow2.c: replace QEMUOptionParameter with QemuOpts in 'qemu-img amend'

Signed-off-by: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
Signed-off-by: Chunyan Liu <cyliu@suse.com>
---
 block.c                   |    4 +-
 block/qcow2.c             |   90 ++++++++++++++++++++------------------------
 include/block/block.h     |    2 +-
 include/block/block_int.h |    2 +-
 qemu-img.c                |   17 ++++----
 5 files changed, 53 insertions(+), 62 deletions(-)

Comments

Eric Blake Feb. 17, 2014, 5:57 p.m. UTC | #1
On 02/11/2014 11:33 PM, Chunyan Liu wrote:
> qcow2.c: replace QEMUOptionParameter with QemuOpts in 'qemu-img amend'
> 
> Signed-off-by: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
> Signed-off-by: Chunyan Liu <cyliu@suse.com>
> ---
>  block.c                   |    4 +-
>  block/qcow2.c             |   90 ++++++++++++++++++++------------------------
>  include/block/block.h     |    2 +-
>  include/block/block_int.h |    2 +-
>  qemu-img.c                |   17 ++++----
>  5 files changed, 53 insertions(+), 62 deletions(-)
> 

> @@ -2228,7 +2220,7 @@ static BlockDriver bdrv_qcow2 = {
>      .bdrv_open          = qcow2_open,
>      .bdrv_close         = qcow2_close,
>      .bdrv_reopen_prepare  = qcow2_reopen_prepare,
> -    .bdrv_create2       = qcow2_create,
> +    .bdrv_create2        = qcow2_create,
>      .bdrv_has_zero_init = bdrv_has_zero_init_1,
>      .bdrv_co_get_block_status = qcow2_co_get_block_status,

Might be worth aligning the = while touching this.

> +++ b/include/block/block_int.h
> @@ -229,7 +229,7 @@ struct BlockDriver {
>          BdrvCheckMode fix);
>  
>      int (*bdrv_amend_options)(BlockDriverState *bs,
> -        QEMUOptionParameter *options);
> +        QemuOpts *opts);

Indentation looks odd here; and with your change to shorter contents,
you can just pull it into one line instead of wrapping and worrying
about indentation in the first place.
diff mbox

Patch

diff --git a/block.c b/block.c
index 69cec2f..d6ddbd0 100644
--- a/block.c
+++ b/block.c
@@ -5382,12 +5382,12 @@  void bdrv_add_before_write_notifier(BlockDriverState *bs,
     notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
 }
 
-int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options)
+int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts)
 {
     if (bs->drv->bdrv_amend_options == NULL) {
         return -ENOTSUP;
     }
-    return bs->drv->bdrv_amend_options(bs, options);
+    return bs->drv->bdrv_amend_options(bs, opts);
 }
 
 /* Used to recurse on single child block filters.
diff --git a/block/qcow2.c b/block/qcow2.c
index cab7097..a3afacb 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2045,65 +2045,57 @@  static int qcow2_downgrade(BlockDriverState *bs, int target_version)
 }
 
 static int qcow2_amend_options(BlockDriverState *bs,
-                               QEMUOptionParameter *options)
+                               QemuOpts *opts)
 {
     BDRVQcowState *s = bs->opaque;
     int old_version = s->qcow_version, new_version = old_version;
     uint64_t new_size = 0;
     const char *backing_file = NULL, *backing_format = NULL;
     bool lazy_refcounts = s->use_lazy_refcounts;
+    const char *compat, *prealloc;
+    uint64_t cluster_size = s->cluster_size;
+    bool encrypt;
     int ret;
-    int i;
 
-    for (i = 0; options[i].name; i++)
-    {
-        if (!options[i].assigned) {
-            /* only change explicitly defined options */
-            continue;
-        }
+    compat = qemu_opt_get_del(opts, "compat");
+    if (!compat) {
+        /* preserve default */
+    } else if (!strcmp(compat, "0.10")) {
+        new_version = 2;
+    } else if (!strcmp(compat, "1.1")) {
+        new_version = 3;
+    } else {
+        fprintf(stderr, "Unknown compatibility level %s.\n", compat);
+        return -EINVAL;
+    }
 
-        if (!strcmp(options[i].name, "compat")) {
-            if (!options[i].value.s) {
-                /* preserve default */
-            } else if (!strcmp(options[i].value.s, "0.10")) {
-                new_version = 2;
-            } else if (!strcmp(options[i].value.s, "1.1")) {
-                new_version = 3;
-            } else {
-                fprintf(stderr, "Unknown compatibility level %s.\n",
-                        options[i].value.s);
-                return -EINVAL;
-            }
-        } else if (!strcmp(options[i].name, "preallocation")) {
-            fprintf(stderr, "Cannot change preallocation mode.\n");
-            return -ENOTSUP;
-        } else if (!strcmp(options[i].name, "size")) {
-            new_size = options[i].value.n;
-        } else if (!strcmp(options[i].name, "backing_file")) {
-            backing_file = options[i].value.s;
-        } else if (!strcmp(options[i].name, "backing_fmt")) {
-            backing_format = options[i].value.s;
-        } else if (!strcmp(options[i].name, "encryption")) {
-            if ((options[i].value.n != !!s->crypt_method)) {
-                fprintf(stderr, "Changing the encryption flag is not "
-                        "supported.\n");
-                return -ENOTSUP;
-            }
-        } else if (!strcmp(options[i].name, "cluster_size")) {
-            if (options[i].value.n != s->cluster_size) {
-                fprintf(stderr, "Changing the cluster size is not "
-                        "supported.\n");
-                return -ENOTSUP;
-            }
-        } else if (!strcmp(options[i].name, "lazy_refcounts")) {
-            lazy_refcounts = options[i].value.n;
-        } else {
-            /* if this assertion fails, this probably means a new option was
-             * added without having it covered here */
-            assert(false);
-        }
+    prealloc = qemu_opt_get_del(opts, "preallocation");
+    if (prealloc) {
+        fprintf(stderr, "Cannot change preallocation mode.\n");
+        return -ENOTSUP;
     }
 
+    new_size = qemu_opt_get_size_del(opts, "size", 0);
+    backing_file = qemu_opt_get_del(opts, "backing_file");
+    backing_format = qemu_opt_get_del(opts, "backing_fmt");
+
+    encrypt = qemu_opt_get_bool_del(opts, "encryption", s->crypt_method);
+    if (encrypt != !!s->crypt_method) {
+        fprintf(stderr, "Changing the encryption flag is not "
+                "supported.\n");
+        return -ENOTSUP;
+    }
+
+    cluster_size = qemu_opt_get_size_del(opts, "cluster_size", cluster_size);
+    if (cluster_size != s->cluster_size) {
+        fprintf(stderr, "Changing the cluster size is not "
+                "supported.\n");
+        return -ENOTSUP;
+    }
+
+    lazy_refcounts = qemu_opt_get_bool_del(opts, "lazy_refcounts",
+                                           lazy_refcounts);
+
     if (new_version != old_version) {
         if (new_version > old_version) {
             /* Upgrade */
@@ -2228,7 +2220,7 @@  static BlockDriver bdrv_qcow2 = {
     .bdrv_open          = qcow2_open,
     .bdrv_close         = qcow2_close,
     .bdrv_reopen_prepare  = qcow2_reopen_prepare,
-    .bdrv_create2       = qcow2_create,
+    .bdrv_create2        = qcow2_create,
     .bdrv_has_zero_init = bdrv_has_zero_init_1,
     .bdrv_co_get_block_status = qcow2_co_get_block_status,
     .bdrv_set_key       = qcow2_set_key,
diff --git a/include/block/block.h b/include/block/block.h
index 0f8cc50..5430057 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -282,7 +282,7 @@  typedef enum {
 
 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
 
-int bdrv_amend_options(BlockDriverState *bs_new, QEMUOptionParameter *options);
+int bdrv_amend_options(BlockDriverState *bs_new, QemuOpts *opts);
 
 /* external snapshots */
 
diff --git a/include/block/block_int.h b/include/block/block_int.h
index 2acce0b..3879d46 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -229,7 +229,7 @@  struct BlockDriver {
         BdrvCheckMode fix);
 
     int (*bdrv_amend_options)(BlockDriverState *bs,
-        QEMUOptionParameter *options);
+        QemuOpts *opts);
 
     void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
 
diff --git a/qemu-img.c b/qemu-img.c
index 18b4ab5..c2a941d 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -2639,7 +2639,8 @@  static int img_amend(int argc, char **argv)
 {
     int c, ret = 0;
     char *options = NULL;
-    QEMUOptionParameter *create_options = NULL, *options_param = NULL;
+    QemuOptsList *create_opts = NULL;
+    QemuOpts *opts = NULL;
     const char *fmt = NULL, *filename;
     bool quiet = false;
     BlockDriverState *bs = NULL;
@@ -2691,17 +2692,15 @@  static int img_amend(int argc, char **argv)
         goto out;
     }
 
-    create_options = append_option_parameters(create_options,
-            bs->drv->create_options);
-    options_param = parse_option_parameters(options, create_options,
-            options_param);
-    if (options_param == NULL) {
+    create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
+    opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
+    if (options && qemu_opts_do_parse(opts, options, NULL)) {
         error_report("Invalid options for file format '%s'", fmt);
         ret = -1;
         goto out;
     }
 
-    ret = bdrv_amend_options(bs, options_param);
+    ret = bdrv_amend_options(bs, opts);
     if (ret < 0) {
         error_report("Error while amending options: %s", strerror(-ret));
         goto out;
@@ -2711,8 +2710,8 @@  out:
     if (bs) {
         bdrv_unref(bs);
     }
-    free_option_parameters(create_options);
-    free_option_parameters(options_param);
+    qemu_opts_del(opts);
+    qemu_opts_free(create_opts);
     if (ret) {
         return 1;
     }