[PATCH 4/4] maint: prefer enum over int for virstoragefile structs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



For internal structs, we might as well be type-safe and let the
compiler help us with less typing required on our part (getting
rid of casts is always nice).  In trying to use enums directly,
I noticed two problems in virstoragefile.h that can't be fixed
without more invasive refactoring: virStorageSource.format is
used as more of a union of multiple enums in storage volume
code (so it has to remain an int), and virStorageSourcePoolDef
refers to pooltype whose enum is declared in src/conf, but where
src/util can't pull in headers from src/conf.

* src/util/virstoragefile.h (virStorageNetHostDef)
(virStorageSourcePoolDef, virStorageSource): Use enums instead of
int for fields of internal types.
* src/qemu/qemu_command.c (qemuParseCommandLine): Cover all values.
* src/conf/domain_conf.c (virDomainDiskSourceParse)
(virDomainDiskSourceFormat): Simplify clients.
* src/qemu/qemu_driver.c
(qemuDomainSnapshotCreateSingleDiskActive)
(qemuDomainSnapshotPrepareDiskExternalBackingInactive)
(qemuDomainSnapshotPrepareDiskExternalOverlayActive)
(qemuDomainSnapshotPrepareDiskInternal): Likewise.

Signed-off-by: Eric Blake <eblake@xxxxxxxxxx>
---
 src/conf/domain_conf.c    |  4 ++--
 src/qemu/qemu_command.c   |  8 ++++++++
 src/qemu/qemu_driver.c    |  8 ++++----
 src/util/virstoragefile.h | 19 ++++++++++---------
 4 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e65b62b..e5ae7c6 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -4972,7 +4972,7 @@ virDomainDiskSourceParse(xmlNodePtr node,

     memset(&host, 0, sizeof(host));

-    switch ((virStorageType)src->type) {
+    switch (src->type) {
     case VIR_STORAGE_TYPE_FILE:
         src->path = virXMLPropString(node, "file");
         break;
@@ -14847,7 +14847,7 @@ virDomainDiskSourceFormat(virBufferPtr buf,
         startupPolicy = virDomainStartupPolicyTypeToString(policy);

     if (src->path || src->nhosts > 0 || src->srcpool || startupPolicy) {
-        switch ((virStorageType)src->type) {
+        switch (src->type) {
         case VIR_STORAGE_TYPE_FILE:
             virBufferAddLit(buf, "<source");
             virBufferEscapeString(buf, " file='%s'", src->path);
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index cfd1bcf..9ae1a96 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -11065,6 +11065,14 @@ qemuParseCommandLine(virCapsPtr qemuCaps,
                         goto error;

                     break;
+                case VIR_STORAGE_NET_PROTOCOL_HTTP:
+                case VIR_STORAGE_NET_PROTOCOL_HTTPS:
+                case VIR_STORAGE_NET_PROTOCOL_FTP:
+                case VIR_STORAGE_NET_PROTOCOL_FTPS:
+                case VIR_STORAGE_NET_PROTOCOL_TFTP:
+                case VIR_STORAGE_NET_PROTOCOL_LAST:
+                    /* ignored for now */
+                    break;
                 }
             }

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 52ca47c..0c91106 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -12368,7 +12368,7 @@ qemuDomainSnapshotPrepareDiskExternalBackingInactive(virDomainDiskDefPtr disk)
         return 0;

     case VIR_STORAGE_TYPE_NETWORK:
-        switch ((virStorageNetProtocol) disk->src.protocol) {
+        switch (disk->src.protocol) {
         case VIR_STORAGE_NET_PROTOCOL_NBD:
         case VIR_STORAGE_NET_PROTOCOL_RBD:
         case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
@@ -12430,7 +12430,7 @@ qemuDomainSnapshotPrepareDiskExternalOverlayActive(virDomainSnapshotDiskDefPtr d
         return 0;

     case VIR_STORAGE_TYPE_NETWORK:
-        switch ((virStorageNetProtocol) disk->src.protocol) {
+        switch (disk->src.protocol) {
         case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
             return 0;

@@ -12575,7 +12575,7 @@ qemuDomainSnapshotPrepareDiskInternal(virConnectPtr conn,
         return 0;

     case VIR_STORAGE_TYPE_NETWORK:
-        switch ((virStorageNetProtocol) disk->src.protocol) {
+        switch (disk->src.protocol) {
         case VIR_STORAGE_NET_PROTOCOL_NBD:
         case VIR_STORAGE_NET_PROTOCOL_RBD:
         case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
@@ -12801,7 +12801,7 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver,
         VIR_STRDUP(persistSource, snap->src.path) < 0)
         goto cleanup;

-    switch ((virStorageType)snap->src.type) {
+    switch (snap->src.type) {
     case VIR_STORAGE_TYPE_BLOCK:
         reuse = true;
         /* fallthrough */
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index e32389e..0a19603 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -150,7 +150,7 @@ typedef virStorageNetHostDef *virStorageNetHostDefPtr;
 struct _virStorageNetHostDef {
     char *name;
     char *port;
-    int transport; /* virStorageNetHostTransport */
+    virStorageNetHostTransport transport;
     char *socket;  /* path to unix socket */
 };

@@ -182,10 +182,10 @@ typedef struct _virStorageSourcePoolDef virStorageSourcePoolDef;
 struct _virStorageSourcePoolDef {
     char *pool; /* pool name */
     char *volume; /* volume name */
-    int voltype; /* virStorageVolType, internal only */
-    int pooltype; /* virStoragePoolType, internal only */
-    int actualtype; /* virStorageType, internal only */
-    int mode; /* virStorageSourcePoolMode */
+    virStorageVolType voltype; /* internal only */
+    int pooltype; /* virStoragePoolType from storage_conf.h, internal only */
+    virStorageType actualtype; /* internal only */
+    virStorageSourcePoolMode mode;
 };
 typedef virStorageSourcePoolDef *virStorageSourcePoolDefPtr;

@@ -208,15 +208,15 @@ typedef virStorageSource *virStorageSourcePtr;
  * backing chains, multiple source disks join to form a single guest
  * view.  */
 struct _virStorageSource {
-    int type; /* virStorageType */
+    virStorageType type;
     char *path;
-    int protocol; /* virStorageNetProtocol */
+    virStorageNetProtocol protocol;
     size_t nhosts;
     virStorageNetHostDefPtr hosts;
     virStorageSourcePoolDefPtr srcpool;
     struct {
         char *username;
-        int secretType; /* virStorageSecretType */
+        virStorageSecretType secretType;
         union {
             unsigned char uuid[VIR_UUID_BUFLEN];
             char *usage;
@@ -225,7 +225,8 @@ struct _virStorageSource {
     virStorageEncryptionPtr encryption;

     char *driverName;
-    int format; /* virStorageFileFormat */
+    int format; /* virStorageFileFormat in domain backing chains, but
+                 * pool-specific enum for storage volumes */
     virBitmapPtr features;
     char *compat;

-- 
1.9.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]