]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
ch: refactor virCHMonitorBuildDiskJson
authorStefan Kober <stefan.kober@cyberus-technology.de>
Thu, 4 Sep 2025 12:10:26 +0000 (14:10 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 8 Sep 2025 14:40:08 +0000 (16:40 +0200)
Refactor BuildDiskJson to return a virJSONValue instead of adding the
disk json to an json array. This makes the function reusable for
hotplugging disks.

On-behalf-of: SAP stefan.kober@sap.com
Signed-off-by: Stefan Kober <stefan.kober@cyberus-technology.de>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/ch/ch_monitor.c

index d369236183667c11943f209ddf1804d0b4c3280b..89cd23005346243ed373716fb77e44c97c0dd2e8 100644 (file)
@@ -234,43 +234,41 @@ virCHMonitorBuildMemoryJson(virJSONValue *content, virDomainDef *vmdef)
     return 0;
 }
 
-static int
-virCHMonitorBuildDiskJson(virJSONValue *disks, virDomainDiskDef *diskdef)
+static virJSONValue*
+virCHMonitorBuildDiskJson(virDomainDiskDef *diskdef)
 {
     g_autoptr(virJSONValue) disk = virJSONValueNewObject();
 
     if (!diskdef->src)
-        return -1;
+        return NULL;
 
     switch (diskdef->src->type) {
     case VIR_STORAGE_TYPE_FILE:
         if (!diskdef->src->path) {
             virReportError(VIR_ERR_INVALID_ARG, "%s",
                            _("Missing disk file path in domain"));
-            return -1;
+            return NULL;
         }
         if (!diskdef->info.alias) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("Missing disk alias"));
-            return -1;
+            return NULL;
         }
         if (diskdef->bus != VIR_DOMAIN_DISK_BUS_VIRTIO) {
             virReportError(VIR_ERR_INVALID_ARG,
                            _("Only virtio bus types are supported for '%1$s'"),
                            diskdef->src->path);
-            return -1;
+            return NULL;
         }
         if (virJSONValueObjectAppendString(disk, "path", diskdef->src->path) < 0)
-            return -1;
+            return NULL;
         if (diskdef->src->readonly) {
             if (virJSONValueObjectAppendBoolean(disk, "readonly", true) < 0)
-                return -1;
+                return NULL;
         }
         if (virJSONValueObjectAppendString(disk, "id", diskdef->info.alias) < 0) {
-            return -1;
+            return NULL;
         }
-        if (virJSONValueArrayAppend(disks, &disk) < 0)
-            return -1;
 
         break;
     case VIR_STORAGE_TYPE_NONE:
@@ -284,10 +282,10 @@ virCHMonitorBuildDiskJson(virJSONValue *disks, virDomainDiskDef *diskdef)
     case VIR_STORAGE_TYPE_LAST:
     default:
         virReportEnumRangeError(virStorageType, diskdef->src->type);
-        return -1;
+        return NULL;
     }
 
-    return 0;
+    return g_steal_pointer(&disk);
 }
 
 static int
@@ -300,7 +298,11 @@ virCHMonitorBuildDisksJson(virJSONValue *content, virDomainDef *vmdef)
         disks = virJSONValueNewArray();
 
         for (i = 0; i < vmdef->ndisks; i++) {
-            if (virCHMonitorBuildDiskJson(disks, vmdef->disks[i]) < 0)
+            g_autoptr(virJSONValue) disk = NULL;
+
+            if ((disk = virCHMonitorBuildDiskJson(vmdef->disks[i])) == NULL)
+                return -1;
+            if (virJSONValueArrayAppend(disks, &disk) < 0)
                 return -1;
         }
         if (virJSONValueObjectAppend(content, "disks", &disks) < 0)