VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/dir2.cpp@ 78178

Last change on this file since 78178 was 78178, checked in by vboxsync, 6 years ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.9 KB
Line 
1/* $Id: dir2.cpp 78178 2019-04-17 18:32:29Z vboxsync $ */
2/** @file
3 * IPRT - Directory Manipulation, Part 2.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DIR
32#include <iprt/dir.h>
33#include "internal/iprt.h"
34
35#include <iprt/alloca.h>
36#include <iprt/assert.h>
37#include <iprt/file.h>
38#include <iprt/err.h>
39#include <iprt/log.h>
40#include <iprt/mem.h>
41#include <iprt/param.h>
42#include <iprt/path.h>
43#include <iprt/string.h>
44#include "internal/path.h"
45
46
47/**
48 * Recursion worker for RTDirRemoveRecursive.
49 *
50 * @returns IPRT status code.
51 * @param pszBuf The path buffer. Contains the abs path to the
52 * directory to recurse into. Trailing slash.
53 * @param cchDir The length of the directory we're recursing into,
54 * including the trailing slash.
55 * @param cbBuf Size of the buffer @a pszBuf points to.
56 * @param pDirEntry The dir entry buffer. (Shared to save stack.)
57 * @param pObjInfo The object info buffer. (ditto)
58 */
59static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, size_t cbBuf, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo)
60{
61 AssertReturn(RTPATH_IS_SLASH(pszBuf[cchDir - 1]), VERR_INTERNAL_ERROR_4);
62
63 /*
64 * Enumerate the directory content and dispose of it.
65 */
66 RTDIR hDir;
67 int rc = RTDirOpen(&hDir, pszBuf);
68 if (RT_FAILURE(rc))
69 return rc;
70 while (RT_SUCCESS(rc = RTDirRead(hDir, pDirEntry, NULL)))
71 {
72 if (!RTDirEntryIsStdDotLink(pDirEntry))
73 {
74 /* Construct the full name of the entry. */
75 if (cchDir + pDirEntry->cbName + 1 /* dir slash */ >= cbBuf)
76 {
77 rc = VERR_FILENAME_TOO_LONG;
78 break;
79 }
80 memcpy(&pszBuf[cchDir], pDirEntry->szName, pDirEntry->cbName + 1);
81
82 /* Deal with the unknown type. */
83 if (pDirEntry->enmType == RTDIRENTRYTYPE_UNKNOWN)
84 {
85 rc = RTPathQueryInfoEx(pszBuf, pObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
86 if (RT_SUCCESS(rc) && RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
87 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
88 else if (RT_SUCCESS(rc) && RTFS_IS_FILE(pObjInfo->Attr.fMode))
89 pDirEntry->enmType = RTDIRENTRYTYPE_FILE;
90 else if (RT_SUCCESS(rc) && RTFS_IS_SYMLINK(pObjInfo->Attr.fMode))
91 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
92 }
93
94 /* Try the delete the fs object. */
95 switch (pDirEntry->enmType)
96 {
97 case RTDIRENTRYTYPE_FILE:
98 rc = RTFileDelete(pszBuf);
99 break;
100
101 case RTDIRENTRYTYPE_DIRECTORY:
102 {
103 size_t cchSubDir = cchDir + pDirEntry->cbName;
104 pszBuf[cchSubDir++] = '/';
105 pszBuf[cchSubDir] = '\0';
106 rc = rtDirRemoveRecursiveSub(pszBuf, cchSubDir, cbBuf, pDirEntry, pObjInfo);
107 if (RT_SUCCESS(rc))
108 {
109 pszBuf[cchSubDir] = '\0';
110 rc = RTDirRemove(pszBuf);
111 }
112 break;
113 }
114
115 //case RTDIRENTRYTYPE_SYMLINK:
116 // rc = RTSymlinkDelete(pszBuf, 0);
117 // break;
118
119 default:
120 /** @todo not implemented yet. */
121 rc = VINF_SUCCESS;
122 break;
123 }
124 if (RT_FAILURE(rc))
125 break;
126 }
127 }
128 if (rc == VERR_NO_MORE_FILES)
129 rc = VINF_SUCCESS;
130 RTDirClose(hDir);
131 return rc;
132}
133
134
135RTDECL(int) RTDirRemoveRecursive(const char *pszPath, uint32_t fFlags)
136{
137 AssertReturn(!(fFlags & ~RTDIRRMREC_F_VALID_MASK), VERR_INVALID_PARAMETER);
138
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 */
155 /** @todo use RTPathReal here instead? */
156 int rc = RTPathAbs(pszPath, pszAbsPath, cbAbsPathBuf);
157 if (RT_SUCCESS(rc))
158 {
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 }
212 }
213 if (pszAbsPathFree)
214 RTMemTmpFree(pszAbsPathFree);
215 return rc;
216}
217
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette