mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-03 10:25:16 +00:00
qemu: Create common code for JSON "query-block" call
Reduce some cut-n-paste code by creating common helper. Make use of the recently added virJSONValueObjectStealArray to grab the devices list as part of the common code (we we can Free the reply) and return devices for each of the callers to continue to parse. NB: This also adds error checking to qemuMonitorJSONDiskNameLookup Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
parent
ebf8b783bf
commit
b0ab72bd43
@ -1772,32 +1772,52 @@ qemuMonitorJSONSetMemoryStatsPeriod(qemuMonitorPtr mon,
|
||||
}
|
||||
|
||||
|
||||
/* qemuMonitorJSONQueryBlock:
|
||||
* @mon: Monitor pointer
|
||||
*
|
||||
* This helper will attempt to make a "query-block" call and check for
|
||||
* errors before returning with the reply.
|
||||
*
|
||||
* Returns: NULL on error, reply on success
|
||||
*/
|
||||
static virJSONValuePtr
|
||||
qemuMonitorJSONQueryBlock(qemuMonitorPtr mon)
|
||||
{
|
||||
virJSONValuePtr cmd;
|
||||
virJSONValuePtr reply = NULL;
|
||||
virJSONValuePtr devices = NULL;
|
||||
|
||||
if (!(cmd = qemuMonitorJSONMakeCommand("query-block", NULL)))
|
||||
return NULL;
|
||||
|
||||
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0 ||
|
||||
qemuMonitorJSONCheckError(cmd, reply) < 0)
|
||||
goto cleanup;
|
||||
|
||||
if (!(devices = virJSONValueObjectStealArray(reply, "return"))) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
_("query-block reply was missing device list"));
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
virJSONValueFree(cmd);
|
||||
virJSONValueFree(reply);
|
||||
return devices;
|
||||
}
|
||||
|
||||
|
||||
int qemuMonitorJSONGetBlockInfo(qemuMonitorPtr mon,
|
||||
virHashTablePtr table)
|
||||
{
|
||||
int ret = -1;
|
||||
size_t i;
|
||||
|
||||
virJSONValuePtr cmd = qemuMonitorJSONMakeCommand("query-block",
|
||||
NULL);
|
||||
virJSONValuePtr reply = NULL;
|
||||
virJSONValuePtr devices;
|
||||
|
||||
if (!cmd)
|
||||
if (!(devices = qemuMonitorJSONQueryBlock(mon)))
|
||||
return -1;
|
||||
|
||||
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
|
||||
goto cleanup;
|
||||
|
||||
if (qemuMonitorJSONCheckError(cmd, reply) < 0)
|
||||
goto cleanup;
|
||||
|
||||
if (!(devices = virJSONValueObjectGetArray(reply, "return"))) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
_("block info reply was missing device list"));
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
for (i = 0; i < virJSONValueArraySize(devices); i++) {
|
||||
virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
|
||||
struct qemuDomainDiskInfo *info;
|
||||
@ -1858,8 +1878,7 @@ int qemuMonitorJSONGetBlockInfo(qemuMonitorPtr mon,
|
||||
|
||||
ret = 0;
|
||||
cleanup:
|
||||
virJSONValueFree(cmd);
|
||||
virJSONValueFree(reply);
|
||||
virJSONValueFree(devices);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -2056,27 +2075,12 @@ qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
|
||||
bool backingChain)
|
||||
{
|
||||
int ret = -1;
|
||||
int rc;
|
||||
size_t i;
|
||||
virJSONValuePtr cmd;
|
||||
virJSONValuePtr reply = NULL;
|
||||
virJSONValuePtr devices;
|
||||
|
||||
if (!(cmd = qemuMonitorJSONMakeCommand("query-block", NULL)))
|
||||
if (!(devices = qemuMonitorJSONQueryBlock(mon)))
|
||||
return -1;
|
||||
|
||||
if ((rc = qemuMonitorJSONCommand(mon, cmd, &reply)) < 0)
|
||||
goto cleanup;
|
||||
|
||||
if (qemuMonitorJSONCheckError(cmd, reply) < 0)
|
||||
goto cleanup;
|
||||
|
||||
if (!(devices = virJSONValueObjectGetArray(reply, "return"))) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
_("query-block reply was missing device list"));
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
for (i = 0; i < virJSONValueArraySize(devices); i++) {
|
||||
virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
|
||||
virJSONValuePtr inserted;
|
||||
@ -2111,8 +2115,7 @@ qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
|
||||
ret = 0;
|
||||
|
||||
cleanup:
|
||||
virJSONValueFree(cmd);
|
||||
virJSONValueFree(reply);
|
||||
virJSONValueFree(devices);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -3987,22 +3990,11 @@ qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon,
|
||||
virStorageSourcePtr target)
|
||||
{
|
||||
char *ret = NULL;
|
||||
virJSONValuePtr cmd = NULL;
|
||||
virJSONValuePtr reply = NULL;
|
||||
virJSONValuePtr devices;
|
||||
size_t i;
|
||||
|
||||
cmd = qemuMonitorJSONMakeCommand("query-block", NULL);
|
||||
if (!cmd)
|
||||
if (!(devices = qemuMonitorJSONQueryBlock(mon)))
|
||||
return NULL;
|
||||
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
|
||||
goto cleanup;
|
||||
|
||||
if (!(devices = virJSONValueObjectGetArray(reply, "return"))) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
_("block info reply was missing device list"));
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
for (i = 0; i < virJSONValueArraySize(devices); i++) {
|
||||
virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
|
||||
@ -4038,8 +4030,7 @@ qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon,
|
||||
device);
|
||||
|
||||
cleanup:
|
||||
virJSONValueFree(cmd);
|
||||
virJSONValueFree(reply);
|
||||
virJSONValueFree(devices);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user