[libvirt] [PATCH 0/2] virjson improvements [incremental backup saga]

Upstream qemu stabilized the QMP command block-dirty-bitmap-merge to take an array of strings as the sources to merge into a destination bitmap; this is a lot nicer for libvirt, compared to the alternative where qemu 3.1 x-block-dirty-bitmap-merge had to be called multiple times with the potential for some odd cleanup after partial failure scenarios. This series prepares virjson for use in my incremental backup work. Eric Blake (2): virjson: always raise vir error on append failures virjson: add convenience wrapper for appending string to array src/util/virjson.h | 2 ++ src/libvirt_private.syms | 1 + src/util/virjson.c | 28 +++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) -- 2.20.1

A function that returns -1 for multiple possible failures, but only raises a libvirt error for some of those failures, is hard to use correctly. Yet both of our JSON object/array appenders fall in that pattern. We should either use the _QUIET memory allocation variants, and make callers decide to report failure; or we can make all failure paths noisy. This patch takes the latter approach. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/util/virjson.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 78f868a162..a028a0813a 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -620,11 +620,16 @@ virJSONValueObjectAppend(virJSONValuePtr object, { char *newkey; - if (object->type != VIR_JSON_TYPE_OBJECT) + if (object->type != VIR_JSON_TYPE_OBJECT) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("expecting JSON object")); return -1; + } - if (virJSONValueObjectHasKey(object, key)) + if (virJSONValueObjectHasKey(object, key)) { + virReportError(VIR_ERR_INTERNAL_ERROR, _("duplicate key '%s'"), key); return -1; + } if (VIR_STRDUP(newkey, key) < 0) return -1; @@ -774,8 +779,10 @@ int virJSONValueArrayAppend(virJSONValuePtr array, virJSONValuePtr value) { - if (array->type != VIR_JSON_TYPE_ARRAY) + if (array->type != VIR_JSON_TYPE_ARRAY) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON array")); return -1; + } if (VIR_REALLOC_N(array->data.array.values, array->data.array.nvalues + 1) < 0) -- 2.20.1

Upcoming patches need an array of strings for use in QMP block-dirty-bitmap-merge. A convenience wrapper cuts down on the verbosity of creating the array, similar to the existing virJSONValueObjectAppendString(). Signed-off-by: Eric Blake <eblake@redhat.com> --- src/util/virjson.h | 2 ++ src/libvirt_private.syms | 1 + src/util/virjson.c | 15 +++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/util/virjson.h b/src/util/virjson.h index a6768d904c..d815e60de9 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -138,6 +138,8 @@ int virJSONValueObjectRemoveKey(virJSONValuePtr object, const char *key, virJSONValuePtr *value) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); +int virJSONValueArrayAppendString(virJSONValuePtr object, const char *value); + virJSONValuePtr virJSONValueFromString(const char *jsonstring); char *virJSONValueToString(virJSONValuePtr object, bool pretty); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 89b8ca3b4f..c1f619f514 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2092,6 +2092,7 @@ virISCSIScanTargets; # util/virjson.h virJSONStringReformat; virJSONValueArrayAppend; +virJSONValueArrayAppendString; virJSONValueArrayForeachSteal; virJSONValueArrayGet; virJSONValueArraySize; diff --git a/src/util/virjson.c b/src/util/virjson.c index a028a0813a..d5d66f879f 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -795,6 +795,21 @@ virJSONValueArrayAppend(virJSONValuePtr array, } +int +virJSONValueArrayAppendString(virJSONValuePtr object, + const char *value) +{ + virJSONValuePtr jvalue = virJSONValueNewString(value); + if (!jvalue) + return -1; + if (virJSONValueArrayAppend(object, jvalue) < 0) { + virJSONValueFree(jvalue); + return -1; + } + return 0; +} + + int virJSONValueObjectHasKey(virJSONValuePtr object, const char *key) -- 2.20.1

On Thu, Jan 24, 2019 at 10:05:53PM -0600, Eric Blake wrote:
Upstream qemu stabilized the QMP command block-dirty-bitmap-merge to take an array of strings as the sources to merge into a destination bitmap; this is a lot nicer for libvirt, compared to the alternative where qemu 3.1 x-block-dirty-bitmap-merge had to be called multiple times with the potential for some odd cleanup after partial failure scenarios.
This series prepares virjson for use in my incremental backup work.
Eric Blake (2): virjson: always raise vir error on append failures virjson: add convenience wrapper for appending string to array
src/util/virjson.h | 2 ++ src/libvirt_private.syms | 1 + src/util/virjson.c | 28 +++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (2)
-
Eric Blake
-
Ján Tomko