In old version, function FindMedium in UIVirtualBox doesn't work for vbox2.2 and 3.0. We assume it will not be used when vbox in these versions.
But when rewriting vboxStorageVolLookupByPath, we found it was compatibe to use FindMedium to get a IHardDisk object, even in vbox old versions. To achieve this, first make FindMedium call FindHardDisk when VBOX_API_VERSION < 4000000. Then change the argument type **IMedium to **IHardDisk. (As the rules in heriachy, we can't transfer a IHardDisk to match IMedium in output) In vbox 2.2 and 3.0, the caller must be aware that they will get a IHardDisk object in return. --- src/vbox/vbox_common.c | 4 ++-- src/vbox/vbox_tmpl.c | 29 +++++++++++++++-------------- src/vbox/vbox_uniformed_api.h | 3 ++- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c index 22f3198..e0836ec 100644 --- a/src/vbox/vbox_common.c +++ b/src/vbox/vbox_common.c @@ -1081,8 +1081,8 @@ vboxAttachDrivesNew(virDomainDefPtr def, vboxGlobalData *data, IMachine *machine continue; } - gVBoxAPI.UIVirtualBox.FindMedium(data->vboxObj, mediumFileUtf16, - deviceType, accessMode, &medium); + gVBoxAPI.UIVirtualBox.FindHardDisk(data->vboxObj, mediumFileUtf16, + deviceType, accessMode, &medium); if (!medium) { PRUnichar *mediumEmpty = NULL; diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index e8cfa98..6c4cd73 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -3553,22 +3553,23 @@ _virtualboxRegisterMachine(IVirtualBox *vboxObj, IMachine *machine) } static nsresult -_virtualboxFindMedium(IVirtualBox *vboxObj ATTRIBUTE_UNUSED, - PRUnichar *location ATTRIBUTE_UNUSED, - PRUint32 deviceType ATTRIBUTE_UNUSED, - PRUint32 accessMode ATTRIBUTE_UNUSED, - IMedium **medium ATTRIBUTE_UNUSED) +_virtualboxFindHardDisk(IVirtualBox *vboxObj, PRUnichar *location, + PRUint32 deviceType ATTRIBUTE_UNUSED, + PRUint32 accessMode ATTRIBUTE_UNUSED, + IHardDisk **hardDisk) { -#if VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000 + /* In vbox 2.2 and 3.0, this function will create a IHardDisk object. + * In vbox 3.1 and later, this function will create a IMedium object. + */ +#if VBOX_API_VERSION < 4000000 + return vboxObj->vtbl->FindHardDisk(vboxObj, location, hardDisk); +#elif VBOX_API_VERSION < 4002000 return vboxObj->vtbl->FindMedium(vboxObj, location, - deviceType, medium); -#elif VBOX_API_VERSION >= 4002000 + deviceType, hardDisk); +#else /* VBOX_API_VERSION >= 4002000 */ return vboxObj->vtbl->OpenMedium(vboxObj, location, - deviceType, accessMode, PR_FALSE, medium); -#else - vboxUnsupported(); - return 0; -#endif + deviceType, accessMode, PR_FALSE, hardDisk); +#endif /* VBOX_API_VERSION >= 4002000 */ } static nsresult @@ -5287,7 +5288,7 @@ static vboxUniformedIVirtualBox _UIVirtualBox = { .CreateMachine = _virtualboxCreateMachine, .CreateHardDiskMedium = _virtualboxCreateHardDiskMedium, .RegisterMachine = _virtualboxRegisterMachine, - .FindMedium = _virtualboxFindMedium, + .FindHardDisk = _virtualboxFindHardDisk, .OpenMedium = _virtualboxOpenMedium, .GetHardDiskByIID = _virtualboxGetHardDiskByIID, .FindDHCPServerByNetworkName = _virtualboxFindDHCPServerByNetworkName, diff --git a/src/vbox/vbox_uniformed_api.h b/src/vbox/vbox_uniformed_api.h index cdc2ebe..385502f 100644 --- a/src/vbox/vbox_uniformed_api.h +++ b/src/vbox/vbox_uniformed_api.h @@ -196,7 +196,8 @@ typedef struct { nsresult (*CreateMachine)(vboxGlobalData *data, virDomainDefPtr def, IMachine **machine, char *uuidstr); nsresult (*CreateHardDiskMedium)(IVirtualBox *vboxObj, PRUnichar *format, PRUnichar *location, IMedium **medium); nsresult (*RegisterMachine)(IVirtualBox *vboxObj, IMachine *machine); - nsresult (*FindMedium)(IVirtualBox *vboxObj, PRUnichar *location, PRUint32 deviceType, PRUint32 accessMode, IMedium **medium); + nsresult (*FindHardDisk)(IVirtualBox *vboxObj, PRUnichar *location, PRUint32 deviceType, + PRUint32 accessMode, IHardDisk **hardDisk); nsresult (*OpenMedium)(IVirtualBox *vboxObj, PRUnichar *location, PRUint32 deviceType, PRUint32 accessMode, IMedium **medium); nsresult (*GetHardDiskByIID)(IVirtualBox *vboxObj, vboxIIDUnion *iidu, IHardDisk **hardDisk); nsresult (*FindDHCPServerByNetworkName)(IVirtualBox *vboxObj, PRUnichar *name, IDHCPServer **server); -- 1.7.9.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list