VirtualBox

Changeset 30380 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jun 22, 2010 4:28:14 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
62987
Message:

Main: make calculateRelativePath methods a bit smarter and rename them to VirtualBox::copyPathRelativeToConfig() and Machine::copyPathRelativeToMachine()

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

Legend:

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

    r30379 r30380  
    50955095
    50965096/**
    5097  * Tries to calculate the relative path of the given absolute path using the
    5098  * directory of the machine settings file as the base directory.
    5099  *
    5100  * @param  aPath    Absolute path to calculate the relative path for.
    5101  * @param  aResult  Where to put the result (used only when it's possible to
    5102  *                  make a relative path from the given absolute path; otherwise
    5103  *                  left untouched).
     5097 * Copies strSource to strTarget, making it relative to the machine folder
     5098 * if it is a subdirectory thereof, or simply copying it otherwise.
     5099 *
     5100 * @param strSource Path to evalue and copy.
     5101 * @param strTarget Buffer to receive target path.
    51045102 *
    51055103 * @note Locks this object for reading.
    51065104 */
    5107 void Machine::calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult)
     5105void Machine::copyPathRelativeToMachine(const Utf8Str &strSource,
     5106                                        Utf8Str &strTarget)
    51085107{
    51095108    AutoCaller autoCaller(this);
     
    51135112
    51145113    AssertReturnVoid(!mData->m_strConfigFileFull.isEmpty());
    5115 
    5116     Utf8Str settingsDir = mData->m_strConfigFileFull;
    5117 
    5118     settingsDir.stripFilename();
    5119     if (RTPathStartsWith(strPath.c_str(), settingsDir.c_str()))
    5120     {
    5121         /* when assigning, we create a separate Utf8Str instance because both
    5122          * aPath and aResult can point to the same memory location when this
    5123          * func is called (if we just do aResult = aPath, aResult will be freed
    5124          * first, and since its the same as aPath, an attempt to copy garbage
    5125          * will be made. */
    5126         aResult = Utf8Str(strPath.c_str() + settingsDir.length() + 1);
    5127     }
     5114    // use strTarget as a temporary buffer to hold the machine settings dir
     5115    strTarget = mData->m_strConfigFileFull;
     5116    strTarget.stripFilename();
     5117    if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
     5118        // is relative: then append what's left
     5119        strTarget.append(strSource.c_str() + strTarget.length());     // include '/'
     5120    else
     5121        // is not relative: then overwrite
     5122        strTarget = strSource;
    51285123}
    51295124
     
    74407435            /* update m_strConfigFileFull amd mConfigFile */
    74417436            mData->m_strConfigFileFull = newConfigFile;
    7442 
    74437437            // compute the relative path too
    7444             Utf8Str path = newConfigFile;
    7445             mParent->calculateRelativePath(path, path);
    7446             mData->m_strConfigFile = path;
     7438            mParent->copyPathRelativeToConfig(newConfigFile, mData->m_strConfigFile);
    74477439
    74487440            // store the old and new so that VirtualBox::saveSettings() can update
     
    74587450
    74597451            /* update the snapshot folder */
    7460             path = mUserData->mSnapshotFolderFull;
     7452            Utf8Str path = mUserData->mSnapshotFolderFull;
    74617453            if (RTPathStartsWith(path.c_str(), configDir.c_str()))
    74627454            {
     
    74647456                                  path.raw() + configDir.length());
    74657457                mUserData->mSnapshotFolderFull = path;
    7466                 calculateRelativePath(path, path);
    7467                 mUserData->mSnapshotFolder = path;
     7458                Utf8Str strTemp;
     7459                copyPathRelativeToMachine(path, strTemp);
     7460                mUserData->mSnapshotFolder = strTemp;
    74687461            }
    74697462
     
    77207713        Assert(!mSSData->mStateFilePath.isEmpty());
    77217714        /* try to make the file name relative to the settings file dir */
    7722         calculateRelativePath(mSSData->mStateFilePath, config.strStateFile);
    7723         if (!config.strStateFile.length())
    7724             // path is not relative (e.g. because snapshot folder was changed to a non-default location):
    7725             config.strStateFile = mSSData->mStateFilePath;
     7715        copyPathRelativeToMachine(mSSData->mStateFilePath, config.strStateFile);
    77267716    }
    77277717    else
     
    81528142            if (!mSSData->mStateFilePath.isEmpty())
    81538143                /* try to make the file name relative to the settings file dir */
    8154                 calculateRelativePath(mSSData->mStateFilePath, mData->pMachineConfigFile->strStateFile);
     8144                copyPathRelativeToMachine(mSSData->mStateFilePath, mData->pMachineConfigFile->strStateFile);
    81558145            else
    81568146                mData->pMachineConfigFile->strStateFile.setNull();
  • trunk/src/VBox/Main/MediumImpl.cpp

    r30377 r30380  
    28662866                                     aNewPath,
    28672867                                     pcszMediumPath + strlen(aOldPath));
    2868         Utf8Str path = newPath;
    2869         m->pVirtualBox->calculateRelativePath(path, path);
    28702868        unconst(m->strLocationFull) = newPath;
     2869
     2870        Utf8Str path;
     2871        m->pVirtualBox->copyPathRelativeToConfig(newPath, path);
    28712872        unconst(m->strLocation) = path;
    28722873
  • trunk/src/VBox/Main/SnapshotImpl.cpp

    r30377 r30380  
    762762    /* stateFile (optional) */
    763763    if (!stateFilePath().isEmpty())
    764         /* try to make the file name relative to the settings file dir */
    765         m->pMachine->calculateRelativePath(stateFilePath(), data.strStateFile);
     764        m->pMachine->copyPathRelativeToMachine(stateFilePath(), data.strStateFile);
    766765    else
    767766        data.strStateFile.setNull();
  • trunk/src/VBox/Main/VirtualBoxImpl.cpp

    r30377 r30380  
    33953395
    33963396/**
    3397  * Tries to calculate the relative path of the given absolute path using the
    3398  * directory of the VirtualBox settings file as the base directory.
    3399  *
    3400  * @param  aPath    Absolute path to calculate the relative path for.
    3401  * @param  aResult  Where to put the result (used only when it's possible to
    3402  *                  make a relative path from the given absolute path; otherwise
    3403  *                  left untouched).
    3404  *
    3405  * @note Doesn't lock any object.
    3406  */
    3407 void VirtualBox::calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult)
     3397 * Copies strSource to strTarget, making it relative to the VirtualBox config folder
     3398 * if it is a subdirectory thereof, or simply copying it otherwise.
     3399 *
     3400 * @param strSource Path to evalue and copy.
     3401 * @param strTarget Buffer to receive target path.
     3402 */
     3403void VirtualBox::copyPathRelativeToConfig(const Utf8Str &strSource,
     3404                                          Utf8Str &strTarget)
    34083405{
    34093406    AutoCaller autoCaller(this);
    34103407    AssertComRCReturnVoid(autoCaller.rc());
    34113408
    3412     /* no need to lock since mHomeDir is const */
    3413 
    3414     Utf8Str settingsDir = m->strHomeDir;
    3415 
    3416     if (RTPathStartsWith(strPath.c_str(), settingsDir.c_str()))
    3417     {
    3418         /* when assigning, we create a separate Utf8Str instance because both
    3419          * aPath and aResult can point to the same memory location when this
    3420          * func is called (if we just do aResult = aPath, aResult will be freed
    3421          * first, and since its the same as aPath, an attempt to copy garbage
    3422          * will be made. */
    3423         aResult = Utf8Str(strPath.c_str() + settingsDir.length() + 1);
    3424     }
     3409    // no need to lock since mHomeDir is const
     3410
     3411    // use strTarget as a temporary buffer to hold the machine settings dir
     3412    strTarget = m->strHomeDir;
     3413    if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
     3414        // is relative: then append what's left
     3415        strTarget.append(strSource.c_str() + strTarget.length());     // include '/'
     3416    else
     3417        // is not relative: then overwrite
     3418        strTarget = strSource;
    34253419}
    34263420
  • trunk/src/VBox/Main/include/MachineImpl.h

    r29864 r30380  
    615615
    616616    int calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult);
    617     void calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult);
     617    void copyPathRelativeToMachine(const Utf8Str &strSource, Utf8Str &strTarget);
    618618
    619619    void getLogFolder(Utf8Str &aLogFolder);
  • trunk/src/VBox/Main/include/VirtualBoxImpl.h

    r30345 r30380  
    282282
    283283    int calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult);
    284     void calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult);
     284    void copyPathRelativeToConfig(const Utf8Str &strSource, Utf8Str &strTarget);
    285285
    286286    HRESULT registerHardDisk(Medium *aHardDisk, bool *pfNeedsSaveSettings);
Note: See TracChangeset for help on using the changeset viewer.

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