Changeset 78178 in vbox
- Timestamp:
- Apr 17, 2019 6:32:29 PM (6 years ago)
- svn:sync-xref-src-repo-rev:
- 130096
- Location:
- trunk/src/VBox/Runtime/r3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/dir.cpp
r78153 r78178 33 33 #include "internal/iprt.h" 34 34 35 #include <iprt/alloca.h> 35 36 #include <iprt/assert.h> 36 37 #include <iprt/file.h> … … 727 728 RTDECL(int) RTDirFlushParent(const char *pszChild) 728 729 { 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); 736 749 return rc; 737 750 } -
trunk/src/VBox/Runtime/r3/dir2.cpp
r78109 r78178 33 33 #include "internal/iprt.h" 34 34 35 #include <iprt/alloca.h> 35 36 #include <iprt/assert.h> 36 37 #include <iprt/file.h> … … 52 53 * @param cchDir The length of the directory we're recursing into, 53 54 * including the trailing slash. 55 * @param cbBuf Size of the buffer @a pszBuf points to. 54 56 * @param pDirEntry The dir entry buffer. (Shared to save stack.) 55 57 * @param pObjInfo The object info buffer. (ditto) 56 58 */ 57 static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo)59 static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, size_t cbBuf, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo) 58 60 { 59 61 AssertReturn(RTPATH_IS_SLASH(pszBuf[cchDir - 1]), VERR_INTERNAL_ERROR_4); … … 71 73 { 72 74 /* 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) 74 76 { 75 77 rc = VERR_FILENAME_TOO_LONG; … … 102 104 pszBuf[cchSubDir++] = '/'; 103 105 pszBuf[cchSubDir] = '\0'; 104 rc = rtDirRemoveRecursiveSub(pszBuf, cchSubDir, pDirEntry, pObjInfo);106 rc = rtDirRemoveRecursiveSub(pszBuf, cchSubDir, cbBuf, pDirEntry, pObjInfo); 105 107 if (RT_SUCCESS(rc)) 106 108 { … … 135 137 AssertReturn(!(fFlags & ~RTDIRRMREC_F_VALID_MASK), VERR_INVALID_PARAMETER); 136 138 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 */ 138 155 /** @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)) 177 158 { 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 } 180 212 } 213 if (pszAbsPathFree) 214 RTMemTmpFree(pszAbsPathFree); 181 215 return rc; 182 216 } -
trunk/src/VBox/Runtime/r3/posix/path-posix.cpp
r76553 r78178 43 43 #include <iprt/env.h> 44 44 #include <iprt/assert.h> 45 #include <iprt/mem.h> 45 46 #include <iprt/string.h> 46 47 #include <iprt/err.h> … … 346 347 RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cchPath) 347 348 { 348 int rc; 349 /* 350 * Try with a reasonably sized buffer first. 351 */ 349 352 char szNativeCurDir[RTPATH_MAX]; 350 353 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 } 355 384 } 356 385
Note:
See TracChangeset
for help on using the changeset viewer.