VirtualBox

Changeset 78101 in vbox


Ignore:
Timestamp:
Apr 10, 2019 4:56:12 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
129957
Message:

Main: Appliance::i_searchUniqueImageFilePath: Untested attempt to clean up some leaky overcooked spaghetti-code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-server/ApplianceImpl.cpp

    r76592 r78101  
    2222#define LOG_GROUP LOG_GROUP_MAIN_APPLIANCE
    2323#include <iprt/path.h>
     24#include <iprt/cpp/path.h>
    2425#include <iprt/cpp/utils.h>
    2526#include <VBox/com/array.h>
     
    915916HRESULT Appliance::i_searchUniqueImageFilePath(const Utf8Str &aMachineFolder, DeviceType_T aDeviceType, Utf8Str &aName) const
    916917{
    917     IMedium *pMedium = NULL;
    918     char *tmpName = RTStrDup(aName.c_str());
    919     char *tmpAbsName = RTPathAbsExDup(aMachineFolder.c_str(), tmpName);
    920     int i = 1;
    921     /* Check if the file exists or if a medium with this path is registered already */
    922     while (    RTPathExists(tmpAbsName)
    923             || mVirtualBox->OpenMedium(Bstr(tmpAbsName).raw(), aDeviceType, AccessMode_ReadWrite,
    924                                        FALSE /* fForceNewUuid */,  &pMedium) != VBOX_E_OBJECT_NOT_FOUND)
    925     {
    926         RTStrFree(tmpAbsName);
    927         char *tmpDir = RTStrDup(aName.c_str());
    928         RTPathStripFilename(tmpDir);
    929         char *tmpFile = RTStrDup(RTPathFilename(aName.c_str()));
    930         RTPathStripSuffix(tmpFile);
    931         const char *pszTmpSuff = RTPathSuffix(aName.c_str());
    932         if (!strcmp(tmpDir, "."))
    933             RTStrAPrintf(&tmpName, "%s_%d%s", tmpFile, i, pszTmpSuff);
    934         else
    935             RTStrAPrintf(&tmpName, "%s%c%s_%d%s", tmpDir, RTPATH_DELIMITER, tmpFile, i, pszTmpSuff);
    936         tmpAbsName = RTPathAbsExDup(aMachineFolder.c_str(), tmpName);
    937         RTStrFree(tmpFile);
    938         RTStrFree(tmpDir);
    939         ++i;
    940     }
    941     aName = tmpName;
    942     RTStrFree(tmpName);
    943 
    944     return S_OK;
     918    /*
     919     * Check if the file exists or if a medium with this path is registered already
     920     */
     921    Utf8Str strAbsName;
     922    ssize_t offDashNum = -1;
     923    ssize_t cchDashNum = 0;
     924    for (unsigned i = 1;; i++)
     925    {
     926        /* Complete the path (could be relative to machine folder). */
     927        int rc = RTPathAbsExCxx(strAbsName, aMachineFolder, aName);
     928        AssertRCReturn(rc, Global::vboxStatusCodeToCOM(rc));  /** @todo stupid caller ignores this */
     929
     930        /* Check that the file does not exist and that there is no media somehow matching the name. */
     931        if (!RTPathExists(strAbsName.c_str()))
     932        {
     933            ComPtr<IMedium> ptrMedium;
     934            HRESULT hrc = mVirtualBox->OpenMedium(Bstr(strAbsName).raw(), aDeviceType, AccessMode_ReadWrite,
     935                                                  FALSE /* fForceNewUuid */, ptrMedium.asOutParam());
     936            if (hrc == VBOX_E_OBJECT_NOT_FOUND)
     937                return S_OK;
     938        }
     939
     940        /* Insert '_%i' before the suffix and try again. */
     941        if (offDashNum < 0)
     942        {
     943            const char *pszSuffix = RTPathSuffix(aName.c_str());
     944            offDashNum = pszSuffix ? pszSuffix - aName.c_str() : aName.length();
     945        }
     946        char   szTmp[32];
     947        size_t cchTmp = RTStrPrintf(szTmp, sizeof(szTmp),  "_%u", i);
     948        aName.replace(offDashNum, cchDashNum, szTmp, cchTmp);
     949        cchDashNum = cchTmp;
     950    }
    945951}
    946952
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