diff mbox

[26/26] qemu-img: migrate QemuOptionParameter usage

Message ID 1395360813-2833-27-git-send-email-l@dorileo.org
State New
Headers show

Commit Message

Leandro Dorileo March 21, 2014, 12:13 a.m. UTC
Do the directly migration from QemuOptionParameter to QemuOpts on
qemu-img.

Signed-off-by: Leandro Dorileo <l@dorileo.org>
---
 qemu-img.c | 166 +++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 95 insertions(+), 71 deletions(-)
diff mbox

Patch

diff --git a/qemu-img.c b/qemu-img.c
index 2e40cc1..1a8ce3c 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -235,7 +235,9 @@  static int read_password(char *buf, int buf_size)
 static int print_block_option_help(const char *filename, const char *fmt)
 {
     BlockDriver *drv, *proto_drv;
-    QEMUOptionParameter *create_options = NULL;
+    QemuOptsList *proto_opts = NULL;
+    QemuOpts *opts;
+    QemuOptsList *list;
 
     /* Find driver and parse its options */
     drv = bdrv_find_format(fmt);
@@ -244,21 +246,32 @@  static int print_block_option_help(const char *filename, const char *fmt)
         return 1;
     }
 
-    create_options = append_option_parameters(create_options,
-                                              drv->create_options);
-
     if (filename) {
         proto_drv = bdrv_find_protocol(filename, true);
         if (!proto_drv) {
             error_report("Unknown protocol '%s'", filename);
             return 1;
         }
-        create_options = append_option_parameters(create_options,
-                                                  proto_drv->create_options);
+        proto_opts = proto_drv->create_options;
+    }
+
+    list = qemu_opts_append(drv->create_options, proto_opts);
+    if (!list) {
+        error_report("Could not allocate option descriptors structure");
+        return 1;
+    }
+
+    opts = qemu_opts_create(list, NULL, 0, &error_abort);
+    if (!list) {
+        error_report("Could not allocate options structure");
+        g_free(list);
+        return 1;
     }
 
-    print_option_help(create_options);
-    free_option_parameters(create_options);
+    qemu_opts_print_help(opts);
+
+    qemu_opts_del(opts);
+    g_free(list);
     return 0;
 }
 
@@ -311,22 +324,26 @@  fail:
     return NULL;
 }
 
-static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
+static int add_old_style_options(const char *fmt, QemuOpts *opts,
                                  const char *base_filename,
                                  const char *base_fmt)
 {
+    int ret;
+
     if (base_filename) {
-        if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
+        ret = qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename);
+        if (ret < 0) {
             error_report("Backing file not supported for file format '%s'",
                          fmt);
-            return -1;
+            return ret;
         }
     }
     if (base_fmt) {
-        if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
+        ret = qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt);
+        if (ret < 0) {
             error_report("Backing file format not supported for file "
                          "format '%s'", fmt);
-            return -1;
+            return ret;
         }
     }
     return 0;
@@ -1153,8 +1170,8 @@  static int img_convert(int argc, char **argv)
     size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
     const uint8_t *buf1;
     BlockDriverInfo bdi;
-    QEMUOptionParameter *param = NULL, *create_options = NULL;
-    QEMUOptionParameter *out_baseimg_param;
+    QemuOpts *create_options;
+    QemuOptsList *opts_list = NULL;
     char *options = NULL;
     const char *snapshot_name = NULL;
     int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
@@ -1340,72 +1357,73 @@  static int img_convert(int argc, char **argv)
         goto out;
     }
 
-    create_options = append_option_parameters(create_options,
-                                              drv->create_options);
-    create_options = append_option_parameters(create_options,
-                                              proto_drv->create_options);
+    opts_list = qemu_opts_append(drv->create_options,
+                                 proto_drv->create_options);
+    if (!opts_list) {
+        error_report("Could not allocate option descriptors structure");
+        ret = -1;
+        goto out;
+    }
+
+    create_options = qemu_opts_create(opts_list, NULL, 0, &error_abort);
+    if (!create_options) {
+        error_report("Could not allocate options structure");
+        ret = -1;
+        goto err_opts;
+    }
 
     if (options) {
-        param = parse_option_parameters(options, create_options, param);
-        if (param == NULL) {
+        ret = qemu_opts_do_parse(create_options, options, NULL);
+        if (ret < 0) {
             error_report("Invalid options for file format '%s'.", out_fmt);
-            ret = -1;
-            goto out;
+            goto err;
         }
-    } else {
-        param = parse_option_parameters("", create_options, param);
     }
 
-    set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
-    ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
+    ret = qemu_opt_set_number(create_options, BLOCK_OPT_SIZE,
+                              total_sectors * 512);
     if (ret < 0) {
-        goto out;
+        goto err;
     }
 
-    /* Get backing file name if -o backing_file was used */
-    out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
-    if (out_baseimg_param) {
-        out_baseimg = out_baseimg_param->value.s;
+    ret = add_old_style_options(out_fmt, create_options, out_baseimg, NULL);
+    if (ret < 0) {
+        goto err;
     }
 
+    /* Get backing file name if -o backing_file was used */
+    out_baseimg = qemu_opt_get(create_options, BLOCK_OPT_BACKING_FILE);
+
     /* Check if compression is supported */
     if (compress) {
-        QEMUOptionParameter *encryption =
-            get_option_parameter(param, BLOCK_OPT_ENCRYPT);
-        QEMUOptionParameter *preallocation =
-            get_option_parameter(param, BLOCK_OPT_PREALLOC);
+        const char *encryption, *preallocation;
+
+        encryption = qemu_opt_get(create_options, BLOCK_OPT_ENCRYPT);
+        preallocation = qemu_opt_get(create_options, BLOCK_OPT_PREALLOC);
 
         if (!drv->bdrv_write_compressed) {
             error_report("Compression not supported for this file format");
             ret = -1;
-            goto out;
-        }
-
-        if (encryption && encryption->value.n) {
-            error_report("Compression and encryption not supported at "
-                         "the same time");
-            ret = -1;
-            goto out;
+            goto err;
         }
 
-        if (preallocation && preallocation->value.s
-            && strcmp(preallocation->value.s, "off"))
+        if (encryption && preallocation && strcmp(preallocation, "off"))
         {
             error_report("Compression and preallocation not supported at "
                          "the same time");
             ret = -1;
-            goto out;
+            goto err;
         }
     }
 
     if (!skip_create) {
         /* Create the new image */
-        ret = bdrv_create(drv, out_filename, param, &local_err);
+        ret = bdrv_create(drv, out_filename, create_options, &local_err);
         if (ret < 0) {
             error_report("%s: error while converting %s: %s",
                          out_filename, out_fmt, error_get_pretty(local_err));
             error_free(local_err);
-            goto out;
+            goto err;
         }
     }
 
@@ -1419,7 +1437,7 @@  static int img_convert(int argc, char **argv)
     out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet);
     if (!out_bs) {
         ret = -1;
-        goto out;
+        goto err;
     }
 
     bs_i = 0;
@@ -1442,11 +1460,11 @@  static int img_convert(int argc, char **argv)
             error_report("unable to get output image length: %s\n",
                          strerror(-output_length));
             ret = -1;
-            goto out;
+            goto err;
         } else if (output_length < total_sectors << BDRV_SECTOR_BITS) {
             error_report("output file is smaller than input file");
             ret = -1;
-            goto out;
+            goto err;
         }
     }
 
