[PATCH 05/14] virStorageSource: Convert 'type' to proper enum

Peter Krempa pkrempa at redhat.com
Thu Mar 10 12:40:33 UTC 2022


Use 'virStorageType' as type for the 'type' member and convert the code
to work properly.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/ch/ch_monitor.c            |  1 +
 src/conf/domain_conf.c         | 27 ++++++++++++++-------------
 src/conf/snapshot_conf.c       | 24 +++++++++++++-----------
 src/conf/storage_source_conf.h |  2 +-
 4 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c
index 60905e36c2..84085b7991 100644
--- a/src/ch/ch_monitor.c
+++ b/src/ch/ch_monitor.c
@@ -198,6 +198,7 @@ virCHMonitorBuildDiskJson(virJSONValue *disks, virDomainDiskDef *diskdef)
     case VIR_STORAGE_TYPE_VOLUME:
     case VIR_STORAGE_TYPE_NVME:
     case VIR_STORAGE_TYPE_VHOST_USER:
+    case VIR_STORAGE_TYPE_LAST:
     default:
         virReportEnumRangeError(virStorageType, diskdef->src->type);
         return -1;
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 69f61aadc1..40ff71d7db 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8502,11 +8502,15 @@ virDomainStorageSourceParseBase(const char *type,
     src = virStorageSourceNew();
     src->type = VIR_STORAGE_TYPE_FILE;

-    if (type &&
-        (src->type = virStorageTypeFromString(type)) <= 0) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("unknown storage source type '%s'"), type);
-        return NULL;
+    if (type) {
+        int tmp;
+        if ((tmp = virStorageTypeFromString(type)) <= 0) {
+            virReportError(VIR_ERR_XML_ERROR,
+                           _("unknown storage source type '%s'"), type);
+            return NULL;
+        }
+
+        src->type = tmp;
     }

     if (format &&
@@ -9055,19 +9059,16 @@ virDomainDiskDefParseSourceXML(virDomainXMLOption *xmlopt,
 {
     g_autoptr(virStorageSource) src = virStorageSourceNew();
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
-    g_autofree char *type = NULL;
     xmlNodePtr tmp;

     ctxt->node = node;

-    src->type = VIR_STORAGE_TYPE_FILE;
-
-    if ((type = virXMLPropString(node, "type")) &&
-        (src->type = virStorageTypeFromString(type)) <= 0) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                       _("unknown disk type '%s'"), type);
+    if (virXMLPropEnumDefault(node, "type",
+                              virStorageTypeFromString,
+                              VIR_XML_PROP_NONZERO,
+                              &src->type,
+                              VIR_STORAGE_TYPE_FILE) < 0)
         return NULL;
-    }

     if ((tmp = virXPathNode("./source[1]", ctxt))) {
         if (virDomainStorageSourceParse(tmp, ctxt, src, flags, xmlopt) < 0)
diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index a2e45632bf..812cca2ed1 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -139,7 +139,6 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node,
                                  virDomainXMLOption *xmlopt)
 {
     g_autofree char *snapshot = NULL;
-    g_autofree char *type = NULL;
     g_autofree char *driver = NULL;
     g_autofree char *name = NULL;
     g_autoptr(virStorageSource) src = virStorageSourceNew();
@@ -165,16 +164,19 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node,
         }
     }

-    if ((type = virXMLPropString(node, "type"))) {
-        if ((src->type = virStorageTypeFromString(type)) <= 0 ||
-            src->type == VIR_STORAGE_TYPE_VOLUME ||
-            src->type == VIR_STORAGE_TYPE_DIR) {
-            virReportError(VIR_ERR_XML_ERROR,
-                           _("unknown disk snapshot type '%s'"), type);
-            return -1;
-        }
-    } else {
-        src->type = VIR_STORAGE_TYPE_FILE;
+    if (virXMLPropEnumDefault(node, "type",
+                              virStorageTypeFromString,
+                              VIR_XML_PROP_NONZERO,
+                              &src->type,
+                              VIR_STORAGE_TYPE_FILE) < 0)
+        return -1;
+
+    if (src->type == VIR_STORAGE_TYPE_VOLUME ||
+        src->type == VIR_STORAGE_TYPE_DIR) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("unsupported disk snapshot type '%s'"),
+                       virStorageTypeToString(src->type));
+        return -1;
     }

     if ((cur = virXPathNode("./source", ctxt)) &&
diff --git a/src/conf/storage_source_conf.h b/src/conf/storage_source_conf.h
index c720d093be..e984421e3d 100644
--- a/src/conf/storage_source_conf.h
+++ b/src/conf/storage_source_conf.h
@@ -269,7 +269,7 @@ struct _virStorageSource {
     virObject parent;

     unsigned int id; /* backing chain identifier, 0 is unset */
-    int type; /* virStorageType */
+    virStorageType type;
     char *path;
     int protocol; /* virStorageNetProtocol */
     char *volume; /* volume name for remote storage */
-- 
2.35.1



More information about the libvir-list mailing list