VirtualBox

Ignore:
Timestamp:
Apr 17, 2019 12:30:08 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
130068
Message:

IPRT: Added RTPATHABS_F_ENSURE_TRAILING_SLASH to RTPathAbsEx and fixed a couple of issues related to VERR_BUFFER_OVERFLOW code paths. bugref:9172

Location:
trunk/src/VBox/Runtime/common/path
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/path/RTPathAbsDup.cpp

    r78048 r78153  
    3232#include <iprt/path.h>
    3333
    34 #include <iprt/err.h>
    35 #include <iprt/param.h>
    36 #include <iprt/string.h>
    37 
    3834
    3935/**
     
    4642RTDECL(char *) RTPathAbsDup(const char *pszPath)
    4743{
    48     /* Try with a static buffer first. */
    49     char szPath[RTPATH_MAX];
    50     int rc = RTPathAbs(pszPath, szPath, sizeof(szPath));
    51     if (RT_SUCCESS(rc))
    52         return RTStrDup(szPath);
    53 
    54     /* If it looks like we ran out of buffer space, double the size until
    55        we reach 64 KB. */
    56     if (rc == VERR_FILENAME_TOO_LONG || rc == VERR_BUFFER_OVERFLOW)
    57     {
    58         size_t cbBuf = RTPATH_MAX;
    59         do
    60         {
    61             cbBuf *= 2;
    62             char *pszBuf = RTStrAlloc(cbBuf);
    63             if (!pszBuf)
    64                 break;
    65             rc = RTPathAbs(pszPath, pszBuf, cbBuf);
    66             if (RT_SUCCESS(rc))
    67                 return pszBuf;
    68             RTStrFree(pszBuf);
    69         } while (cbBuf <= _32K);
    70     }
    71     return NULL;
     44    return RTPathAbsExDup(NULL, pszPath, RTPATH_STR_F_STYLE_HOST);
    7245}
    7346
  • trunk/src/VBox/Runtime/common/path/RTPathAbsEx.cpp

    r78098 r78153  
    185185    {
    186186        Assert(pBaseParsed->cComps > 1);
    187         if (iLast >= 0 || (pParsed->fProps & RTPATH_PROP_DIR_SLASH))
     187        if (   iLast >= 0
     188            || (pParsed->fProps & RTPATH_PROP_DIR_SLASH)
     189            || (fFlags & RTPATHABS_F_ENSURE_TRAILING_SLASH) )
    188190            pBaseParsed->fProps |= RTPATH_PROP_DIR_SLASH;
    189191        else
     
    191193    }
    192194
     195    /* Apply the trailing flash flag to the input path: */
     196    if (   iLast >= 0
     197        && (fFlags & RTPATHABS_F_ENSURE_TRAILING_SLASH))
     198        pParsed->fProps |= RTPATH_PROP_DIR_SLASH;
     199
    193200    /*
    194201     * Combine the two.  RTPathParsedReassemble can handle in place stuff, as
     
    207214            rc = RTPathParsedReassemble(pszPath, pParsed, fFlags & RTPATH_STR_F_STYLE_MASK,
    208215                                        &pszAbsPath[cchBaseInPlace], *pcbAbsPath - cchBaseInPlace);
    209             *pcbAbsPath = cchBaseInPlace + pParsed->cchPath;
    210216            if (RT_SUCCESS(rc))
     217            {
     218                *pcbAbsPath = cchBaseInPlace + pParsed->cchPath;
    211219                Assert(*pcbAbsPath == strlen(pszAbsPath));
     220            }
     221            else
     222                *pcbAbsPath = cchBaseInPlace + pParsed->cchPath + 1;
    212223        }
    213224        else
     
    219230        {
    220231            RTPathParsedReassemble(pszPath, pParsed, fFlags & RTPATH_STR_F_STYLE_MASK, pszAbsPath, 0);
    221             *pcbAbsPath = pBaseParsed->cchPath + pParsed->cchPath;
     232            *pcbAbsPath = pBaseParsed->cchPath + pParsed->cchPath + 1;
    222233        }
    223234        else
    224             *pcbAbsPath = pBaseParsed->cchPath;
     235            *pcbAbsPath = pBaseParsed->cchPath + 1;
    225236    }
    226237
     
    448459        i++;
    449460    }
     461
     462    /*
     463     * Before we continue, ensure trailing slash if requested.
     464     */
     465    if (   (fFlags & RTPATHABS_F_ENSURE_TRAILING_SLASH)
     466        && iLast > 0)
     467        pParsed->fProps |= RTPATH_PROP_DIR_SLASH;
    450468
    451469    /*
     
    527545                {
    528546                    int rc2 = RTPathParsedReassemble(pszPath, pParsed, fFlags & RTPATH_STR_F_STYLE_MASK, pszAbsPath, 0);
    529                     Assert(rc2 == VERR_BUFFER_OVERFLOW); RT_NOREF(rc2);
     547                    Assert(rc2 == VERR_BUFFER_OVERFLOW);
    530548
    531549                    char *pszTmp = (char *)RTMemTmpAlloc(RTPATH_BIG_MAX);
    532550                    if (pszTmp)
    533551                    {
    534                         rc = RTPathGetCurrentDrive(pszTmp, RTPATH_BIG_MAX);
    535                         if (RT_SUCCESS(rc))
     552                        rc2 = RTPathGetCurrentDrive(pszTmp, RTPATH_BIG_MAX);
     553                        if (RT_SUCCESS(rc2))
    536554                            *pcbAbsPath = strlen(pszTmp) + pParsed->cchPath + 1;
    537555                        else
     
    570588                pParsed->aComps[i].cch = 0;
    571589    }
     590
     591    if (   (fFlags & RTPATHABS_F_ENSURE_TRAILING_SLASH)
     592        && pParsed->cComps > 1)
     593        pParsed->fProps |= RTPATH_PROP_DIR_SLASH;
     594
    572595    int rc = RTPathParsedReassemble(pszPath, pParsed, fFlags & RTPATH_STR_F_STYLE_MASK, pszAbsPath, *pcbAbsPath);
    573596    *pcbAbsPath = pParsed->cchPath + (rc == VERR_BUFFER_OVERFLOW);
     
    589612
    590613    AssertCompile(RTPATH_STR_F_STYLE_HOST == 0);
    591     AssertReturn(   RTPATH_STR_F_IS_VALID(fFlags, RTPATHABS_F_STOP_AT_BASE | RTPATHABS_F_STOP_AT_CWD)
     614    AssertReturn(   RTPATH_STR_F_IS_VALID(fFlags, RTPATHABS_F_STOP_AT_BASE | RTPATHABS_F_STOP_AT_CWD | RTPATHABS_F_ENSURE_TRAILING_SLASH)
    592615                 && !(fFlags & RTPATH_STR_F_MIDDLE), VERR_INVALID_FLAGS);
    593616    if ((fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_HOST)
  • trunk/src/VBox/Runtime/common/path/RTPathAbsExDup.cpp

    r78104 r78153  
    3939RTDECL(char *) RTPathAbsExDup(const char *pszBase, const char *pszPath, uint32_t fFlags)
    4040{
    41     char szPath[RTPATH_MAX];
    42     size_t cbPath = sizeof(szPath);
    43     int rc = RTPathAbsEx(pszBase, pszPath, fFlags, szPath, &cbPath);
    44     if (RT_SUCCESS(rc))
    45         return RTStrDup(szPath);
     41    unsigned    cTries    = 16;
     42    size_t      cbAbsPath = RTPATH_MAX / 2;
     43    for (;;)
     44    {
     45        char  *pszAbsPath = RTStrAlloc(cbAbsPath);
     46        if (pszAbsPath)
     47        {
     48            size_t cbActual = cbAbsPath;
     49            int rc = RTPathAbsEx(pszBase, pszPath, fFlags, pszAbsPath, &cbActual);
     50            if (RT_SUCCESS(rc))
     51            {
     52                if (cbActual < cbAbsPath / 2)
     53                    RTStrRealloc(&pszAbsPath, cbActual + 1);
     54                return pszAbsPath;
     55            }
    4656
    47     if (rc == VERR_BUFFER_OVERFLOW)
    48     {
    49         size_t   cbPrevPath = sizeof(szPath);
    50         uint32_t cTries = 8;
    51         while (cTries-- > 0)
    52         {
    53             cbPath     = RT_MAX(RT_ALIGN_Z(cbPath + 16, 64), cbPrevPath + 256);
    54             cbPrevPath = cbPath;
    55             char *pszAbsPath = (char *)RTStrAlloc(cbPath);
    56             if (pszAbsPath)
    57             {
    58                 rc = RTPathAbsEx(pszBase, pszPath, fFlags, pszAbsPath, &cbPath);
    59                 if (RT_SUCCESS(rc))
    60                     return pszAbsPath;
    61                 RTStrFree(pszAbsPath);
    62             }
    63             else
     57            RTStrFree(pszAbsPath);
     58
     59            if (rc != VERR_BUFFER_OVERFLOW)
    6460                break;
     61
     62            if (--cTries == 0)
     63                break;
     64
     65            cbAbsPath = RT_MAX(RT_ALIGN_Z(cbActual + 16, 64), cbAbsPath + 256);
    6566        }
     67        else
     68            break;
    6669    }
    6770    return 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