]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: block: Refactor logic in qemuBlockStorageSourceGetBlockdevProps
authorPeter Krempa <pkrempa@redhat.com>
Tue, 12 Sep 2023 14:36:31 +0000 (16:36 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 17 Oct 2023 12:16:15 +0000 (14:16 +0200)
Restructure the conditions so that we can use virJSONValueObjectAdd with
a clearer logic for backing store control.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/qemu/qemu_block.c

index 7e870baa2fd86e9f33393f63a7dc8ada53080cdb..9b6d901e8c3ad6712e20ca2ad90b121ee038c1eb 100644 (file)
@@ -1309,39 +1309,41 @@ qemuBlockStorageSourceGetBlockdevProps(virStorageSource *src,
                                        virStorageSource *backingStore)
 {
     g_autoptr(virJSONValue) props = NULL;
+    const char *backingFormatterStr = NULL;
+    const char *backingNodename = NULL;
     const char *storagenode = src->nodestorage;
 
     if (qemuBlockStorageSourceNeedsStorageSliceLayer(src))
         storagenode = src->sliceStorage->nodename;
 
-    if (!(props = qemuBlockStorageSourceGetBlockdevFormatProps(src)))
-        return NULL;
-
-    if (virJSONValueObjectAppendString(props, "file", storagenode) < 0)
+    if (virStorageSourceIsBacking(backingStore) &&
+        src->format < VIR_STORAGE_FILE_BACKING) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("storage format '%1$s' does not support backing store"),
+                       virStorageFileFormatTypeToString(src->format));
         return NULL;
+    }
 
-    if (backingStore) {
-        if (src->format >= VIR_STORAGE_FILE_BACKING) {
-            if (virStorageSourceIsBacking(backingStore)) {
-                if (virJSONValueObjectAppendString(props, "backing",
-                                                   backingStore->nodeformat) < 0)
-                    return NULL;
-            } else {
-                /* chain is terminated, indicate that no detection should happen
-                 * in qemu */
-                if (virJSONValueObjectAppendNull(props, "backing") < 0)
-                    return NULL;
-            }
+    if (backingStore &&
+        src->format >= VIR_STORAGE_FILE_BACKING) {
+        if (virStorageSourceIsBacking(backingStore)) {
+            backingFormatterStr = "s:backing";
+            backingNodename = backingStore->nodeformat;
         } else {
-            if (virStorageSourceIsBacking(backingStore)) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                               _("storage format '%1$s' does not support backing store"),
-                               virStorageFileFormatTypeToString(src->format));
-                return NULL;
-            }
+            /* chain is terminated, indicate that no detection should happen in qemu */
+            backingFormatterStr = "n:backing";
         }
     }
 
+    if (!(props = qemuBlockStorageSourceGetBlockdevFormatProps(src)))
+        return NULL;
+
+    if (virJSONValueObjectAdd(&props,
+                              "s:file", storagenode,
+                              backingFormatterStr, backingNodename,
+                              NULL) < 0)
+        return 0;
+
     return g_steal_pointer(&props);
 }