VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/RTFsMountpointsEnum-win.cpp

Last change on this file was 102661, checked in by vboxsync, 5 months ago

IPRT/RTFsMountpointsEnum: Gracefully skip volumes a regular user does not have access to, instead of failing completely. We might want to have a flag for that (later). ​bugref:10415

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/* $Id: RTFsMountpointsEnum-win.cpp 102661 2023-12-20 18:21:40Z vboxsync $ */
2/** @file
3 * IPRT - File System, RTFsMountpointsEnum, Windows.
4 */
5
6/*
7 * Copyright (C) 2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/nt/nt-and-windows.h>
43#include "internal-r3-win.h"
44
45#include <iprt/asm.h>
46#include <iprt/assert.h>
47#include <iprt/errcore.h>
48#include <iprt/file.h>
49#include <iprt/once.h>
50#include <iprt/path.h>
51#include <iprt/string.h>
52#include <iprt/utf16.h>
53
54#include <iprt/win/windows.h>
55
56
57/*********************************************************************************************************************************
58* Structures and Typedefs *
59*********************************************************************************************************************************/
60/* kernel32.dll: */
61typedef HANDLE (WINAPI *PFNFINDFIRSTVOLUMEW)(LPWSTR, DWORD);
62typedef BOOL (WINAPI *PFNFINDNEXTVOLUMEW)(HANDLE, LPWSTR, DWORD);
63typedef BOOL (WINAPI *PFNFINDVOLUMECLOSE)(HANDLE);
64typedef BOOL (WINAPI *PFNGETVOLUMEPATHNAMESFORVOLUMENAMEW)(LPCWSTR, LPWCH, DWORD, PDWORD);
65typedef HANDLE (WINAPI *PFNFINDFIRSTVOLUMEMOUNTPOINTW)(LPCWSTR, LPWSTR, DWORD);
66typedef BOOL (WINAPI *PFNFINDNEXTVOLUMEMOUNTPOINTW)(HANDLE, LPWSTR, DWORD);
67typedef BOOL (WINAPI *PFNFINDVOLUMEMOUNTPOINTCLOSE)(HANDLE);
68
69
70/*********************************************************************************************************************************
71* Global Variables *
72*********************************************************************************************************************************/
73
74/* kernel32.dll: */
75static PFNFINDFIRSTVOLUMEW g_pfnFindFirstVolumeW = NULL;
76static PFNFINDNEXTVOLUMEW g_pfnFindNextVolumeW = NULL;
77static PFNFINDVOLUMECLOSE g_pfnFindVolumeClose = NULL;
78static PFNGETVOLUMEPATHNAMESFORVOLUMENAMEW g_pfnGetVolumePathNamesForVolumeNameW = NULL;
79static PFNFINDFIRSTVOLUMEMOUNTPOINTW g_pfnFindFirstVolumeMountPointW = NULL;
80static PFNFINDNEXTVOLUMEMOUNTPOINTW g_pfnFindNextVolumeMountPointW = NULL;
81static PFNFINDVOLUMEMOUNTPOINTCLOSE g_pfnFindVolumeMountPointClose = NULL;
82
83/** Init once structure we need. */
84static RTONCE g_rtFsWinResolveOnce = RTONCE_INITIALIZER;
85
86
87/**
88 * Initialize the import APIs.
89 *
90 * @returns IPRT status code.
91 * @retval VERR_NOT_SUPPORTED if not supported.
92 * @param pvUser Ignored.
93 */
94static DECLCALLBACK(int) rtFsWinResolveOnce(void *pvUser)
95{
96 RT_NOREF(pvUser);
97
98 /*
99 * kernel32.dll volume APIs introduced after NT4.
100 */
101 g_pfnFindFirstVolumeW = (PFNFINDFIRSTVOLUMEW )GetProcAddress(g_hModKernel32, "FindFirstVolumeW");
102 g_pfnFindNextVolumeW = (PFNFINDNEXTVOLUMEW )GetProcAddress(g_hModKernel32, "FindNextVolumeW");
103 g_pfnFindVolumeClose = (PFNFINDVOLUMECLOSE )GetProcAddress(g_hModKernel32, "FindVolumeClose");
104 g_pfnGetVolumePathNamesForVolumeNameW = (PFNGETVOLUMEPATHNAMESFORVOLUMENAMEW)GetProcAddress(g_hModKernel32, "GetVolumePathNamesForVolumeNameW");
105 g_pfnFindFirstVolumeMountPointW = (PFNFINDFIRSTVOLUMEMOUNTPOINTW )GetProcAddress(g_hModKernel32, "FindFirstVolumeMountPointW");
106 g_pfnFindNextVolumeMountPointW = (PFNFINDNEXTVOLUMEMOUNTPOINTW )GetProcAddress(g_hModKernel32, "FindNextVolumeMountPointW");
107 g_pfnFindVolumeMountPointClose = (PFNFINDVOLUMEMOUNTPOINTCLOSE )GetProcAddress(g_hModKernel32, "FindVolumeMountPointClose");
108
109 if ( !g_pfnFindFirstVolumeW
110 || !g_pfnFindNextVolumeW
111 || !g_pfnFindVolumeClose
112 || !g_pfnGetVolumePathNamesForVolumeNameW
113 || !g_pfnFindFirstVolumeMountPointW
114 || !g_pfnFindNextVolumeMountPointW
115 || !g_pfnFindVolumeMountPointClose)
116 return VERR_NOT_SUPPORTED;
117
118 return VINF_SUCCESS;
119}
120
121/**
122 * Handles a mountpoint callback with WCHAR (as UTF-16) strings.
123 *
124 * @returns IPRT status code. Failure terminates the enumeration.
125 * @param pfnCallback Callback function to invoke.
126 * @param pvUser User-supplied pointer.
127 * @param pwszStr WCHAR string to pass to the callback function.
128 */
129DECLINLINE(int) rtFsWinHandleCallback(PFNRTFSMOUNTPOINTENUM pfnCallback, void *pvUser, PWCHAR pwszStr)
130{
131 char *psz;
132 int rc = RTUtf16ToUtf8((PCRTUTF16)pwszStr, &psz);
133 AssertRCReturn(rc, rc);
134 rc = pfnCallback(psz, pvUser);
135 RTStrFree(psz);
136 return rc;
137}
138
139/**
140 * Mountpoint enumeration worker.
141 *
142 * This always enumerates all available/connected (disk-based) mountpoints and
143 * can be used for other functions to perform (custom) filtering.
144 *
145 * @returns IPRT status code.
146 * @param fRemote Also enumerates (currently connected) remote network resources.
147 * @param pfnCallback Callback function to invoke.
148 * @param pvUser User-supplied pointer. Optional and can be NULL.
149 */
150static int rtFsWinMountpointsEnumWorker(bool fRemote, PFNRTFSMOUNTPOINTENUM pfnCallback, void *pvUser)
151{
152 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
153 /* pvUser is optional. */
154
155 int rc = VINF_SUCCESS;
156
157 SetLastError(0);
158
159 WCHAR wszVol[RTPATH_MAX];
160 HANDLE hVol = g_pfnFindFirstVolumeW(wszVol, RT_ELEMENTS(wszVol));
161 if ( hVol
162 && hVol != INVALID_HANDLE_VALUE)
163 {
164 do
165 {
166 WCHAR wszMp[RTPATH_MAX];
167 HANDLE hMp = g_pfnFindFirstVolumeMountPointW(wszVol, wszMp, RT_ELEMENTS(wszMp));
168 if ( hMp
169 && hMp != INVALID_HANDLE_VALUE)
170 {
171 do
172 {
173 rc = rtFsWinHandleCallback(pfnCallback, pvUser, wszMp);
174 if (RT_FAILURE(rc))
175 break;
176 }
177 while (g_pfnFindNextVolumeMountPointW(hMp, wszMp, RT_ELEMENTS(wszMp)));
178 g_pfnFindVolumeMountPointClose(hMp);
179 }
180 else
181 {
182 DWORD const dwErr = GetLastError();
183 if ( dwErr != ERROR_NO_MORE_FILES
184 && dwErr != ERROR_PATH_NOT_FOUND
185 && dwErr != ERROR_UNRECOGNIZED_VOLUME
186 && dwErr != ERROR_ACCESS_DENIED) /* Can happen for regular users, so just skip this stuff then. */
187 rc = RTErrConvertFromWin32(GetLastError());
188 }
189
190 if (RT_SUCCESS(rc))
191 {
192 DWORD cwch = 0;
193 if (g_pfnGetVolumePathNamesForVolumeNameW(wszVol, wszMp, RT_ELEMENTS(wszMp), &cwch))
194 {
195 size_t off = 0;
196 while (off < cwch)
197 {
198 size_t cwc = wcslen(&wszMp[off]);
199 if (cwc)
200 {
201 rc = rtFsWinHandleCallback(pfnCallback, pvUser, &wszMp[off]);
202 if (RT_FAILURE(rc))
203 break;
204 }
205 off += cwc + 1;
206 }
207 }
208 else
209 rc = RTErrConvertFromWin32(GetLastError());
210 }
211
212 } while (RT_SUCCESS(rc) && g_pfnFindNextVolumeW(hVol, wszVol, RT_ELEMENTS(wszVol)));
213 g_pfnFindVolumeClose(hVol);
214 }
215 else
216 rc = RTErrConvertFromWin32(GetLastError());
217
218 if ( RT_SUCCESS(rc)
219 && fRemote)
220 {
221 HANDLE hEnum;
222 DWORD dwErr = WNetOpenEnumA(RESOURCE_CONNECTED, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, NULL, &hEnum);
223 if (dwErr == NO_ERROR)
224 {
225 for (;;)
226 {
227 DWORD cResources = 1;
228 DWORD cbRet = sizeof(wszVol);
229 dwErr = WNetEnumResourceW(hEnum, &cResources, wszVol, &cbRet);
230 if (dwErr == NO_ERROR)
231 {
232 NETRESOURCEW const *pRsrc = (NETRESOURCEW const *)wszVol;
233 if ( pRsrc
234 && pRsrc->lpLocalName)
235 {
236 rc = rtFsWinHandleCallback(pfnCallback, pvUser, (PWCHAR)pRsrc->lpLocalName);
237 if (RT_FAILURE(rc))
238 break;
239 }
240 }
241 else
242 {
243 if (dwErr != ERROR_NO_MORE_ITEMS)
244 rc = RTErrConvertFromWin32(dwErr);
245 break;
246 }
247 }
248 }
249 else
250 rc = RTErrConvertFromWin32(dwErr);
251 }
252
253 return rc;
254}
255
256
257RTR3DECL(int) RTFsMountpointsEnum(PFNRTFSMOUNTPOINTENUM pfnCallback, void *pvUser)
258{
259 int rc = RTOnce(&g_rtFsWinResolveOnce, rtFsWinResolveOnce, NULL);
260 if (RT_FAILURE(rc))
261 return rc;
262
263 return rtFsWinMountpointsEnumWorker(true /* fRemote */, pfnCallback, pvUser);
264}
265
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use