]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virStorageSource: Convert 'type' to proper enum
authorPeter Krempa <pkrempa@redhat.com>
Tue, 8 Mar 2022 14:02:29 +0000 (15:02 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 11 Mar 2022 12:55:49 +0000 (13:55 +0100)
Use 'virStorageType' as type for the 'type' member and convert the code
to work properly.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/ch/ch_monitor.c
src/conf/domain_conf.c
src/conf/snapshot_conf.c
src/conf/storage_source_conf.h

index 60905e36c28abc7e1df5663a285aa208078a0583..84085b7991718f00a48813a6dc21ed089ea64fb5 100644 (file)
@@ -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;
index 69f61aadc1abf00bebd702de014bc234d08d1ce1..40ff71d7db45d94e01a715775398dbc757dffe1b 100644 (file)
@@ -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)
index a2e45632bf366cc223847bd5b287ecbff18e047c..812cca2ed1113065a0568f675a236f9dedcb89f9 100644 (file)
@@ -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)) &&
index c720d093bed57ca8359d680b679263e8f253a15f..e984421e3d94280a6f55dadc880ae580cac0c956 100644 (file)
@@ -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 */