Blame SOURCES/libvirt-virStorageSourceParseBackingJSON-Allow-json-pseudo-URIs-without-file-wrapper.patch

d76c62
From 3a01f1148ea3da0a572a196f987f29f12e397ec1 Mon Sep 17 00:00:00 2001
d76c62
Message-Id: <3a01f1148ea3da0a572a196f987f29f12e397ec1@dist-git>
d76c62
From: Peter Krempa <pkrempa@redhat.com>
d76c62
Date: Wed, 19 Feb 2020 15:10:09 +0100
d76c62
Subject: [PATCH] virStorageSourceParseBackingJSON: Allow 'json:' pseudo URIs
d76c62
 without 'file' wrapper
d76c62
MIME-Version: 1.0
d76c62
Content-Type: text/plain; charset=UTF-8
d76c62
Content-Transfer-Encoding: 8bit
d76c62
d76c62
There are two possibilities:
d76c62
1) json:{"file":{"driver":...}}
d76c62
2) json:{"driver":...}
d76c62
d76c62
Our code didn't work properly with the second one as it was expecting
d76c62
the 'file' wrapper. Conditionalize the removal to only the situation
d76c62
when the top level doesn't have "driver".
d76c62
d76c62
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
d76c62
Reviewed-by: Ján Tomko <jtomko@redhat.com>
d76c62
(cherry picked from commit f8e097570ea9eb0b446f9ff5159b4e9c337e6b07)
d76c62
d76c62
https://bugzilla.redhat.com/show_bug.cgi?id=1791788
d76c62
Message-Id: <26a21da3bf0642ed6ac80184f97337fba98b0c14.1582120424.git.pkrempa@redhat.com>
d76c62
Reviewed-by: Ján Tomko <jtomko@redhat.com>
d76c62
---
d76c62
 src/util/virstoragefile.c | 41 +++++++++++++++++++++++++--------------
d76c62
 1 file changed, 26 insertions(+), 15 deletions(-)
d76c62
d76c62
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
d76c62
index b6749b150c..dd05de188f 100644
d76c62
--- a/src/util/virstoragefile.c
d76c62
+++ b/src/util/virstoragefile.c
d76c62
@@ -3521,10 +3521,17 @@ virStorageSourceParseBackingJSONRaw(virStorageSourcePtr src,
d76c62
                                     const char *jsonstr,
d76c62
                                     int opaque G_GNUC_UNUSED)
d76c62
 {
d76c62
-    /* There are no interesting attributes in raw driver.
d76c62
-     * Treat it as pass-through.
d76c62
-     */
d76c62
-    return virStorageSourceParseBackingJSONInternal(src, json, jsonstr);
d76c62
+    virJSONValuePtr file;
d76c62
+
d76c62
+    /* 'raw' is a format driver so it can have protocol driver children */
d76c62
+    if (!(file = virJSONValueObjectGetObject(json, "file"))) {
d76c62
+        virReportError(VIR_ERR_INVALID_ARG,
d76c62
+                       _("JSON backing volume definition '%s' lacks 'file' object"),
d76c62
+                       jsonstr);
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    return virStorageSourceParseBackingJSONInternal(src, file, jsonstr);
d76c62
 }
d76c62
 
d76c62
 
d76c62
@@ -3601,18 +3608,10 @@ virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src,
d76c62
                                          virJSONValuePtr json,
d76c62
                                          const char *jsonstr)
d76c62
 {
d76c62
-    virJSONValuePtr file;
d76c62
     const char *drvname;
d76c62
     size_t i;
d76c62
 
d76c62
-    if (!(file = virJSONValueObjectGetObject(json, "file"))) {
d76c62
-        virReportError(VIR_ERR_INVALID_ARG,
d76c62
-                       _("JSON backing volume definition '%s' lacks 'file' object"),
d76c62
-                       jsonstr);
d76c62
-        return -1;
d76c62
-    }
d76c62
-
d76c62
-    if (!(drvname = virJSONValueObjectGetString(file, "driver"))) {
d76c62
+    if (!(drvname = virJSONValueObjectGetString(json, "driver"))) {
d76c62
         virReportError(VIR_ERR_INVALID_ARG,
d76c62
                        _("JSON backing volume definition '%s' lacks driver name"),
d76c62
                        jsonstr);
d76c62
@@ -3621,7 +3620,7 @@ virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src,
d76c62
 
d76c62
     for (i = 0; i < G_N_ELEMENTS(jsonParsers); i++) {
d76c62
         if (STREQ(drvname, jsonParsers[i].drvname))
d76c62
-            return jsonParsers[i].func(src, file, jsonstr, jsonParsers[i].opaque);
d76c62
+            return jsonParsers[i].func(src, json, jsonstr, jsonParsers[i].opaque);
d76c62
     }
d76c62
 
d76c62
     virReportError(VIR_ERR_INTERNAL_ERROR,
d76c62
@@ -3637,6 +3636,7 @@ virStorageSourceParseBackingJSON(virStorageSourcePtr src,
d76c62
 {
d76c62
     g_autoptr(virJSONValue) root = NULL;
d76c62
     g_autoptr(virJSONValue) deflattened = NULL;
d76c62
+    virJSONValuePtr file = NULL;
d76c62
 
d76c62
     if (!(root = virJSONValueFromString(json)))
d76c62
         return -1;
d76c62
@@ -3644,7 +3644,18 @@ virStorageSourceParseBackingJSON(virStorageSourcePtr src,
d76c62
     if (!(deflattened = virJSONValueObjectDeflatten(root)))
d76c62
         return -1;
d76c62
 
d76c62
-    return virStorageSourceParseBackingJSONInternal(src, deflattened, json);
d76c62
+    /* There are 2 possible syntaxes:
d76c62
+     * 1) json:{"file":{"driver":...}}
d76c62
+     * 2) json:{"driver":...}
d76c62
+     * Remove the 'file' wrapper object in case 1.
d76c62
+     */
d76c62
+    if (!virJSONValueObjectHasKey(deflattened, "driver"))
d76c62
+        file = virJSONValueObjectGetObject(deflattened, "file");
d76c62
+
d76c62
+    if (!file)
d76c62
+        file = deflattened;
d76c62
+
d76c62
+    return virStorageSourceParseBackingJSONInternal(src, file, json);
d76c62
 }
d76c62
 
d76c62
 
d76c62
-- 
d76c62
2.25.0
d76c62