[PATCH 14/24] maint: improve VIR_ERR_INVALID_NETWORK usage

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

 



When checking for a valid network, we weren't consistent on
whether we reported an invalid network or a connection.  Similar
to previous patches, use a common macro to make it nicer.

* src/datatypes.h (virCheckNetworkReturn, virCheckNetworkGoto): New
macros.
(VIR_IS_NETWORK, VIR_IS_CONNECTED_NETWORK): Drop unused macros.
* src/libvirt.c: Use macro throughout.
(virLibNetworkError): Drop unused macro.

Signed-off-by: Eric Blake <eblake@xxxxxxxxxx>
---
 src/datatypes.h |  29 +++++++++++--
 src/libvirt.c   | 131 ++++++++++++++++----------------------------------------
 2 files changed, 61 insertions(+), 99 deletions(-)

diff --git a/src/datatypes.h b/src/datatypes.h
index ca400ec..9c40f89 100644
--- a/src/datatypes.h
+++ b/src/datatypes.h
@@ -84,10 +84,31 @@ extern virClassPtr virStoragePoolClass;
         }                                                               \
     } while (0)

-# define VIR_IS_NETWORK(obj) \
-    (virObjectIsClass((obj), virNetworkClass))
-# define VIR_IS_CONNECTED_NETWORK(obj) \
-    (VIR_IS_NETWORK(obj) && virObjectIsClass((obj)->conn, virConnectClass))
+# define virCheckNetworkReturn(obj, retval)                             \
+    do {                                                                \
+        virNetworkPtr _net = (obj);                                     \
+        if (!virObjectIsClass(_net, virNetworkClass) ||                 \
+            !virObjectIsClass(_net->conn, virConnectClass)) {           \
+            virReportErrorHelper(VIR_FROM_NETWORK,                      \
+                                 VIR_ERR_INVALID_NETWORK,               \
+                                 __FILE__, __FUNCTION__, __LINE__,      \
+                                 __FUNCTION__);                         \
+            virDispatchError(NULL);                                     \
+            return retval;                                              \
+        }                                                               \
+    } while (0)
+# define virCheckNetworkGoto(obj, label)                                \
+    do {                                                                \
+        virNetworkPtr _net = (obj);                                     \
+        if (!virObjectIsClass(_net, virNetworkClass) ||                 \
+            !virObjectIsClass(_net->conn, virConnectClass)) {           \
+            virReportErrorHelper(VIR_FROM_NETWORK,                      \
+                                 VIR_ERR_INVALID_NETWORK,               \
+                                 __FILE__, __FUNCTION__, __LINE__,      \
+                                 __FUNCTION__);                         \
+            goto label;                                                 \
+        }                                                               \
+    } while (0)

 # define VIR_IS_INTERFACE(obj) \
     (virObjectIsClass((obj), virInterfaceClass))
diff --git a/src/libvirt.c b/src/libvirt.c
index accbe69..8fca384 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -518,9 +518,6 @@ DllMain(HINSTANCE instance ATTRIBUTE_UNUSED,
 #define virLibDomainError(code, ...)                              \
     virReportErrorHelper(VIR_FROM_DOM, code, __FILE__,            \
                          __FUNCTION__, __LINE__, __VA_ARGS__)
-#define virLibNetworkError(code, ...)                             \
-    virReportErrorHelper(VIR_FROM_NETWORK, code, __FILE__,        \
-                         __FUNCTION__, __LINE__, __VA_ARGS__)
 #define virLibStoragePoolError(code, ...)                         \
     virReportErrorHelper(VIR_FROM_STORAGE, code, __FILE__,        \
                          __FUNCTION__, __LINE__, __VA_ARGS__)
@@ -10509,11 +10506,8 @@ virNetworkGetConnect(virNetworkPtr net)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(net)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return NULL;
-    }
+    virCheckNetworkReturn(net, NULL);
+
     return net->conn;
 }

@@ -10934,12 +10928,9 @@ virNetworkUndefine(virNetworkPtr network)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(network, -1);
     conn = network->conn;
