提交 b0ab72bd 编写于 作者: J John Ferlan

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: NJohn Ferlan <jferlan@redhat.com>
...@@ -1772,32 +1772,52 @@ qemuMonitorJSONSetMemoryStatsPeriod(qemuMonitorPtr mon, ...@@ -1772,32 +1772,52 @@ qemuMonitorJSONSetMemoryStatsPeriod(qemuMonitorPtr mon,
} }
int qemuMonitorJSONGetBlockInfo(qemuMonitorPtr mon, /* qemuMonitorJSONQueryBlock:
virHashTablePtr table) * @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)
{ {
int ret = -1; virJSONValuePtr cmd;
size_t i;
virJSONValuePtr cmd = qemuMonitorJSONMakeCommand("query-block",
NULL);
virJSONValuePtr reply = NULL; virJSONValuePtr reply = NULL;
virJSONValuePtr devices; virJSONValuePtr devices = NULL;
if (!cmd) if (!(cmd = qemuMonitorJSONMakeCommand("query-block", NULL)))
return -1; return NULL;
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
goto cleanup;
if (qemuMonitorJSONCheckError(cmd, reply) < 0) if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0 ||
qemuMonitorJSONCheckError(cmd, reply) < 0)
goto cleanup; goto cleanup;
if (!(devices = virJSONValueObjectGetArray(reply, "return"))) { if (!(devices = virJSONValueObjectStealArray(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("block info reply was missing device list")); _("query-block reply was missing device list"));
goto cleanup; goto cleanup;
} }
cleanup:
virJSONValueFree(cmd);
virJSONValueFree(reply);
return devices;
}
int qemuMonitorJSONGetBlockInfo(qemuMonitorPtr mon,
virHashTablePtr table)
{
int ret = -1;
size_t i;
virJSONValuePtr devices;
if (!(devices = qemuMonitorJSONQueryBlock(mon)))
return -1;
for (i = 0; i < virJSONValueArraySize(devices); i++) { for (i = 0; i < virJSONValueArraySize(devices); i++) {
virJSONValuePtr dev = virJSONValueArrayGet(devices, i); virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
struct qemuDomainDiskInfo *info; struct qemuDomainDiskInfo *info;
...@@ -1858,8 +1878,7 @@ int qemuMonitorJSONGetBlockInfo(qemuMonitorPtr mon, ...@@ -1858,8 +1878,7 @@ int qemuMonitorJSONGetBlockInfo(qemuMonitorPtr mon,
ret = 0; ret = 0;
cleanup: cleanup:
virJSONValueFree(cmd); virJSONValueFree(devices);
virJSONValueFree(reply);
return ret; return ret;
} }
...@@ -2056,27 +2075,12 @@ qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon, ...@@ -2056,27 +2075,12 @@ qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
bool backingChain) bool backingChain)
{ {
int ret = -1; int ret = -1;
int rc;
size_t i; size_t i;
virJSONValuePtr cmd;
virJSONValuePtr reply = NULL;
virJSONValuePtr devices; virJSONValuePtr devices;
if (!(cmd = qemuMonitorJSONMakeCommand("query-block", NULL))) if (!(devices = qemuMonitorJSONQueryBlock(mon)))
return -1; 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++) { for (i = 0; i < virJSONValueArraySize(devices); i++) {
virJSONValuePtr dev = virJSONValueArrayGet(devices, i); virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
virJSONValuePtr inserted; virJSONValuePtr inserted;
...@@ -2111,8 +2115,7 @@ qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon, ...@@ -2111,8 +2115,7 @@ qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
ret = 0; ret = 0;
cleanup: cleanup:
virJSONValueFree(cmd); virJSONValueFree(devices);
virJSONValueFree(reply);
return ret; return ret;
} }
...@@ -3987,22 +3990,11 @@ qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon, ...@@ -3987,22 +3990,11 @@ qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon,
virStorageSourcePtr target) virStorageSourcePtr target)
{ {
char *ret = NULL; char *ret = NULL;
virJSONValuePtr cmd = NULL;
virJSONValuePtr reply = NULL;
virJSONValuePtr devices; virJSONValuePtr devices;
size_t i; size_t i;
cmd = qemuMonitorJSONMakeCommand("query-block", NULL); if (!(devices = qemuMonitorJSONQueryBlock(mon)))
if (!cmd)
return NULL; 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++) { for (i = 0; i < virJSONValueArraySize(devices); i++) {
virJSONValuePtr dev = virJSONValueArrayGet(devices, i); virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
...@@ -4038,8 +4030,7 @@ qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon, ...@@ -4038,8 +4030,7 @@ qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon,
device); device);
cleanup: cleanup:
virJSONValueFree(cmd); virJSONValueFree(devices);
virJSONValueFree(reply);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册
反馈
建议
客服 返回
顶部