VirtualBox

Changeset 35813 in vbox for trunk/src


Ignore:
Timestamp:
Feb 1, 2011 2:30:31 PM (14 years ago)
Author:
vboxsync
Message:

Main: reduce code duplication in host drive management code; add missing caller + locking for safety

Location:
trunk/src/VBox/Main
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/HostImpl.h

    r35638 r35813  
    129129    HRESULT buildDVDDrivesList(MediaList &list);
    130130    HRESULT buildFloppyDrivesList(MediaList &list);
     131    HRESULT findHostDriveByNameOrId(DeviceType_T mediumType, const Utf8Str &strNameOrId, ComObjPtr<Medium> &pMedium);
    131132
    132133#if defined(RT_OS_SOLARIS) && defined(VBOX_USE_LIBHAL)
  • trunk/src/VBox/Main/src-server/HostImpl.cpp

    r35809 r35813  
    393393        ComPtr<IProgress> progress;
    394394
    395         int r = NetIfCreateHostOnlyNetworkInterface(m->pParent, 
    396                                                     hif.asOutParam(), 
     395        int r = NetIfCreateHostOnlyNetworkInterface(m->pParent,
     396                                                    hif.asOutParam(),
    397397                                                    progress.asOutParam(),
    398398                                                    it->c_str());
     
    13071307    CheckComArgOutPointerValid(aDrive);
    13081308
    1309     *aDrive = NULL;
    1310 
    1311     SafeIfaceArray<IMedium> drivevec;
    1312     HRESULT rc = COMGETTER(DVDDrives)(ComSafeArrayAsOutParam(drivevec));
    1313     if (FAILED(rc)) return rc;
    1314 
    1315     for (size_t i = 0; i < drivevec.size(); ++i)
    1316     {
    1317         ComPtr<IMedium> drive = drivevec[i];
    1318         Bstr name, location, id;
    1319         rc = drive->COMGETTER(Name)(name.asOutParam());
    1320         if (FAILED(rc)) return rc;
    1321         rc = drive->COMGETTER(Location)(location.asOutParam());
    1322         if (FAILED(rc)) return rc;
    1323         rc = drive->COMGETTER(Id)(id.asOutParam());
    1324         if (FAILED(rc)) return rc;
    1325         if (   name == aName
    1326             || location == aName
    1327             || (!Guid(aName).isEmpty() && aName == id))
    1328             return drive.queryInterfaceTo(aDrive);
    1329     }
    1330 
    1331     return setError(VBOX_E_OBJECT_NOT_FOUND,
    1332                     Medium::tr("The host DVD drive named '%ls' could not be found"), aName);
     1309    ComObjPtr<Medium>medium;
     1310    HRESULT rc = findHostDriveByNameOrId(DeviceType_DVD, Utf8Str(aName), medium);
     1311    if (SUCCEEDED(rc))
     1312        return medium.queryInterfaceTo(aDrive);
     1313    else
     1314        return setError(rc, Medium::tr("The host DVD drive named '%ls' could not be found"), aName);
    13331315}
    13341316
     
    13401322    *aDrive = NULL;
    13411323
    1342     SafeIfaceArray<IMedium> drivevec;
    1343     HRESULT rc = COMGETTER(FloppyDrives)(ComSafeArrayAsOutParam(drivevec));
    1344     if (FAILED(rc)) return rc;
    1345 
    1346     for (size_t i = 0; i < drivevec.size(); ++i)
    1347     {
    1348         ComPtr<IMedium> drive = drivevec[i];
    1349         Bstr name;
    1350         rc = drive->COMGETTER(Name)(name.asOutParam());
    1351         if (FAILED(rc)) return rc;
    1352         if (name == aName)
    1353             return drive.queryInterfaceTo(aDrive);
    1354     }
    1355 
    1356     return setError(VBOX_E_OBJECT_NOT_FOUND,
    1357                     Medium::tr("The host floppy drive named '%ls' could not be found"), aName);
     1324    ComObjPtr<Medium>medium;
     1325    HRESULT rc = findHostDriveByNameOrId(DeviceType_Floppy, Utf8Str(aName), medium);
     1326    if (SUCCEEDED(rc))
     1327        return medium.queryInterfaceTo(aDrive);
     1328    else
     1329        return setError(rc, Medium::tr("The host floppy drive named '%ls' could not be found"), aName);
    13581330}
    13591331
     
    17581730        {
    17591731            Medium *pThis = *it;
     1732            AutoCaller mediumCaller(pThis);
     1733            AutoReadLock mediumLock(pThis COMMA_LOCKVAL_SRC_POS);
    17601734            if (pThis->getId() == uuid)
    17611735            {
     
    17961770        {
    17971771            Medium *pThis = *it;
     1772            AutoCaller mediumCaller(pThis);
     1773            AutoReadLock mediumLock(pThis COMMA_LOCKVAL_SRC_POS);
    17981774            if (pThis->getLocationFull() == strLocationFull)
    17991775            {
     
    18051781
    18061782    return VBOX_E_OBJECT_NOT_FOUND;
     1783}
     1784
     1785/**
     1786 * Goes through the list of host drives that would be returned by getDrives()
     1787 * and looks for a host drive with the given name, location or ID. If found,
     1788 * it sets pMedium to that drive; otherwise returns VBOX_E_OBJECT_NOT_FOUND.
     1789 *
     1790 * @param mediumType  Must be DeviceType_DVD or DeviceType_Floppy.
     1791 * @param strNameOrId Name or full location or UUID of host drive to look for.
     1792 * @param pMedium     Medium object, if found…
     1793 * @return VBOX_E_OBJECT_NOT_FOUND if not found, or S_OK if found, or errors from getDrives().
     1794 */
     1795HRESULT Host::findHostDriveByNameOrId(DeviceType_T mediumType,
     1796                                      const Utf8Str &strNameOrId,
     1797                                      ComObjPtr<Medium> &pMedium)
     1798{
     1799    MediaList *pllMedia;
     1800
     1801    AutoWriteLock wlock(m->drivesLock COMMA_LOCKVAL_SRC_POS);
     1802
     1803    Guid uuid(strNameOrId);
     1804    if (!uuid.isEmpty())
     1805        return findHostDriveById(mediumType, uuid, true /* fRefresh */, pMedium);
     1806
     1807    // string is not a syntactically valid UUID: try a name then
     1808    return findHostDriveByName(mediumType, strNameOrId, true /* fRefresh */, pMedium);
    18071809}
    18081810
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette