[libvirt] [PATCH v2 1/6] qemu: Move and rename qemuBuildObjectCommandlineFromJSON

John Ferlan jferlan at redhat.com
Tue May 31 22:39:35 UTC 2016


Move the module from qemu_command.c to a new module virqemu.c and
rename the API to virQEMUBuildObjectCommandline.

This API will then be shareable with qemu-img and the need to build
a security object for luks support.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 po/POTFILES.in              |   1 +
 src/Makefile.am             |   1 +
 src/libvirt_private.syms    |   4 ++
 src/qemu/qemu_command.c     | 126 +++------------------------------------
 src/qemu/qemu_command.h     |   4 --
 src/util/virqemu.c          | 142 ++++++++++++++++++++++++++++++++++++++++++++
 src/util/virqemu.h          |  34 +++++++++++
 tests/qemucommandutiltest.c |   9 ++-
 8 files changed, 195 insertions(+), 126 deletions(-)
 create mode 100644 src/util/virqemu.c
 create mode 100644 src/util/virqemu.h

diff --git a/po/POTFILES.in b/po/POTFILES.in
index 0d92448..2a6fae4 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -225,6 +225,7 @@ src/util/virpidfile.c
 src/util/virpolkit.c
 src/util/virportallocator.c
 src/util/virprocess.c
+src/util/virqemu.c
 src/util/virrandom.c
 src/util/virrotatingfile.c
 src/util/virscsi.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 12b66c2..f3c9a14 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -153,6 +153,7 @@ UTIL_SOURCES =							\
 		util/virportallocator.c util/virportallocator.h \
 		util/virprobe.h					\
 		util/virprocess.c util/virprocess.h		\
+		util/virqemu.c util/virqemu.h			\
 		util/virrandom.h util/virrandom.c		\
 		util/virrotatingfile.h util/virrotatingfile.c   \
 		util/virscsi.c util/virscsi.h			\
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e325168..d25baae 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2142,6 +2142,10 @@ virProcessTranslateStatus;
 virProcessWait;
 
 
+# util/virqemu.h
+virQEMUBuildObjectCommandlineFromJSON;
+
+
 # util/virrandom.h
 virRandom;
 virRandomBits;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 368bd87..1455c0d 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -37,6 +37,7 @@
 #include "virfile.h"
 #include "virnetdev.h"
 #include "virnetdevbridge.h"
+#include "virqemu.h"
 #include "virstring.h"
 #include "virtime.h"
 #include "viruuid.h"
@@ -286,116 +287,6 @@ qemuVirCommandGetDevSet(virCommandPtr cmd, int fd)
 }
 
 
-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);
-        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;
-}
-
-
-static int
-qemuBuildObjectCommandLineProps(const char *key,
-                                const virJSONValue *value,
-                                void *opaque)
-{
-    return qemuBuildObjectCommandLinePropsInternal(key, value, opaque, false);
-}
-
-
-char *
-qemuBuildObjectCommandlineFromJSON(const char *type,
-                                   const char *alias,
-                                   virJSONValuePtr props)
-{
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
-    char *ret = NULL;
-
-    virBufferAsprintf(&buf, "%s,id=%s", type, alias);
-
-    if (virJSONValueObjectForeachKeyValue(props,
-                                          qemuBuildObjectCommandLineProps,
-                                          &buf) < 0)
-        goto cleanup;
-
-    if (virBufferCheckError(&buf) < 0)
-        goto cleanup;
-
-    ret = virBufferContentAndReset(&buf);
-
- cleanup:
-    virBufferFreeAndReset(&buf);
-    return ret;
-}
-
-
 char *qemuDeviceDriveHostAlias(virDomainDiskDefPtr disk)
 {
     char *ret;
@@ -680,8 +571,9 @@ qemuBuildObjectSecretCommandLine(virCommandPtr cmd,
     if (qemuBuildSecretInfoProps(secinfo, &type, &props) < 0)
         return -1;
 
-    if (!(tmp = qemuBuildObjectCommandlineFromJSON(type, secinfo->s.aes.alias,
-                                                   props)))
+    if (!(tmp = virQEMUBuildObjectCommandlineFromJSON(type,
+                                                      secinfo->s.aes.alias,
+                                                      props)))
         goto cleanup;
 
     virCommandAddArgList(cmd, "-object", tmp, NULL);
@@ -3226,9 +3118,9 @@ qemuBuildMemoryCellBackendStr(virDomainDefPtr def,
                                         &props, false)) < 0)
         goto cleanup;
 
-    if (!(*backendStr = qemuBuildObjectCommandlineFromJSON(backendType,
-                                                           alias,
-                                                           props)))
+    if (!(*backendStr = virQEMUBuildObjectCommandlineFromJSON(backendType,
+                                                              alias,
+                                                              props)))
         goto cleanup;
 
     ret = rc;
