Index: /trunk/src/VBox/Runtime/r3/dir.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/dir.cpp	(revision 78177)
+++ /trunk/src/VBox/Runtime/r3/dir.cpp	(revision 78178)
@@ -33,4 +33,5 @@
 #include "internal/iprt.h"
 
+#include <iprt/alloca.h>
 #include <iprt/assert.h>
 #include <iprt/file.h>
@@ -727,11 +728,23 @@
 RTDECL(int) RTDirFlushParent(const char *pszChild)
 {
-    char szPath[RTPATH_MAX];
-    int rc = RTStrCopy(szPath, sizeof(szPath), pszChild);
-    if (RT_SUCCESS(rc))
-    {
-        RTPathStripFilename(szPath);
-        rc = RTDirFlush(szPath);
-    }
+    char        *pszPath;
+    char        *pszPathFree = NULL;
+    size_t const cchChild    = strlen(pszChild);
+    if (cchChild < RTPATH_MAX)
+        pszPath = (char *)alloca(cchChild);
+    else
+    {
+        pszPathFree = pszPath = (char *)RTMemTmpAlloc(cchChild + 1);
+        if (!pszPath)
+            return VERR_NO_TMP_MEMORY;
+    }
+    memcpy(pszPath, pszChild, cchChild);
+    pszPath[cchChild] = '\0';
+    RTPathStripFilename(pszPath);
+
+    int rc = RTDirFlush(pszPath);
+
+    if (pszPathFree)
+        RTMemTmpFree(pszPathFree);
     return rc;
 }
Index: /trunk/src/VBox/Runtime/r3/dir2.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/dir2.cpp	(revision 78177)
+++ /trunk/src/VBox/Runtime/r3/dir2.cpp	(revision 78178)
@@ -33,4 +33,5 @@
 #include "internal/iprt.h"
 
+#include <iprt/alloca.h>
 #include <iprt/assert.h>
 #include <iprt/file.h>
@@ -52,8 +53,9 @@
  * @param   cchDir              The length of the directory we're recursing into,
  *                              including the trailing slash.
+ * @param   cbBuf               Size of the buffer @a pszBuf points to.
  * @param   pDirEntry           The dir entry buffer.  (Shared to save stack.)
  * @param   pObjInfo            The object info buffer.  (ditto)
  */
