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

John Ferlan jferlan at redhat.com
Fri May 27 13:57:07 UTC 2016


Move the module from qemu_command.c to virjson.c and rename to
virJSONBuildObjectCommandline.

Nothing in the API is "specific" to the qemuBuild* processing and it'll
be useful to "share" with upcoming changes in the storage_backend which
will build a secret object for the 'qemu-img' command.

Also virJSONValueObjectForeachKeyValue and virJSONValueGetArrayAsBitmap
can be static helpers.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/libvirt_private.syms    |   3 +-
 src/qemu/qemu_command.c     | 124 +++-----------------------------------------
 src/qemu/qemu_command.h     |   4 --
 src/util/virjson.c          | 117 +++++++++++++++++++++++++++++++++++++++--
 src/util/virjson.h          |  12 ++---
 tests/qemucommandutiltest.c |   7 +--
 6 files changed, 129 insertions(+), 138 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e325168..e4bddd3 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1684,6 +1684,7 @@ virISCSIScanTargets;
 
 
 # util/virjson.h
+virJSONBuildObjectCommandline;
 virJSONValueArrayAppend;
 virJSONValueArrayGet;
 virJSONValueArraySize;
@@ -1691,7 +1692,6 @@ virJSONValueArraySteal;
 virJSONValueCopy;
 virJSONValueFree;
 virJSONValueFromString;
-virJSONValueGetArrayAsBitmap;
 virJSONValueGetBoolean;
 virJSONValueGetNumberDouble;
 virJSONValueGetNumberInt;
@@ -1726,7 +1726,6 @@ virJSONValueObjectAppendNumberUlong;
 virJSONValueObjectAppendString;
 virJSONValueObjectCreate;
 virJSONValueObjectCreateVArgs;
-virJSONValueObjectForeachKeyValue;
 virJSONValueObjectGet;
 virJSONValueObjectGetArray;
 virJSONValueObjectGetBoolean;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 368bd87..c55f42e 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -286,117 +286,8 @@ 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)
+qemuDeviceDriveHostAlias(virDomainDiskDefPtr disk)
 {
     char *ret;
 
@@ -680,8 +571,8 @@ qemuBuildObjectSecretCommandLine(virCommandPtr cmd,
     if (qemuBuildSecretInfoProps(secinfo, &type, &props) < 0)
         return -1;
 
-    if (!(tmp = qemuBuildObjectCommandlineFromJSON(type, secinfo->s.aes.alias,
-                                                   props)))
+    if (!(tmp = virJSONBuildObjectCommandline(type, secinfo->s.aes.alias,
+                                              props)))
         goto cleanup;
 
     virCommandAddArgList(cmd, "-object", tmp, NULL);
@@ -3226,9 +3117,8 @@ qemuBuildMemoryCellBackendStr(virDomainDefPtr def,
                                         &props, false)) < 0)
         goto cleanup;
 
-    if (!(*backendStr = qemuBuildObjectCommandlineFromJSON(backendType,
-                                                           alias,
-                                                           props)))
+    if (!(*backendStr = virJSONBuildObjectCommandline(backendType, alias,
+                                                      props)))
         goto cleanup;
 
     ret = rc;
@@ -3268,7 +3158,7 @@ qemuBuildMemoryDimmBackendStr(virDomainMemoryDefPtr mem,
                                   &backendType, &props, true) < 0)
         goto cleanup;
 
-    ret = qemuBuildObjectCommandlineFromJSON(backendType, alias, props);
+    ret = virJSONBuildObjectCommandline(backendType, alias, props);
 
  cleanup:
     VIR_FREE(alias);
