VirtualBox

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

Last change on this file since 83998 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.5 KB
Line 
1/* $Id: dir2.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Directory Manipulation, Part 2.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 * @param fFlags RTDIRRMREC_F_XXX.
59 */
60static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, size_t cbBuf, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo,
61 uint32_t fFlags)
62{
63 AssertReturn(RTPATH_IS_SLASH(pszBuf[cchDir - 1]), VERR_INTERNAL_ERROR_4);
64
65 /*
66 * Enumerate the directory content and dispose of it.
67 */
68 RTDIR hDir;
69 int rc = RTDirOpenFiltered(&hDir, pszBuf, RTDIRFILTER_NONE, fFlags & RTDIRRMREC_F_NO_ABS_PATH ? RTDIR_F_NO_ABS_PATH : 0);
70 if (RT_FAILURE(rc))
71 return rc;
72 while (RT_SUCCESS(rc = RTDirRead(hDir, pDirEntry, NULL)))
73 {
74 if (!RTDirEntryIsStdDotLink(pDirEntry))
75 {
76 /* Construct the full name of the entry. */
77 if (cchDir + pDirEntry->cbName + 1 /* dir slash */ >= cbBuf)
78 {
79 rc = VERR_FILENAME_TOO_LONG;
80 break;
81 }
82 memcpy(&pszBuf[cchDir], pDirEntry->szName, pDirEntry->cbName + 1);
83
84 /* Deal with the unknown type. */
85 if (pDirEntry->enmType == RTDIRENTRYTYPE_UNKNOWN)
86 {
87 rc = RTPathQueryInfoEx(pszBuf, pObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
88 if (RT_SUCCESS(rc) && RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
89 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
90 else if (RT_SUCCESS(rc) && RTFS_IS_FILE(pObjInfo->Attr.fMode))
91 pDirEntry->enmType = RTDIRENTRYTYPE_FILE;
92 else if (RT_SUCCESS(rc) && RTFS_IS_SYMLINK(pObjInfo->Attr.fMode))
93 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
94 }
95
96 /* Try the delete the fs object. */
97 switch (pDirEntry->enmType)
98 {
99 case RTDIRENTRYTYPE_FILE:
100 rc = RTFileDelete(pszBuf);
101 break;
102
103 case RTDIRENTRYTYPE_DIRECTORY:
104 {
105 size_t cchSubDir = cchDir + pDirEntry->cbName;
106 pszBuf[cchSubDir++] = '/';
107 pszBuf[cchSubDir] = '\0';
108 rc = rtDirRemoveRecursiveSub(pszBuf, cchSubDir, cbBuf, pDirEntry, pObjInfo, fFlags);
109 if (RT_SUCCESS(rc))
110 {
111 pszBuf[cchSubDir] = '\0';
112 rc = RTDirRemove(pszBuf);
113 }
114 break;
115 }
116
117 //case RTDIRENTRYTYPE_SYMLINK:
118 // rc = RTSymlinkDelete(pszBuf, 0);
119 // break;
120
121 default:
122 /** @todo not implemented yet. */
123 rc = VINF_SUCCESS;
124 break;
125 }
126 if (RT_FAILURE(rc))
127 break;
128 }
129 }
130 if (rc == VERR_NO_MORE_FILES)
131 rc = VINF_SUCCESS;
132 RTDirClose(hDir);
133 return rc;
134}
135
136
137RTDECL(int) RTDirRemoveRecursive(const char *pszPath, uint32_t fFlags)
138{
139 AssertReturn(!(fFlags & ~RTDIRRMREC_F_VALID_MASK), VERR_INVALID_PARAMETER);
140
141 /*
142 * Allocate path buffer.
143 */
144 char *pszAbsPath;
145 size_t cbAbsPathBuf = RTPATH_BIG_MAX;
146 char *pszAbsPathFree = pszAbsPath = (char *)RTMemTmpAlloc(cbAbsPathBuf);
147 if (!pszAbsPath)
148 {
149 cbAbsPathBuf = RTPATH_MAX;
150 pszAbsPath = (char *)alloca(RTPATH_MAX);
151 }
152
153 /*
154 * Get an absolute path because this is easier to work with and
155 * eliminates any races with changing CWD.
156 */
157 int rc;
158 if (!(fFlags & RTDIRRMREC_F_NO_ABS_PATH))
159 rc = RTPathAbs(pszPath, pszAbsPath, cbAbsPathBuf);
160 else if (*pszPath != '\0')
161 rc = RTStrCopy(pszAbsPath, cbAbsPathBuf, pszPath);
162 else
163 rc = VERR_PATH_ZERO_LENGTH;
164 if (RT_SUCCESS(rc))
165 {
166 /*
167 * This API is not permitted applied to the root of anything.
168 */
169 union
170 {
171 RTPATHPARSED Parsed;
172 uint8_t abParsed[RTPATHPARSED_MIN_SIZE];
173 } uBuf;
174 RTPathParse(pszPath, &uBuf.Parsed, sizeof(uBuf), RTPATH_STR_F_STYLE_HOST);
175 if ( uBuf.Parsed.cComps <= 1
176 && (uBuf.Parsed.fProps & RTPATH_PROP_ROOT_SLASH))
177 rc = VERR_ACCESS_DENIED;
178 else
179 {
180 /*
181 * Because of the above restriction, we never have to deal with the root
182 * slash problem and can safely strip any trailing slashes and add a
183 * definite one.
184 */
185 RTPathStripTrailingSlash(pszAbsPath);
186 size_t cchAbsPath = strlen(pszAbsPath);
187 if (cchAbsPath + 1 < cbAbsPathBuf)
188 {
189 pszAbsPath[cchAbsPath++] = RTPATH_SLASH;
190 pszAbsPath[cchAbsPath] = '\0';
191
192 /*
193 * Check if it exists so we can return quietly if it doesn't.
194 */
195 RTFSOBJINFO SharedObjInfoBuf;
196 rc = RTPathQueryInfoEx(pszAbsPath, &SharedObjInfoBuf, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
197 if ( rc == VERR_PATH_NOT_FOUND
198 || rc == VERR_FILE_NOT_FOUND)
199 rc = VINF_SUCCESS;
200 else if ( RT_SUCCESS(rc)
201 && RTFS_IS_DIRECTORY(SharedObjInfoBuf.Attr.fMode))
202 {
203 /*
204 * We're all set for the recursion now, so get going.
205 */
206 RTDIRENTRY SharedDirEntryBuf;
207 rc = rtDirRemoveRecursiveSub(pszAbsPath, cchAbsPath, cbAbsPathBuf,
208 &SharedDirEntryBuf, &SharedObjInfoBuf, fFlags);
209
210 /*
211 * Remove the specified directory if desired and removing the content was successful.
212 */
213 if ( RT_SUCCESS(rc)
214 && !(fFlags & RTDIRRMREC_F_CONTENT_ONLY))
215 {
216 pszAbsPath[cchAbsPath] = 0;
217 rc = RTDirRemove(pszAbsPath);
218 }
219 }
220 else if (RT_SUCCESS(rc))
221 rc = VERR_NOT_A_DIRECTORY;
222
223 }
224 else
225 rc = VERR_FILENAME_TOO_LONG;
226 }
227 }
228 if (pszAbsPathFree)
229 RTMemTmpFree(pszAbsPathFree);
230 return rc;
231}
232
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