[libvirt] [PATCH 06/20] vbox: Rewrite vboxStoragePoolNumOfVolumes

Taowei uaedante at gmail.com
Wed Aug 20 12:46:20 UTC 2014


We use typedef IMedium IHardDisk to make IHardDisk hierachy from
IMedium (Actually it did on vbox 2.2 and 3.0's C++ API).
So when calling
    VBOX_MEDIUM_FUNC_ARG*(IHardDisk, func, args)
we can directly replace it to
    gVBoxAPI.UIMedium.func(IHardDisk, args)

The vbox CAPI is generate from vbox's C++ version. In which the
IHardDisk is hierachy from IMedium in vbox2.2 and 3.0 and since
3.1 IHardDisk is merged into IMedium.
Here we assume IHardDisk always hierachy from IMedium.

When dealing with this two types, we get some rules from it's
hierachy relationship.

When using IHardDisk and IMedium as input, we can't transfer a
IMedium to IHardDisk. Like:
    gVBoxAPI.UIHardDisk.func(IHardDisk *hardDisk, args)
    Here, we can't put a *IMedium as a argument.

When using IHardDisk and IMedium as output, we can't transfer a
IHardDisk to IMedium. Like:
    gVBoxAPI.UIMachine.GetMedium(IMedium **out)
    Here, we can't put a **IHardDisk as a argument. If this case
    do happen, we either change the API to GetHardDisk or write a
    new one.
---
 src/vbox/vbox_common.c        |   36 ++++++++++++++++++++++++++++++++
 src/vbox/vbox_common.h        |   12 +++++++++++
 src/vbox/vbox_tmpl.c          |   46 +++++++++++------------------------------
 src/vbox/vbox_uniformed_api.h |    3 +++
 4 files changed, 63 insertions(+), 34 deletions(-)

diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c
index 6996e7a..4ca0a10 100644
--- a/src/vbox/vbox_common.c
+++ b/src/vbox/vbox_common.c
@@ -8361,6 +8361,42 @@ virStoragePoolPtr vboxStoragePoolLookupByName(virConnectPtr conn, const char *na
     return ret;
 }
 
+int vboxStoragePoolNumOfVolumes(virStoragePoolPtr pool)
+{
+    VBOX_OBJECT_CHECK(pool->conn, int, -1);
+    vboxArray hardDisks = VBOX_ARRAY_INITIALIZER;
+    PRUint32 hardDiskAccessible = 0;
+    nsresult rc;
+    size_t i;
+
+    rc = gVBoxAPI.UArray.vboxArrayGet(&hardDisks, data->vboxObj,
+                                      gVBoxAPI.UArray.handleGetHardDisks(data->vboxObj));
+    if (NS_FAILED(rc)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("could not get number of volumes in the pool: %s, rc=%08x"),
+                       pool->name, (unsigned)rc);
+        return ret;
+    }
+
+    for (i = 0; i < hardDisks.count; ++i) {
+        IHardDisk *hardDisk = hardDisks.items[i];
+        PRUint32 hddstate;
+
+        if (!hardDisk)
+            continue;
+
+        gVBoxAPI.UIMedium.GetState(hardDisk, &hddstate);
+        if (hddstate != MediaState_Inaccessible)
+            hardDiskAccessible++;
+    }
+
+    gVBoxAPI.UArray.vboxArrayRelease(&hardDisks);
+
+    ret = hardDiskAccessible;
+
+    return ret;
+}
+
 /**
  * Function Tables
  */
diff --git a/src/vbox/vbox_common.h b/src/vbox/vbox_common.h
index 76288d4..71592c9 100644
--- a/src/vbox/vbox_common.h
+++ b/src/vbox/vbox_common.h
@@ -278,6 +278,17 @@ enum HostNetworkInterfaceType
     HostNetworkInterfaceType_HostOnly = 2
 };
 
+enum MediaState
+{
+    MediaState_NotCreated = 0,
+    MediaState_Created = 1,
+    MediaState_LockedRead = 2,
+    MediaState_LockedWrite = 3,
+    MediaState_Inaccessible = 4,
+    MediaState_Creating = 5,
+    MediaState_Deleting = 6
+};
+
 # define VBOX_E_OBJECT_NOT_FOUND 0x80BB0001
 # define VBOX_E_INVALID_VM_STATE 0x80BB0002
 # define VBOX_E_VM_ERROR 0x80BB0003
@@ -318,5 +329,6 @@ typedef nsISupports IDisplay;
 typedef nsISupports IHost;
 typedef nsISupports IHostNetworkInterface;
 typedef nsISupports IDHCPServer;
+typedef IMedium IHardDisk;
 
 #endif /* VBOX_COMMON_H */
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
index 3cca406..75eb73d 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -2021,40 +2021,6 @@ _registerDomainEvent(virDriverPtr driver)
  * The Storage Functions here on
  */
 
