The code is written generic enough to be reused. Move it into a separate function. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/qemu/qemu_capabilities.c | 62 ++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 41132b6bcb..c3fb0d704f 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -1243,14 +1243,19 @@ static struct virQEMUCapsStringFlags virQEMUCapsQMPSchemaQueries[] = { { "blockdev-add/arg-type/+qcow2/encrypt/+luks/key-secret", QEMU_CAPS_QCOW2_LUKS }, }; -struct virQEMUCapsObjectTypeProps { +typedef struct _virQEMUCapsObjectTypeProps virQEMUCapsObjectTypeProps; +struct _virQEMUCapsObjectTypeProps { const char *type; struct virQEMUCapsStringFlags *props; size_t nprops; int capsCondition; }; -static struct virQEMUCapsObjectTypeProps virQEMUCapsDeviceProps[] = { +typedef int (*virQEMUCapsObjectTypePropsCB)(qemuMonitorPtr mon, + const char *type, + char ***props); + +static virQEMUCapsObjectTypeProps virQEMUCapsDeviceProps[] = { { "virtio-blk-pci", virQEMUCapsDevicePropsVirtioBlk, ARRAY_CARDINALITY(virQEMUCapsDevicePropsVirtioBlk), -1 }, @@ -2060,6 +2065,35 @@ virQEMUCapsProbeQMPEvents(virQEMUCapsPtr qemuCaps, return 0; } +static int +virQEMUCapsProbeQMPGenericProps(virQEMUCapsPtr qemuCaps, + qemuMonitorPtr mon, + virQEMUCapsObjectTypeProps *props, + size_t nprops, + virQEMUCapsObjectTypePropsCB propsGetCB) +{ + int nvalues; + char **values; + size_t i; + + for (i = 0; i < nprops; i++) { + const char *type = props[i].type; + int cap = props[i].capsCondition; + + if (cap >= 0 && !virQEMUCapsGet(qemuCaps, cap)) + continue; + + if ((nvalues = propsGetCB(mon, type, &values)) < 0) + return -1; + virQEMUCapsProcessStringFlags(qemuCaps, + props[i].nprops, + props[i].props, + nvalues, values); + virStringListFreeCount(values, nvalues); + } + + return 0; +} static int virQEMUCapsProbeQMPDevices(virQEMUCapsPtr qemuCaps, @@ -2067,7 +2101,6 @@ virQEMUCapsProbeQMPDevices(virQEMUCapsPtr qemuCaps, { int nvalues; char **values; - size_t i; if ((nvalues = qemuMonitorGetObjectTypes(mon, &values)) < 0) return -1; @@ -2077,23 +2110,12 @@ virQEMUCapsProbeQMPDevices(virQEMUCapsPtr qemuCaps, nvalues, values); virStringListFreeCount(values, nvalues); - for (i = 0; i < ARRAY_CARDINALITY(virQEMUCapsDeviceProps); i++) { - const char *device = virQEMUCapsDeviceProps[i].type; - int cap = virQEMUCapsDeviceProps[i].capsCondition; - - if (cap >= 0 && !virQEMUCapsGet(qemuCaps, cap)) - continue; - - if ((nvalues = qemuMonitorGetDeviceProps(mon, - device, - &values)) < 0) - return -1; - virQEMUCapsProcessStringFlags(qemuCaps, - virQEMUCapsDeviceProps[i].nprops, - virQEMUCapsDeviceProps[i].props, - nvalues, values); - virStringListFreeCount(values, nvalues); - } + if (virQEMUCapsProbeQMPGenericProps(qemuCaps, + mon, + virQEMUCapsDeviceProps, + ARRAY_CARDINALITY(virQEMUCapsDeviceProps), + qemuMonitorGetDeviceProps) < 0) + return -1; return 0; } -- 2.16.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list