VirtualBox

Ignore:
Timestamp:
Nov 9, 2018 10:25:30 PM (6 years ago)
Author:
vboxsync
Message:

Main,VBoxManage,FE/Qt: Extended the createSharedFolder and ISharedFolder methods with a mount poit parameter/attribute for use when auto-mounting. This is especially useful for Windows and OS/2 guests which operates with drive letters. The change has not yet trickled down to the guest interface and VBoxService.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-client/ConsoleImpl.cpp

    r75361 r75380  
    15161516//static
    15171517const char *Console::sSSMConsoleUnit = "ConsoleData";
    1518 //static
    1519 uint32_t Console::sSSMConsoleVer = 0x00010001;
     1518/** The saved state version.  */
     1519#define CONSOLE_SAVED_STATE_VERSION                         UINT32_C(0x00010002)
     1520/** The saved state version, pre shared folder autoMountPoint.  */
     1521#define CONSOLE_SAVED_STATE_VERSION_PRE_AUTO_MOUNT_POINT    UINT32_C(0x00010001)
    15201522
    15211523inline static const char *networkAdapterTypeToName(NetworkAdapterType_T adapterType)
     
    15691571        uint32_t version = 0;
    15701572        vrc = SSMR3Seek(ssm, sSSMConsoleUnit, 0 /* iInstance */, &version);
    1571         if (SSM_VERSION_MAJOR(version) == SSM_VERSION_MAJOR(sSSMConsoleVer))
     1573        if (SSM_VERSION_MAJOR(version) == SSM_VERSION_MAJOR(CONSOLE_SAVED_STATE_VERSION))
    15721574        {
    15731575            if (RT_SUCCESS(vrc))
     
    16141616    AutoReadLock alock(that COMMA_LOCKVAL_SRC_POS);
    16151617
    1616     int vrc = SSMR3PutU32(pSSM, (uint32_t)that->m_mapSharedFolders.size());
    1617     AssertRC(vrc);
     1618    SSMR3PutU32(pSSM, (uint32_t)that->m_mapSharedFolders.size());
    16181619
    16191620    for (SharedFolderMap::const_iterator it = that->m_mapSharedFolders.begin();
     
    16251626        AutoReadLock sfLock(pSF COMMA_LOCKVAL_SRC_POS);
    16261627
    1627         Utf8Str name = pSF->i_getName();
    1628         vrc = SSMR3PutU32(pSSM, (uint32_t)name.length() + 1 /* term. 0 */);
    1629         AssertRC(vrc);
    1630         vrc = SSMR3PutStrZ(pSSM, name.c_str());
    1631         AssertRC(vrc);
    1632 
    1633         Utf8Str hostPath = pSF->i_getHostPath();
    1634         vrc = SSMR3PutU32(pSSM, (uint32_t)hostPath.length() + 1 /* term. 0 */);
    1635         AssertRC(vrc);
    1636         vrc = SSMR3PutStrZ(pSSM, hostPath.c_str());
    1637         AssertRC(vrc);
    1638 
    1639         vrc = SSMR3PutBool(pSSM, !!pSF->i_isWritable());
    1640         AssertRC(vrc);
    1641 
    1642         vrc = SSMR3PutBool(pSSM, !!pSF->i_isAutoMounted());
    1643         AssertRC(vrc);
    1644     }
    1645 
    1646     return;
     1628        const Utf8Str &name = pSF->i_getName();
     1629        SSMR3PutU32(pSSM, (uint32_t)name.length() + 1 /* term. 0 */);
     1630        SSMR3PutStrZ(pSSM, name.c_str());
     1631
     1632        const Utf8Str &hostPath = pSF->i_getHostPath();
     1633        SSMR3PutU32(pSSM, (uint32_t)hostPath.length() + 1 /* term. 0 */);
     1634        SSMR3PutStrZ(pSSM, hostPath.c_str());
     1635
     1636        SSMR3PutBool(pSSM, !!pSF->i_isWritable());
     1637        SSMR3PutBool(pSSM, !!pSF->i_isAutoMounted());
     1638
     1639        const Utf8Str &rStrAutoMountPoint = pSF->i_getAutoMountPoint();
     1640        SSMR3PutU32(pSSM, (uint32_t)rStrAutoMountPoint.length() + 1 /* term. 0 */);
     1641        SSMR3PutStrZ(pSSM, rStrAutoMountPoint.c_str());
     1642    }
    16471643}
    16481644
     
    16651661    LogFlowFunc(("\n"));
    16661662
    1667     if (SSM_VERSION_MAJOR_CHANGED(uVersion, sSSMConsoleVer))
     1663    if (SSM_VERSION_MAJOR_CHANGED(uVersion, CONSOLE_SAVED_STATE_VERSION))
    16681664        return VERR_VERSION_MISMATCH;
    16691665    Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
     
    17061702        bool autoMount = false;
    17071703
    1708         uint32_t szBuf = 0;
     1704        uint32_t cbStr = 0;
    17091705        char *buf = NULL;
    17101706
    1711         vrc = SSMR3GetU32(pSSM, &szBuf);
     1707        vrc = SSMR3GetU32(pSSM, &cbStr);
    17121708        AssertRCReturn(vrc, vrc);
    1713         buf = new char[szBuf];
    1714         vrc = SSMR3GetStrZ(pSSM, buf, szBuf);
     1709        buf = new char[cbStr];
     1710        vrc = SSMR3GetStrZ(pSSM, buf, cbStr);
    17151711        AssertRC(vrc);
    17161712        strName = buf;
    17171713        delete[] buf;
    17181714
    1719         vrc = SSMR3GetU32(pSSM, &szBuf);
     1715        vrc = SSMR3GetU32(pSSM, &cbStr);
    17201716        AssertRCReturn(vrc, vrc);
    1721         buf = new char[szBuf];
    1722         vrc = SSMR3GetStrZ(pSSM, buf, szBuf);
     1717        buf = new char[cbStr];
     1718        vrc = SSMR3GetStrZ(pSSM, buf, cbStr);
    17231719        AssertRC(vrc);
    17241720        strHostPath = buf;
    17251721        delete[] buf;
    17261722
    1727         if (u32Version > 0x00010000)
     1723        if (u32Version >= CONSOLE_SAVED_STATE_VERSION_PRE_AUTO_MOUNT_POINT)
    17281724            SSMR3GetBool(pSSM, &writable);
    17291725
    1730         if (u32Version > 0x00010000) // ???
     1726        if (   u32Version >= CONSOLE_SAVED_STATE_VERSION_PRE_AUTO_MOUNT_POINT
     1727#ifndef VBOX_OSE /* This broke saved state when introduced in r63916 (4.0). */
     1728            && SSMR3HandleRevision(pSSM) >= 63916
     1729#endif
     1730           )
    17311731            SSMR3GetBool(pSSM, &autoMount);
     1732
     1733        Utf8Str strAutoMountPoint;
     1734        if (u32Version >= CONSOLE_SAVED_STATE_VERSION)
     1735        {
     1736            vrc = SSMR3GetU32(pSSM, &cbStr);
     1737            AssertRCReturn(vrc, vrc);
     1738            vrc = strAutoMountPoint.reserveNoThrow(cbStr);
     1739            AssertRCReturn(vrc, vrc);
     1740            vrc = SSMR3GetStrZ(pSSM, strAutoMountPoint.mutableRaw(), cbStr);
     1741            AssertRCReturn(vrc, vrc);
     1742            strAutoMountPoint.jolt();
     1743        }
    17321744
    17331745        ComObjPtr<SharedFolder> pSharedFolder;
     
    17381750                                         writable,
    17391751                                         autoMount,
     1752                                         strAutoMountPoint,
    17401753                                         false /* fFailOnError */);
    17411754        AssertComRCReturn(rc, VERR_INTERNAL_ERROR);
     
    29412954}
    29422955
    2943 HRESULT Console::createSharedFolder(const com::Utf8Str &aName, const com::Utf8Str &aHostPath, BOOL aWritable, BOOL aAutomount)
     2956HRESULT Console::createSharedFolder(const com::Utf8Str &aName, const com::Utf8Str &aHostPath, BOOL aWritable,
     2957                                    BOOL aAutomount, const com::Utf8Str &aAutoMountPoint)
    29442958{
    29452959    LogFlowThisFunc(("Entering for '%s' -> '%s'\n", aName.c_str(), aHostPath.c_str()));
     
    29742988                             !!aWritable,
    29752989                             !!aAutomount,
     2990                             aAutoMountPoint,
    29762991                             true /* fFailOnError */);
    29772992    if (FAILED(rc)) return rc;
     
    29953010
    29963011        /* second, create the given folder */
    2997         rc = i_createSharedFolder(aName, SharedFolderData(aHostPath, !!aWritable, !!aAutomount));
     3012        rc = i_createSharedFolder(aName, SharedFolderData(aHostPath, !!aWritable, !!aAutomount, aAutoMountPoint));
    29983013        if (FAILED(rc))
    29993014            return rc;
     
    76837698                sharedFolders[it->first] = SharedFolderData(pSF->i_getHostPath(),
    76847699                                                            pSF->i_isWritable(),
    7685                                                             pSF->i_isAutoMounted());
     7700                                                            pSF->i_isAutoMounted(),
     7701                                                            pSF->i_getAutoMountPoint());
    76867702            }
    76877703        }
     
    84418457                ComPtr<ISharedFolder> pSharedFolder = folders[i];
    84428458
    8443                 Bstr bstrName;
    8444                 Bstr bstrHostPath;
     8459                Bstr bstr;
     8460                rc = pSharedFolder->COMGETTER(Name)(bstr.asOutParam());
     8461                if (FAILED(rc)) throw rc;
     8462                Utf8Str strName(bstr);
     8463
     8464                rc = pSharedFolder->COMGETTER(HostPath)(bstr.asOutParam());
     8465                if (FAILED(rc)) throw rc;
     8466                Utf8Str strHostPath(bstr);
     8467
    84458468                BOOL writable;
    8446                 BOOL autoMount;
    8447 
    8448                 rc = pSharedFolder->COMGETTER(Name)(bstrName.asOutParam());
    8449                 if (FAILED(rc)) throw rc;
    8450                 Utf8Str strName(bstrName);
    8451 
    8452                 rc = pSharedFolder->COMGETTER(HostPath)(bstrHostPath.asOutParam());
    8453                 if (FAILED(rc)) throw rc;
    8454                 Utf8Str strHostPath(bstrHostPath);
    8455 
    84568469                rc = pSharedFolder->COMGETTER(Writable)(&writable);
    84578470                if (FAILED(rc)) throw rc;
    84588471
     8472                BOOL autoMount;
    84598473                rc = pSharedFolder->COMGETTER(AutoMount)(&autoMount);
    84608474                if (FAILED(rc)) throw rc;
    84618475
     8476                rc = pSharedFolder->COMGETTER(AutoMountPoint)(bstr.asOutParam());
     8477                if (FAILED(rc)) throw rc;
     8478                Utf8Str strAutoMountPoint(bstr);
     8479
    84628480                m_mapMachineSharedFolders.insert(std::make_pair(strName,
    8463                                                                 SharedFolderData(strHostPath, !!writable, !!autoMount)));
     8481                                                                SharedFolderData(strHostPath, !!writable,
     8482                                                                                 !!autoMount, strAutoMountPoint)));
    84648483
    84658484                /* send changes to HGCM if the VM is running */
     
    84888507                            /* create the new machine folder */
    84898508                            rc = i_createSharedFolder(strName,
    8490                                                       SharedFolderData(strHostPath, !!writable, !!autoMount));
     8509                                                      SharedFolderData(strHostPath, !!writable, !!autoMount, strAutoMountPoint));
    84918510                            if (FAILED(rc)) throw rc;
    84928511                        }
     
    85778596HRESULT Console::i_createSharedFolder(const Utf8Str &strName, const SharedFolderData &aData)
    85788597{
     8598    Log(("Adding shared folder '%s' -> '%s'\n", strName.c_str(), aData.m_strHostPath.c_str()));
     8599
     8600    /*
     8601     * Sanity checks
     8602     */
    85798603    ComAssertRet(strName.isNotEmpty(), E_FAIL);
    85808604    ComAssertRet(aData.m_strHostPath.isNotEmpty(), E_FAIL);
    85818605
    8582     /* sanity checks */
    85838606    AssertReturn(mpUVM, E_FAIL);
    85848607    AssertReturn(m_pVMMDev && m_pVMMDev->isShFlActive(), E_FAIL);
    85858608
    8586     VBOXHGCMSVCPARM parms[SHFL_CPARMS_ADD_MAPPING];
    8587     SHFLSTRING *pFolderName, *pMapName;
    8588     size_t cbString;
    8589 
    8590     Bstr value;
    8591     HRESULT hrc = mMachine->GetExtraData(BstrFmt("VBoxInternal2/SharedFoldersEnableSymlinksCreate/%s",
    8592                                                  strName.c_str()).raw(),
    8593                                          value.asOutParam());
    8594     bool fSymlinksCreate = hrc == S_OK && value == "1";
    8595 
    8596     Log(("Adding shared folder '%s' -> '%s'\n", strName.c_str(), aData.m_strHostPath.c_str()));
    8597 
    8598     // check whether the path is valid and exists
    8599     char hostPathFull[RTPATH_MAX];
    8600     int vrc = RTPathAbsEx(NULL,
    8601                           aData.m_strHostPath.c_str(),
    8602                           hostPathFull,
    8603                           sizeof(hostPathFull));
    8604 
    8605     bool fMissing = false;
     8609    /*
     8610     * Find out whether we should allow symbolic link creation.
     8611     */
     8612    Bstr bstrValue;
     8613    HRESULT hrc = mMachine->GetExtraData(BstrFmt("VBoxInternal2/SharedFoldersEnableSymlinksCreate/%s", strName.c_str()).raw(),
     8614                                         bstrValue.asOutParam());
     8615    bool fSymlinksCreate = hrc == S_OK && bstrValue == "1";
     8616
     8617    /*
     8618     * Check whether the path is valid and exists.
     8619     */
     8620    char szAbsHostPath[RTPATH_MAX];
     8621    int vrc = RTPathAbsEx(NULL, aData.m_strHostPath.c_str(), szAbsHostPath, sizeof(szAbsHostPath));
    86068622    if (RT_FAILURE(vrc))
    86078623        return setErrorBoth(E_INVALIDARG, vrc, tr("Invalid shared folder path: '%s' (%Rrc)"), aData.m_strHostPath.c_str(), vrc);
    8608     if (!RTPathExists(hostPathFull))
    8609         fMissing = true;
    8610 
    8611     /* Check whether the path is full (absolute) */
    8612     if (RTPathCompare(aData.m_strHostPath.c_str(), hostPathFull) != 0)
     8624
     8625    /* Check whether the path is full (absolute).  ASSUMING a RTPATH_MAX of ~4K
     8626       this also checks that the length is within bounds of a SHFLSTRING.  */
     8627    if (RTPathCompare(aData.m_strHostPath.c_str(), szAbsHostPath) != 0)
    86138628        return setError(E_INVALIDARG,
    86148629                        tr("Shared folder path '%s' is not absolute"),
    86158630                        aData.m_strHostPath.c_str());
    86168631
    8617     // now that we know the path is good, give it to HGCM
    8618 
    8619     Bstr bstrName(strName);
    8620     Bstr bstrHostPath(aData.m_strHostPath);
    8621 
    8622     cbString = (bstrHostPath.length() + 1) * sizeof(RTUTF16);
    8623     if (cbString >= UINT16_MAX)
    8624         return setError(E_INVALIDARG, tr("The name is too long"));
    8625     pFolderName = (SHFLSTRING*)RTMemAllocZ(SHFLSTRING_HEADER_SIZE + cbString);
    8626     Assert(pFolderName);
    8627     memcpy(pFolderName->String.ucs2, bstrHostPath.raw(), cbString);
    8628 
    8629     pFolderName->u16Size   = (uint16_t)cbString;
    8630     pFolderName->u16Length = (uint16_t)(cbString - sizeof(RTUTF16));
    8631 
    8632     parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
    8633     parms[0].u.pointer.addr = pFolderName;
    8634     parms[0].u.pointer.size = ShflStringSizeOfBuffer(pFolderName);
    8635 
    8636     cbString = (bstrName.length() + 1) * sizeof(RTUTF16);
    8637     if (cbString >= UINT16_MAX)
    8638     {
    8639         RTMemFree(pFolderName);
    8640         return setError(E_INVALIDARG, tr("The host path is too long"));
    8641     }
    8642     pMapName = (SHFLSTRING*)RTMemAllocZ(SHFLSTRING_HEADER_SIZE + cbString);
    8643     Assert(pMapName);
    8644     memcpy(pMapName->String.ucs2, bstrName.raw(), cbString);
    8645 
    8646     pMapName->u16Size   = (uint16_t)cbString;
    8647     pMapName->u16Length = (uint16_t)(cbString - sizeof(RTUTF16));
    8648 
    8649     parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
    8650     parms[1].u.pointer.addr = pMapName;
    8651     parms[1].u.pointer.size = ShflStringSizeOfBuffer(pMapName);
    8652 
    8653     parms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
    8654     parms[2].u.uint32 = (aData.m_fWritable ? SHFL_ADD_MAPPING_F_WRITABLE : 0)
    8655                       | (aData.m_fAutoMount ? SHFL_ADD_MAPPING_F_AUTOMOUNT : 0)
    8656                       | (fSymlinksCreate ? SHFL_ADD_MAPPING_F_CREATE_SYMLINKS : 0)
    8657                       | (fMissing ? SHFL_ADD_MAPPING_F_MISSING : 0)
    8658                       ;
    8659 
    8660     vrc = m_pVMMDev->hgcmHostCall("VBoxSharedFolders",
    8661                                   SHFL_FN_ADD_MAPPING,
    8662                                   SHFL_CPARMS_ADD_MAPPING, &parms[0]);
    8663     RTMemFree(pFolderName);
    8664     RTMemFree(pMapName);
    8665 
    8666     if (RT_FAILURE(vrc))
    8667         return setErrorBoth(E_FAIL, vrc, tr("Could not create a shared folder '%s' mapped to '%s' (%Rrc)"),
    8668                             strName.c_str(), aData.m_strHostPath.c_str(), vrc);
    8669 
    8670     if (fMissing)
    8671         return setError(E_INVALIDARG,
    8672                         tr("Shared folder path '%s' does not exist on the host"),
    8673                         aData.m_strHostPath.c_str());
    8674 
    8675     return S_OK;
     8632    bool const fMissing = !RTPathExists(szAbsHostPath);
     8633
     8634    /*
     8635     * Check the other two string lengths before converting them all to SHFLSTRINGS.
     8636     */
     8637    if (strName.length() >= _2K)
     8638        return setError(E_INVALIDARG, tr("Shared folder name is too long: %zu bytes"), strName.length());
     8639    if (aData.m_strAutoMountPoint.length() >= RTPATH_MAX)
     8640        return setError(E_INVALIDARG, tr("Shared folder mountp point too long: %zu bytes"), aData.m_strAutoMountPoint.length());
     8641
     8642    PSHFLSTRING pHostPath       = ShflStringDupUtf8AsUtf16(aData.m_strHostPath.c_str());
     8643    PSHFLSTRING pName           = ShflStringDupUtf8AsUtf16(strName.c_str());
     8644    PSHFLSTRING pAutoMountPoint = ShflStringDupUtf8AsUtf16(aData.m_strAutoMountPoint.c_str());
     8645    if (pHostPath && pName && pAutoMountPoint)
     8646    {
     8647        /*
     8648         * Make a SHFL_FN_ADD_MAPPING call to tell the service about folder.
     8649         */
     8650        VBOXHGCMSVCPARM aParams[SHFL_CPARMS_ADD_MAPPING];
     8651        SHFLSTRING_TO_HGMC_PARAM(&aParams[0], pHostPath);
     8652        SHFLSTRING_TO_HGMC_PARAM(&aParams[1], pName);
     8653        aParams[2].setUInt32(  (aData.m_fWritable  ? SHFL_ADD_MAPPING_F_WRITABLE : 0)
     8654                             | (aData.m_fAutoMount ? SHFL_ADD_MAPPING_F_AUTOMOUNT : 0)
     8655                             | (fSymlinksCreate    ? SHFL_ADD_MAPPING_F_CREATE_SYMLINKS : 0)
     8656                             | (fMissing           ? SHFL_ADD_MAPPING_F_MISSING : 0));
     8657        SHFLSTRING_TO_HGMC_PARAM(&aParams[3], pAutoMountPoint);
     8658        AssertCompile(SHFL_CPARMS_ADD_MAPPING == 4);
     8659
     8660        vrc = m_pVMMDev->hgcmHostCall("VBoxSharedFolders", SHFL_FN_ADD_MAPPING, SHFL_CPARMS_ADD_MAPPING, aParams);
     8661        if (RT_FAILURE(vrc))
     8662            hrc = setErrorBoth(E_FAIL, vrc, tr("Could not create a shared folder '%s' mapped to '%s' (%Rrc)"),
     8663                               strName.c_str(), aData.m_strHostPath.c_str(), vrc);
     8664
     8665        else if (fMissing)
     8666            hrc = setError(E_INVALIDARG,
     8667                           tr("Shared folder path '%s' does not exist on the host"),
     8668                           aData.m_strHostPath.c_str());
     8669        else
     8670            hrc = S_OK;
     8671    }
     8672    else
     8673        hrc = E_OUTOFMEMORY;
     8674    RTMemFree(pAutoMountPoint);
     8675    RTMemFree(pName);
     8676    RTMemFree(pHostPath);
     8677    return hrc;
    86768678}
    86778679
     
    1017610178                 * Register our load/save state file handlers
    1017710179                 */
    10178                 vrc = SSMR3RegisterExternal(pConsole->mpUVM, sSSMConsoleUnit, 0 /*iInstance*/, sSSMConsoleVer, 0 /* cbGuess */,
     10180                vrc = SSMR3RegisterExternal(pConsole->mpUVM, sSSMConsoleUnit, 0 /*iInstance*/,
     10181                                            CONSOLE_SAVED_STATE_VERSION, 0 /* cbGuess */,
    1017910182                                            NULL, NULL, NULL,
    1018010183                                            NULL, i_saveStateFileExec, NULL,
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