For storage volume validation, we weren't consistent on
whether to use VIR_FROM_NONE or VIR_FROM_STORAGE. Similar
to previous patches, use a common macro to make it nicer.
virStorageVolCreateXMLFrom allows cross-connection cloning,
where the error is reported against the connection of the
destination pool.
* src/datatypes.h (virCheckStorageVolReturn)
(virCheckStorageVolGoto): New macros.
(VIR_IS_STORAGE_VOL, VIR_IS_CONNECTED_STORAGE_VOL): Drop
unused macros.
* src/libvirt.c: Use macro throughout.
(virLibStorageVolError): Drop unused macro.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
src/datatypes.h | 29 +++++++++++++--
src/libvirt.c | 112 ++++++++++++--------------------------------------------
2 files changed, 49 insertions(+), 92 deletions(-)
diff --git a/src/datatypes.h b/src/datatypes.h
index 0be271d..2f4b894 100644
--- a/src/datatypes.h
+++ b/src/datatypes.h
@@ -138,10 +138,31 @@ extern virClassPtr virStoragePoolClass;
} \
} while (0)
-# define VIR_IS_STORAGE_VOL(obj) \
- (virObjectIsClass((obj), virStorageVolClass))
-# define VIR_IS_CONNECTED_STORAGE_VOL(obj) \
- (VIR_IS_STORAGE_VOL(obj) && virObjectIsClass((obj)->conn,
virConnectClass))
+# define virCheckStorageVolReturn(obj, retval) \
+ do { \
+ virStorageVolPtr _vol = (obj); \
+ if (!virObjectIsClass(_vol, virStorageVolClass) || \
+ !virObjectIsClass(_vol->conn, virConnectClass)) { \
+ virReportErrorHelper(VIR_FROM_STORAGE, \
+ VIR_ERR_INVALID_STORAGE_VOL, \
+ __FILE__, __FUNCTION__, __LINE__, \
+ __FUNCTION__); \
+ virDispatchError(NULL); \
+ return retval; \
+ } \
+ } while (0)
+# define virCheckStorageVolGoto(obj, label) \
+ do { \
+ virStorageVolPtr _vol = (obj); \
+ if (!virObjectIsClass(_vol, virStorageVolClass) || \
+ !virObjectIsClass(_vol->conn, virConnectClass)) { \
+ virReportErrorHelper(VIR_FROM_STORAGE, \
+ VIR_ERR_INVALID_STORAGE_VOL, \
+ __FILE__, __FUNCTION__, __LINE__, \
+ __FUNCTION__); \
+ goto label; \
+ } \
+ } while (0)
# define VIR_IS_NODE_DEVICE(obj) \
(virObjectIsClass((obj), virNodeDeviceClass))
diff --git a/src/libvirt.c b/src/libvirt.c
index 65dc364..47e8bd3 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 virLibStorageVolError(code, ...) \
- virReportErrorHelper(VIR_FROM_STORAGE, code, __FILE__, \
- __FUNCTION__, __LINE__, __VA_ARGS__)
#define virLibNodeDeviceError(code, ...) \
virReportErrorHelper(VIR_FROM_NODEDEV, code, __FILE__, \
__FUNCTION__, __LINE__, __VA_ARGS__)
@@ -12583,11 +12580,7 @@ virStoragePoolLookupByVolume(virStorageVolPtr vol)
virResetLastError();
- if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
- virLibConnError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return NULL;
- }
+ virCheckStorageVolReturn(vol, NULL);
if (vol->conn->storageDriver &&
vol->conn->storageDriver->storagePoolLookupByVolume) {
virStoragePoolPtr ret;
@@ -13364,11 +13357,8 @@ virStorageVolGetConnect(virStorageVolPtr vol)
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return NULL;
- }
+ virCheckStorageVolReturn(vol, NULL);
+
return vol->conn;
}
@@ -13500,11 +13490,8 @@ virStorageVolGetName(virStorageVolPtr vol)
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return NULL;
- }
+ virCheckStorageVolReturn(vol, NULL);
+
return vol->name;
}
@@ -13526,11 +13513,8 @@ virStorageVolGetKey(virStorageVolPtr vol)
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return NULL;
- }
+ virCheckStorageVolReturn(vol, NULL);
+
return vol->key;
}
@@ -13612,12 +13596,7 @@ virStorageVolCreateXMLFrom(virStoragePoolPtr pool,
virResetLastError();
virCheckStoragePoolReturn(pool, NULL);
-
- if (!VIR_IS_STORAGE_VOL(clonevol)) {
- virLibConnError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- goto error;
- }
-
+ virCheckStorageVolGoto(clonevol, error);
virCheckNonNullArgGoto(xmlDesc, error);
virCheckReadOnlyGoto(pool->conn->flags | clonevol->conn->flags, error);
@@ -13671,10 +13650,7 @@ virStorageVolDownload(virStorageVolPtr vol,
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibConnError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- return -1;
- }
+ virCheckStorageVolReturn(vol, -1);
if (!VIR_IS_STREAM(stream)) {
virLibConnError(VIR_ERR_INVALID_STREAM, __FUNCTION__);
@@ -13738,10 +13714,7 @@ virStorageVolUpload(virStorageVolPtr vol,
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibConnError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- return -1;
- }
+ virCheckStorageVolReturn(vol, -1);
if (!VIR_IS_STREAM(stream)) {
virLibConnError(VIR_ERR_INVALID_STREAM, __FUNCTION__);
@@ -13789,13 +13762,9 @@ virStorageVolDelete(virStorageVolPtr vol,
virResetLastError();
- if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return -1;
- }
-
+ virCheckStorageVolReturn(vol, -1);
conn = vol->conn;
+
virCheckReadOnlyGoto(conn->flags, error);
if (conn->storageDriver && conn->storageDriver->storageVolDelete) {
@@ -13832,13 +13801,9 @@ virStorageVolWipe(virStorageVolPtr vol,
virResetLastError();
- if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return -1;
- }
-
+ virCheckStorageVolReturn(vol, -1);
conn = vol->conn;
+
virCheckReadOnlyGoto(conn->flags, error);
if (conn->storageDriver && conn->storageDriver->storageVolWipe) {
@@ -13879,13 +13844,9 @@ virStorageVolWipePattern(virStorageVolPtr vol,
virResetLastError();
- if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return -1;
- }
-
+ virCheckStorageVolReturn(vol, -1);
conn = vol->conn;
+
virCheckReadOnlyGoto(conn->flags, error);
if (conn->storageDriver &&
conn->storageDriver->storageVolWipePattern) {
@@ -13921,11 +13882,8 @@ virStorageVolFree(virStorageVolPtr vol)
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return -1;
- }
+ virCheckStorageVolReturn(vol, -1);
+
virObjectUnref(vol);
return 0;
}
@@ -13955,11 +13913,8 @@ virStorageVolRef(virStorageVolPtr vol)
virResetLastError();
- if ((!VIR_IS_CONNECTED_STORAGE_VOL(vol))) {
- virLibConnError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return -1;
- }
+ virCheckStorageVolReturn(vol, -1);
+
virObjectRef(vol);
return 0;
}
@@ -13986,11 +13941,7 @@ virStorageVolGetInfo(virStorageVolPtr vol,
memset(info, 0, sizeof(virStorageVolInfo));
- if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return -1;
- }
+ virCheckStorageVolReturn(vol, -1);
virCheckNonNullArgGoto(info, error);
conn = vol->conn;
@@ -14030,12 +13981,7 @@ virStorageVolGetXMLDesc(virStorageVolPtr vol,
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return NULL;
- }
-
+ virCheckStorageVolReturn(vol, NULL);
conn = vol->conn;
if (conn->storageDriver &&
conn->storageDriver->storageVolGetXMLDesc) {
@@ -14075,12 +14021,7 @@ virStorageVolGetPath(virStorageVolPtr vol)
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return NULL;
- }
-
+ virCheckStorageVolReturn(vol, NULL);
conn = vol->conn;
if (conn->storageDriver && conn->storageDriver->storageVolGetPath)
{
@@ -14143,12 +14084,7 @@ virStorageVolResize(virStorageVolPtr vol,
virResetLastError();
- if (!VIR_IS_STORAGE_VOL(vol)) {
- virLibStorageVolError(VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
- virDispatchError(NULL);
- return -1;
- }
-
+ virCheckStorageVolReturn(vol, -1);
conn = vol->conn;
virCheckReadOnlyGoto(conn->flags, error);
--
1.8.4.2