render / rpms / libvirt

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