-static int vboxStoragePoolNumOfVolumes(virStoragePoolPtr pool)
-{
-    VBOX_OBJECT_CHECK(pool->conn, int, -1);
-    vboxArray hardDisks = VBOX_ARRAY_INITIALIZER;
-    PRUint32 hardDiskAccessible = 0;
-    nsresult rc;
-    size_t i;
-
-    rc = vboxArrayGet(&hardDisks, data->vboxObj, data->vboxObj->vtbl->GetHardDisks);
-    if (NS_SUCCEEDED(rc)) {
-        for (i = 0; i < hardDisks.count; ++i) {
-            IHardDisk *hardDisk = hardDisks.items[i];
-            if (hardDisk) {
-                PRUint32 hddstate;
-
-                VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
-                if (hddstate != MediaState_Inaccessible)
-                    hardDiskAccessible++;
-            }
-        }
-
-        vboxArrayRelease(&hardDisks);
-
-        ret = hardDiskAccessible;
-    } else {
-        ret = -1;
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("could not get number of volumes in the pool: %s, rc=%08x"),
-                       pool->name, (unsigned)rc);
-    }
-
-    return ret;
-}
-
 static int vboxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names, int nnames) {
     VBOX_OBJECT_CHECK(pool->conn, int, -1);
     vboxArray hardDisks = VBOX_ARRAY_INITIALIZER;
@@ -3566,6 +3532,11 @@ static void* _handleGetMachines(IVirtualBox *vboxObj)
     return vboxObj->vtbl->GetMachines;
 }
 
+static void* _handleGetHardDisks(IVirtualBox *vboxObj)
+{
+    return vboxObj->vtbl->GetHardDisks;
+}
+
 static void* _handleUSBGetDeviceFilters(IUSBCommon *USBCommon)
 {
     return USBCommon->vtbl->GetDeviceFilters;
@@ -4915,6 +4886,11 @@ static nsresult _mediumGetLocation(IMedium *medium, PRUnichar **location)
     return medium->vtbl->GetLocation(medium, location);
 }
 
+static nsresult _mediumGetState(IMedium *medium, PRUint32 *state)
+{
+    return medium->vtbl->GetState(medium, state);
+}
+
 static nsresult _mediumGetReadOnly(IMedium *medium ATTRIBUTE_UNUSED,
                                    PRBool *readOnly ATTRIBUTE_UNUSED)
 {
@@ -5432,6 +5408,7 @@ static vboxUniformedArray _UArray = {
     .vboxArrayGetWithIIDArg = _vboxArrayGetWithIIDArg,
     .vboxArrayRelease = vboxArrayRelease,
     .handleGetMachines = _handleGetMachines,
+    .handleGetHardDisks = _handleGetHardDisks,
     .handleUSBGetDeviceFilters = _handleUSBGetDeviceFilters,
     .handleMachineGetMediumAttachments = _handleMachineGetMediumAttachments,
     .handleMachineGetSharedFolders = _handleMachineGetSharedFolders,
@@ -5634,6 +5611,7 @@ static vboxUniformedIUSBDeviceFilter _UIUSBDeviceFilter = {
 static vboxUniformedIMedium _UIMedium = {
     .GetId = _mediumGetId,
     .GetLocation = _mediumGetLocation,
+    .GetState = _mediumGetState,
     .GetReadOnly = _mediumGetReadOnly,
     .GetParent = _mediumGetParent,
     .GetChildren = _mediumGetChildren,
diff --git a/src/vbox/vbox_uniformed_api.h b/src/vbox/vbox_uniformed_api.h
index cffb7f7..9709934 100644
--- a/src/vbox/vbox_uniformed_api.h
+++ b/src/vbox/vbox_uniformed_api.h
@@ -170,6 +170,7 @@ typedef struct {
     void (*vboxArrayRelease)(vboxArray *array);
     /* Generate function pointers for vboxArrayGet */
     void* (*handleGetMachines)(IVirtualBox *vboxObj);
+    void* (*handleGetHardDisks)(IVirtualBox *vboxObj);
     void* (*handleUSBGetDeviceFilters)(IUSBCommon *USBCommon);
     void* (*handleMachineGetMediumAttachments)(IMachine *machine);
     void* (*handleMachineGetSharedFolders)(IMachine *machine);
@@ -407,6 +408,7 @@ typedef struct {
 typedef struct {
     nsresult (*GetId)(IMedium *medium, vboxIIDUnion *iidu);
     nsresult (*GetLocation)(IMedium *medium, PRUnichar **location);
+    nsresult (*GetState)(IMedium *medium, PRUint32 *state);
     nsresult (*GetReadOnly)(IMedium *medium, PRBool *readOnly);
     nsresult (*GetParent)(IMedium *medium, IMedium **parent);
     nsresult (*GetChildren)(IMedium *medium, PRUint32 *childrenSize, IMedium ***children);
@@ -592,6 +594,7 @@ int vboxStorageClose(virConnectPtr conn);
 int vboxConnectNumOfStoragePools(virConnectPtr conn);
 int vboxConnectListStoragePools(virConnectPtr conn, char **const names, int nnames);
 virStoragePoolPtr vboxStoragePoolLookupByName(virConnectPtr conn, const char *name);
+int vboxStoragePoolNumOfVolumes(virStoragePoolPtr pool);
 
 /* Version specified functions for installing uniformed API */
 void vbox22InstallUniformedAPI(vboxUniformedAPI *pVBoxAPI);
-- 
1.7.9.5




More information about the libvir-list mailing list