05bba0
From c2eb29efe61ffe88d3aff449446a9ac0f0f045a5 Mon Sep 17 00:00:00 2001
05bba0
From: Richard Jones <rjones@redhat.com>
05bba0
Date: Tue, 2 Jun 2015 09:46:37 +0200
05bba0
Subject: [PATCH 3/4] block: Allow JSON filenames
05bba0
05bba0
Message-id: <1433238397-2500-3-git-send-email-rjones@redhat.com>
05bba0
Patchwork-id: 65279
05bba0
O-Subject: [RHEL-7.2 qemu-kvm PATCH 2/2] block: Allow JSON filenames
05bba0
Bugzilla: 1226697
05bba0
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
05bba0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
05bba0
RH-Acked-by: Max Reitz <mreitz@redhat.com>
05bba0
05bba0
From: Max Reitz <mreitz@redhat.com>
05bba0
05bba0
If the filename given to bdrv_open() is prefixed with "json:", parse the
05bba0
rest as a JSON object and merge the result into the options QDict. If
05bba0
there are conflicts, the options QDict takes precedence.
05bba0
05bba0
Signed-off-by: Max Reitz <mreitz@redhat.com>
05bba0
Reviewed-by: Eric Blake <eblake@redhat.com>
05bba0
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
05bba0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
05bba0
---
05bba0
 block.c | 41 +++++++++++++++++++++++++++++++++++++++++
05bba0
 1 file changed, 41 insertions(+)
05bba0
05bba0
diff --git a/block.c b/block.c
05bba0
index 45543d5..f2caf20 100644
05bba0
--- a/block.c
05bba0
+++ b/block.c
05bba0
@@ -1085,6 +1085,33 @@ static void extract_subqdict(QDict *src, QDict **dst, const char *start)
05bba0
     }
05bba0
 }
05bba0
 
05bba0
+static QDict *parse_json_filename(const char *filename, Error **errp)
05bba0
+{
05bba0
+    QObject *options_obj;
05bba0
+    QDict *options;
05bba0
+    int ret;
05bba0
+
05bba0
+    ret = strstart(filename, "json:", &filename);
05bba0
+    assert(ret);
05bba0
+
05bba0
+    options_obj = qobject_from_json(filename);
05bba0
+    if (!options_obj) {
05bba0
+        error_setg(errp, "Could not parse the JSON options");
05bba0
+        return NULL;
05bba0
+    }
05bba0
+
05bba0
+    if (qobject_type(options_obj) != QTYPE_QDICT) {
05bba0
+        qobject_decref(options_obj);
05bba0
+        error_setg(errp, "Invalid JSON object given");
05bba0
+        return NULL;
05bba0
+    }
05bba0
+
05bba0
+    options = qobject_to_qdict(options_obj);
05bba0
+    qdict_flatten(options);
05bba0
+
05bba0
+    return options;
05bba0
+}
05bba0
+
05bba0
 /*
05bba0
  * Opens a disk image (raw, qcow2, vmdk, ...)
05bba0
  *
05bba0
@@ -1109,6 +1136,20 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
05bba0
         options = qdict_new();
05bba0
     }
05bba0
 
05bba0
+    if (filename && g_str_has_prefix(filename, "json:")) {
05bba0
+        QDict *json_options = parse_json_filename(filename, &local_err);
05bba0
+        if (local_err) {
05bba0
+            ret = -EINVAL;
05bba0
+            goto fail;
05bba0
+        }
05bba0
+
05bba0
+        /* Options given in the filename have lower priority than options
05bba0
+         * specified directly */
05bba0
+        qdict_join(options, json_options, false);
05bba0
+        QDECREF(json_options);
05bba0
+        filename = NULL;
05bba0
+    }
05bba0
+
05bba0
     bs->options = options;
05bba0
     options = qdict_clone_shallow(options);
05bba0
 
05bba0
-- 
05bba0
1.8.3.1
05bba0