libvirt/SOURCES/libvirt-virStorageSourceParseBackingJSON-Allow-json-pseudo-URIs-without-file-wrapper.patch
2021-09-10 11:58:15 +00:00

116 lines
4.3 KiB
Diff

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