26ba25
From c5a822e1af01299f91faac2193b918392146b189 Mon Sep 17 00:00:00 2001
26ba25
From: Markus Armbruster <armbru@redhat.com>
26ba25
Date: Mon, 18 Jun 2018 08:43:16 +0200
26ba25
Subject: [PATCH 018/268] qobject: Move block-specific qdict code to
26ba25
 block-qdict.c
26ba25
26ba25
RH-Author: Markus Armbruster <armbru@redhat.com>
26ba25
Message-id: <20180618084330.30009-10-armbru@redhat.com>
26ba25
Patchwork-id: 80722
26ba25
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 09/23] qobject: Move block-specific qdict code to block-qdict.c
26ba25
Bugzilla: 1557995
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
26ba25
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
26ba25
26ba25
Pure code motion, except for two brace placements and a comment
26ba25
tweaked to appease checkpatch.
26ba25
26ba25
Signed-off-by: Markus Armbruster <armbru@redhat.com>
26ba25
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
(cherry picked from commit 0bcc8e5bd8d6fd6e5cb6462054f7cfa45b282f9a)
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 MAINTAINERS               |   2 +
26ba25
 qobject/Makefile.objs     |   1 +
26ba25
 qobject/block-qdict.c     | 640 ++++++++++++++++++++++++++++++++++++++++++++
26ba25
 qobject/qdict.c           | 629 --------------------------------------------
26ba25
 tests/Makefile.include    |   4 +
26ba25
 tests/check-block-qdict.c | 655 ++++++++++++++++++++++++++++++++++++++++++++++
26ba25
 tests/check-qdict.c       | 642 ---------------------------------------------
26ba25
 7 files changed, 1302 insertions(+), 1271 deletions(-)
26ba25
 create mode 100644 qobject/block-qdict.c
26ba25
 create mode 100644 tests/check-block-qdict.c
26ba25
26ba25
diff --git a/MAINTAINERS b/MAINTAINERS
26ba25
index 24b7016..ad5edc8 100644
26ba25
--- a/MAINTAINERS
26ba25
+++ b/MAINTAINERS
26ba25
@@ -1339,6 +1339,8 @@ F: qemu-img*
26ba25
 F: qemu-io*
26ba25
 F: tests/qemu-iotests/
26ba25
 F: util/qemu-progress.c
26ba25
+F: qobject/block-qdict.c
26ba25
+F: test/check-block-qdict.c
26ba25
 T: git git://repo.or.cz/qemu/kevin.git block
26ba25
 
26ba25
 Block I/O path
26ba25
diff --git a/qobject/Makefile.objs b/qobject/Makefile.objs
26ba25
index 002d258..7b12c9c 100644
26ba25
--- a/qobject/Makefile.objs
26ba25
+++ b/qobject/Makefile.objs
26ba25
@@ -1,2 +1,3 @@
26ba25
 util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o qlit.o
26ba25
 util-obj-y += qjson.o qobject.o json-lexer.o json-streamer.o json-parser.o
