VirtualBox

Ignore:
Timestamp:
Oct 14, 2010 4:20:15 PM (14 years ago)
Author:
vboxsync
Message:

Main: have Machine::MountMedium() behave like AttachDevice (use IMedium* pointer instead of UUID); add missing saveSettings calls there

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/MachineImpl.cpp

    r33108 r33140  
    33273327    // if this becomes true then we need to call saveSettings in the end
    33283328    // @todo r=dj there is no error handling so far...
    3329     bool fNeedsGlobalSaveSettings = false;
     3329    bool fNeedsMachineSaveSettings = false,
     3330         fNeedsGlobalSaveSettings = false;
    33303331
    33313332    // request the host lock first, since might be calling Host methods for getting host drives;
     
    36713672        if (mData->pMachineConfigFile->canHaveOwnMediaRegistry())
    36723673            // machine XML is VirtualBox 4.0 or higher:
    3673             medium->addRegistry(getId());        // machine UUID
     3674            medium->addRegistry(getId(),         // machine UUID
     3675                                &fNeedsMachineSaveSettings);
    36743676        else
    3675             medium->addRegistry(mParent->getGlobalRegistryId()); // VirtualBox global registry UUID
     3677            medium->addRegistry(mParent->getGlobalRegistryId(), // VirtualBox global registry UUID
     3678                                &fNeedsGlobalSaveSettings);
    36763679    }
    36773680
     
    36803683    mMediaData.backup();
    36813684    mMediaData->mAttachments.push_back(attachment);
     3685
     3686    if (fNeedsMachineSaveSettings)
     3687        saveSettings(&fNeedsGlobalSaveSettings, SaveS_Force);
    36823688
    36833689    if (fNeedsGlobalSaveSettings)
     
    37983804                                  LONG aControllerPort,
    37993805                                  LONG aDevice,
    3800                                   IN_BSTR aId,
     3806                                  IMedium *aMedium,
    38013807                                  BOOL aForce)
    38023808{
     
    38103816    if (FAILED(autoCaller.rc())) return autoCaller.rc();
    38113817
    3812     // we're calling host methods for getting DVD and floppy drives so lock host first
    3813     AutoMultiWriteLock2 alock(mParent->host(), this COMMA_LOCKVAL_SRC_POS);
     3818    // request the host lock first, since might be calling Host methods for getting host drives;
     3819    // next, protect the media tree all the while we're in here, as well as our member variables
     3820    AutoMultiWriteLock3 multiLock(mParent->host()->lockHandle(),
     3821                                  this->lockHandle(),
     3822                                  &mParent->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
    38143823
    38153824    ComObjPtr<MediumAttachment> pAttach = findAttachment(mMediaData->mAttachments,
     
    38273836    oldmedium = pAttach->getMedium();
    38283837
    3829     Guid uuid(aId);
    3830     ComObjPtr<Medium> medium;
    3831     DeviceType_T mediumType = pAttach->getType();
    3832     switch (mediumType)
    3833     {
    3834         case DeviceType_DVD:
    3835         case DeviceType_Floppy:
    3836             rc = mParent->findRemoveableMedium(mediumType, uuid, true /* fRefresh */, medium);
    3837             if (FAILED(rc)) return rc;
    3838         break;
    3839 
    3840         default:
    3841             return setError(VBOX_E_INVALID_OBJECT_STATE,
    3842                             tr("Cannot change medium attached to device slot %d on port %d of controller '%ls'"),
    3843                             aDevice, aControllerPort, aControllerName);
    3844     }
    3845 
    3846     if (SUCCEEDED(rc))
    3847     {
    3848         setModified(IsModified_Storage);
    3849         mMediaData.backup();
    3850 
    3851         /* The backup operation makes the pAttach reference point to the
    3852          * old settings. Re-get the correct reference. */
     3838    ComObjPtr<Medium> pMedium = static_cast<Medium*>(aMedium);
     3839    if (aMedium && pMedium.isNull())
     3840        return setError(E_INVALIDARG, "The given medium pointer is invalid");
     3841
     3842    AutoCaller mediumCaller(pMedium);
     3843    if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
     3844
     3845    AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
     3846    if (pMedium)
     3847    {
     3848        DeviceType_T mediumType = pAttach->getType();
     3849        switch (mediumType)
     3850        {
     3851            case DeviceType_DVD:
     3852            case DeviceType_Floppy:
     3853            break;
     3854
     3855            default:
     3856                return setError(VBOX_E_INVALID_OBJECT_STATE,
     3857                                tr("The device at port %d, device %d of controller '%ls' of this virtual machine is not removeable"),
     3858                                aControllerPort,
     3859                                aDevice,
     3860                                aControllerName);
     3861        }
     3862    }
     3863
     3864    setModified(IsModified_Storage);
     3865    mMediaData.backup();
     3866
     3867    bool fNeedsMachineSaveSettings = false,
     3868         fNeedsGlobalSaveSettings = false;
     3869
     3870    {
     3871        // The backup operation makes the pAttach reference point to the
     3872        // old settings. Re-get the correct reference.
    38533873        pAttach = findAttachment(mMediaData->mAttachments,
    38543874                                 aControllerName,
     
    38563876                                 aDevice);
    38573877        AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
    3858         /* For non-hard disk media, detach straight away. */
    3859         if (mediumType != DeviceType_HardDisk && !oldmedium.isNull())
     3878        if (!oldmedium.isNull())
    38603879            oldmedium->removeBackReference(mData->mUuid);
    3861         if (!medium.isNull())
    3862         {
    3863             medium->addBackReference(mData->mUuid);
     3880        if (!pMedium.isNull())
     3881        {
     3882            pMedium->addBackReference(mData->mUuid);
     3883
    38643884            // and decide which medium registry to use now that the medium is attached:
    38653885            if (mData->pMachineConfigFile->canHaveOwnMediaRegistry())
    3866                 // machine XML is VirtualBox 4.0 or higher:
    3867                 medium->addRegistry(getId());        // machine UUID
     3886                // machine XML is VirtualBox 4.0 or higher: machine UUID
     3887                pMedium->addRegistry(getId(),
     3888                                     &fNeedsMachineSaveSettings);
    38683889            else
    3869                 medium->addRegistry(mParent->getGlobalRegistryId()); // VirtualBox global registry UUID
    3870         }
    3871 
    3872         pAttach->updateMedium(medium);
    3873 
    3874         setModified(IsModified_Storage);
    3875     }
    3876 
    3877     alock.leave();
     3890                // VirtualBox global registry UUID
     3891                pMedium->addRegistry(mParent->getGlobalRegistryId(),
     3892                                     &fNeedsGlobalSaveSettings);
     3893        }
     3894
     3895        pAttach->updateMedium(pMedium);
     3896    }
     3897
     3898    setModified(IsModified_Storage);
     3899
     3900    mediumLock.release();
     3901    multiLock.release();
    38783902    rc = onMediumChange(pAttach, aForce);
    3879     alock.enter();
     3903    multiLock.acquire();
     3904    mediumLock.acquire();
    38803905
    38813906    /* On error roll back this change only. */
    38823907    if (FAILED(rc))
    38833908    {
    3884         if (!medium.isNull())
    3885             medium->removeBackReference(mData->mUuid);
     3909        if (!pMedium.isNull())
     3910            pMedium->removeBackReference(mData->mUuid);
    38863911        pAttach = findAttachment(mMediaData->mAttachments,
    38873912                                 aControllerName,
     
    38923917            return rc;
    38933918        AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
    3894         /* For non-hard disk media, re-attach straight away. */
    3895         if (mediumType != DeviceType_HardDisk && !oldmedium.isNull())
     3919        if (!oldmedium.isNull())
    38963920            oldmedium->addBackReference(mData->mUuid);
    38973921        pAttach->updateMedium(oldmedium);
     3922    }
     3923
     3924    mediumLock.release();
     3925    multiLock.release();
     3926
     3927    if (fNeedsMachineSaveSettings)
     3928    {
     3929        AutoWriteLock machineLock(this COMMA_LOCKVAL_SRC_POS);
     3930        saveSettings(&fNeedsGlobalSaveSettings, SaveS_Force);
     3931    }
     3932
     3933    if (fNeedsGlobalSaveSettings)
     3934    {
     3935        // save the global settings; for that we should hold only the VirtualBox lock
     3936        AutoWriteLock vboxLock(mParent COMMA_LOCKVAL_SRC_POS);
     3937        mParent->saveSettings();
    38983938    }
    38993939
     
    74657505            if (puuidRegistry)
    74667506                // caller wants registry ID to be set on all attached media (OVF import case)
    7467                 medium->addRegistry(*puuidRegistry);
     7507                medium->addRegistry(*puuidRegistry,
     7508                                    NULL /* pfNeedsSaveSettings */);
    74687509        }
    74697510
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