@@ -1455,7 +1473,7 @@  static int img_convert(int argc, char **argv)
     if (ret < 0) {
         if (compress) {
             error_report("could not get block driver info");
-            goto out;
+            goto err;
         }
     } else {
         cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
@@ -1465,7 +1483,7 @@  static int img_convert(int argc, char **argv)
         if (cluster_sectors <= 0 || cluster_sectors > bufsectors) {
             error_report("invalid cluster size");
             ret = -1;
-            goto out;
+            goto err;
         }
         sector_num = 0;
 
@@ -1508,7 +1526,7 @@  static int img_convert(int argc, char **argv)
                 if (ret < 0) {
                     error_report("error while reading sector %" PRId64 ": %s",
                                  bs_num, strerror(-ret));
-                    goto out;
+                    goto err;
                 }
 
                 buf2 += nlow * 512;
@@ -1523,7 +1541,7 @@  static int img_convert(int argc, char **argv)
                 if (ret != 0) {
                     error_report("error while compressing sector %" PRId64
                                  ": %s", sector_num, strerror(-ret));
-                    goto out;
+                    goto err;
                 }
             }
             sector_num += n;
@@ -1539,7 +1557,7 @@  static int img_convert(int argc, char **argv)
         if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) {
             ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP);
             if (ret < 0) {
-                goto out;
+                goto err;
             }
             has_zero_init = 1;
         }
@@ -1582,7 +1600,7 @@  restart:
                     error_report("error while reading block status of sector %"
                                  PRId64 ": %s", sector_num - bs_offset,
                                  strerror(-ret));
-                    goto out;
+                    goto err;
                 }
                 /* If the output image is zero initialized, we are not working
                  * on a shared base and the input is zero we can skip the next
@@ -1636,7 +1654,7 @@  restart:
             if (ret < 0) {
                 error_report("error while reading sector %" PRId64 ": %s",
                              sector_num - bs_offset, strerror(-ret));
-                goto out;
+                goto err;
             }
             /* NOTE: at the same time we convert, we do not write zero
                sectors to have a chance to compress the image. Ideally, we
@@ -1649,7 +1667,7 @@  restart:
                     if (ret < 0) {
                         error_report("error while writing sector %" PRId64
                                      ": %s", sector_num, strerror(-ret));
-                        goto out;
+                        goto err;
                     }
                 }
                 sector_num += n1;
@@ -1659,13 +1677,16 @@  restart:
             qemu_progress_print(100.0 * sectors_read / sectors_to_read, 0);
         }
     }
+
+err:
+    qemu_opts_del(create_options);
+err_opts:
+    g_free(opts_list);
 out:
     if (!ret) {
         qemu_progress_print(100, 0);
     }
     qemu_progress_end();
-    free_option_parameters(create_options);
-    free_option_parameters(param);
     qemu_vfree(buf);
     if (sn_opts) {
         qemu_opts_del(sn_opts);
@@ -2651,7 +2672,7 @@  static int img_amend(int argc, char **argv)
 {
     int c, ret = 0;
     char *options = NULL;
-    QEMUOptionParameter *create_options = NULL, *options_param = NULL;
+    QemuOpts *opts = NULL;
     const char *fmt = NULL, *filename;
     bool quiet = false;
     BlockDriverState *bs = NULL;
@@ -2721,17 +2742,21 @@  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) {
+    opts = qemu_opts_create(bs->drv->create_options, NULL, 0, &error_abort);
+    if (!opts) {
+        error_report("Could not create the options structure");
+        ret = -1;
+        goto out;
+    }
+
+    ret = qemu_opts_do_parse(opts, options, NULL);
+    if (ret < 0) {
         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;
@@ -2741,8 +2766,7 @@  out:
     if (bs) {
         bdrv_unref(bs);
     }
-    free_option_parameters(create_options);
-    free_option_parameters(options_param);
+    qemu_opts_del(opts);
     g_free(options);
 
     if (ret) {