26ba25
+util-obj-y += block-qdict.o
26ba25
diff --git a/qobject/block-qdict.c b/qobject/block-qdict.c
26ba25
new file mode 100644
26ba25
index 0000000..fb92010
26ba25
--- /dev/null
26ba25
+++ b/qobject/block-qdict.c
26ba25
@@ -0,0 +1,640 @@
26ba25
+/*
26ba25
+ * Special QDict functions used by the block layer
26ba25
+ *
26ba25
+ * Copyright (c) 2013-2018 Red Hat, Inc.
26ba25
+ *
26ba25
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
26ba25
+ * See the COPYING.LIB file in the top-level directory.
26ba25
+ */
26ba25
+
26ba25
+#include "qemu/osdep.h"
26ba25
+#include "block/qdict.h"
26ba25
+#include "qapi/qmp/qlist.h"
26ba25
+#include "qemu/cutils.h"
26ba25
+#include "qapi/error.h"
26ba25
+
26ba25
+/**
26ba25
+ * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
26ba25
+ * value of 'key' in 'src' is copied there (and the refcount increased
26ba25
+ * accordingly).
26ba25
+ */
26ba25
+void qdict_copy_default(QDict *dst, QDict *src, const char *key)
26ba25
+{
26ba25
+    QObject *val;
26ba25
+
26ba25
+    if (qdict_haskey(dst, key)) {
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    val = qdict_get(src, key);
26ba25
+    if (val) {
26ba25
+        qdict_put_obj(dst, key, qobject_ref(val));
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
26ba25
+ * new QString initialised by 'val' is put there.
26ba25
+ */
26ba25
+void qdict_set_default_str(QDict *dst, const char *key, const char *val)
26ba25
+{
26ba25
+    if (qdict_haskey(dst, key)) {
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    qdict_put_str(dst, key, val);
26ba25
+}
26ba25
+
26ba25
+static void qdict_flatten_qdict(QDict *qdict, QDict *target,
26ba25
+                                const char *prefix);
26ba25
+
26ba25
+static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
26ba25
+{
26ba25
+    QObject *value;
26ba25
+    const QListEntry *entry;
26ba25
+    char *new_key;
26ba25
+    int i;
26ba25
+
26ba25
+    /* This function is never called with prefix == NULL, i.e., it is always
26ba25
+     * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
26ba25
+     * need to remove list entries during the iteration (the whole list will be
26ba25
+     * deleted eventually anyway from qdict_flatten_qdict()). */
26ba25
+    assert(prefix);
26ba25
+
26ba25
+    entry = qlist_first(qlist);
26ba25
+
26ba25
+    for (i = 0; entry; entry = qlist_next(entry), i++) {
26ba25
+        value = qlist_entry_obj(entry);
26ba25
+        new_key = g_strdup_printf("%s.%i", prefix, i);
26ba25
+
26ba25
+        if (qobject_type(value) == QTYPE_QDICT) {
26ba25
+            qdict_flatten_qdict(qobject_to(QDict, value), target, new_key);
26ba25
+        } else if (qobject_type(value) == QTYPE_QLIST) {
26ba25
+            qdict_flatten_qlist(qobject_to(QList, value), target, new_key);
26ba25
+        } else {
26ba25
+            /* All other types are moved to the target unchanged. */
26ba25
+            qdict_put_obj(target, new_key, qobject_ref(value));
26ba25
+        }
26ba25
+
26ba25
+        g_free(new_key);
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
26ba25
+{
26ba25
+    QObject *value;
26ba25
+    const QDictEntry *entry, *next;
26ba25
+    char *new_key;
26ba25
+    bool delete;
26ba25
+
26ba25
+    entry = qdict_first(qdict);
26ba25
+
26ba25
+    while (entry != NULL) {
26ba25
+
26ba25
+        next = qdict_next(qdict, entry);
26ba25
+        value = qdict_entry_value(entry);
26ba25
+        new_key = NULL;
26ba25
+        delete = false;
26ba25
+
26ba25
+        if (prefix) {
26ba25
+            new_key = g_strdup_printf("%s.%s", prefix, entry->key);
26ba25
+        }
26ba25
+
26ba25
+        if (qobject_type(value) == QTYPE_QDICT) {
26ba25
+            /* Entries of QDicts are processed recursively, the QDict object
26ba25
+             * itself disappears. */
26ba25
+            qdict_flatten_qdict(qobject_to(QDict, value), target,
26ba25
+                                new_key ? new_key : entry->key);
26ba25
+            delete = true;
26ba25
+        } else if (qobject_type(value) == QTYPE_QLIST) {
26ba25
+            qdict_flatten_qlist(qobject_to(QList, value), target,
26ba25
+                                new_key ? new_key : entry->key);
26ba25
+            delete = true;
26ba25
+        } else if (prefix) {
26ba25
+            /* All other objects are moved to the target unchanged. */
26ba25
+            qdict_put_obj(target, new_key, qobject_ref(value));
26ba25
+            delete = true;
26ba25
+        }
26ba25
+
26ba25
+        g_free(new_key);
26ba25
+
26ba25
+        if (delete) {
26ba25
+            qdict_del(qdict, entry->key);
26ba25
+
26ba25
+            /* Restart loop after modifying the iterated QDict */
26ba25
+            entry = qdict_first(qdict);
26ba25
+            continue;
26ba25
+        }
26ba25
+
26ba25
+        entry = next;
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_flatten(): For each nested QDict with key x, all fields with key y
26ba25
+ * are moved to this QDict and their key is renamed to "x.y". For each nested
26ba25
+ * QList with key x, the field at index y is moved to this QDict with the key
26ba25
+ * "x.y" (i.e., the reverse of what qdict_array_split() does).
26ba25
+ * This operation is applied recursively for nested QDicts and QLists.
26ba25
+ */
26ba25
+void qdict_flatten(QDict *qdict)
26ba25
+{
26ba25
+    qdict_flatten_qdict(qdict, qdict, NULL);
26ba25
+}
26ba25
+
26ba25
+/* extract all the src QDict entries starting by start into dst */
26ba25
+void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
26ba25
+
26ba25
+{
26ba25
+    const QDictEntry *entry, *next;
26ba25
+    const char *p;
26ba25
+
26ba25
+    *dst = qdict_new();
26ba25
+    entry = qdict_first(src);
26ba25
+
26ba25
+    while (entry != NULL) {
26ba25
+        next = qdict_next(src, entry);
26ba25
+        if (strstart(entry->key, start, &p)) {
26ba25
+            qdict_put_obj(*dst, p, qobject_ref(entry->value));
26ba25
+            qdict_del(src, entry->key);
26ba25
+        }
26ba25
+        entry = next;
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+static int qdict_count_prefixed_entries(const QDict *src, const char *start)
26ba25
+{
26ba25
+    const QDictEntry *entry;
26ba25
+    int count = 0;
26ba25
+
26ba25
+    for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
26ba25
+        if (strstart(entry->key, start, NULL)) {
26ba25
+            if (count == INT_MAX) {
26ba25
+                return -ERANGE;
26ba25
+            }
26ba25
+            count++;
26ba25
+        }
26ba25
+    }
26ba25
+
26ba25
+    return count;
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_array_split(): This function moves array-like elements of a QDict into
26ba25
+ * a new QList. Every entry in the original QDict with a key "%u" or one
26ba25
+ * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
26ba25
+ * incrementally counting up, will be moved to a new QDict at index %u in the
26ba25
+ * output QList with the key prefix removed, if that prefix is "%u.". If the
26ba25
+ * whole key is just "%u", the whole QObject will be moved unchanged without
26ba25
+ * creating a new QDict. The function terminates when there is no entry in the
26ba25
+ * QDict with a prefix directly (incrementally) following the last one; it also
26ba25
+ * returns if there are both entries with "%u" and "%u." for the same index %u.
26ba25
+ * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
26ba25
+ *      (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
26ba25
+ *       => [{"a": 42, "b": 23}, {"x": 0}, 66]
26ba25
+ *      and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
26ba25
+ */
26ba25
+void qdict_array_split(QDict *src, QList **dst)
26ba25
+{
26ba25
+    unsigned i;
26ba25
+
26ba25
+    *dst = qlist_new();
26ba25
+
26ba25
+    for (i = 0; i < UINT_MAX; i++) {
26ba25
+        QObject *subqobj;
26ba25
+        bool is_subqdict;
26ba25
+        QDict *subqdict;
26ba25
+        char indexstr[32], prefix[32];
26ba25
+        size_t snprintf_ret;
26ba25
+
26ba25
+        snprintf_ret = snprintf(indexstr, 32, "%u", i);
26ba25
+        assert(snprintf_ret < 32);
26ba25
+
26ba25
+        subqobj = qdict_get(src, indexstr);
26ba25
+
26ba25
+        snprintf_ret = snprintf(prefix, 32, "%u.", i);
26ba25
+        assert(snprintf_ret < 32);
26ba25
+
26ba25
+        /* Overflow is the same as positive non-zero results */
26ba25
+        is_subqdict = qdict_count_prefixed_entries(src, prefix);
26ba25
+
26ba25
+        /*
26ba25
+         * There may be either a single subordinate object (named
26ba25
+         * "%u") or multiple objects (each with a key prefixed "%u."),
26ba25
+         * but not both.
26ba25
+         */
26ba25
+        if (!subqobj == !is_subqdict) {
26ba25
+            break;
26ba25
+        }
26ba25
+
26ba25
+        if (is_subqdict) {
26ba25
+            qdict_extract_subqdict(src, &subqdict, prefix);
26ba25
+            assert(qdict_size(subqdict) > 0);
26ba25
+        } else {
26ba25
+            qobject_ref(subqobj);
26ba25
+            qdict_del(src, indexstr);
26ba25
+        }
26ba25
+
26ba25
+        qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict));
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_split_flat_key:
26ba25
+ * @key: the key string to split
26ba25
+ * @prefix: non-NULL pointer to hold extracted prefix
26ba25
+ * @suffix: non-NULL pointer to remaining suffix
26ba25
+ *
26ba25
+ * Given a flattened key such as 'foo.0.bar', split it into two parts
26ba25
+ * at the first '.' separator. Allows double dot ('..') to escape the
26ba25
+ * normal separator.
26ba25
+ *
26ba25
+ * e.g.
26ba25
+ *    'foo.0.bar' -> prefix='foo' and suffix='0.bar'
26ba25
+ *    'foo..0.bar' -> prefix='foo.0' and suffix='bar'
26ba25
+ *
26ba25
+ * The '..' sequence will be unescaped in the returned 'prefix'
26ba25
+ * string. The 'suffix' string will be left in escaped format, so it
26ba25
+ * can be fed back into the qdict_split_flat_key() key as the input
26ba25
+ * later.
26ba25
+ *
26ba25
+ * The caller is responsible for freeing the string returned in @prefix
26ba25
+ * using g_free().
26ba25
+ */
26ba25
+static void qdict_split_flat_key(const char *key, char **prefix,
26ba25
+                                 const char **suffix)
26ba25
+{
26ba25
+    const char *separator;
26ba25
+    size_t i, j;
26ba25
+
26ba25
+    /* Find first '.' separator, but if there is a pair '..'
26ba25
+     * that acts as an escape, so skip over '..' */
26ba25
+    separator = NULL;
26ba25
+    do {
26ba25
+        if (separator) {
26ba25
+            separator += 2;
26ba25
+        } else {
26ba25
+            separator = key;
26ba25
+        }
26ba25
+        separator = strchr(separator, '.');
26ba25
+    } while (separator && separator[1] == '.');
26ba25
+
26ba25
+    if (separator) {
26ba25
+        *prefix = g_strndup(key, separator - key);
26ba25
+        *suffix = separator + 1;
26ba25
+    } else {
26ba25
+        *prefix = g_strdup(key);
26ba25
+        *suffix = NULL;
26ba25
+    }
26ba25
+
26ba25
+    /* Unescape the '..' sequence into '.' */
26ba25
+    for (i = 0, j = 0; (*prefix)[i] != '\0'; i++, j++) {
26ba25
+        if ((*prefix)[i] == '.') {
26ba25
+            assert((*prefix)[i + 1] == '.');
26ba25
+            i++;
26ba25
+        }
26ba25
+        (*prefix)[j] = (*prefix)[i];
26ba25
+    }
26ba25
+    (*prefix)[j] = '\0';
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_is_list:
26ba25
+ * @maybe_list: dict to check if keys represent list elements.
26ba25
+ *
26ba25
+ * Determine whether all keys in @maybe_list are valid list elements.
26ba25
+ * If @maybe_list is non-zero in length and all the keys look like
26ba25
+ * valid list indexes, this will return 1. If @maybe_list is zero
26ba25
+ * length or all keys are non-numeric then it will return 0 to indicate
26ba25
+ * it is a normal qdict. If there is a mix of numeric and non-numeric
26ba25
+ * keys, or the list indexes are non-contiguous, an error is reported.
26ba25
+ *
26ba25
+ * Returns: 1 if a valid list, 0 if a dict, -1 on error
26ba25
+ */
26ba25
+static int qdict_is_list(QDict *maybe_list, Error **errp)
26ba25
+{
26ba25
+    const QDictEntry *ent;
26ba25
+    ssize_t len = 0;
26ba25
+    ssize_t max = -1;
26ba25
+    int is_list = -1;
26ba25
+    int64_t val;
26ba25
+
26ba25
+    for (ent = qdict_first(maybe_list); ent != NULL;
26ba25
+         ent = qdict_next(maybe_list, ent)) {
26ba25
+
26ba25
+        if (qemu_strtoi64(ent->key, NULL, 10, &val) == 0) {
26ba25
+            if (is_list == -1) {
26ba25
+                is_list = 1;
26ba25
+            } else if (!is_list) {
26ba25
+                error_setg(errp,
26ba25
+                           "Cannot mix list and non-list keys");
26ba25
+                return -1;
26ba25
+            }
26ba25
+            len++;
26ba25
+            if (val > max) {
26ba25
+                max = val;
26ba25
+            }
26ba25
+        } else {
26ba25
+            if (is_list == -1) {
26ba25
+                is_list = 0;
26ba25
+            } else if (is_list) {
26ba25
+                error_setg(errp,
26ba25
+                           "Cannot mix list and non-list keys");
26ba25
+                return -1;
26ba25
+            }
26ba25
+        }
26ba25
+    }
26ba25
+
26ba25
+    if (is_list == -1) {
26ba25
+        assert(!qdict_size(maybe_list));
26ba25
+        is_list = 0;
26ba25
+    }
26ba25
+
26ba25
+    /* NB this isn't a perfect check - e.g. it won't catch
26ba25
+     * a list containing '1', '+1', '01', '3', but that
26ba25
+     * does not matter - we've still proved that the
26ba25
+     * input is a list. It is up the caller to do a
26ba25
+     * stricter check if desired */
26ba25
+    if (len != (max + 1)) {
26ba25
+        error_setg(errp, "List indices are not contiguous, "
26ba25
+                   "saw %zd elements but %zd largest index",
26ba25
+                   len, max);
26ba25
+        return -1;
26ba25
+    }
26ba25
+
26ba25
+    return is_list;
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_crumple:
26ba25
+ * @src: the original flat dictionary (only scalar values) to crumple
26ba25
+ *
26ba25
+ * Takes a flat dictionary whose keys use '.' separator to indicate
26ba25
+ * nesting, and values are scalars, and crumples it into a nested
26ba25
+ * structure.
26ba25
+ *
26ba25
+ * To include a literal '.' in a key name, it must be escaped as '..'
26ba25
+ *
26ba25
+ * For example, an input of:
26ba25
+ *
26ba25
+ * { 'foo.0.bar': 'one', 'foo.0.wizz': '1',
26ba25
+ *   'foo.1.bar': 'two', 'foo.1.wizz': '2' }
26ba25
+ *
26ba25
+ * will result in an output of:
26ba25
+ *
26ba25
+ * {
26ba25
+ *   'foo': [
26ba25
+ *      { 'bar': 'one', 'wizz': '1' },
26ba25
+ *      { 'bar': 'two', 'wizz': '2' }
26ba25
+ *   ],
26ba25
+ * }
26ba25
+ *
26ba25
+ * The following scenarios in the input dict will result in an
26ba25
+ * error being returned:
26ba25
+ *
26ba25
+ *  - Any values in @src are non-scalar types
26ba25
+ *  - If keys in @src imply that a particular level is both a
26ba25
+ *    list and a dict. e.g., "foo.0.bar" and "foo.eek.bar".
26ba25
+ *  - If keys in @src imply that a particular level is a list,
26ba25
+ *    but the indices are non-contiguous. e.g. "foo.0.bar" and
26ba25
+ *    "foo.2.bar" without any "foo.1.bar" present.
26ba25
+ *  - If keys in @src represent list indexes, but are not in
26ba25
+ *    the "%zu" format. e.g. "foo.+0.bar"
26ba25
+ *
26ba25
+ * Returns: either a QDict or QList for the nested data structure, or NULL
26ba25
+ * on error
26ba25
+ */
26ba25
+QObject *qdict_crumple(const QDict *src, Error **errp)
26ba25
+{
26ba25
+    const QDictEntry *ent;
26ba25
+    QDict *two_level, *multi_level = NULL;
26ba25
+    QObject *dst = NULL, *child;
26ba25
+    size_t i;
26ba25
+    char *prefix = NULL;
26ba25
+    const char *suffix = NULL;
26ba25
+    int is_list;
26ba25
+
26ba25
+    two_level = qdict_new();
26ba25
+
26ba25
+    /* Step 1: split our totally flat dict into a two level dict */
26ba25
+    for (ent = qdict_first(src); ent != NULL; ent = qdict_next(src, ent)) {
26ba25
+        if (qobject_type(ent->value) == QTYPE_QDICT ||
26ba25
+            qobject_type(ent->value) == QTYPE_QLIST) {
26ba25
+            error_setg(errp, "Value %s is not a scalar",
26ba25
+                       ent->key);
26ba25
+            goto error;
26ba25
+        }
26ba25
+
26ba25
+        qdict_split_flat_key(ent->key, &prefix, &suffix);
26ba25
+
26ba25
+        child = qdict_get(two_level, prefix);
26ba25
+        if (suffix) {
26ba25
+            QDict *child_dict = qobject_to(QDict, child);
26ba25
+            if (!child_dict) {
26ba25
+                if (child) {
26ba25
+                    error_setg(errp, "Key %s prefix is already set as a scalar",
26ba25
+                               prefix);
26ba25
+                    goto error;
26ba25
+                }
26ba25
+
26ba25
+                child_dict = qdict_new();
26ba25
+                qdict_put_obj(two_level, prefix, QOBJECT(child_dict));
26ba25
+            }
26ba25
+
26ba25
+            qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
26ba25
+        } else {
26ba25
+            if (child) {
26ba25
+                error_setg(errp, "Key %s prefix is already set as a dict",
26ba25
+                           prefix);
26ba25
+                goto error;
26ba25
+            }
26ba25
+            qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
26ba25
+        }
26ba25
+
26ba25
+        g_free(prefix);
26ba25
+        prefix = NULL;
26ba25
+    }
26ba25
+
26ba25
+    /* Step 2: optionally process the two level dict recursively
26ba25
+     * into a multi-level dict */
26ba25
+    multi_level = qdict_new();
26ba25
+    for (ent = qdict_first(two_level); ent != NULL;
26ba25
+         ent = qdict_next(two_level, ent)) {
26ba25
+        QDict *dict = qobject_to(QDict, ent->value);
26ba25
+        if (dict) {
26ba25
+            child = qdict_crumple(dict, errp);
26ba25
+            if (!child) {
26ba25
+                goto error;
26ba25
+            }
26ba25
+
26ba25
+            qdict_put_obj(multi_level, ent->key, child);
26ba25
+        } else {
26ba25
+            qdict_put_obj(multi_level, ent->key, qobject_ref(ent->value));
26ba25
+        }
26ba25
+    }
26ba25
+    qobject_unref(two_level);
26ba25
+    two_level = NULL;
26ba25
+
26ba25
+    /* Step 3: detect if we need to turn our dict into list */
26ba25
+    is_list = qdict_is_list(multi_level, errp);
26ba25
+    if (is_list < 0) {
26ba25
+        goto error;
26ba25
+    }
26ba25
+
26ba25
+    if (is_list) {
26ba25
+        dst = QOBJECT(qlist_new());
26ba25
+
26ba25
+        for (i = 0; i < qdict_size(multi_level); i++) {
26ba25
+            char *key = g_strdup_printf("%zu", i);
26ba25
+
26ba25
+            child = qdict_get(multi_level, key);
26ba25
+            g_free(key);
26ba25
+
26ba25
+            if (!child) {
26ba25
+                error_setg(errp, "Missing list index %zu", i);
26ba25
+                goto error;
26ba25
+            }
26ba25
+
26ba25
+            qlist_append_obj(qobject_to(QList, dst), qobject_ref(child));
26ba25
+        }
26ba25
+        qobject_unref(multi_level);
26ba25
+        multi_level = NULL;
26ba25
+    } else {
26ba25
+        dst = QOBJECT(multi_level);
26ba25
+    }
26ba25
+
26ba25
+    return dst;
26ba25
+
26ba25
+ error:
26ba25
+    g_free(prefix);
26ba25
+    qobject_unref(multi_level);
26ba25
+    qobject_unref(two_level);
26ba25
+    qobject_unref(dst);
26ba25
+    return NULL;
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_array_entries(): Returns the number of direct array entries if the
26ba25
+ * sub-QDict of src specified by the prefix in subqdict (or src itself for
26ba25
+ * prefix == "") is valid as an array, i.e. the length of the created list if
26ba25
+ * the sub-QDict would become empty after calling qdict_array_split() on it. If
26ba25
+ * the array is not valid, -EINVAL is returned.
26ba25
+ */
26ba25
+int qdict_array_entries(QDict *src, const char *subqdict)
26ba25
+{
26ba25
+    const QDictEntry *entry;
26ba25
+    unsigned i;
26ba25
+    unsigned entries = 0;
26ba25
+    size_t subqdict_len = strlen(subqdict);
26ba25
+
26ba25
+    assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
26ba25
+
26ba25
+    /* qdict_array_split() loops until UINT_MAX, but as we want to return
26ba25
+     * negative errors, we only have a signed return value here. Any additional
26ba25
+     * entries will lead to -EINVAL. */
26ba25
+    for (i = 0; i < INT_MAX; i++) {
26ba25
+        QObject *subqobj;
26ba25
+        int subqdict_entries;
26ba25
+        char *prefix = g_strdup_printf("%s%u.", subqdict, i);
26ba25
+
26ba25
+        subqdict_entries = qdict_count_prefixed_entries(src, prefix);
26ba25
+
26ba25
+        /* Remove ending "." */
26ba25
+        prefix[strlen(prefix) - 1] = 0;
26ba25
+        subqobj = qdict_get(src, prefix);
26ba25
+
26ba25
+        g_free(prefix);
26ba25
+
26ba25
+        if (subqdict_entries < 0) {
26ba25
+            return subqdict_entries;
26ba25
+        }
26ba25
+
26ba25
+        /* There may be either a single subordinate object (named "%u") or
26ba25
+         * multiple objects (each with a key prefixed "%u."), but not both. */
26ba25
+        if (subqobj && subqdict_entries) {
26ba25
+            return -EINVAL;
26ba25
+        } else if (!subqobj && !subqdict_entries) {
26ba25
+            break;
26ba25
+        }
26ba25
+
26ba25
+        entries += subqdict_entries ? subqdict_entries : 1;
26ba25
+    }
26ba25
+
26ba25
+    /* Consider everything handled that isn't part of the given sub-QDict */
26ba25
+    for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
26ba25
+        if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
26ba25
+            entries++;
26ba25
+        }
26ba25
+    }
26ba25
+
26ba25
+    /* Anything left in the sub-QDict that wasn't handled? */
26ba25
+    if (qdict_size(src) != entries) {
26ba25
+        return -EINVAL;
26ba25
+    }
26ba25
+
26ba25
+    return i;
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
26ba25
+ * elements from src to dest.
26ba25
+ *
26ba25
+ * If an element from src has a key already present in dest, it will not be
26ba25
+ * moved unless overwrite is true.
26ba25
+ *
26ba25
+ * If overwrite is true, the conflicting values in dest will be discarded and
26ba25
+ * replaced by the corresponding values from src.
26ba25
+ *
26ba25
+ * Therefore, with overwrite being true, the src QDict will always be empty when
26ba25
+ * this function returns. If overwrite is false, the src QDict will be empty
26ba25
+ * iff there were no conflicts.
26ba25
+ */
26ba25
+void qdict_join(QDict *dest, QDict *src, bool overwrite)
26ba25
+{
26ba25
+    const QDictEntry *entry, *next;
26ba25
+
26ba25
+    entry = qdict_first(src);
26ba25
+    while (entry) {
26ba25
+        next = qdict_next(src, entry);
26ba25
+
26ba25
+        if (overwrite || !qdict_haskey(dest, entry->key)) {
26ba25
+            qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
26ba25
+            qdict_del(src, entry->key);
26ba25
+        }
26ba25
+
26ba25
+        entry = next;
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+/**
26ba25
+ * qdict_rename_keys(): Rename keys in qdict according to the replacements
26ba25
+ * specified in the array renames. The array must be terminated by an entry
26ba25
+ * with from = NULL.
26ba25
+ *
26ba25
+ * The renames are performed individually in the order of the array, so entries
26ba25
+ * may be renamed multiple times and may or may not conflict depending on the
26ba25
+ * order of the renames array.
26ba25
+ *
26ba25
+ * Returns true for success, false in error cases.
26ba25
+ */
26ba25
+bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp)
26ba25
+{
26ba25
+    QObject *qobj;
26ba25
+
26ba25
+    while (renames->from) {
26ba25
+        if (qdict_haskey(qdict, renames->from)) {
26ba25
+            if (qdict_haskey(qdict, renames->to)) {
26ba25
+                error_setg(errp, "'%s' and its alias '%s' can't be used at the "
26ba25
+                           "same time", renames->to, renames->from);
26ba25
+                return false;
26ba25
+            }
26ba25
+
26ba25
+            qobj = qdict_get(qdict, renames->from);
26ba25
+            qdict_put_obj(qdict, renames->to, qobject_ref(qobj));
26ba25
+            qdict_del(qdict, renames->from);
26ba25
+        }
26ba25
+
26ba25
+        renames++;
26ba25
+    }
26ba25
+    return true;
26ba25
+}
26ba25
diff --git a/qobject/qdict.c b/qobject/qdict.c
26ba25
index 0554c64..3d8c2f7 100644
26ba25
--- a/qobject/qdict.c
26ba25
+++ b/qobject/qdict.c
26ba25
@@ -11,17 +11,11 @@
26ba25
  */
26ba25
 
26ba25
 #include "qemu/osdep.h"
26ba25
-#include "block/qdict.h"
26ba25
 #include "qapi/qmp/qnum.h"
26ba25
 #include "qapi/qmp/qdict.h"
26ba25
 #include "qapi/qmp/qbool.h"
26ba25
-#include "qapi/qmp/qlist.h"
26ba25
 #include "qapi/qmp/qnull.h"
26ba25
 #include "qapi/qmp/qstring.h"
26ba25
-#include "qapi/error.h"
26ba25
-#include "qemu/queue.h"
26ba25
-#include "qemu-common.h"
26ba25
-#include "qemu/cutils.h"
26ba25
 
26ba25
 /**
26ba25
  * qdict_new(): Create a new QDict
26ba25
@@ -464,626 +458,3 @@ void qdict_destroy_obj(QObject *obj)
26ba25
 
26ba25
     g_free(qdict);
26ba25
 }
26ba25
-
26ba25
-/**
26ba25
- * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
26ba25
- * value of 'key' in 'src' is copied there (and the refcount increased
26ba25
- * accordingly).
26ba25
- */
26ba25
-void qdict_copy_default(QDict *dst, QDict *src, const char *key)
26ba25
-{
26ba25
-    QObject *val;
26ba25
-
26ba25
-    if (qdict_haskey(dst, key)) {
26ba25
-        return;
26ba25
-    }
26ba25
-
26ba25
-    val = qdict_get(src, key);
26ba25
-    if (val) {
26ba25
-        qdict_put_obj(dst, key, qobject_ref(val));
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
26ba25
- * new QString initialised by 'val' is put there.
26ba25
- */
26ba25
-void qdict_set_default_str(QDict *dst, const char *key, const char *val)
26ba25
-{
26ba25
-    if (qdict_haskey(dst, key)) {
26ba25
-        return;
26ba25
-    }
26ba25
-
26ba25
-    qdict_put_str(dst, key, val);
26ba25
-}
26ba25
-
26ba25
-static void qdict_flatten_qdict(QDict *qdict, QDict *target,
26ba25
-                                const char *prefix);
26ba25
-
26ba25
-static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
26ba25
-{
26ba25
-    QObject *value;
26ba25
-    const QListEntry *entry;
26ba25
-    char *new_key;
26ba25
-    int i;
26ba25
-
26ba25
-    /* This function is never called with prefix == NULL, i.e., it is always
26ba25
-     * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
26ba25
-     * need to remove list entries during the iteration (the whole list will be
26ba25
-     * deleted eventually anyway from qdict_flatten_qdict()). */
26ba25
-    assert(prefix);
26ba25
-
26ba25
-    entry = qlist_first(qlist);
26ba25
-
26ba25
-    for (i = 0; entry; entry = qlist_next(entry), i++) {
26ba25
-        value = qlist_entry_obj(entry);
26ba25
-        new_key = g_strdup_printf("%s.%i", prefix, i);
26ba25
-
26ba25
-        if (qobject_type(value) == QTYPE_QDICT) {
26ba25
-            qdict_flatten_qdict(qobject_to(QDict, value), target, new_key);
26ba25
-        } else if (qobject_type(value) == QTYPE_QLIST) {
26ba25
-            qdict_flatten_qlist(qobject_to(QList, value), target, new_key);
26ba25
-        } else {
26ba25
-            /* All other types are moved to the target unchanged. */
26ba25
-            qdict_put_obj(target, new_key, qobject_ref(value));
26ba25
-        }
26ba25
-
26ba25
-        g_free(new_key);
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
26ba25
-{
26ba25
-    QObject *value;
26ba25
-    const QDictEntry *entry, *next;
26ba25
-    char *new_key;
26ba25
-    bool delete;
26ba25
-
26ba25
-    entry = qdict_first(qdict);
26ba25
-
26ba25
-    while (entry != NULL) {
26ba25
-
26ba25
-        next = qdict_next(qdict, entry);
26ba25
-        value = qdict_entry_value(entry);
26ba25
-        new_key = NULL;
26ba25
-        delete = false;
26ba25
-
26ba25
-        if (prefix) {
26ba25
-            new_key = g_strdup_printf("%s.%s", prefix, entry->key);
26ba25
-        }
26ba25
-
26ba25
-        if (qobject_type(value) == QTYPE_QDICT) {
26ba25
-            /* Entries of QDicts are processed recursively, the QDict object
26ba25
-             * itself disappears. */
26ba25
-            qdict_flatten_qdict(qobject_to(QDict, value), target,
26ba25
-                                new_key ? new_key : entry->key);
26ba25
-            delete = true;
26ba25
-        } else if (qobject_type(value) == QTYPE_QLIST) {
26ba25
-            qdict_flatten_qlist(qobject_to(QList, value), target,
26ba25
-                                new_key ? new_key : entry->key);
26ba25
-            delete = true;
26ba25
-        } else if (prefix) {
26ba25
-            /* All other objects are moved to the target unchanged. */
26ba25
-            qdict_put_obj(target, new_key, qobject_ref(value));
26ba25
-            delete = true;
26ba25
-        }
26ba25
-
26ba25
-        g_free(new_key);
26ba25
-
26ba25
-        if (delete) {
26ba25
-            qdict_del(qdict, entry->key);
26ba25
-
26ba25
-            /* Restart loop after modifying the iterated QDict */
26ba25
-            entry = qdict_first(qdict);
26ba25
-            continue;
26ba25
-        }
26ba25
-
26ba25
-        entry = next;
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_flatten(): For each nested QDict with key x, all fields with key y
26ba25
- * are moved to this QDict and their key is renamed to "x.y". For each nested
26ba25
- * QList with key x, the field at index y is moved to this QDict with the key
26ba25
- * "x.y" (i.e., the reverse of what qdict_array_split() does).
26ba25
- * This operation is applied recursively for nested QDicts and QLists.
26ba25
- */
26ba25
-void qdict_flatten(QDict *qdict)
26ba25
-{
26ba25
-    qdict_flatten_qdict(qdict, qdict, NULL);
26ba25
-}
26ba25
-
26ba25
-/* extract all the src QDict entries starting by start into dst */
26ba25
-void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
26ba25
-
26ba25
-{
26ba25
-    const QDictEntry *entry, *next;
26ba25
-    const char *p;
26ba25
-
26ba25
-    *dst = qdict_new();
26ba25
-    entry = qdict_first(src);
26ba25
-
26ba25
-    while (entry != NULL) {
26ba25
-        next = qdict_next(src, entry);
26ba25
-        if (strstart(entry->key, start, &p)) {
26ba25
-            qdict_put_obj(*dst, p, qobject_ref(entry->value));
26ba25
-            qdict_del(src, entry->key);
26ba25
-        }
26ba25
-        entry = next;
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-static int qdict_count_prefixed_entries(const QDict *src, const char *start)
26ba25
-{
26ba25
-    const QDictEntry *entry;
26ba25
-    int count = 0;
26ba25
-
26ba25
-    for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
26ba25
-        if (strstart(entry->key, start, NULL)) {
26ba25
-            if (count == INT_MAX) {
26ba25
-                return -ERANGE;
26ba25
-            }
26ba25
-            count++;
26ba25
-        }
26ba25
-    }
26ba25
-
26ba25
-    return count;
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_array_split(): This function moves array-like elements of a QDict into
26ba25
- * a new QList. Every entry in the original QDict with a key "%u" or one
26ba25
- * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
26ba25
- * incrementally counting up, will be moved to a new QDict at index %u in the
26ba25
- * output QList with the key prefix removed, if that prefix is "%u.". If the
26ba25
- * whole key is just "%u", the whole QObject will be moved unchanged without
26ba25
- * creating a new QDict. The function terminates when there is no entry in the
26ba25
- * QDict with a prefix directly (incrementally) following the last one; it also
26ba25
- * returns if there are both entries with "%u" and "%u." for the same index %u.
26ba25
- * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
26ba25
- *      (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
26ba25
- *       => [{"a": 42, "b": 23}, {"x": 0}, 66]
26ba25
- *      and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
26ba25
- */
26ba25
-void qdict_array_split(QDict *src, QList **dst)
26ba25
-{
26ba25
-    unsigned i;
26ba25
-
26ba25
-    *dst = qlist_new();
26ba25
-
26ba25
-    for (i = 0; i < UINT_MAX; i++) {
26ba25
-        QObject *subqobj;
26ba25
-        bool is_subqdict;
26ba25
-        QDict *subqdict;
26ba25
-        char indexstr[32], prefix[32];
26ba25
-        size_t snprintf_ret;
26ba25
-
26ba25
-        snprintf_ret = snprintf(indexstr, 32, "%u", i);
26ba25
-        assert(snprintf_ret < 32);
26ba25
-
26ba25
-        subqobj = qdict_get(src, indexstr);
26ba25
-
26ba25
-        snprintf_ret = snprintf(prefix, 32, "%u.", i);
26ba25
-        assert(snprintf_ret < 32);
26ba25
-
26ba25
-        /* Overflow is the same as positive non-zero results */
26ba25
-        is_subqdict = qdict_count_prefixed_entries(src, prefix);
26ba25
-
26ba25
-        // There may be either a single subordinate object (named "%u") or
26ba25
-        // multiple objects (each with a key prefixed "%u."), but not both.
26ba25
-        if (!subqobj == !is_subqdict) {
26ba25
-            break;
26ba25
-        }
26ba25
-
26ba25
-        if (is_subqdict) {
26ba25
-            qdict_extract_subqdict(src, &subqdict, prefix);
26ba25
-            assert(qdict_size(subqdict) > 0);
26ba25
-        } else {
26ba25
-            qobject_ref(subqobj);
26ba25
-            qdict_del(src, indexstr);
26ba25
-        }
26ba25
-
26ba25
-        qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict));
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_split_flat_key:
26ba25
- * @key: the key string to split
26ba25
- * @prefix: non-NULL pointer to hold extracted prefix
26ba25
- * @suffix: non-NULL pointer to remaining suffix
26ba25
- *
26ba25
- * Given a flattened key such as 'foo.0.bar', split it into two parts
26ba25
- * at the first '.' separator. Allows double dot ('..') to escape the
26ba25
- * normal separator.
26ba25
- *
26ba25
- * e.g.
26ba25
- *    'foo.0.bar' -> prefix='foo' and suffix='0.bar'
26ba25
- *    'foo..0.bar' -> prefix='foo.0' and suffix='bar'
26ba25
- *
26ba25
- * The '..' sequence will be unescaped in the returned 'prefix'
26ba25
- * string. The 'suffix' string will be left in escaped format, so it
26ba25
- * can be fed back into the qdict_split_flat_key() key as the input
26ba25
- * later.
26ba25
- *
26ba25
- * The caller is responsible for freeing the string returned in @prefix
26ba25
- * using g_free().
26ba25
- */
26ba25
-static void qdict_split_flat_key(const char *key, char **prefix,
26ba25
-                                 const char **suffix)
26ba25
-{
26ba25
-    const char *separator;
26ba25
-    size_t i, j;
26ba25
-
26ba25
-    /* Find first '.' separator, but if there is a pair '..'
26ba25
-     * that acts as an escape, so skip over '..' */
26ba25
-    separator = NULL;
26ba25
-    do {
26ba25
-        if (separator) {
26ba25
-            separator += 2;
26ba25
-        } else {
26ba25
-            separator = key;
26ba25
-        }
26ba25
-        separator = strchr(separator, '.');
26ba25
-    } while (separator && separator[1] == '.');
26ba25
-
26ba25
-    if (separator) {
26ba25
-        *prefix = g_strndup(key, separator - key);
26ba25
-        *suffix = separator + 1;
26ba25
-    } else {
26ba25
-        *prefix = g_strdup(key);
26ba25
-        *suffix = NULL;
26ba25
-    }
26ba25
-
26ba25
-    /* Unescape the '..' sequence into '.' */
26ba25
-    for (i = 0, j = 0; (*prefix)[i] != '\0'; i++, j++) {
26ba25
-        if ((*prefix)[i] == '.') {
26ba25
-            assert((*prefix)[i + 1] == '.');
26ba25
-            i++;
26ba25
-        }
26ba25
-        (*prefix)[j] = (*prefix)[i];
26ba25
-    }
26ba25
-    (*prefix)[j] = '\0';
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_is_list:
26ba25
- * @maybe_list: dict to check if keys represent list elements.
26ba25
- *
26ba25
- * Determine whether all keys in @maybe_list are valid list elements.
26ba25
- * If @maybe_list is non-zero in length and all the keys look like
26ba25
- * valid list indexes, this will return 1. If @maybe_list is zero
26ba25
- * length or all keys are non-numeric then it will return 0 to indicate
26ba25
- * it is a normal qdict. If there is a mix of numeric and non-numeric
26ba25
- * keys, or the list indexes are non-contiguous, an error is reported.
26ba25
- *
26ba25
- * Returns: 1 if a valid list, 0 if a dict, -1 on error
26ba25
- */
26ba25
-static int qdict_is_list(QDict *maybe_list, Error **errp)
26ba25
-{
26ba25
-    const QDictEntry *ent;
26ba25
-    ssize_t len = 0;
26ba25
-    ssize_t max = -1;
26ba25
-    int is_list = -1;
26ba25
-    int64_t val;
26ba25
-
26ba25
-    for (ent = qdict_first(maybe_list); ent != NULL;
26ba25
-         ent = qdict_next(maybe_list, ent)) {
26ba25
-
26ba25
-        if (qemu_strtoi64(ent->key, NULL, 10, &val) == 0) {
26ba25
-            if (is_list == -1) {
26ba25
-                is_list = 1;
26ba25
-            } else if (!is_list) {
26ba25
-                error_setg(errp,
26ba25
-                           "Cannot mix list and non-list keys");
26ba25
-                return -1;
26ba25
-            }
26ba25
-            len++;
26ba25
-            if (val > max) {
26ba25
-                max = val;
26ba25
-            }
26ba25
-        } else {
26ba25
-            if (is_list == -1) {
26ba25
-                is_list = 0;
26ba25
-            } else if (is_list) {
26ba25
-                error_setg(errp,
26ba25
-                           "Cannot mix list and non-list keys");
26ba25
-                return -1;
26ba25
-            }
26ba25
-        }
26ba25
-    }
26ba25
-
26ba25
-    if (is_list == -1) {
26ba25
-        assert(!qdict_size(maybe_list));
26ba25
-        is_list = 0;
26ba25
-    }
26ba25
-
26ba25
-    /* NB this isn't a perfect check - e.g. it won't catch
26ba25
-     * a list containing '1', '+1', '01', '3', but that
26ba25
-     * does not matter - we've still proved that the
26ba25
-     * input is a list. It is up the caller to do a
26ba25
-     * stricter check if desired */
26ba25
-    if (len != (max + 1)) {
26ba25
-        error_setg(errp, "List indices are not contiguous, "
26ba25
-                   "saw %zd elements but %zd largest index",
26ba25
-                   len, max);
26ba25
-        return -1;
26ba25
-    }
26ba25
-
26ba25
-    return is_list;
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_crumple:
26ba25
- * @src: the original flat dictionary (only scalar values) to crumple
26ba25
- *
26ba25
- * Takes a flat dictionary whose keys use '.' separator to indicate
26ba25
- * nesting, and values are scalars, and crumples it into a nested
26ba25
- * structure.
26ba25
- *
26ba25
- * To include a literal '.' in a key name, it must be escaped as '..'
26ba25
- *
26ba25
- * For example, an input of:
26ba25
- *
26ba25
- * { 'foo.0.bar': 'one', 'foo.0.wizz': '1',
26ba25
- *   'foo.1.bar': 'two', 'foo.1.wizz': '2' }
26ba25
- *
26ba25
- * will result in an output of:
26ba25
- *
26ba25
- * {
26ba25
- *   'foo': [
26ba25
- *      { 'bar': 'one', 'wizz': '1' },
26ba25
- *      { 'bar': 'two', 'wizz': '2' }
26ba25
- *   ],
26ba25
- * }
26ba25
- *
26ba25
- * The following scenarios in the input dict will result in an
26ba25
- * error being returned:
26ba25
- *
26ba25
- *  - Any values in @src are non-scalar types
26ba25
- *  - If keys in @src imply that a particular level is both a
26ba25
- *    list and a dict. e.g., "foo.0.bar" and "foo.eek.bar".
26ba25
- *  - If keys in @src imply that a particular level is a list,
26ba25
- *    but the indices are non-contiguous. e.g. "foo.0.bar" and
26ba25
- *    "foo.2.bar" without any "foo.1.bar" present.
26ba25
- *  - If keys in @src represent list indexes, but are not in
26ba25
- *    the "%zu" format. e.g. "foo.+0.bar"
26ba25
- *
26ba25
- * Returns: either a QDict or QList for the nested data structure, or NULL
26ba25
- * on error
26ba25
- */
26ba25
-QObject *qdict_crumple(const QDict *src, Error **errp)
26ba25
-{
26ba25
-    const QDictEntry *ent;
26ba25
-    QDict *two_level, *multi_level = NULL;
26ba25
-    QObject *dst = NULL, *child;
26ba25
-    size_t i;
26ba25
-    char *prefix = NULL;
26ba25
-    const char *suffix = NULL;
26ba25
-    int is_list;
26ba25
-
26ba25
-    two_level = qdict_new();
26ba25
-
26ba25
-    /* Step 1: split our totally flat dict into a two level dict */
26ba25
-    for (ent = qdict_first(src); ent != NULL; ent = qdict_next(src, ent)) {
26ba25
-        if (qobject_type(ent->value) == QTYPE_QDICT ||
26ba25
-            qobject_type(ent->value) == QTYPE_QLIST) {
26ba25
-            error_setg(errp, "Value %s is not a scalar",
26ba25
-                       ent->key);
26ba25
-            goto error;
26ba25
-        }
26ba25
-
26ba25
-        qdict_split_flat_key(ent->key, &prefix, &suffix);
26ba25
-
26ba25
-        child = qdict_get(two_level, prefix);
26ba25
-        if (suffix) {
26ba25
-            QDict *child_dict = qobject_to(QDict, child);
26ba25
-            if (!child_dict) {
26ba25
-                if (child) {
26ba25
-                    error_setg(errp, "Key %s prefix is already set as a scalar",
26ba25
-                               prefix);
26ba25
-                    goto error;
26ba25
-                }
26ba25
-
26ba25
-                child_dict = qdict_new();
26ba25
-                qdict_put_obj(two_level, prefix, QOBJECT(child_dict));
26ba25
-            }
26ba25
-
26ba25
-            qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
26ba25
-        } else {
26ba25
-            if (child) {
26ba25
-                error_setg(errp, "Key %s prefix is already set as a dict",
26ba25
-                           prefix);
26ba25
-                goto error;
26ba25
-            }
26ba25
-            qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
26ba25
-        }
26ba25
-
26ba25
-        g_free(prefix);
26ba25
-        prefix = NULL;
26ba25
-    }
26ba25
-
26ba25
-    /* Step 2: optionally process the two level dict recursively
26ba25
-     * into a multi-level dict */
26ba25
-    multi_level = qdict_new();
26ba25
-    for (ent = qdict_first(two_level); ent != NULL;
26ba25
-         ent = qdict_next(two_level, ent)) {
26ba25
-        QDict *dict = qobject_to(QDict, ent->value);
26ba25
-        if (dict) {
26ba25
-            child = qdict_crumple(dict, errp);
26ba25
-            if (!child) {
26ba25
-                goto error;
26ba25
-            }
26ba25
-
26ba25
-            qdict_put_obj(multi_level, ent->key, child);
26ba25
-        } else {
26ba25
-            qdict_put_obj(multi_level, ent->key, qobject_ref(ent->value));
26ba25
-        }
26ba25
-    }
26ba25
-    qobject_unref(two_level);
26ba25
-    two_level = NULL;
26ba25
-
26ba25
-    /* Step 3: detect if we need to turn our dict into list */
26ba25
-    is_list = qdict_is_list(multi_level, errp);
26ba25
-    if (is_list < 0) {
26ba25
-        goto error;
26ba25
-    }
26ba25
-
26ba25
-    if (is_list) {
26ba25
-        dst = QOBJECT(qlist_new());
26ba25
-
26ba25
-        for (i = 0; i < qdict_size(multi_level); i++) {
26ba25
-            char *key = g_strdup_printf("%zu", i);
26ba25
-
26ba25
-            child = qdict_get(multi_level, key);
26ba25
-            g_free(key);
26ba25
-
26ba25
-            if (!child) {
26ba25
-                error_setg(errp, "Missing list index %zu", i);
26ba25
-                goto error;
26ba25
-            }
26ba25
-
26ba25
-            qlist_append_obj(qobject_to(QList, dst), qobject_ref(child));
26ba25
-        }
26ba25
-        qobject_unref(multi_level);
26ba25
-        multi_level = NULL;
26ba25
-    } else {
26ba25
-        dst = QOBJECT(multi_level);
26ba25
-    }
26ba25
-
26ba25
-    return dst;
26ba25
-
26ba25
- error:
26ba25
-    g_free(prefix);
26ba25
-    qobject_unref(multi_level);
26ba25
-    qobject_unref(two_level);
26ba25
-    qobject_unref(dst);
26ba25
-    return NULL;
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_array_entries(): Returns the number of direct array entries if the
26ba25
- * sub-QDict of src specified by the prefix in subqdict (or src itself for
26ba25
- * prefix == "") is valid as an array, i.e. the length of the created list if
26ba25
- * the sub-QDict would become empty after calling qdict_array_split() on it. If
26ba25
- * the array is not valid, -EINVAL is returned.
26ba25
- */
26ba25
-int qdict_array_entries(QDict *src, const char *subqdict)
26ba25
-{
26ba25
-    const QDictEntry *entry;
26ba25
-    unsigned i;
26ba25
-    unsigned entries = 0;
26ba25
-    size_t subqdict_len = strlen(subqdict);
26ba25
-
26ba25
-    assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
26ba25
-
26ba25
-    /* qdict_array_split() loops until UINT_MAX, but as we want to return
26ba25
-     * negative errors, we only have a signed return value here. Any additional
26ba25
-     * entries will lead to -EINVAL. */
26ba25
-    for (i = 0; i < INT_MAX; i++) {
26ba25
-        QObject *subqobj;
26ba25
-        int subqdict_entries;
26ba25
-        char *prefix = g_strdup_printf("%s%u.", subqdict, i);
26ba25
-
26ba25
-        subqdict_entries = qdict_count_prefixed_entries(src, prefix);
26ba25
-
26ba25
-        /* Remove ending "." */
26ba25
-        prefix[strlen(prefix) - 1] = 0;
26ba25
-        subqobj = qdict_get(src, prefix);
26ba25
-
26ba25
-        g_free(prefix);
26ba25
-
26ba25
-        if (subqdict_entries < 0) {
26ba25
-            return subqdict_entries;
26ba25
-        }
26ba25
-
26ba25
-        /* There may be either a single subordinate object (named "%u") or
26ba25
-         * multiple objects (each with a key prefixed "%u."), but not both. */
26ba25
-        if (subqobj && subqdict_entries) {
26ba25
-            return -EINVAL;
26ba25
-        } else if (!subqobj && !subqdict_entries) {
26ba25
-            break;
26ba25
-        }
26ba25
-
26ba25
-        entries += subqdict_entries ? subqdict_entries : 1;
26ba25
-    }
26ba25
-
26ba25
-    /* Consider everything handled that isn't part of the given sub-QDict */
26ba25
-    for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
26ba25
-        if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
26ba25
-            entries++;
26ba25
-        }
26ba25
-    }
26ba25
-
26ba25
-    /* Anything left in the sub-QDict that wasn't handled? */
26ba25
-    if (qdict_size(src) != entries) {
26ba25
-        return -EINVAL;
26ba25
-    }
26ba25
-
26ba25
-    return i;
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
26ba25
- * elements from src to dest.
26ba25
- *
26ba25
- * If an element from src has a key already present in dest, it will not be
26ba25
- * moved unless overwrite is true.
26ba25
- *
26ba25
- * If overwrite is true, the conflicting values in dest will be discarded and
26ba25
- * replaced by the corresponding values from src.
26ba25
- *
26ba25
- * Therefore, with overwrite being true, the src QDict will always be empty when
26ba25
- * this function returns. If overwrite is false, the src QDict will be empty
26ba25
- * iff there were no conflicts.
26ba25
- */
26ba25
-void qdict_join(QDict *dest, QDict *src, bool overwrite)
26ba25
-{
26ba25
-    const QDictEntry *entry, *next;
26ba25
-
26ba25
-    entry = qdict_first(src);
26ba25
-    while (entry) {
26ba25
-        next = qdict_next(src, entry);
26ba25
-
26ba25
-        if (overwrite || !qdict_haskey(dest, entry->key)) {
26ba25
-            qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
26ba25
-            qdict_del(src, entry->key);
26ba25
-        }
26ba25
-
26ba25
-        entry = next;
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-/**
26ba25
- * qdict_rename_keys(): Rename keys in qdict according to the replacements
26ba25
- * specified in the array renames. The array must be terminated by an entry
26ba25
- * with from = NULL.
26ba25
- *
26ba25
- * The renames are performed individually in the order of the array, so entries
26ba25
- * may be renamed multiple times and may or may not conflict depending on the
26ba25
- * order of the renames array.
26ba25
- *
26ba25
- * Returns true for success, false in error cases.
26ba25
- */
26ba25
-bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp)
26ba25
-{
26ba25
-    QObject *qobj;
26ba25
-
26ba25
-    while (renames->from) {
26ba25
-        if (qdict_haskey(qdict, renames->from)) {
26ba25
-            if (qdict_haskey(qdict, renames->to)) {
26ba25
-                error_setg(errp, "'%s' and its alias '%s' can't be used at the "
26ba25
-                           "same time", renames->to, renames->from);
26ba25
-                return false;
26ba25
-            }
26ba25
-
26ba25
-            qobj = qdict_get(qdict, renames->from);
26ba25
-            qdict_put_obj(qdict, renames->to, qobject_ref(qobj));
26ba25
-            qdict_del(qdict, renames->from);
26ba25
-        }
26ba25
-
26ba25
-        renames++;
26ba25
-    }
26ba25
-    return true;
26ba25
-}
26ba25
diff --git a/tests/Makefile.include b/tests/Makefile.include
26ba25
index 0464e52..cb12ee6 100644
26ba25
--- a/tests/Makefile.include
26ba25
+++ b/tests/Makefile.include
26ba25
@@ -40,6 +40,8 @@ SYSEMU_TARGET_LIST := $(subst -softmmu.mak,,$(notdir \
26ba25
 
26ba25
 check-unit-y = tests/check-qdict$(EXESUF)
26ba25
 gcov-files-check-qdict-y = qobject/qdict.c
26ba25
+check-unit-y = tests/check-block-qdict$(EXESUF)
26ba25
+gcov-files-check-block-qdict-y = qobject/block-qdict.c
26ba25
 check-unit-y += tests/test-char$(EXESUF)
26ba25
 gcov-files-check-qdict-y = chardev/char.c
26ba25
 check-unit-y += tests/check-qnum$(EXESUF)
26ba25
@@ -580,6 +582,7 @@ GENERATED_FILES += tests/test-qapi-types.h tests/test-qapi-visit.h \
26ba25
 test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
26ba25
 	tests/check-qlist.o tests/check-qnull.o tests/check-qobject.o \
26ba25
 	tests/check-qjson.o tests/check-qlit.o \
26ba25
+	tests/check-block-qtest.o \
26ba25
 	tests/test-coroutine.o tests/test-string-output-visitor.o \
26ba25
 	tests/test-string-input-visitor.o tests/test-qobject-output-visitor.o \
26ba25
 	tests/test-clone-visitor.o \
26ba25
@@ -610,6 +613,7 @@ test-block-obj-y = $(block-obj-y) $(test-io-obj-y) tests/iothread.o
26ba25
 tests/check-qnum$(EXESUF): tests/check-qnum.o $(test-util-obj-y)
26ba25
 tests/check-qstring$(EXESUF): tests/check-qstring.o $(test-util-obj-y)
26ba25
 tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
26ba25
+tests/check-block-qdict$(EXESUF): tests/check-block-qdict.o $(test-util-obj-y)
26ba25
 tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)
26ba25
 tests/check-qnull$(EXESUF): tests/check-qnull.o $(test-util-obj-y)
26ba25
 tests/check-qobject$(EXESUF): tests/check-qobject.o $(test-util-obj-y)
26ba25
diff --git a/tests/check-block-qdict.c b/tests/check-block-qdict.c
26ba25
new file mode 100644
26ba25
index 0000000..5b9f4d5
26ba25
--- /dev/null
26ba25
+++ b/tests/check-block-qdict.c
26ba25
@@ -0,0 +1,655 @@
26ba25
+/*
26ba25
+ * Unit-tests for Block layer QDict extras
26ba25
+ *
26ba25
+ * Copyright (c) 2013-2018 Red Hat, Inc.
26ba25
+ *
26ba25
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
26ba25
+ * See the COPYING.LIB file in the top-level directory.
26ba25
+ */
26ba25
+
26ba25
+#include "qemu/osdep.h"
26ba25
+#include "block/qdict.h"
26ba25
+#include "qapi/qmp/qlist.h"
26ba25
+#include "qapi/qmp/qnum.h"
26ba25
+#include "qapi/error.h"
26ba25
+
26ba25
+static void qdict_defaults_test(void)
26ba25
+{
26ba25
+    QDict *dict, *copy;
26ba25
+
26ba25
+    dict = qdict_new();
26ba25
+    copy = qdict_new();
26ba25
+
26ba25
+    qdict_set_default_str(dict, "foo", "abc");
26ba25
+    qdict_set_default_str(dict, "foo", "def");
26ba25
+    g_assert_cmpstr(qdict_get_str(dict, "foo"), ==, "abc");
26ba25
+    qdict_set_default_str(dict, "bar", "ghi");
26ba25
+
26ba25
+    qdict_copy_default(copy, dict, "foo");
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "foo"), ==, "abc");
26ba25
+    qdict_set_default_str(copy, "bar", "xyz");
26ba25
+    qdict_copy_default(copy, dict, "bar");
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "bar"), ==, "xyz");
26ba25
+
26ba25
+    qobject_unref(copy);
26ba25
+    qobject_unref(dict);
26ba25
+}
26ba25
+
26ba25
+static void qdict_flatten_test(void)
26ba25
+{
26ba25
+    QList *list1 = qlist_new();
26ba25
+    QList *list2 = qlist_new();
26ba25
+    QDict *dict1 = qdict_new();
26ba25
+    QDict *dict2 = qdict_new();
26ba25
+    QDict *dict3 = qdict_new();
26ba25
+
26ba25
+    /*
26ba25
+     * Test the flattening of
26ba25
+     *
26ba25
+     * {
26ba25
+     *     "e": [
26ba25
+     *         42,
26ba25
+     *         [
26ba25
+     *             23,
26ba25
+     *             66,
26ba25
+     *             {
26ba25
+     *                 "a": 0,
26ba25
+     *                 "b": 1
26ba25
+     *             }
26ba25
+     *         ]
26ba25
+     *     ],
26ba25
+     *     "f": {
26ba25
+     *         "c": 2,
26ba25
+     *         "d": 3,
26ba25
+     *     },
26ba25
+     *     "g": 4
26ba25
+     * }
26ba25
+     *
26ba25
+     * to
26ba25
+     *
26ba25
+     * {
26ba25
+     *     "e.0": 42,
26ba25
+     *     "e.1.0": 23,
26ba25
+     *     "e.1.1": 66,
26ba25
+     *     "e.1.2.a": 0,
26ba25
+     *     "e.1.2.b": 1,
26ba25
+     *     "f.c": 2,
26ba25
+     *     "f.d": 3,
26ba25
+     *     "g": 4
26ba25
+     * }
26ba25
+     */
26ba25
+
26ba25
+    qdict_put_int(dict1, "a", 0);
26ba25
+    qdict_put_int(dict1, "b", 1);
26ba25
+
26ba25
+    qlist_append_int(list1, 23);
26ba25
+    qlist_append_int(list1, 66);
26ba25
+    qlist_append(list1, dict1);
26ba25
+    qlist_append_int(list2, 42);
26ba25
+    qlist_append(list2, list1);
26ba25
+
26ba25
+    qdict_put_int(dict2, "c", 2);
26ba25
+    qdict_put_int(dict2, "d", 3);
26ba25
+    qdict_put(dict3, "e", list2);
26ba25
+    qdict_put(dict3, "f", dict2);
26ba25
+    qdict_put_int(dict3, "g", 4);
26ba25
+
26ba25
+    qdict_flatten(dict3);
26ba25
+
26ba25
+    g_assert(qdict_get_int(dict3, "e.0") == 42);
26ba25
+    g_assert(qdict_get_int(dict3, "e.1.0") == 23);
26ba25
+    g_assert(qdict_get_int(dict3, "e.1.1") == 66);
26ba25
+    g_assert(qdict_get_int(dict3, "e.1.2.a") == 0);
26ba25
+    g_assert(qdict_get_int(dict3, "e.1.2.b") == 1);
26ba25
+    g_assert(qdict_get_int(dict3, "f.c") == 2);
26ba25
+    g_assert(qdict_get_int(dict3, "f.d") == 3);
26ba25
+    g_assert(qdict_get_int(dict3, "g") == 4);
26ba25
+
26ba25
+    g_assert(qdict_size(dict3) == 8);
26ba25
+
26ba25
+    qobject_unref(dict3);
26ba25
+}
26ba25
+
26ba25
+static void qdict_array_split_test(void)
26ba25
+{
26ba25
+    QDict *test_dict = qdict_new();
26ba25
+    QDict *dict1, *dict2;
26ba25
+    QNum *int1;
26ba25
+    QList *test_list;
26ba25
+
26ba25
+    /*
26ba25
+     * Test the split of
26ba25
+     *
26ba25
+     * {
26ba25
+     *     "1.x": 0,
26ba25
+     *     "4.y": 1,
26ba25
+     *     "0.a": 42,
26ba25
+     *     "o.o": 7,
26ba25
+     *     "0.b": 23,
26ba25
+     *     "2": 66
26ba25
+     * }
26ba25
+     *
26ba25
+     * to
26ba25
+     *
26ba25
+     * [
26ba25
+     *     {
26ba25
+     *         "a": 42,
26ba25
+     *         "b": 23
26ba25
+     *     },
26ba25
+     *     {
26ba25
+     *         "x": 0
26ba25
+     *     },
26ba25
+     *     66
26ba25
+     * ]
26ba25
+     *
26ba25
+     * and
26ba25
+     *
26ba25
+     * {
26ba25
+     *     "4.y": 1,
26ba25
+     *     "o.o": 7
26ba25
+     * }
26ba25
+     *
26ba25
+     * (remaining in the old QDict)
26ba25
+     *
26ba25
+     * This example is given in the comment of qdict_array_split().
26ba25
+     */
26ba25
+
26ba25
+    qdict_put_int(test_dict, "1.x", 0);
26ba25
+    qdict_put_int(test_dict, "4.y", 1);
26ba25
+    qdict_put_int(test_dict, "0.a", 42);
26ba25
+    qdict_put_int(test_dict, "o.o", 7);
26ba25
+    qdict_put_int(test_dict, "0.b", 23);
26ba25
+    qdict_put_int(test_dict, "2", 66);
26ba25
+
26ba25
+    qdict_array_split(test_dict, &test_list);
26ba25
+
26ba25
+    dict1 = qobject_to(QDict, qlist_pop(test_list));
26ba25
+    dict2 = qobject_to(QDict, qlist_pop(test_list));
26ba25
+    int1 = qobject_to(QNum, qlist_pop(test_list));
26ba25
+
26ba25
+    g_assert(dict1);
26ba25
+    g_assert(dict2);
26ba25
+    g_assert(int1);
26ba25
+    g_assert(qlist_empty(test_list));
26ba25
+
26ba25
+    qobject_unref(test_list);
26ba25
+
26ba25
+    g_assert(qdict_get_int(dict1, "a") == 42);
26ba25
+    g_assert(qdict_get_int(dict1, "b") == 23);
26ba25
+
26ba25
+    g_assert(qdict_size(dict1) == 2);
26ba25
+
26ba25
+    qobject_unref(dict1);
26ba25
+
26ba25
+    g_assert(qdict_get_int(dict2, "x") == 0);
26ba25
+
26ba25
+    g_assert(qdict_size(dict2) == 1);
26ba25
+
26ba25
+    qobject_unref(dict2);
26ba25
+
26ba25
+    g_assert_cmpint(qnum_get_int(int1), ==, 66);
26ba25
+
26ba25
+    qobject_unref(int1);
26ba25
+
26ba25
+    g_assert(qdict_get_int(test_dict, "4.y") == 1);
26ba25
+    g_assert(qdict_get_int(test_dict, "o.o") == 7);
26ba25
+
26ba25
+    g_assert(qdict_size(test_dict) == 2);
26ba25
+
26ba25
+    qobject_unref(test_dict);
26ba25
+
26ba25
+    /*
26ba25
+     * Test the split of
26ba25
+     *
26ba25
+     * {
26ba25
+     *     "0": 42,
26ba25
+     *     "1": 23,
26ba25
+     *     "1.x": 84
26ba25
+     * }
26ba25
+     *
26ba25
+     * to
26ba25
+     *
26ba25
+     * [
26ba25
+     *     42
26ba25
+     * ]
26ba25
+     *
26ba25
+     * and
26ba25
+     *
26ba25
+     * {
26ba25
+     *     "1": 23,
26ba25
+     *     "1.x": 84
26ba25
+     * }
26ba25
+     *
26ba25
+     * That is, test whether splitting stops if there is both an entry with key
26ba25
+     * of "%u" and other entries with keys prefixed "%u." for the same index.
26ba25
+     */
26ba25
+
26ba25
+    test_dict = qdict_new();
26ba25
+
26ba25
+    qdict_put_int(test_dict, "0", 42);
26ba25
+    qdict_put_int(test_dict, "1", 23);
26ba25
+    qdict_put_int(test_dict, "1.x", 84);
26ba25
+
26ba25
+    qdict_array_split(test_dict, &test_list);
26ba25
+
26ba25
+    int1 = qobject_to(QNum, qlist_pop(test_list));
26ba25
+
26ba25
+    g_assert(int1);
26ba25
+    g_assert(qlist_empty(test_list));
26ba25
+
26ba25
+    qobject_unref(test_list);
26ba25
+
26ba25
+    g_assert_cmpint(qnum_get_int(int1), ==, 42);
26ba25
+
26ba25
+    qobject_unref(int1);
26ba25
+
26ba25
+    g_assert(qdict_get_int(test_dict, "1") == 23);
26ba25
+    g_assert(qdict_get_int(test_dict, "1.x") == 84);
26ba25
+
26ba25
+    g_assert(qdict_size(test_dict) == 2);
26ba25
+
26ba25
+    qobject_unref(test_dict);
26ba25
+}
26ba25
+
26ba25
+static void qdict_array_entries_test(void)
26ba25
+{
26ba25
+    QDict *dict = qdict_new();
26ba25
+
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0);
26ba25
+
26ba25
+    qdict_put_int(dict, "bar", 0);
26ba25
+    qdict_put_int(dict, "baz.0", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0);
26ba25
+
26ba25
+    qdict_put_int(dict, "foo.1", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL);
26ba25
+    qdict_put_int(dict, "foo.0", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 2);
26ba25
+    qdict_put_int(dict, "foo.bar", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL);
26ba25
+    qdict_del(dict, "foo.bar");
26ba25
+
26ba25
+    qdict_put_int(dict, "foo.2.a", 0);
26ba25
+    qdict_put_int(dict, "foo.2.b", 0);
26ba25
+    qdict_put_int(dict, "foo.2.c", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 3);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
26ba25
+
26ba25
+    qobject_unref(dict);
26ba25
+
26ba25
+    dict = qdict_new();
26ba25
+    qdict_put_int(dict, "1", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
26ba25
+    qdict_put_int(dict, "0", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, ""), ==, 2);
26ba25
+    qdict_put_int(dict, "bar", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
26ba25
+    qdict_del(dict, "bar");
26ba25
+
26ba25
+    qdict_put_int(dict, "2.a", 0);
26ba25
+    qdict_put_int(dict, "2.b", 0);
26ba25
+    qdict_put_int(dict, "2.c", 0);
26ba25
+    g_assert_cmpint(qdict_array_entries(dict, ""), ==, 3);
26ba25
+
26ba25
+    qobject_unref(dict);
26ba25
+}
26ba25
+
26ba25
+static void qdict_join_test(void)
26ba25
+{
26ba25
+    QDict *dict1, *dict2;
26ba25
+    bool overwrite = false;
26ba25
+    int i;
26ba25
+
26ba25
+    dict1 = qdict_new();
26ba25
+    dict2 = qdict_new();
26ba25
+
26ba25
+    /* Test everything once without overwrite and once with */
26ba25
+    do {
26ba25
+        /* Test empty dicts */
26ba25
+        qdict_join(dict1, dict2, overwrite);
26ba25
+
26ba25
+        g_assert(qdict_size(dict1) == 0);
26ba25
+        g_assert(qdict_size(dict2) == 0);
26ba25
+
26ba25
+        /* First iteration: Test movement */
26ba25
+        /* Second iteration: Test empty source and non-empty destination */
26ba25
+        qdict_put_int(dict2, "foo", 42);
26ba25
+
26ba25
+        for (i = 0; i < 2; i++) {
26ba25
+            qdict_join(dict1, dict2, overwrite);
26ba25
+
26ba25
+            g_assert(qdict_size(dict1) == 1);
26ba25
+            g_assert(qdict_size(dict2) == 0);
26ba25
+
26ba25
+            g_assert(qdict_get_int(dict1, "foo") == 42);
26ba25
+        }
26ba25
+
26ba25
+        /* Test non-empty source and destination without conflict */
26ba25
+        qdict_put_int(dict2, "bar", 23);
26ba25
+
26ba25
+        qdict_join(dict1, dict2, overwrite);
26ba25
+
26ba25
+        g_assert(qdict_size(dict1) == 2);
26ba25
+        g_assert(qdict_size(dict2) == 0);
26ba25
+
26ba25
+        g_assert(qdict_get_int(dict1, "foo") == 42);
26ba25
+        g_assert(qdict_get_int(dict1, "bar") == 23);
26ba25
+
26ba25
+        /* Test conflict */
26ba25
+        qdict_put_int(dict2, "foo", 84);
26ba25
+
26ba25
+        qdict_join(dict1, dict2, overwrite);
26ba25
+
26ba25
+        g_assert(qdict_size(dict1) == 2);
26ba25
+        g_assert(qdict_size(dict2) == !overwrite);
26ba25
+
26ba25
+        g_assert(qdict_get_int(dict1, "foo") == (overwrite ? 84 : 42));
26ba25
+        g_assert(qdict_get_int(dict1, "bar") == 23);
26ba25
+
26ba25
+        if (!overwrite) {
26ba25
+            g_assert(qdict_get_int(dict2, "foo") == 84);
26ba25
+        }
26ba25
+
26ba25
+        /* Check the references */
26ba25
+        g_assert(qdict_get(dict1, "foo")->base.refcnt == 1);
26ba25
+        g_assert(qdict_get(dict1, "bar")->base.refcnt == 1);
26ba25
+
26ba25
+        if (!overwrite) {
26ba25
+            g_assert(qdict_get(dict2, "foo")->base.refcnt == 1);
26ba25
+        }
26ba25
+
26ba25
+        /* Clean up */
26ba25
+        qdict_del(dict1, "foo");
26ba25
+        qdict_del(dict1, "bar");
26ba25
+
26ba25
+        if (!overwrite) {
26ba25
+            qdict_del(dict2, "foo");
26ba25
+        }
26ba25
+    } while (overwrite ^= true);
26ba25
+
26ba25
+    qobject_unref(dict1);
26ba25
+    qobject_unref(dict2);
26ba25
+}
26ba25
+
26ba25
+static void qdict_crumple_test_recursive(void)
26ba25
+{
26ba25
+    QDict *src, *dst, *rule, *vnc, *acl, *listen;
26ba25
+    QList *rules;
26ba25
+
26ba25
+    src = qdict_new();
26ba25
+    qdict_put_str(src, "vnc.listen.addr", "127.0.0.1");
26ba25
+    qdict_put_str(src, "vnc.listen.port", "5901");
26ba25
+    qdict_put_str(src, "vnc.acl.rules.0.match", "fred");
26ba25
+    qdict_put_str(src, "vnc.acl.rules.0.policy", "allow");
26ba25
+    qdict_put_str(src, "vnc.acl.rules.1.match", "bob");
26ba25
+    qdict_put_str(src, "vnc.acl.rules.1.policy", "deny");
26ba25
+    qdict_put_str(src, "vnc.acl.default", "deny");
26ba25
+    qdict_put_str(src, "vnc.acl..name", "acl0");
26ba25
+    qdict_put_str(src, "vnc.acl.rule..name", "acl0");
26ba25
+
26ba25
+    dst = qobject_to(QDict, qdict_crumple(src, &error_abort));
26ba25
+    g_assert(dst);
26ba25
+    g_assert_cmpint(qdict_size(dst), ==, 1);
26ba25
+
26ba25
+    vnc = qdict_get_qdict(dst, "vnc");
26ba25
+    g_assert(vnc);
26ba25
+    g_assert_cmpint(qdict_size(vnc), ==, 3);
26ba25
+
26ba25
+    listen = qdict_get_qdict(vnc, "listen");
26ba25
+    g_assert(listen);
26ba25
+    g_assert_cmpint(qdict_size(listen), ==, 2);
26ba25
+    g_assert_cmpstr("127.0.0.1", ==, qdict_get_str(listen, "addr"));
26ba25
+    g_assert_cmpstr("5901", ==, qdict_get_str(listen, "port"));
26ba25
+
26ba25
+    acl = qdict_get_qdict(vnc, "acl");
26ba25
+    g_assert(acl);
26ba25
+    g_assert_cmpint(qdict_size(acl), ==, 3);
26ba25
+
26ba25
+    rules = qdict_get_qlist(acl, "rules");
26ba25
+    g_assert(rules);
26ba25
+    g_assert_cmpint(qlist_size(rules), ==, 2);
26ba25
+
26ba25
+    rule = qobject_to(QDict, qlist_pop(rules));
26ba25
+    g_assert(rule);
26ba25
+    g_assert_cmpint(qdict_size(rule), ==, 2);
26ba25
+    g_assert_cmpstr("fred", ==, qdict_get_str(rule, "match"));
26ba25
+    g_assert_cmpstr("allow", ==, qdict_get_str(rule, "policy"));
26ba25
+    qobject_unref(rule);
26ba25
+
26ba25
+    rule = qobject_to(QDict, qlist_pop(rules));
26ba25
+    g_assert(rule);
26ba25
+    g_assert_cmpint(qdict_size(rule), ==, 2);
26ba25
+    g_assert_cmpstr("bob", ==, qdict_get_str(rule, "match"));
26ba25
+    g_assert_cmpstr("deny", ==, qdict_get_str(rule, "policy"));
26ba25
+    qobject_unref(rule);
26ba25
+
26ba25
+    /* With recursive crumpling, we should see all names unescaped */
26ba25
+    g_assert_cmpstr("acl0", ==, qdict_get_str(vnc, "acl.name"));
26ba25
+    g_assert_cmpstr("acl0", ==, qdict_get_str(acl, "rule.name"));
26ba25
+
26ba25
+    qobject_unref(src);
26ba25
+    qobject_unref(dst);
26ba25
+}
26ba25
+
26ba25
+static void qdict_crumple_test_empty(void)
26ba25
+{
26ba25
+    QDict *src, *dst;
26ba25
+
26ba25
+    src = qdict_new();
26ba25
+
26ba25
+    dst = qobject_to(QDict, qdict_crumple(src, &error_abort));
26ba25
+
26ba25
+    g_assert_cmpint(qdict_size(dst), ==, 0);
26ba25
+
26ba25
+    qobject_unref(src);
26ba25
+    qobject_unref(dst);
26ba25
+}
26ba25
+
26ba25
+static int qdict_count_entries(QDict *dict)
26ba25
+{
26ba25
+    const QDictEntry *e;
26ba25
+    int count = 0;
26ba25
+
26ba25
+    for (e = qdict_first(dict); e; e = qdict_next(dict, e)) {
26ba25
+        count++;
26ba25
+    }
26ba25
+
26ba25
+    return count;
26ba25
+}
26ba25
+
26ba25
+static void qdict_rename_keys_test(void)
26ba25
+{
26ba25
+    QDict *dict = qdict_new();
26ba25
+    QDict *copy;
26ba25
+    QDictRenames *renames;
26ba25
+    Error *local_err = NULL;
26ba25
+
26ba25
+    qdict_put_str(dict, "abc", "foo");
26ba25
+    qdict_put_str(dict, "abcdef", "bar");
26ba25
+    qdict_put_int(dict, "number", 42);
26ba25
+    qdict_put_bool(dict, "flag", true);
26ba25
+    qdict_put_null(dict, "nothing");
26ba25
+
26ba25
+    /* Empty rename list */
26ba25
+    renames = (QDictRenames[]) {
26ba25
+        { NULL, "this can be anything" }
26ba25
+    };
26ba25
+    copy = qdict_clone_shallow(dict);
26ba25
+    qdict_rename_keys(copy, renames, &error_abort);
26ba25
+
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "foo");
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "abcdef"), ==, "bar");
26ba25
+    g_assert_cmpint(qdict_get_int(copy, "number"), ==, 42);
26ba25
+    g_assert_cmpint(qdict_get_bool(copy, "flag"), ==, true);
26ba25
+    g_assert(qobject_type(qdict_get(copy, "nothing")) == QTYPE_QNULL);
26ba25
+    g_assert_cmpint(qdict_count_entries(copy), ==, 5);
26ba25
+
26ba25
+    qobject_unref(copy);
26ba25
+
26ba25
+    /* Simple rename of all entries */
26ba25
+    renames = (QDictRenames[]) {
26ba25
+        { "abc",        "str1" },
26ba25
+        { "abcdef",     "str2" },
26ba25
+        { "number",     "int" },
26ba25
+        { "flag",       "bool" },
26ba25
+        { "nothing",    "null" },
26ba25
+        { NULL , NULL }
26ba25
+    };
26ba25
+    copy = qdict_clone_shallow(dict);
26ba25
+    qdict_rename_keys(copy, renames, &error_abort);
26ba25
+
26ba25
+    g_assert(!qdict_haskey(copy, "abc"));
26ba25
+    g_assert(!qdict_haskey(copy, "abcdef"));
26ba25
+    g_assert(!qdict_haskey(copy, "number"));
26ba25
+    g_assert(!qdict_haskey(copy, "flag"));
26ba25
+    g_assert(!qdict_haskey(copy, "nothing"));
26ba25
+
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "str1"), ==, "foo");
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "str2"), ==, "bar");
26ba25
+    g_assert_cmpint(qdict_get_int(copy, "int"), ==, 42);
26ba25
+    g_assert_cmpint(qdict_get_bool(copy, "bool"), ==, true);
26ba25
+    g_assert(qobject_type(qdict_get(copy, "null")) == QTYPE_QNULL);
26ba25
+    g_assert_cmpint(qdict_count_entries(copy), ==, 5);
26ba25
+
26ba25
+    qobject_unref(copy);
26ba25
+
26ba25
+    /* Renames are processed top to bottom */
26ba25
+    renames = (QDictRenames[]) {
26ba25
+        { "abc",        "tmp" },
26ba25
+        { "abcdef",     "abc" },
26ba25
+        { "number",     "abcdef" },
26ba25
+        { "flag",       "number" },
26ba25
+        { "nothing",    "flag" },
26ba25
+        { "tmp",        "nothing" },
26ba25
+        { NULL , NULL }
26ba25
+    };
26ba25
+    copy = qdict_clone_shallow(dict);
26ba25
+    qdict_rename_keys(copy, renames, &error_abort);
26ba25
+
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "nothing"), ==, "foo");
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "bar");
26ba25
+    g_assert_cmpint(qdict_get_int(copy, "abcdef"), ==, 42);
26ba25
+    g_assert_cmpint(qdict_get_bool(copy, "number"), ==, true);
26ba25
+    g_assert(qobject_type(qdict_get(copy, "flag")) == QTYPE_QNULL);
26ba25
+    g_assert(!qdict_haskey(copy, "tmp"));
26ba25
+    g_assert_cmpint(qdict_count_entries(copy), ==, 5);
26ba25
+
26ba25
+    qobject_unref(copy);
26ba25
+
26ba25
+    /* Conflicting rename */
26ba25
+    renames = (QDictRenames[]) {
26ba25
+        { "abcdef",     "abc" },
26ba25
+        { NULL , NULL }
26ba25
+    };
26ba25
+    copy = qdict_clone_shallow(dict);
26ba25
+    qdict_rename_keys(copy, renames, &local_err);
26ba25
+
26ba25
+    g_assert(local_err != NULL);
26ba25
+    error_free(local_err);
26ba25
+    local_err = NULL;
26ba25
+
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "foo");
26ba25
+    g_assert_cmpstr(qdict_get_str(copy, "abcdef"), ==, "bar");
26ba25
+    g_assert_cmpint(qdict_get_int(copy, "number"), ==, 42);
26ba25
+    g_assert_cmpint(qdict_get_bool(copy, "flag"), ==, true);
26ba25
+    g_assert(qobject_type(qdict_get(copy, "nothing")) == QTYPE_QNULL);
26ba25
+    g_assert_cmpint(qdict_count_entries(copy), ==, 5);
26ba25
+
26ba25
+    qobject_unref(copy);
26ba25
+
26ba25
+    /* Renames in an empty dict */
26ba25
+    renames = (QDictRenames[]) {
26ba25
+        { "abcdef",     "abc" },
26ba25
+        { NULL , NULL }
26ba25
+    };
26ba25
+
26ba25
+    qobject_unref(dict);
26ba25
+    dict = qdict_new();
26ba25
+
26ba25
+    qdict_rename_keys(dict, renames, &error_abort);
26ba25
+    g_assert(qdict_first(dict) == NULL);
26ba25
+
26ba25
+    qobject_unref(dict);
26ba25
+}
26ba25
+
26ba25
+static void qdict_crumple_test_bad_inputs(void)
26ba25
+{
26ba25
+    QDict *src;
26ba25
+    Error *error = NULL;
26ba25
+
26ba25
+    src = qdict_new();
26ba25
+    /* rule.0 can't be both a string and a dict */
26ba25
+    qdict_put_str(src, "rule.0", "fred");
26ba25
+    qdict_put_str(src, "rule.0.policy", "allow");
26ba25
+
26ba25
+    g_assert(qdict_crumple(src, &error) == NULL);
26ba25
+    g_assert(error != NULL);
26ba25
+    error_free(error);
26ba25
+    error = NULL;
26ba25
+    qobject_unref(src);
26ba25
+
26ba25
+    src = qdict_new();
26ba25
+    /* rule can't be both a list and a dict */
26ba25
+    qdict_put_str(src, "rule.0", "fred");
26ba25
+    qdict_put_str(src, "rule.a", "allow");
26ba25
+
26ba25
+    g_assert(qdict_crumple(src, &error) == NULL);
26ba25
+    g_assert(error != NULL);
26ba25
+    error_free(error);
26ba25
+    error = NULL;
26ba25
+    qobject_unref(src);
26ba25
+
26ba25
+    src = qdict_new();
26ba25
+    /* The input should be flat, ie no dicts or lists */
26ba25
+    qdict_put(src, "rule.a", qdict_new());
26ba25
+    qdict_put_str(src, "rule.b", "allow");
26ba25
+
26ba25
+    g_assert(qdict_crumple(src, &error) == NULL);
26ba25
+    g_assert(error != NULL);
26ba25
+    error_free(error);
26ba25
+    error = NULL;
26ba25
+    qobject_unref(src);
26ba25
+
26ba25
+    src = qdict_new();
26ba25
+    /* List indexes must not have gaps */
26ba25
+    qdict_put_str(src, "rule.0", "deny");
26ba25
+    qdict_put_str(src, "rule.3", "allow");
26ba25
+
26ba25
+    g_assert(qdict_crumple(src, &error) == NULL);
26ba25
+    g_assert(error != NULL);
26ba25
+    error_free(error);
26ba25
+    error = NULL;
26ba25
+    qobject_unref(src);
26ba25
+
26ba25
+    src = qdict_new();
26ba25
+    /* List indexes must be in %zu format */
26ba25
+    qdict_put_str(src, "rule.0", "deny");
26ba25
+    qdict_put_str(src, "rule.+1", "allow");
26ba25
+
26ba25
+    g_assert(qdict_crumple(src, &error) == NULL);
26ba25
+    g_assert(error != NULL);
26ba25
+    error_free(error);
26ba25
+    error = NULL;
26ba25
+    qobject_unref(src);
26ba25
+}
26ba25
+
26ba25
+int main(int argc, char **argv)
26ba25
+{
26ba25
+    g_test_init(&argc, &argv, NULL);
26ba25
+
26ba25
+    g_test_add_func("/public/defaults", qdict_defaults_test);
26ba25
+    g_test_add_func("/public/flatten", qdict_flatten_test);
26ba25
+    g_test_add_func("/public/array_split", qdict_array_split_test);
26ba25
+    g_test_add_func("/public/array_entries", qdict_array_entries_test);
26ba25
+    g_test_add_func("/public/join", qdict_join_test);
26ba25
+    g_test_add_func("/public/crumple/recursive",
26ba25
+                    qdict_crumple_test_recursive);
26ba25
+    g_test_add_func("/public/crumple/empty",
26ba25
+                    qdict_crumple_test_empty);
26ba25
+    g_test_add_func("/public/crumple/bad_inputs",
26ba25
+                    qdict_crumple_test_bad_inputs);
26ba25
+
26ba25
+    g_test_add_func("/public/rename_keys", qdict_rename_keys_test);
26ba25
+
26ba25
+    return g_test_run();
26ba25
+}
26ba25
diff --git a/tests/check-qdict.c b/tests/check-qdict.c
26ba25
index 93e2112..86e9fe7 100644
26ba25
--- a/tests/check-qdict.c
26ba25
+++ b/tests/check-qdict.c
26ba25
@@ -11,13 +11,7 @@
26ba25
  */
26ba25
 
26ba25
 #include "qemu/osdep.h"
26ba25
-#include "block/qdict.h"
26ba25
 #include "qapi/qmp/qdict.h"
26ba25
-#include "qapi/qmp/qlist.h"
26ba25
-#include "qapi/qmp/qnum.h"
26ba25
-#include "qapi/qmp/qstring.h"
26ba25
-#include "qapi/error.h"
26ba25
-#include "qemu-common.h"
26ba25
 
26ba25
 /*
26ba25
  * Public Interface test-cases
26ba25
@@ -157,28 +151,6 @@ static void qdict_get_try_str_test(void)
26ba25
     qobject_unref(tests_dict);
26ba25
 }
26ba25
 
26ba25
-static void qdict_defaults_test(void)
26ba25
-{