From e285053dc730bef0e37e627cb58314741f611e6d Mon Sep 17 00:00:00 2001 From: Stefan Kober Date: Thu, 4 Sep 2025 14:10:26 +0200 Subject: [PATCH] ch: refactor virCHMonitorBuildDiskJson 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 Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- src/ch/ch_monitor.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c index d369236183..89cd230053 100644 --- a/src/ch/ch_monitor.c +++ b/src/ch/ch_monitor.c @@ -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) -- 2.47.3