VirtualBox

Changeset 78178 in vbox


Ignore:
Timestamp:
Apr 17, 2019 6:32:29 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
130096
Message:

IPRT: Relaxing RTPATH_MAX in a couple of places. bugref:9172

Location:
trunk/src/VBox/Runtime/r3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/dir.cpp

    r78153 r78178  
    3333#include "internal/iprt.h"
    3434
     35#include <iprt/alloca.h>
    3536#include <iprt/assert.h>
    3637#include <iprt/file.h>
     
    727728RTDECL(int) RTDirFlushParent(const char *pszChild)
    728729{
    729     char szPath[RTPATH_MAX];
    730     int rc = RTStrCopy(szPath, sizeof(szPath), pszChild);
    731     if (RT_SUCCESS(rc))
    732     {
    733         RTPathStripFilename(szPath);
    734         rc = RTDirFlush(szPath);
    735     }
     730    char        *pszPath;
     731    char        *pszPathFree = NULL;
     732    size_t const cchChild    = strlen(pszChild);
     733    if (cchChild < RTPATH_MAX)
     734        pszPath = (char *)alloca(cchChild);
     735    else
     736    {
     737        pszPathFree = pszPath = (char *)RTMemTmpAlloc(cchChild + 1);
     738        if (!pszPath)
     739            return VERR_NO_TMP_MEMORY;
     740    }
     741    memcpy(pszPath, pszChild, cchChild);
     742    pszPath[cchChild] = '\0';
     743    RTPathStripFilename(pszPath);
     744
     745    int rc = RTDirFlush(pszPath);
     746
     747    if (pszPathFree)
     748        RTMemTmpFree(pszPathFree);
    736749    return rc;
    737750}
  • trunk/src/VBox/Runtime/r3/dir2.cpp

    r78109 r78178  
    3333#include "internal/iprt.h"
    3434
     35#include <iprt/alloca.h>
    3536#include <iprt/assert.h>
    3637#include <iprt/file.h>
     
    5253 * @param   cchDir              The length of the directory we're recursing into,
    5354 *                              including the trailing slash.
     55 * @param   cbBuf               Size of the buffer @a pszBuf points to.
    5456 * @param   pDirEntry           The dir entry buffer.  (Shared to save stack.)
    5557 * @param   pObjInfo            The object info buffer.  (ditto)
    5658 */
    57 static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo)
     59static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, size_t cbBuf, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo)
    5860{
    5961    AssertReturn(RTPATH_IS_SLASH(pszBuf[cchDir - 1]), VERR_INTERNAL_ERROR_4);
     
    7173        {
    7274            /* Construct the full name of the entry. */
    73             if (cchDir + pDirEntry->cbName + 1 /* dir slash */ >= RTPATH_MAX)
     75            if (cchDir + pDirEntry->cbName + 1 /* dir slash */ >= cbBuf)
    7476            {
    7577                rc = VERR_FILENAME_TOO_LONG;
     
    102104                    pszBuf[cchSubDir++] = '/';
    103105                    pszBuf[cchSubDir]   = '\0';
    104                     rc = rtDirRemoveRecursiveSub(pszBuf, cchSubDir, pDirEntry, pObjInfo);
     106                    rc = rtDirRemoveRecursiveSub(pszBuf, cchSubDir, cbBuf, pDirEntry, pObjInfo);
    105107                    if (RT_SUCCESS(rc))
    106108                    {
     
    135137    AssertReturn(!(fFlags & ~RTDIRRMREC_F_VALID_MASK), VERR_INVALID_PARAMETER);
    136138
    137     /* Get an absolute path because this is easier to work with. */
     139    /*
     140     * Allocate path buffer.
     141     */
     142    char  *pszAbsPath;
     143    size_t cbAbsPathBuf   = RTPATH_BIG_MAX;
     144    char  *pszAbsPathFree = pszAbsPath = (char *)RTMemTmpAlloc(cbAbsPathBuf);
     145    if (!pszAbsPath)
     146    {
     147        cbAbsPathBuf = RTPATH_MAX;
     148        pszAbsPath   = (char *)alloca(RTPATH_MAX);
     149    }
     150
     151    /*
     152     * Get an absolute path because this is easier to work with and
     153     * eliminates any races with changing CWD.
     154     */
    138155    /** @todo use RTPathReal here instead? */
    139     char szAbsPath[RTPATH_MAX];
    140     int rc = RTPathAbs(pszPath, szAbsPath, sizeof(szAbsPath));
    141     if (RT_FAILURE(rc))
    142         return rc;
    143 
    144     /* This API is not permitted applied to the root of anything. */
    145     if (RTPathCountComponents(szAbsPath) <= 1)
    146         return VERR_ACCESS_DENIED;
    147 
    148     /* Because of the above restriction, we never have to deal with the root
    149        slash problem and can safely strip any trailing slashes and add a
    150        definite one. */
    151     RTPathStripTrailingSlash(szAbsPath);
    152     size_t cchAbsPath = strlen(szAbsPath);
    153     if (cchAbsPath + 1 >= RTPATH_MAX)
    154         return VERR_FILENAME_TOO_LONG;
    155     szAbsPath[cchAbsPath++] = '/';
    156     szAbsPath[cchAbsPath] = 0;
    157 
    158     /* Check if it exists so we can return quietly if it doesn't. */
    159     RTFSOBJINFO SharedObjInfoBuf;
    160     rc = RTPathQueryInfoEx(szAbsPath, &SharedObjInfoBuf, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
    161     if (   rc == VERR_PATH_NOT_FOUND
    162         || rc == VERR_FILE_NOT_FOUND)
    163         return VINF_SUCCESS;
    164     if (RT_FAILURE(rc))
    165         return rc;
    166     if (!RTFS_IS_DIRECTORY(SharedObjInfoBuf.Attr.fMode))
    167         return VERR_NOT_A_DIRECTORY;
    168 
    169     /* We're all set for the recursion now, so get going. */
    170     RTDIRENTRY  SharedDirEntryBuf;
    171     rc = rtDirRemoveRecursiveSub(szAbsPath, cchAbsPath, &SharedDirEntryBuf, &SharedObjInfoBuf);
    172 
    173     /* Remove the specified directory if desired and removing the content was
    174        successful. */
    175     if (   RT_SUCCESS(rc)
    176         && !(fFlags & RTDIRRMREC_F_CONTENT_ONLY))
     156    int rc = RTPathAbs(pszPath, pszAbsPath, cbAbsPathBuf);
     157    if (RT_SUCCESS(rc))
    177158    {
    178         szAbsPath[cchAbsPath] = 0;
    179         rc = RTDirRemove(szAbsPath);
     159        /*
     160         * This API is not permitted applied to the root of anything.
     161         */
     162        if (RTPathCountComponents(pszAbsPath) <= 1)
     163            rc = VERR_ACCESS_DENIED;
     164        else
     165        {
     166            /*
     167             * Because of the above restriction, we never have to deal with the root
     168             * slash problem and can safely strip any trailing slashes and add a
     169             * definite one.
     170             */
     171            RTPathStripTrailingSlash(pszAbsPath);
     172            size_t cchAbsPath = strlen(pszAbsPath);
     173            if (cchAbsPath + 1 < cbAbsPathBuf)
     174            {
     175                pszAbsPath[cchAbsPath++] = RTPATH_SLASH;
     176                pszAbsPath[cchAbsPath]   = '\0';
     177
     178                /*
     179                 * Check if it exists so we can return quietly if it doesn't.
     180                 */
     181                RTFSOBJINFO SharedObjInfoBuf;
     182                rc = RTPathQueryInfoEx(pszAbsPath, &SharedObjInfoBuf, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
     183                if (   rc == VERR_PATH_NOT_FOUND
     184                    || rc == VERR_FILE_NOT_FOUND)
     185                    rc = VINF_SUCCESS;
     186                else if (   RT_SUCCESS(rc)
     187                         && RTFS_IS_DIRECTORY(SharedObjInfoBuf.Attr.fMode))
     188                {
     189                    /*
     190                     * We're all set for the recursion now, so get going.
     191                     */
     192                    RTDIRENTRY SharedDirEntryBuf;
     193                    rc = rtDirRemoveRecursiveSub(pszAbsPath, cchAbsPath, cbAbsPathBuf, &SharedDirEntryBuf, &SharedObjInfoBuf);
     194
     195                    /*
     196                     * Remove the specified directory if desired and removing the content was successful.
     197                     */
     198                    if (   RT_SUCCESS(rc)
     199                        && !(fFlags & RTDIRRMREC_F_CONTENT_ONLY))
     200                    {
     201                        pszAbsPath[cchAbsPath] = 0;
     202                        rc = RTDirRemove(pszAbsPath);
     203                    }
     204                }
     205                else if (RT_SUCCESS(rc))
     206                    rc = VERR_NOT_A_DIRECTORY;
     207
     208            }
     209            else
     210                rc = VERR_FILENAME_TOO_LONG;
     211        }
    180212    }
     213    if (pszAbsPathFree)
     214        RTMemTmpFree(pszAbsPathFree);
    181215    return rc;
    182216}
  • trunk/src/VBox/Runtime/r3/posix/path-posix.cpp

    r76553 r78178  
    4343#include <iprt/env.h>
    4444#include <iprt/assert.h>
     45#include <iprt/mem.h>
    4546#include <iprt/string.h>
    4647#include <iprt/err.h>
     
    346347RTDECL(int)  RTPathGetCurrent(char *pszPath, size_t cchPath)
    347348{
    348     int rc;
     349    /*
     350     * Try with a reasonably sized buffer first.
     351     */
    349352    char szNativeCurDir[RTPATH_MAX];
    350353    if (getcwd(szNativeCurDir, sizeof(szNativeCurDir)) != NULL)
    351         rc = rtPathFromNativeCopy(pszPath, cchPath, szNativeCurDir, NULL);
    352     else
    353         rc = RTErrConvertFromErrno(errno);
    354     return rc;
     354        return rtPathFromNativeCopy(pszPath, cchPath, szNativeCurDir, NULL);
     355
     356    /*
     357     * Retry a few times with really big buffers if we failed because CWD is unreasonably long.
     358     */
     359    int iErr = errno;
     360    if (iErr != ERANGE)
     361        return RTErrConvertFromErrno(iErr);
     362
     363    size_t cbNativeTmp = RTPATH_BIG_MAX;
     364    for (;;)
     365    {
     366        char *pszNativeTmp = (char *)RTMemTmpAlloc(cbNativeTmp);
     367        if (!pszNativeTmp)
     368            return VERR_NO_TMP_MEMORY;
     369        if (getcwd(pszNativeTmp, cbNativeTmp) != NULL)
     370        {
     371            int rc = rtPathFromNativeCopy(pszPath, cchPath, pszNativeTmp, NULL);
     372            RTMemTmpFree(pszNativeTmp);
     373            return rc;
     374        }
     375        iErr = errno;
     376        RTMemTmpFree(pszNativeTmp);
     377        if (iErr != ERANGE)
     378            return RTErrConvertFromErrno(iErr);
     379
     380        cbNativeTmp += RTPATH_BIG_MAX;
     381        if (cbNativeTmp > RTPATH_BIG_MAX * 4)
     382            return VERR_FILENAME_TOO_LONG;
     383    }
    355384}
    356385
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