1 | /* $Id: dir2.cpp 34015 2010-11-12 00:15:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Directory Manipulation, Part 2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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/assert.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/log.h>
|
---|
39 | #include <iprt/mem.h>
|
---|
40 | #include <iprt/param.h>
|
---|
41 | #include <iprt/path.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include "internal/path.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Recursion worker for RTDirRemoveRecursive.
|
---|
48 | *
|
---|
49 | * @returns IPRT status code.
|
---|
50 | * @param pszBuf The path buffer. Contains the abs path to the
|
---|
51 | * directory to recurse into. Trailing slash.
|
---|
52 | * @param cchDir The length of the directory we're cursing into,
|
---|
53 | * including the trailing slash.
|
---|
54 | * @param pDirEntry The dir entry buffer. (Shared to save stack.)
|
---|
55 | * @param pObjInfo The object info buffer. (ditto)
|
---|
56 | */
|
---|
57 | static int rtDirRemoveRecursiveSub(char *pszBuf, size_t cchDir, PRTDIRENTRY pDirEntry, PRTFSOBJINFO pObjInfo)
|
---|
58 | {
|
---|
59 | AssertReturn(RTPATH_IS_SLASH(pszBuf[cchDir - 1]), VERR_INTERNAL_ERROR_4);
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * Enumerate the directory content and dispose of it.
|
---|
63 | */
|
---|
64 | PRTDIR pDir;
|
---|
65 | int rc = RTDirOpen(&pDir, pszBuf);
|
---|
66 | if (RT_FAILURE(rc))
|
---|
67 | return rc;
|
---|
68 | while (RT_SUCCESS(rc = RTDirRead(pDir, pDirEntry, NULL)))
|
---|
69 | {
|
---|
70 | if ( pDirEntry->szName[0] != '.'
|
---|
71 | || pDirEntry->cbName > 2
|
---|
72 | || ( pDirEntry->cbName == 2
|
---|
73 | && pDirEntry->szName[1] != '.')
|
---|
74 | )
|
---|
75 | {
|
---|
76 | /* Construct the full name of the entry. */
|
---|
77 | if (cchDir + pDirEntry->cbName + 1 /* dir slash */ >= RTPATH_MAX)
|
---|
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, pDirEntry, pObjInfo);
|
---|
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);
|
---|
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(pDir);
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | RTDECL(int) RTDirRemoveRecursive(const char *pszPath, uint32_t fFlags)
|
---|
138 | {
|
---|
139 | AssertReturn(!(fFlags & ~RTDIRRMREC_F_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
140 |
|
---|
141 | /* Get an absolute path because this is easier to work with. */
|
---|
142 | /** @todo use RTPathReal here instead? */
|
---|
143 | char szAbsPath[RTPATH_MAX];
|
---|
144 | int rc = RTPathAbs(pszPath, szAbsPath, sizeof(szAbsPath));
|
---|
145 | if (RT_FAILURE(rc))
|
---|
146 | return rc;
|
---|
147 |
|
---|
148 | /* This API is not permitted applied to the root of anything. */
|
---|
149 | if (RTPathCountComponents(szAbsPath) <= 1)
|
---|
150 | return VERR_ACCESS_DENIED;
|
---|
151 |
|
---|
152 | /* Because of the above restriction, we never have to deal with the root
|
---|
153 | slash problem and can safely strip any trailing slashes and add a
|
---|
154 | definite one. */
|
---|
155 | RTPathStripTrailingSlash(szAbsPath);
|
---|
156 | size_t cchAbsPath = strlen(szAbsPath);
|
---|
157 | if (cchAbsPath + 1 >= RTPATH_MAX)
|
---|
158 | return VERR_FILENAME_TOO_LONG;
|
---|
159 | szAbsPath[cchAbsPath++] = '/';
|
---|
160 | szAbsPath[cchAbsPath] = 0;
|
---|
161 |
|
---|
162 | /* Check if it exists so we can return quietly if it doesn't. */
|
---|
163 | RTFSOBJINFO SharedObjInfoBuf;
|
---|
164 | rc = RTPathQueryInfoEx(szAbsPath, &SharedObjInfoBuf, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
165 | if ( rc == VERR_PATH_NOT_FOUND
|
---|
166 | || rc == VERR_FILE_NOT_FOUND)
|
---|
167 | return VINF_SUCCESS;
|
---|
168 | if (RT_FAILURE(rc))
|
---|
169 | return rc;
|
---|
170 | if (!RTFS_IS_DIRECTORY(SharedObjInfoBuf.Attr.fMode))
|
---|
171 | return VERR_NOT_A_DIRECTORY;
|
---|
172 |
|
---|
173 | /* We're all set for the recursion now, so get going. */
|
---|
174 | RTDIRENTRY SharedDirEntryBuf;
|
---|
175 | rc = rtDirRemoveRecursiveSub(szAbsPath, cchAbsPath, &SharedDirEntryBuf, &SharedObjInfoBuf);
|
---|
176 |
|
---|
177 | /* Remove the specified directory if desired and removing the content was
|
---|
178 | successful. */
|
---|
179 | if ( RT_SUCCESS(rc)
|
---|
180 | && !(fFlags & RTDIRRMREC_F_CONTENT_ONLY))
|
---|
181 | {
|
---|
182 | szAbsPath[cchAbsPath] = 0;
|
---|
183 | rc = RTDirRemove(szAbsPath);
|
---|
184 | }
|
---|
185 | return rc;
|
---|
186 | }
|
---|
187 |
|
---|