@@ -3268,7 +3160,7 @@ qemuBuildMemoryDimmBackendStr(virDomainMemoryDefPtr mem,
                                   &backendType, &props, true) < 0)
         goto cleanup;
 
-    ret = qemuBuildObjectCommandlineFromJSON(backendType, alias, props);
+    ret = virQEMUBuildObjectCommandlineFromJSON(backendType, alias, props);
 
  cleanup:
     VIR_FREE(alias);
@@ -5411,7 +5303,7 @@ qemuBuildRNGBackendStr(virDomainRNGDefPtr rng,
     if (qemuBuildRNGBackendProps(rng, qemuCaps, &type, &props) < 0)
         goto cleanup;
 
-    ret = qemuBuildObjectCommandlineFromJSON(type, alias, props);
+    ret = virQEMUBuildObjectCommandlineFromJSON(type, alias, props);
 
  cleanup:
     VIR_FREE(alias);
diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index 1c22705..d2b7fa7 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -44,10 +44,6 @@
 
 VIR_ENUM_DECL(qemuVideo)
 
-char *qemuBuildObjectCommandlineFromJSON(const char *type,
-                                         const char *alias,
-                                         virJSONValuePtr props);
-
 virCommandPtr qemuBuildCommandLine(virQEMUDriverPtr driver,
                                    virLogManagerPtr logManager,
                                    virDomainDefPtr def,
diff --git a/src/util/virqemu.c b/src/util/virqemu.c
new file mode 100644
index 0000000..f87e20b
--- /dev/null
+++ b/src/util/virqemu.c
@@ -0,0 +1,142 @@
+/*
+ * virqemu.c: QEMU object parsing/formatting
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+#include <config.h>
+
+#include "virbuffer.h"
+#include "virerror.h"
+#include "virlog.h"
+#include "virqemu.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("util.qemu");
+
+
+static int
+virQEMUBuildObjectCommandLinePropsInternal(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);
+        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 (virQEMUBuildObjectCommandLinePropsInternal(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;
+}
+
+
+static int
+virQEMUBuildObjectCommandLineProps(const char *key,
+                                   const virJSONValue *value,
+                                   void *opaque)
+{
+    return virQEMUBuildObjectCommandLinePropsInternal(key, value, opaque, false);
+}
+
+
+char *
+virQEMUBuildObjectCommandlineFromJSON(const char *type,
+                                      const char *alias,
+                                      virJSONValuePtr props)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char *ret = NULL;
+
+    virBufferAsprintf(&buf, "%s,id=%s", type, alias);
+
+    if (virJSONValueObjectForeachKeyValue(props,
+                                          virQEMUBuildObjectCommandLineProps,
+                                          &buf) < 0)
+        goto cleanup;
+
+    if (virBufferCheckError(&buf) < 0)
+        goto cleanup;
+
+    ret = virBufferContentAndReset(&buf);
+
+ cleanup:
+    virBufferFreeAndReset(&buf);
+    return ret;
+}
diff --git a/src/util/virqemu.h b/src/util/virqemu.h
new file mode 100644
index 0000000..0a72202
--- /dev/null
+++ b/src/util/virqemu.h
@@ -0,0 +1,34 @@
+/*
+ * virqemu.h: qemu specific object parsing/formatting
+ *
+ * Copyright (C) 2009, 2012-2016 Red Hat, Inc.
+ * Copyright (C) 2009 Daniel P. Berrange
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+#ifndef __VIR_QEMU_H_
+# define __VIR_QEMU_H_
+
+# include "internal.h"
+# include "virjson.h"
+
+char *virQEMUBuildObjectCommandlineFromJSON(const char *type,
+                                            const char *alias,
+                                            virJSONValuePtr props);
+
+#endif /* __VIR_QEMU_H_ */
diff --git a/tests/qemucommandutiltest.c b/tests/qemucommandutiltest.c
index bd457f8..5072a62 100644
--- a/tests/qemucommandutiltest.c
+++ b/tests/qemucommandutiltest.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Red Hat, Inc.
+ * Copyright (C) 2015-2016 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -19,8 +19,8 @@
 
 #include <config.h>
 
-#include "qemu/qemu_command.h"
 #include "util/virjson.h"
+#include "util/virqemu.h"
 #include "testutils.h"
 #include "testutilsqemu.h"
 
@@ -51,9 +51,8 @@ testQemuCommandBuildObjectFromJSON(const void *opaque)
                     data->expectprops ? data->expectprops : "") < 0)
         return -1;
 
-    result = qemuBuildObjectCommandlineFromJSON("testobject",
-                                                "testalias",
-                                                val);
+    result = virQEMUBuildObjectCommandlineFromJSON("testobject",
+                                                   "testalias", val);
 
     if (STRNEQ_NULLABLE(expect, result)) {
         fprintf(stderr, "\nFailed to create object string. "
-- 
2.5.5




More information about the libvir-list mailing list