-static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo)
+static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, size_t cbBuf, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo)
 {
     AssertReturn(RTPATH_IS_SLASH(pszBuf[cchDir - 1]), VERR_INTERNAL_ERROR_4);
@@ -71,5 +73,5 @@
         {
             /* Construct the full name of the entry. */
-            if (cchDir + pDirEntry->cbName + 1 /* dir slash */ >= RTPATH_MAX)
+            if (cchDir + pDirEntry->cbName + 1 /* dir slash */ >= cbBuf)
             {
                 rc = VERR_FILENAME_TOO_LONG;
@@ -102,5 +104,5 @@
                     pszBuf[cchSubDir++] = '/';
                     pszBuf[cchSubDir]   = '\0';
-                    rc = rtDirRemoveRecursiveSub(pszBuf, cchSubDir, pDirEntry, pObjInfo);
+                    rc = rtDirRemoveRecursiveSub(pszBuf, cchSubDir, cbBuf, pDirEntry, pObjInfo);
                     if (RT_SUCCESS(rc))
                     {
@@ -135,48 +137,80 @@
     AssertReturn(!(fFlags & ~RTDIRRMREC_F_VALID_MASK), VERR_INVALID_PARAMETER);
 
-    /* Get an absolute path because this is easier to work with. */
+    /*
+     * Allocate path buffer.
+     */
+    char  *pszAbsPath;
+    size_t cbAbsPathBuf   = RTPATH_BIG_MAX;
+    char  *pszAbsPathFree = pszAbsPath = (char *)RTMemTmpAlloc(cbAbsPathBuf);
+    if (!pszAbsPath)
+    {
+        cbAbsPathBuf = RTPATH_MAX;
+        pszAbsPath   = (char *)alloca(RTPATH_MAX);
+    }
+
+    /*
+     * Get an absolute path because this is easier to work with and
+     * eliminates any races with changing CWD.
+     */
     /** @todo use RTPathReal here instead? */
-    char szAbsPath[RTPATH_MAX];
-    int rc = RTPathAbs(pszPath, szAbsPath, sizeof(szAbsPath));
-    if (RT_FAILURE(rc))
-        return rc;
-
-    /* This API is not permitted applied to the root of anything. */
-    if (RTPathCountComponents(szAbsPath) <= 1)
-        return VERR_ACCESS_DENIED;
-
-    /* Because of the above restriction, we never have to deal with the root
-       slash problem and can safely strip any trailing slashes and add a
-       definite one. */
-    RTPathStripTrailingSlash(szAbsPath);
-    size_t cchAbsPath = strlen(szAbsPath);
-    if (cchAbsPath + 1 >= RTPATH_MAX)
-        return VERR_FILENAME_TOO_LONG;
-    szAbsPath[cchAbsPath++] = '/';
-    szAbsPath[cchAbsPath] = 0;
-
-    /* Check if it exists so we can return quietly if it doesn't. */
-    RTFSOBJINFO SharedObjInfoBuf;
-    rc = RTPathQueryInfoEx(szAbsPath, &SharedObjInfoBuf, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
-    if (   rc == VERR_PATH_NOT_FOUND
-        || rc == VERR_FILE_NOT_FOUND)
-        return VINF_SUCCESS;
-    if (RT_FAILURE(rc))
-        return rc;
-    if (!RTFS_IS_DIRECTORY(SharedObjInfoBuf.Attr.fMode))
-        return VERR_NOT_A_DIRECTORY;
-
-    /* We're all set for the recursion now, so get going. */
-    RTDIRENTRY  SharedDirEntryBuf;
-    rc = rtDirRemoveRecursiveSub(szAbsPath, cchAbsPath, &SharedDirEntryBuf, &SharedObjInfoBuf);
-
-    /* Remove the specified directory if desired and removing the content was
-       successful. */
-    if (   RT_SUCCESS(rc)
-        && !(fFlags & RTDIRRMREC_F_CONTENT_ONLY))
+    int rc = RTPathAbs(pszPath, pszAbsPath, cbAbsPathBuf);
+    if (RT_SUCCESS(rc))
     {
-        szAbsPath[cchAbsPath] = 0;
-        rc = RTDirRemove(szAbsPath);
+        /*
+         * This API is not permitted applied to the root of anything.
+         */
+        if (RTPathCountComponents(pszAbsPath) <= 1)
+            rc = VERR_ACCESS_DENIED;
+        else
+        {
+            /*
+             * Because of the above restriction, we never have to deal with the root
+             * slash problem and can safely strip any trailing slashes and add a
+             * definite one.
+             */
+            RTPathStripTrailingSlash(pszAbsPath);
+            size_t cchAbsPath = strlen(pszAbsPath);
+            if (cchAbsPath + 1 < cbAbsPathBuf)
+            {
+                pszAbsPath[cchAbsPath++] = RTPATH_SLASH;
+                pszAbsPath[cchAbsPath]   = '\0';
+
+                /*
+                 * Check if it exists so we can return quietly if it doesn't.
+                 */
+                RTFSOBJINFO SharedObjInfoBuf;
+                rc = RTPathQueryInfoEx(pszAbsPath, &SharedObjInfoBuf, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
+                if (   rc == VERR_PATH_NOT_FOUND
+                    || rc == VERR_FILE_NOT_FOUND)
+                    rc = VINF_SUCCESS;
+                else if (   RT_SUCCESS(rc)
+                         && RTFS_IS_DIRECTORY(SharedObjInfoBuf.Attr.fMode))
+                {
+                    /*
+                     * We're all set for the recursion now, so get going.
+                     */
+                    RTDIRENTRY SharedDirEntryBuf;
+                    rc = rtDirRemoveRecursiveSub(pszAbsPath, cchAbsPath, cbAbsPathBuf, &SharedDirEntryBuf, &SharedObjInfoBuf);
+
+                    /*
+                     * Remove the specified directory if desired and removing the content was successful.
+                     */
+                    if (   RT_SUCCESS(rc)
+                        && !(fFlags & RTDIRRMREC_F_CONTENT_ONLY))
+                    {
+                        pszAbsPath[cchAbsPath] = 0;
+                        rc = RTDirRemove(pszAbsPath);
+                    }
+                }
+                else if (RT_SUCCESS(rc))
+                    rc = VERR_NOT_A_DIRECTORY;
+
+            }
+            else
+                rc = VERR_FILENAME_TOO_LONG;
+        }
     }
+    if (pszAbsPathFree)
+        RTMemTmpFree(pszAbsPathFree);
     return rc;
 }
Index: /trunk/src/VBox/Runtime/r3/posix/path-posix.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/posix/path-posix.cpp	(revision 78177)
+++ /trunk/src/VBox/Runtime/r3/posix/path-posix.cpp	(revision 78178)
@@ -43,4 +43,5 @@
 #include <iprt/env.h>
 #include <iprt/assert.h>
+#include <iprt/mem.h>
 #include <iprt/string.h>
 #include <iprt/err.h>
@@ -346,11 +347,39 @@
 RTDECL(int)  RTPathGetCurrent(char *pszPath, size_t cchPath)
 {
-    int rc;
+    /*
+     * Try with a reasonably sized buffer first.
+     */
     char szNativeCurDir[RTPATH_MAX];
     if (getcwd(szNativeCurDir, sizeof(szNativeCurDir)) != NULL)
-        rc = rtPathFromNativeCopy(pszPath, cchPath, szNativeCurDir, NULL);
-    else
-        rc = RTErrConvertFromErrno(errno);
-    return rc;
+        return rtPathFromNativeCopy(pszPath, cchPath, szNativeCurDir, NULL);
+
+    /*
+     * Retry a few times with really big buffers if we failed because CWD is unreasonably long.
+     */
+    int iErr = errno;
+    if (iErr != ERANGE)
+        return RTErrConvertFromErrno(iErr);
+
+    size_t cbNativeTmp = RTPATH_BIG_MAX;
+    for (;;)
+    {
+        char *pszNativeTmp = (char *)RTMemTmpAlloc(cbNativeTmp);
+        if (!pszNativeTmp)
+            return VERR_NO_TMP_MEMORY;
+        if (getcwd(pszNativeTmp, cbNativeTmp) != NULL)
+        {
+            int rc = rtPathFromNativeCopy(pszPath, cchPath, pszNativeTmp, NULL);
+            RTMemTmpFree(pszNativeTmp);
+            return rc;
+        }
+        iErr = errno;
+        RTMemTmpFree(pszNativeTmp);
+        if (iErr != ERANGE)
+            return RTErrConvertFromErrno(iErr);
+
+        cbNativeTmp += RTPATH_BIG_MAX;
+        if (cbNativeTmp > RTPATH_BIG_MAX * 4)
+            return VERR_FILENAME_TOO_LONG;
+    }
 }
 
