From f43b7d60d8e03c4b284a7217ea59c2aba5c2ca83 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Mon, 26 Jun 2017 18:42:07 +0200 Subject: [PATCH] util: json: Don't remove the 'file' subobject when deflattening Currently the function would deflatten the object by dropping the 'file' prefix from the attributes. This does not really scale well or adhere to the documentation. Until we refactor the worker to properly deflatten everything we at least simulate it by adding the "file" wrapper object back. --- src/util/virjson.c | 19 +++++++++++++------ src/util/virstoragefile.c | 8 +++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 3c3a54bc7c..d55913a143 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -2012,17 +2012,24 @@ virJSONValueObjectDeflattenWorker(const char *key, virJSONValuePtr virJSONValueObjectDeflatten(virJSONValuePtr json) { - virJSONValuePtr ret; + virJSONValuePtr deflattened; + virJSONValuePtr ret = NULL; - if (!(ret = virJSONValueNewObject())) + if (!(deflattened = virJSONValueNewObject())) return NULL; if (virJSONValueObjectForeachKeyValue(json, virJSONValueObjectDeflattenWorker, - ret) < 0) { - virJSONValueFree(ret); - return NULL; - } + deflattened) < 0) + goto cleanup; + + if (virJSONValueObjectCreate(&ret, "a:file", deflattened, NULL) < 0) + goto cleanup; + + deflattened = NULL; + + cleanup: + virJSONValueFree(deflattened); return ret; } diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 52c5301fff..ff78bc968e 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -3260,7 +3260,13 @@ virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src, if (!(fixedroot = virJSONValueObjectDeflatten(json))) goto cleanup; - file = fixedroot; + if (!(file = virJSONValueObjectGetObject(fixedroot, "file"))) { + str = virJSONValueToString(json, false); + virReportError(VIR_ERR_INVALID_ARG, + _("JSON backing volume defintion '%s' lacks 'file' object"), + NULLSTR(str)); + goto cleanup; + } } if (!(drvname = virJSONValueObjectGetString(file, "driver"))) {