[PATCH 01/18] qemuBlockStorageSourceGetBackendProps: Convert boolean arguments to flags

Peter Krempa pkrempa at redhat.com
Fri Jul 10 14:33:22 UTC 2020


Upcoming commit will need to add another flag for the function so
convert it to a bitwise-or'd array of flags to prevent having 4
booleans.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_block.c   | 32 +++++++++++++++++++++-----------
 src/qemu/qemu_block.h   | 10 +++++++---
 src/qemu/qemu_command.c |  3 ++-
 tests/qemublocktest.c   | 18 ++++++++++++------
 4 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index a2eabbcd64..10ddf53b3b 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -1052,26 +1052,32 @@ qemuBlockStorageSourceGetBlockdevGetCacheProps(virStorageSourcePtr src,
 /**
  * qemuBlockStorageSourceGetBackendProps:
  * @src: disk source
- * @legacy: use legacy formatting of attributes (for -drive / old qemus)
- * @onlytarget: omit any data which does not identify the image itself
- * @autoreadonly: use the auto-read-only feature of qemu
+ * @flags: bitwise-or of qemuBlockStorageSourceBackendPropsFlags
+ *
+ * Flags:
+ *   QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY:
+ *      use legacy formatting of attributes (for -drive / old qemus)
+ *  QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY:
+ *      omit any data which does not identify the image itself
+ *  QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY:
+ *      use the auto-read-only feature of qemu
  *
  * Creates a JSON object describing the underlying storage or protocol of a
  * storage source. Returns NULL on error and reports an appropriate error message.
  */
 virJSONValuePtr
 qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
-                                      bool legacy,
-                                      bool onlytarget,
-                                      bool autoreadonly)
+                                      unsigned int flags)
 {
     int actualType = virStorageSourceGetActualType(src);
     g_autoptr(virJSONValue) fileprops = NULL;
     const char *driver = NULL;
     virTristateBool aro = VIR_TRISTATE_BOOL_ABSENT;
     virTristateBool ro = VIR_TRISTATE_BOOL_ABSENT;
+    bool onlytarget = flags & QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY;
+    bool legacy = flags & QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY;

-    if (autoreadonly) {
+    if (flags & QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY) {
         aro = VIR_TRISTATE_BOOL_YES;
     } else {
         if (src->readonly)
@@ -1576,15 +1582,18 @@ qemuBlockStorageSourceAttachPrepareBlockdev(virStorageSourcePtr src,
                                             bool autoreadonly)
 {
     g_autoptr(qemuBlockStorageSourceAttachData) data = NULL;
+    unsigned int backendpropsflags = 0;
+
+    if (autoreadonly)
+        backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY;

     if (VIR_ALLOC(data) < 0)
         return NULL;

     if (!(data->formatProps = qemuBlockStorageSourceGetBlockdevProps(src,
                                                                      backingStore)) ||
-        !(data->storageProps = qemuBlockStorageSourceGetBackendProps(src, false,
-                                                                     false,
-                                                                     autoreadonly)))
+        !(data->storageProps = qemuBlockStorageSourceGetBackendProps(src,
+                                                                     backendpropsflags)))
         return NULL;

     data->storageNodeName = src->nodestorage;
@@ -2108,7 +2117,8 @@ qemuBlockGetBackingStoreString(virStorageSourcePtr src,
     }

     /* use json: pseudo protocol otherwise */
-    if (!(backingProps = qemuBlockStorageSourceGetBackendProps(src, false, true, false)))
+    if (!(backingProps = qemuBlockStorageSourceGetBackendProps(src,
+                                                               QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY)))
         return NULL;

     props = backingProps;
diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h
index b1bdb39613..715739e59c 100644
--- a/src/qemu/qemu_block.h
+++ b/src/qemu/qemu_block.h
@@ -56,11 +56,15 @@ qemuBlockGetNodeData(virJSONValuePtr data);
 bool
 qemuBlockStorageSourceSupportsConcurrentAccess(virStorageSourcePtr src);

+typedef enum {
+    QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY = 1 << 0,
+    QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY = 1 << 1,
+    QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY = 1 << 2,
+} qemuBlockStorageSourceBackendPropsFlags;
+
 virJSONValuePtr
 qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
-                                      bool legacy,
-                                      bool onlytarget,
-                                      bool autoreadonly);
+                                      unsigned int flags);

 virURIPtr
 qemuBlockStorageSourceGetURI(virStorageSourcePtr src);
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 24e99e13b8..c41976b903 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1193,7 +1193,8 @@ qemuDiskSourceGetProps(virStorageSourcePtr src)
     g_autoptr(virJSONValue) props = NULL;
     virJSONValuePtr ret;

-    if (!(props = qemuBlockStorageSourceGetBackendProps(src, true, false, false)))
+    if (!(props = qemuBlockStorageSourceGetBackendProps(src,
+                                                        QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY)))
         return NULL;

     if (virJSONValueObjectCreate(&ret, "a:file", &props, NULL) < 0)
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
index 51283793e9..025e96ab4b 100644
--- a/tests/qemublocktest.c
+++ b/tests/qemublocktest.c
@@ -62,6 +62,10 @@ testBackingXMLjsonXML(const void *args)
     g_autoptr(virStorageSource) xmlsrc = NULL;
     g_autoptr(virStorageSource) jsonsrc = NULL;
     g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
+    unsigned int backendpropsflags = 0;
+
+    if (data->legacy)
+        backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY;

     if (!(xmlsrc = virStorageSourceNew()))
         return -1;
@@ -77,9 +81,7 @@ testBackingXMLjsonXML(const void *args)
     }

     if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc,
-                                                               data->legacy,
-                                                               false,
-                                                               false))) {
+                                                               backendpropsflags))) {
         fprintf(stderr, "failed to format disk source json\n");
         return -1;
     }
@@ -159,7 +161,8 @@ testJSONtoJSON(const void *args)
         return -1;
     }

-    if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src, false, false, true))) {
+    if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src,
+                                                             QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY))) {
         fprintf(stderr, "failed to format disk source json\n");
         return -1;
     }
@@ -290,6 +293,9 @@ testQemuDiskXMLToProps(const void *opaque)

     for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) {
         g_autofree char *backingstore = NULL;
+        unsigned int backendpropsflagsnormal = QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY;
+        unsigned int backendpropsflagstarget = QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY |
+                                               QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY;

         if (testQemuDiskXMLToJSONFakeSecrets(n) < 0)
             return -1;
@@ -300,8 +306,8 @@ testQemuDiskXMLToProps(const void *opaque)
         qemuDomainPrepareDiskSourceData(disk, n);

         if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n, n->backingStore)) ||
-            !(storageSrcOnlyProps = qemuBlockStorageSourceGetBackendProps(n, false, true, true)) ||
-            !(storageProps = qemuBlockStorageSourceGetBackendProps(n, false, false, true)) ||
+            !(storageSrcOnlyProps = qemuBlockStorageSourceGetBackendProps(n, backendpropsflagstarget)) ||
+            !(storageProps = qemuBlockStorageSourceGetBackendProps(n, backendpropsflagsnormal)) ||
             !(backingstore = qemuBlockGetBackingStoreString(n, true))) {
             if (!data->fail) {
                 VIR_TEST_VERBOSE("failed to generate qemu blockdev props");
-- 
2.26.2




More information about the libvir-list mailing list