VirtualBox

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

Last change on this file since 102647 was 102647, checked in by vboxsync, 14 months ago

IPRT: Implemented RTFsMountpointsEnum(). Extended tstRTFsQueries testcase. bugref:10415

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* $Id: RTFsMountpointsEnum-win.cpp 102647 2023-12-20 12:01:26Z 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/file.h>
43
44#include <iprt/assert.h>
45#include <iprt/err.h>
46#include <iprt/path.h>
47#include <iprt/string.h>
48#include <iprt/utf16.h>
49
50#include <iprt/win/windows.h>
51
52
53/**
54 * Handles a mountpoint callback with WCHAR (as UTF-16) strings.
55 *
56 * @returns IPRT status code. Failure terminates the enumeration.
57 * @param pfnCallback Callback function to invoke.
58 * @param pvUser User-supplied pointer.
59 * @param pwszStr WCHAR string to pass to the callback function.
60 */
61DECLINLINE(int) rtFsWinHandleCallback(PFNRTFSMOUNTPOINTENUM pfnCallback, void *pvUser, PWCHAR pwszStr)
62{
63 char *psz;
64 int rc = RTUtf16ToUtf8((PCRTUTF16)pwszStr, &psz);
65 AssertRCReturn(rc, rc);
66 rc = pfnCallback(psz, pvUser);
67 RTStrFree(psz);
68 return rc;
69}
70
71/**
72 * Mountpoint enumeration worker.
73 *
74 * This always enumerates all available/connected (disk-based) mountpoints and
75 * can be used for other functions to perform (custom) filtering.
76 *
77 * @returns IPRT status code.
78 * @param fRemote Also enumerates (currently connected) remote network resources.
79 * @param pfnCallback Callback function to invoke.
80 * @param pvUser User-supplied pointer. Optional and can be NULL.
81 */
82static int rtFsWinMountpointsEnumWorker(bool fRemote, PFNRTFSMOUNTPOINTENUM pfnCallback, void *pvUser)
83{
84 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
85 /* pvUser is optional. */
86
87 int rc = VINF_SUCCESS;
88
89 SetLastError(0);
90
91 WCHAR wszVol[RTPATH_MAX];
92 HANDLE hVol = FindFirstVolumeW(wszVol, RT_ELEMENTS(wszVol));
93 if ( hVol
94 && hVol != INVALID_HANDLE_VALUE)
95 {
96 do
97 {
98 WCHAR wszMp[RTPATH_MAX];
99 HANDLE hMp = FindFirstVolumeMountPointW(wszVol, wszMp, RT_ELEMENTS(wszMp));
100 if ( hMp
101 && hMp != INVALID_HANDLE_VALUE)
102 {
103 do
104 {
105 rc = rtFsWinHandleCallback(pfnCallback, pvUser, wszMp);
106 if (RT_FAILURE(rc))
107 break;
108 }
109 while (FindNextVolumeMountPointW(hMp, wszMp, RT_ELEMENTS(wszMp)));
110 FindVolumeMountPointClose(hMp);
111 }
112 else if ( GetLastError() != ERROR_NO_MORE_FILES
113 && GetLastError() != ERROR_PATH_NOT_FOUND
114 && GetLastError() != ERROR_UNRECOGNIZED_VOLUME)
115 rc = RTErrConvertFromWin32(GetLastError());
116
117 if (RT_SUCCESS(rc))
118 {
119 DWORD cwch = 0;
120 if (GetVolumePathNamesForVolumeNameW(wszVol, wszMp, RT_ELEMENTS(wszMp), &cwch))
121 {
122 size_t off = 0;
123 while (off < cwch)
124 {
125 size_t cwc = wcslen(&wszMp[off]);
126 if (cwc)
127 {
128 rc = rtFsWinHandleCallback(pfnCallback, pvUser, &wszMp[off]);
129 if (RT_FAILURE(rc))
130 break;
131 }
132 off += cwc + 1;
133 }
134 }
135 else
136 rc = RTErrConvertFromWin32(GetLastError());
137 }
138
139 } while (RT_SUCCESS(rc) && FindNextVolumeW(hVol, wszVol, RT_ELEMENTS(wszVol)));
140 FindVolumeClose(hVol);
141 }
142 else
143 rc = RTErrConvertFromWin32(GetLastError());
144
145 if ( RT_SUCCESS(rc)
146 && fRemote)
147 {
148 HANDLE hEnum;
149 DWORD dwErr = WNetOpenEnumA(RESOURCE_CONNECTED, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, NULL, &hEnum);
150 if (dwErr == NO_ERROR)
151 {
152 for (;;)
153 {
154 DWORD cResources = 1;
155 DWORD cbRet = sizeof(wszVol);
156 dwErr = WNetEnumResourceW(hEnum, &cResources, wszVol, &cbRet);
157 if (dwErr == NO_ERROR)
158 {
159 NETRESOURCEW const *pRsrc = (NETRESOURCEW const *)wszVol;
160 if ( pRsrc
161 && pRsrc->lpLocalName)
162 {
163 rc = rtFsWinHandleCallback(pfnCallback, pvUser, (PWCHAR)pRsrc->lpLocalName);
164 if (RT_FAILURE(rc))
165 break;
166 }
167 }
168 else
169 {
170 if (dwErr != ERROR_NO_MORE_ITEMS)
171 rc = RTErrConvertFromWin32(dwErr);
172 break;
173 }
174 }
175 }
176 else
177 rc = RTErrConvertFromWin32(dwErr);
178 }
179
180 return rc;
181}
182
183
184RTR3DECL(int) RTFsMountpointsEnum(PFNRTFSMOUNTPOINTENUM pfnCallback, void *pvUser)
185{
186 return rtFsWinMountpointsEnumWorker(true /* fRemote */, pfnCallback, pvUser);
187}
188
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