@@ -5411,7 +5301,7 @@ qemuBuildRNGBackendStr(virDomainRNGDefPtr rng,
     if (qemuBuildRNGBackendProps(rng, qemuCaps, &type, &props) < 0)
         goto cleanup;
 
-    ret = qemuBuildObjectCommandlineFromJSON(type, alias, props);
+    ret = virJSONBuildObjectCommandline(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/virjson.c b/src/util/virjson.c
index 1022cfc..46e4661 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -1,7 +1,7 @@
 /*
  * virjson.c: JSON object parsing/formatting
  *
- * Copyright (C) 2009-2010, 2012-2015 Red Hat, Inc.
+ * Copyright (C) 2009-2010, 2012-2016 Red Hat, Inc.
  * Copyright (C) 2009 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -25,6 +25,7 @@
 
 #include "virjson.h"
 #include "viralloc.h"
+#include "virbuffer.h"
 #include "virerror.h"
 #include "virlog.h"
 #include "virstring.h"
@@ -1000,7 +1001,7 @@ virJSONValueGetBoolean(virJSONValuePtr val,
  * Returns 0 on success and fills @bitmap; -1 on error and  @bitmap is set to
  * NULL.
  */
-int
+static int
 virJSONValueGetArrayAsBitmap(const virJSONValue *val,
                              virBitmapPtr *bitmap)
 {
@@ -1219,7 +1220,7 @@ virJSONValueObjectIsNull(virJSONValuePtr object,
  * Returns 0 if all elements were iterated, -2 if @cb returned negative value
  * during iteration and -1 on generic errors.
  */
-int
+static int
 virJSONValueObjectForeachKeyValue(virJSONValuePtr object,
                                   virJSONValueObjectIteratorFunc cb,
                                   void *opaque)
@@ -1817,3 +1818,113 @@ virJSONValueToString(virJSONValuePtr object ATTRIBUTE_UNUSED,
     return NULL;
 }
 #endif
+
+
+static int
+virJSONBuildObjectCommandLinePropsInternal(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 (virJSONBuildObjectCommandLinePropsInternal(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
+virJSONBuildObjectCommandLineProps(const char *key,
+                                   const virJSONValue *value,
+                                   void *opaque)
+{
+    return virJSONBuildObjectCommandLinePropsInternal(key, value, opaque, false);
+}
+
+
+char *
+virJSONBuildObjectCommandline(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,
+                                          virJSONBuildObjectCommandLineProps,
+                                          &buf) < 0)
+        goto cleanup;
+
+    if (virBufferCheckError(&buf) < 0)
+        goto cleanup;
+
+    ret = virBufferContentAndReset(&buf);
+
+ cleanup:
+    virBufferFreeAndReset(&buf);
+    return ret;
+}
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 66ed48a..eb7d097 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -1,7 +1,7 @@
 /*
  * virjson.h: JSON object parsing/formatting
  *
- * Copyright (C) 2009, 2012-2015 Red Hat, Inc.
+ * 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
@@ -129,8 +129,6 @@ int virJSONValueGetNumberLong(virJSONValuePtr object, long long *value);
 int virJSONValueGetNumberUlong(virJSONValuePtr object, unsigned long long *value);
 int virJSONValueGetNumberDouble(virJSONValuePtr object, double *value);
 int virJSONValueGetBoolean(virJSONValuePtr object, bool *value);
-int virJSONValueGetArrayAsBitmap(const virJSONValue *val, virBitmapPtr *bitmap)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 bool virJSONValueIsNull(virJSONValuePtr object);
 virJSONValuePtr virJSONValueObjectGetObject(virJSONValuePtr object,
                                             const char *key);
@@ -167,10 +165,10 @@ typedef int (*virJSONValueObjectIteratorFunc)(const char *key,
                                               const virJSONValue *value,
                                               void *opaque);
 
-int virJSONValueObjectForeachKeyValue(virJSONValuePtr object,
-                                      virJSONValueObjectIteratorFunc cb,
-                                      void *opaque);
-
 virJSONValuePtr virJSONValueCopy(virJSONValuePtr in);
 
+char *virJSONBuildObjectCommandline(const char *type,
+                                    const char *alias,
+                                    virJSONValuePtr props);
+
 #endif /* __VIR_JSON_H_ */
diff --git a/tests/qemucommandutiltest.c b/tests/qemucommandutiltest.c
index bd457f8..d1a73cf 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,7 +19,6 @@
 
 #include <config.h>
 
-#include "qemu/qemu_command.h"
 #include "util/virjson.h"
 #include "testutils.h"
 #include "testutilsqemu.h"
@@ -51,9 +50,7 @@ testQemuCommandBuildObjectFromJSON(const void *opaque)
                     data->expectprops ? data->expectprops : "") < 0)
         return -1;
 
-    result = qemuBuildObjectCommandlineFromJSON("testobject",
-                                                "testalias",
-                                                val);
+    result = virJSONBuildObjectCommandline("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