+
     virCheckReadOnlyGoto(conn->flags, error);

     if (conn->networkDriver && conn->networkDriver->networkUndefine) {
@@ -10991,14 +10982,10 @@ virNetworkUpdate(virNetworkPtr network,

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(network, -1);
     conn = network->conn;
-    virCheckReadOnlyGoto(conn->flags, error);

+    virCheckReadOnlyGoto(conn->flags, error);
     virCheckNonNullArgGoto(xml, error);

     if (conn->networkDriver && conn->networkDriver->networkUpdate) {
@@ -11035,12 +11022,9 @@ virNetworkCreate(virNetworkPtr network)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(network, -1);
     conn = network->conn;
+
     virCheckReadOnlyGoto(conn->flags, error);

     if (conn->networkDriver && conn->networkDriver->networkCreate) {
@@ -11078,13 +11062,9 @@ virNetworkDestroy(virNetworkPtr network)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
-
+    virCheckNetworkReturn(network, -1);
     conn = network->conn;
+
     virCheckReadOnlyGoto(conn->flags, error);

     if (conn->networkDriver && conn->networkDriver->networkDestroy) {
@@ -11119,11 +11099,8 @@ virNetworkFree(virNetworkPtr network)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(network, -1);
+
     virObjectUnref(network);
     return 0;
 }
@@ -11154,11 +11131,8 @@ virNetworkRef(virNetworkPtr network)

     virResetLastError();

-    if ((!VIR_IS_CONNECTED_NETWORK(network))) {
-        virLibConnError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(network, -1);
+
     virObjectRef(network);
     return 0;
 }
@@ -11180,11 +11154,8 @@ virNetworkGetName(virNetworkPtr network)

     virResetLastError();

-    if (!VIR_IS_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return NULL;
-    }
+    virCheckNetworkReturn(network, NULL);
+
     return network->name;
 }

@@ -11205,11 +11176,7 @@ virNetworkGetUUID(virNetworkPtr network, unsigned char *uuid)

     virResetLastError();

-    if (!VIR_IS_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(network, -1);
     virCheckNonNullArgGoto(uuid, error);

     memcpy(uuid, &network->uuid[0], VIR_UUID_BUFLEN);
@@ -11239,11 +11206,7 @@ virNetworkGetUUIDString(virNetworkPtr network, char *buf)

     virResetLastError();

-    if (!VIR_IS_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(network, -1);
     virCheckNonNullArgGoto(buf, error);

     virUUIDFormat(network->uuid, buf);
@@ -11279,12 +11242,7 @@ virNetworkGetXMLDesc(virNetworkPtr network, unsigned int flags)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return NULL;
-    }
-
+    virCheckNetworkReturn(network, NULL);
     conn = network->conn;

     if (conn->networkDriver && conn->networkDriver->networkGetXMLDesc) {
@@ -11321,12 +11279,7 @@ virNetworkGetBridgeName(virNetworkPtr network)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return NULL;
-    }
-
+    virCheckNetworkReturn(network, NULL);
     conn = network->conn;

     if (conn->networkDriver && conn->networkDriver->networkGetBridgeName) {
@@ -11365,11 +11318,7 @@ virNetworkGetAutostart(virNetworkPtr network,

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(network, -1);
     virCheckNonNullArgGoto(autostart, error);

     conn = network->conn;
@@ -11409,16 +11358,11 @@ virNetworkSetAutostart(virNetworkPtr network,

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(network)) {
-        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
-
-    virCheckReadOnlyGoto(network->conn->flags, error);
-
+    virCheckNetworkReturn(network, -1);
     conn = network->conn;

+    virCheckReadOnlyGoto(conn->flags, error);
+
     if (conn->networkDriver && conn->networkDriver->networkSetAutostart) {
         int ret;
         ret = conn->networkDriver->networkSetAutostart(network, autostart);
@@ -16743,11 +16687,8 @@ virNetworkIsActive(virNetworkPtr net)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(net)) {
-        virLibConnError(VIR_ERR_INVALID_CONN, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(net, -1);
+
     if (net->conn->networkDriver->networkIsActive) {
         int ret;
         ret = net->conn->networkDriver->networkIsActive(net);
@@ -16779,11 +16720,8 @@ virNetworkIsPersistent(virNetworkPtr net)

     virResetLastError();

-    if (!VIR_IS_CONNECTED_NETWORK(net)) {
-        virLibConnError(VIR_ERR_INVALID_CONN, __FUNCTION__);
-        virDispatchError(NULL);
-        return -1;
-    }
+    virCheckNetworkReturn(net, -1);
+
     if (net->conn->networkDriver->networkIsPersistent) {
         int ret;
         ret = net->conn->networkDriver->networkIsPersistent(net);
@@ -18125,11 +18063,14 @@ virConnectNetworkEventRegisterAny(virConnectPtr conn,
     virResetLastError();

     virCheckConnectReturn(conn, -1);
-    if (net != NULL &&
-        !(VIR_IS_CONNECTED_NETWORK(net) && net->conn == conn)) {
-        virLibConnError(VIR_ERR_INVALID_CONN, __FUNCTION__);
-        virDispatchError(conn);
-        return -1;
+    if (net) {
+        virCheckNetworkGoto(net, error);
+        if (net->conn != conn) {
+            virReportInvalidArg(net,
+                                _("network '%s' in %s must match connection"),
+                                net->name, __FUNCTION__);
+            goto error;
+        }
     }
     virCheckNonNullArgGoto(cb, error);
     virCheckNonNegativeArgGoto(eventID, error);
-- 
1.8.4.2

--
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]