render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 32d58910c9d5a775315a42a76666844927c4dad1 Mon Sep 17 00:00:00 2001
Message-Id: <32d58910c9d5a775315a42a76666844927c4dad1@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Wed, 19 Feb 2020 15:10:26 +0100
Subject: [PATCH] virStorageSourceParseBackingJSONRaw: Parse 'offset' and
 'size' attributes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If the parsed 'raw' format JSON string has 'offset' or 'size' attributes
parse them as the format slice.

https://bugzilla.redhat.com/show_bug.cgi?id=1791788

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
(cherry picked from commit e8a819e87f806e6c6690614c40dbeab0bd2e800e)
Message-Id: <88f7d393b22949a8d6afdae44f8215aa06e65329.1582120424.git.pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/virstoragefile.c | 20 ++++++++++++++++++++
 tests/virstoragetest.c    |  6 +++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 0be4168d6e..fcbc97d96a 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -3551,8 +3551,28 @@ virStorageSourceParseBackingJSONRaw(virStorageSourcePtr src,
                                     const char *jsonstr,
                                     int opaque G_GNUC_UNUSED)
 {
+    bool has_offset = virJSONValueObjectHasKey(json, "offset");
+    bool has_size = virJSONValueObjectHasKey(json, "size");
     virJSONValuePtr file;
 
+    if (has_offset || has_size) {
+        src->sliceStorage = g_new0(virStorageSourceSlice, 1);
+
+        if (has_offset &&
+            virJSONValueObjectGetNumberUlong(json, "offset", &src->sliceStorage->offset) < 0) {
+            virReportError(VIR_ERR_INVALID_ARG, "%s",
+                           _("malformed 'offset' property of 'raw' driver"));
+            return -1;
+        }
+
+        if (has_size &&
+            virJSONValueObjectGetNumberUlong(json, "size", &src->sliceStorage->size) < 0) {
+            virReportError(VIR_ERR_INVALID_ARG, "%s",
+                           _("malformed 'size' property of 'raw' driver"));
+            return -1;
+        }
+    }
+
     /* 'raw' is a format driver so it can have protocol driver children */
     if (!(file = virJSONValueObjectGetObject(json, "file"))) {
         virReportError(VIR_ERR_INVALID_ARG,
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
index 25d41f0de4..39040bf4cb 100644
--- a/tests/virstoragetest.c
+++ b/tests/virstoragetest.c
@@ -1600,7 +1600,11 @@ mymain(void)
                                                 "\"filename\": \"/tmp/testfle\""
                                               "}"
                                   "}",
-                            "<source file='/tmp/testfle'/>\n", 0);
+                            "<source file='/tmp/testfle'>\n"
+                            "  <slices>\n"
+                            "    <slice type='storage' offset='10752' size='4063232'/>\n"
+                            "  </slices>\n"
+                            "</source>\n", 0);
 
 #endif /* WITH_YAJL */
 
-- 
2.25.0