[libvirt] [PATCH 4/9] util: json: Don't remove the 'file' subobject when deflattening

Peter Krempa posted 9 patches 8 years, 7 months ago
[libvirt] [PATCH 4/9] util: json: Don't remove the 'file' subobject when deflattening
Posted by Peter Krempa 8 years, 7 months ago
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 8ab542432..a8e28cd1b 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 52c5301ff..d24502fbf 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"),
+                str);
+            goto cleanup;
+        }
     }

     if (!(drvname = virJSONValueObjectGetString(file, "driver"))) {
-- 
2.12.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 4/9] util: json: Don't remove the 'file' subobject when deflattening
Posted by John Ferlan 8 years, 7 months ago

On 06/27/2017 08:46 AM, Peter Krempa wrote:
> 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 8ab542432..a8e28cd1b 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 52c5301ff..d24502fbf 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"),
> +                str);

Not aligned properly under VIR_ERR_INVALID_ARG

Similar to driver below, this should be NULLSTR(str);

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

> +            goto cleanup;
> +        }
>      }
> 
>      if (!(drvname = virJSONValueObjectGetString(file, "driver"))) {
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list