diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1c170b6ded..948c7b58aa 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2204,6 +2204,7 @@ virProcessWait; # util/virqemu.h virQEMUBuildBufferEscapeComma; +virQEMUBuildCommandLineJSON; virQEMUBuildLuksOpts; virQEMUBuildObjectCommandlineFromJSON; diff --git a/src/util/virqemu.c b/src/util/virqemu.c index 7d181e19d3..a5d53852bd 100644 --- a/src/util/virqemu.c +++ b/src/util/virqemu.c @@ -33,10 +33,10 @@ VIR_LOG_INIT("util.qemu"); static int -virQEMUBuildObjectCommandLinePropsInternal(const char *key, - const virJSONValue *value, - virBufferPtr buf, - bool nested) +virQEMUBuildCommandLineJSONRecurse(const char *key, + const virJSONValue *value, + virBufferPtr buf, + bool nested) { virJSONValuePtr elem; virBitmapPtr bitmap = NULL; @@ -64,7 +64,8 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key, case VIR_JSON_TYPE_ARRAY: if (nested) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("nested -object property arrays are not supported")); + _("nested JSON array to commandline conversion is " + "not supported")); return -1; } @@ -87,8 +88,7 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key, elem = virJSONValueArrayGet((virJSONValuePtr)value, i); /* recurse to avoid duplicating code */ - if (virQEMUBuildObjectCommandLinePropsInternal(key, elem, buf, - true) < 0) + if (virQEMUBuildCommandLineJSONRecurse(key, elem, buf, true) < 0) return -1; } } @@ -108,11 +108,34 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key, static int -virQEMUBuildObjectCommandLineProps(const char *key, +virQEMUBuildCommandLineJSONIterate(const char *key, const virJSONValue *value, void *opaque) { - return virQEMUBuildObjectCommandLinePropsInternal(key, value, opaque, false); + return virQEMUBuildCommandLineJSONRecurse(key, value, opaque, false); +} + + +/** + * virQEMUBuildCommandLineJSON: + * @value: json object containing the value + * @buf: otuput buffer + * + * Formats JSON value object into command line parameters suitable for use with + * qemu. + * + * Returns 0 on success -1 on error. + */ +int +virQEMUBuildCommandLineJSON(const virJSONValue *value, + virBufferPtr buf) +{ + if (virJSONValueObjectForeachKeyValue(value, + virQEMUBuildCommandLineJSONIterate, + buf) < 0) + return -1; + + return 0; } @@ -126,9 +149,7 @@ virQEMUBuildObjectCommandlineFromJSON(const char *type, virBufferAsprintf(&buf, "%s,id=%s", type, alias); - if (virJSONValueObjectForeachKeyValue(props, - virQEMUBuildObjectCommandLineProps, - &buf) < 0) + if (virQEMUBuildCommandLineJSON(props, &buf) < 0) goto cleanup; if (virBufferCheckError(&buf) < 0) diff --git a/src/util/virqemu.h b/src/util/virqemu.h index eb75d1b287..efc6c42c4d 100644 --- a/src/util/virqemu.h +++ b/src/util/virqemu.h @@ -29,6 +29,9 @@ # include "virjson.h" # include "virstorageencryption.h" +int virQEMUBuildCommandLineJSON(const virJSONValue *value, + virBufferPtr buf); + char *virQEMUBuildObjectCommandlineFromJSON(const char *type, const char *alias, virJSONValuePtr props); diff --git a/tests/qemucommandutiltest.c b/tests/qemucommandutiltest.c index c02d1db1b4..21fef1c458 100644 --- a/tests/qemucommandutiltest.c +++ b/tests/qemucommandutiltest.c @@ -33,11 +33,12 @@ typedef struct } testQemuCommandBuildObjectFromJSONData; static int -testQemuCommandBuildObjectFromJSON(const void *opaque) +testQemuCommandBuildFromJSON(const void *opaque) { const testQemuCommandBuildObjectFromJSONData *data = opaque; virJSONValuePtr val = NULL; char *expect = NULL; + virBuffer buf = VIR_BUFFER_INITIALIZER; char *result = NULL; int ret = -1; @@ -46,13 +47,18 @@ testQemuCommandBuildObjectFromJSON(const void *opaque) return -1; } - if (virAsprintf(&expect, "testobject,id=testalias%s%s", - data->expectprops ? "," : "", - data->expectprops ? data->expectprops : "") < 0) + if (data->expectprops && + virAsprintf(&expect, ",%s", data->expectprops) < 0) return -1; - result = virQEMUBuildObjectCommandlineFromJSON("testobject", - "testalias", val); + if (virQEMUBuildCommandLineJSON(val, &buf) < 0) { + fprintf(stderr, + "\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n", + data->props); + goto cleanup; + } + + result = virBufferContentAndReset(&buf); if (STRNEQ_NULLABLE(expect, result)) { fprintf(stderr, "\nFailed to create object string. " @@ -80,14 +86,14 @@ mymain(void) return EXIT_AM_SKIP; #endif - virTestCounterReset("testQemuCommandBuildObjectFromJSON"); + virTestCounterReset("testQemuCommandBuildFromJSON"); #define DO_TEST_COMMAND_OBJECT_FROM_JSON(PROPS, EXPECT) \ do { \ data1.props = PROPS; \ data1.expectprops = EXPECT; \ if (virTestRun(virTestCounterNext(), \ - testQemuCommandBuildObjectFromJSON, \ + testQemuCommandBuildFromJSON, \ &data1) < 0) \ ret = -1; \ } while (0)