[libvirt] [PATCH 06/12] qemu: command: Add helper to format -object strings from JSON representation

Michal Privoznik mprivozn at redhat.com
Thu Jan 29 13:11:27 UTC 2015


On 28.01.2015 11:30, Peter Krempa wrote:
> Unlike -device, qemu uses a JSON object to add backend "objects" via the
> monitor rather than the string that would be passed on the commandline.
> 
> To be able to reuse code parts that configure backends for various
> devices, this patch adds a helper that will allow generating the command
> line representations from the JSON property object.
> ---
>  src/qemu/qemu_command.c     | 111 +++++++++++++++++++++++++++++++++++++++++
>  src/qemu/qemu_command.h     |   4 ++
>  tests/Makefile.am           |  13 ++++-
>  tests/qemucommandutiltest.c | 118 ++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 244 insertions(+), 2 deletions(-)
>  create mode 100644 tests/qemucommandutiltest.c
> 
> diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
> index 100deed..6f298ac 100644
> --- a/src/qemu/qemu_command.c
> +++ b/src/qemu/qemu_command.c
> @@ -416,6 +416,117 @@ qemuDomainSupportsNetdev(virDomainDefPtr def,
>      return virQEMUCapsGet(qemuCaps, QEMU_CAPS_NETDEV);
>  }
> 
> +
> +static int
> +qemuBuildObjectCommandLinePropsInternal(const char *key,
> +                                        const virJSONValue *value,
> +                                        virBufferPtr buf,
> +                                        bool nested)
> +{
> +    virJSONValuePtr elem;
> +    virBitmapPtr bitmap = NULL;
> +    ssize_t pos = -1;
> +    ssize_t end;
> +    size_t i;
> +
> +    switch ((virJSONType) value->type) {
> +    case VIR_JSON_TYPE_STRING:
> +        virBufferAsprintf(buf, ",%s=%s", key, value->data.string);
> +        break;
> +
> +    case VIR_JSON_TYPE_NUMBER:
> +        virBufferAsprintf(buf, ",%s=%s", key, value->data.number);

So a number is a string? Me goes check the struct, and you're right. I
don't even ...

> +        break;
> +
> +    case VIR_JSON_TYPE_BOOLEAN:
> +        if (value->data.boolean)
> +            virBufferAsprintf(buf, ",%s=yes", key);
> +        else
> +            virBufferAsprintf(buf, ",%s=no", key);
> +
> +        break;
> +
> +    case VIR_JSON_TYPE_ARRAY:
> +        if (nested) {
> +            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                           _("nested -object property arrays are not supported"));
> +            return -1;
> +        }
> +
> +        if (virJSONValueGetArrayAsBitmap(value, &bitmap) == 0) {
> +            while ((pos = virBitmapNextSetBit(bitmap, pos)) > -1) {
> +                if ((end = virBitmapNextClearBit(bitmap, pos)) < 0)
> +                    end = virBitmapLastSetBit(bitmap) + 1;
> +
> +                if (end - 1 > pos) {
> +                    virBufferAsprintf(buf, ",%s=%zd-%zd", key, pos, end - 1);
> +                    pos = end;
> +                } else {
> +                    virBufferAsprintf(buf, ",%s=%zd", key, pos);
> +                }
> +            }
> +        } else {
> +            /* fallback, treat the array as a non-bitmap, adding the key
> +             * for each member */
> +            for (i = 0; i < virJSONValueArraySize(value); i++) {
> +                elem = virJSONValueArrayGet((virJSONValuePtr)value, i);
> +
> +                /* recurse to avoid duplicating code */
> +                if (qemuBuildObjectCommandLinePropsInternal(key, elem, buf,
> +                                                            true) < 0)
> +                    return -1;
> +            }
> +        }
> +        break;
> +
> +    case VIR_JSON_TYPE_OBJECT:
> +    case VIR_JSON_TYPE_NULL:
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("NULL and OBJECT JSON types can't be converted to "
> +                         "commandline string"));
> +        return -1;
> +    }
> +
> +    virBitmapFree(bitmap);
> +    return 0;
> +}
> +

Michal




More information about the libvir-list mailing list