1 | /* $Id: VBoxGuestR3LibSharedFolders.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, shared folders.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2022 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 | #include <iprt/string.h>
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/cpp/autores.h>
|
---|
35 | #include <iprt/stdarg.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #include <VBox/shflsvc.h> /** @todo File should be moved to VBox/HostServices/SharedFolderSvc.h */
|
---|
38 |
|
---|
39 | #include "VBoxGuestR3LibInternal.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Connects to the shared folder service.
|
---|
44 | *
|
---|
45 | * @returns VBox status code
|
---|
46 | * @param pidClient Where to put the client id on success. The client id
|
---|
47 | * must be passed to all the other calls to the service.
|
---|
48 | */
|
---|
49 | VBGLR3DECL(int) VbglR3SharedFolderConnect(HGCMCLIENTID *pidClient)
|
---|
50 | {
|
---|
51 | return VbglR3HGCMConnect("VBoxSharedFolders", pidClient);
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Disconnect from the shared folder service.
|
---|
57 | *
|
---|
58 | * @returns VBox status code.
|
---|
59 | * @param idClient The client id returned by VbglR3InfoSvcConnect().
|
---|
60 | */
|
---|
61 | VBGLR3DECL(int) VbglR3SharedFolderDisconnect(HGCMCLIENTID idClient)
|
---|
62 | {
|
---|
63 | return VbglR3HGCMDisconnect(idClient);
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Checks whether a shared folder share exists or not.
|
---|
69 | *
|
---|
70 | * @returns True if shared folder exists, false if not.
|
---|
71 | * @param idClient The client id returned by VbglR3InfoSvcConnect().
|
---|
72 | * @param pszShareName Shared folder name to check.
|
---|
73 | */
|
---|
74 | VBGLR3DECL(bool) VbglR3SharedFolderExists(HGCMCLIENTID idClient, const char *pszShareName)
|
---|
75 | {
|
---|
76 | AssertPtr(pszShareName);
|
---|
77 |
|
---|
78 | uint32_t cMappings;
|
---|
79 | VBGLR3SHAREDFOLDERMAPPING *paMappings;
|
---|
80 |
|
---|
81 | /** @todo Use some caching here? */
|
---|
82 | bool fFound = false;
|
---|
83 | int rc = VbglR3SharedFolderGetMappings(idClient, true /* Only process auto-mounted folders */, &paMappings, &cMappings);
|
---|
84 | if (RT_SUCCESS(rc))
|
---|
85 | {
|
---|
86 | for (uint32_t i = 0; i < cMappings && !fFound; i++)
|
---|
87 | {
|
---|
88 | char *pszName = NULL;
|
---|
89 | rc = VbglR3SharedFolderGetName(idClient, paMappings[i].u32Root, &pszName);
|
---|
90 | if ( RT_SUCCESS(rc)
|
---|
91 | && *pszName)
|
---|
92 | {
|
---|
93 | if (RTStrICmp(pszName, pszShareName) == 0)
|
---|
94 | fFound = true;
|
---|
95 | RTStrFree(pszName);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | VbglR3SharedFolderFreeMappings(paMappings);
|
---|
99 | }
|
---|
100 | return fFound;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Get the list of available shared folders.
|
---|
106 | *
|
---|
107 | * @returns VBox status code.
|
---|
108 | * @param idClient The client id returned by VbglR3SharedFolderConnect().
|
---|
109 | * @param fAutoMountOnly Flag whether only auto-mounted shared folders
|
---|
110 | * should be reported.
|
---|
111 | * @param ppaMappings Allocated array which will retrieve the mapping info. Needs
|
---|
112 | * to be freed with VbglR3SharedFolderFreeMappings() later.
|
---|
113 | * @param pcMappings The number of mappings returned in @a ppaMappings.
|
---|
114 | */
|
---|
115 | VBGLR3DECL(int) VbglR3SharedFolderGetMappings(HGCMCLIENTID idClient, bool fAutoMountOnly,
|
---|
116 | PVBGLR3SHAREDFOLDERMAPPING *ppaMappings, uint32_t *pcMappings)
|
---|
117 | {
|
---|
118 | AssertPtrReturn(pcMappings, VERR_INVALID_PARAMETER);
|
---|
119 | AssertPtrReturn(ppaMappings, VERR_INVALID_PARAMETER);
|
---|
120 |
|
---|
121 | *pcMappings = 0;
|
---|
122 | *ppaMappings = NULL;
|
---|
123 |
|
---|
124 | VBoxSFQueryMappings Msg;
|
---|
125 | VBGL_HGCM_HDR_INIT(&Msg.callInfo, idClient, SHFL_FN_QUERY_MAPPINGS, 3);
|
---|
126 |
|
---|
127 | /* Set the mapping flags. */
|
---|
128 | uint32_t u32Flags = 0; /** @todo SHFL_MF_UTF8 is not implemented yet. */
|
---|
129 | if (fAutoMountOnly) /* We only want the mappings which get auto-mounted. */
|
---|
130 | u32Flags |= SHFL_MF_AUTOMOUNT;
|
---|
131 | VbglHGCMParmUInt32Set(&Msg.flags, u32Flags);
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Prepare and get the actual mappings from the host service.
|
---|
135 | */
|
---|
136 | int rc = VINF_SUCCESS;
|
---|
137 | uint32_t cMappings = 8; /* Should be a good default value. */
|
---|
138 | uint32_t cbSize = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
|
---|
139 | VBGLR3SHAREDFOLDERMAPPING *ppaMappingsTemp = (PVBGLR3SHAREDFOLDERMAPPING)RTMemAllocZ(cbSize);
|
---|
140 | if (!ppaMappingsTemp)
|
---|
141 | return VERR_NO_MEMORY;
|
---|
142 |
|
---|
143 | do
|
---|
144 | {
|
---|
145 | VbglHGCMParmUInt32Set(&Msg.numberOfMappings, cMappings);
|
---|
146 | VbglHGCMParmPtrSet(&Msg.mappings, ppaMappingsTemp, cbSize);
|
---|
147 |
|
---|
148 | rc = VbglR3HGCMCall(&Msg.callInfo, sizeof(Msg));
|
---|
149 | if (RT_SUCCESS(rc))
|
---|
150 | {
|
---|
151 | VbglHGCMParmUInt32Get(&Msg.numberOfMappings, pcMappings);
|
---|
152 |
|
---|
153 | /* Do we have more mappings than we have allocated space for? */
|
---|
154 | if (rc == VINF_BUFFER_OVERFLOW)
|
---|
155 | {
|
---|
156 | cMappings = *pcMappings;
|
---|
157 | cbSize = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
|
---|
158 | void *pvNew = RTMemRealloc(ppaMappingsTemp, cbSize);
|
---|
159 | AssertPtrBreakStmt(pvNew, rc = VERR_NO_MEMORY);
|
---|
160 | ppaMappingsTemp = (PVBGLR3SHAREDFOLDERMAPPING)pvNew;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | } while (rc == VINF_BUFFER_OVERFLOW); /** @todo r=bird: This won't happen because the weird host code never returns it. */
|
---|
164 |
|
---|
165 | if ( RT_FAILURE(rc)
|
---|
166 | || !*pcMappings)
|
---|
167 | {
|
---|
168 | RTMemFree(ppaMappingsTemp);
|
---|
169 | ppaMappingsTemp = NULL;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /* In this case, just return success with 0 mappings */
|
---|
173 | if ( rc == VERR_INVALID_PARAMETER
|
---|
174 | && fAutoMountOnly)
|
---|
175 | rc = VINF_SUCCESS;
|
---|
176 |
|
---|
177 | *ppaMappings = ppaMappingsTemp;
|
---|
178 |
|
---|
179 | return rc;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Frees the shared folder mappings allocated by
|
---|
185 | * VbglR3SharedFolderGetMappings() before.
|
---|
186 | *
|
---|
187 | * @param paMappings What
|
---|
188 | */
|
---|
189 | VBGLR3DECL(void) VbglR3SharedFolderFreeMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings)
|
---|
190 | {
|
---|
191 | if (paMappings)
|
---|
192 | RTMemFree(paMappings);
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Get the real name of a shared folder.
|
---|
198 | *
|
---|
199 | * @returns VBox status code.
|
---|
200 | * @param idClient The client id returned by VbglR3InvsSvcConnect().
|
---|
201 | * @param u32Root Root ID of shared folder to get the name for.
|
---|
202 | * @param ppszName Where to return the name string. This shall be
|
---|
203 | * freed by calling RTStrFree.
|
---|
204 | */
|
---|
205 | VBGLR3DECL(int) VbglR3SharedFolderGetName(HGCMCLIENTID idClient, uint32_t u32Root, char **ppszName)
|
---|
206 | {
|
---|
207 | AssertPtr(ppszName);
|
---|
208 |
|
---|
209 | VBoxSFQueryMapName Msg;
|
---|
210 | VBGL_HGCM_HDR_INIT(&Msg.callInfo, idClient, SHFL_FN_QUERY_MAP_NAME, 2);
|
---|
211 |
|
---|
212 | int rc;
|
---|
213 | uint32_t cbString = SHFLSTRING_HEADER_SIZE + SHFL_MAX_LEN * sizeof(RTUTF16);
|
---|
214 | PSHFLSTRING pString = (PSHFLSTRING)RTMemAlloc(cbString);
|
---|
215 | if (pString)
|
---|
216 | {
|
---|
217 | if (!ShflStringInitBuffer(pString, cbString))
|
---|
218 | {
|
---|
219 | RTMemFree(pString);
|
---|
220 | return VERR_INVALID_PARAMETER;
|
---|
221 | }
|
---|
222 |
|
---|
223 | VbglHGCMParmUInt32Set(&Msg.root, u32Root);
|
---|
224 | VbglHGCMParmPtrSet(&Msg.name, pString, cbString);
|
---|
225 |
|
---|
226 | rc = VbglR3HGCMCall(&Msg.callInfo, sizeof(Msg));
|
---|
227 | if (RT_SUCCESS(rc))
|
---|
228 | {
|
---|
229 | *ppszName = NULL;
|
---|
230 | rc = RTUtf16ToUtf8(&pString->String.ucs2[0], ppszName);
|
---|
231 | }
|
---|
232 | RTMemFree(pString);
|
---|
233 | }
|
---|
234 | else
|
---|
235 | rc = VERR_NO_MEMORY;
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Queries information about a shared folder.
|
---|
242 | *
|
---|
243 | * @returns VBox status code.
|
---|
244 | *
|
---|
245 | * @param idClient The client ID.
|
---|
246 | * @param idRoot The root ID of the folder to query information for.
|
---|
247 | * @param fQueryFlags SHFL_MIQF_XXX.
|
---|
248 | * @param ppszName Where to return the pointer to the name.
|
---|
249 | * Free using RTStrFree. Optional.
|
---|
250 | * @param ppszMountPoint Where to return the pointer to the auto mount point.
|
---|
251 | * Free using RTStrFree. Optional.
|
---|
252 | * @param pfFlags Where to return the flags (SHFL_MIF_XXX). Optional.
|
---|
253 | * @param puRootIdVersion where to return the root ID version. Optional.
|
---|
254 | * This helps detecting root-id reuse.
|
---|
255 | *
|
---|
256 | * @remarks ASSUMES UTF-16 connection to host.
|
---|
257 | */
|
---|
258 | VBGLR3DECL(int) VbglR3SharedFolderQueryFolderInfo(HGCMCLIENTID idClient, uint32_t idRoot, uint64_t fQueryFlags,
|
---|
259 | char **ppszName, char **ppszMountPoint,
|
---|
260 | uint64_t *pfFlags, uint32_t *puRootIdVersion)
|
---|
261 | {
|
---|
262 | AssertReturn(!(fQueryFlags & ~(SHFL_MIQF_DRIVE_LETTER | SHFL_MIQF_PATH)), VERR_INVALID_FLAGS);
|
---|
263 |
|
---|
264 | /*
|
---|
265 | * Allocate string buffers first.
|
---|
266 | */
|
---|
267 | int rc;
|
---|
268 | PSHFLSTRING pNameBuf = (PSHFLSTRING)RTMemAlloc(SHFLSTRING_HEADER_SIZE + (SHFL_MAX_LEN + 1) * sizeof(RTUTF16));
|
---|
269 | PSHFLSTRING pMountPoint = (PSHFLSTRING)RTMemAlloc(SHFLSTRING_HEADER_SIZE + (260 + 1) * sizeof(RTUTF16));
|
---|
270 | if (pNameBuf && pMountPoint)
|
---|
271 | {
|
---|
272 | ShflStringInitBuffer(pNameBuf, SHFLSTRING_HEADER_SIZE + (SHFL_MAX_LEN + 1) * sizeof(RTUTF16));
|
---|
273 | ShflStringInitBuffer(pMountPoint, SHFLSTRING_HEADER_SIZE + (260 + 1) * sizeof(RTUTF16));
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * Make the call.
|
---|
277 | */
|
---|
278 | VBoxSFQueryMapInfo Msg;
|
---|
279 | VBGL_HGCM_HDR_INIT(&Msg.callInfo, idClient, SHFL_FN_QUERY_MAP_INFO, 5);
|
---|
280 | VbglHGCMParmUInt32Set(&Msg.root, idRoot);
|
---|
281 | VbglHGCMParmPtrSet(&Msg.name, pNameBuf, SHFLSTRING_HEADER_SIZE + pNameBuf->u16Size);
|
---|
282 | VbglHGCMParmPtrSet(&Msg.mountPoint, pMountPoint, SHFLSTRING_HEADER_SIZE + pMountPoint->u16Size);
|
---|
283 | VbglHGCMParmUInt64Set(&Msg.flags, fQueryFlags);
|
---|
284 | VbglHGCMParmUInt32Set(&Msg.rootIdVersion, 0);
|
---|
285 |
|
---|
286 | rc = VbglR3HGCMCall(&Msg.callInfo, sizeof(Msg));
|
---|
287 | if (RT_SUCCESS(rc))
|
---|
288 | {
|
---|
289 | /*
|
---|
290 | * Copy out the results.
|
---|
291 | */
|
---|
292 | if (puRootIdVersion)
|
---|
293 | *puRootIdVersion = Msg.rootIdVersion.u.value64;
|
---|
294 |
|
---|
295 | if (pfFlags)
|
---|
296 | *pfFlags = Msg.flags.u.value64;
|
---|
297 |
|
---|
298 | if (ppszName)
|
---|
299 | {
|
---|
300 | *ppszName = NULL;
|
---|
301 | rc = RTUtf16ToUtf8Ex(pNameBuf->String.utf16, pNameBuf->u16Length / sizeof(RTUTF16), ppszName, 0, NULL);
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (ppszMountPoint && RT_SUCCESS(rc))
|
---|
305 | {
|
---|
306 | *ppszMountPoint = NULL;
|
---|
307 | rc = RTUtf16ToUtf8Ex(pMountPoint->String.utf16, pMountPoint->u16Length / sizeof(RTUTF16), ppszMountPoint, 0, NULL);
|
---|
308 | if (RT_FAILURE(rc) && ppszName)
|
---|
309 | {
|
---|
310 | RTStrFree(*ppszName);
|
---|
311 | *ppszName = NULL;
|
---|
312 | }
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 | else
|
---|
317 | rc = VERR_NO_MEMORY;
|
---|
318 | RTMemFree(pMountPoint);
|
---|
319 | RTMemFree(pNameBuf);
|
---|
320 | return rc;
|
---|
321 | }
|
---|
322 |
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Waits for changes to the mappings (add, remove, restore).
|
---|
326 | *
|
---|
327 | * @returns VBox status code.
|
---|
328 | * @retval VINF_SUCCESS on change
|
---|
329 | * @retval VINF_TRY_AGAIN on restore.
|
---|
330 | * @retval VERR_OUT_OF_RESOURCES if there are too many guys waiting.
|
---|
331 | *
|
---|
332 | * @param idClient The client ID.
|
---|
333 | * @param uPrevVersion The mappings config version number returned the last
|
---|
334 | * time around. Use UINT32_MAX for the first call.
|
---|
335 | * @param puCurVersion Where to return the current mappings config version.
|
---|
336 | */
|
---|
337 | VBGLR3DECL(int) VbglR3SharedFolderWaitForMappingsChanges(HGCMCLIENTID idClient, uint32_t uPrevVersion, uint32_t *puCurVersion)
|
---|
338 | {
|
---|
339 | VBoxSFWaitForMappingsChanges Msg;
|
---|
340 | VBGL_HGCM_HDR_INIT(&Msg.callInfo, idClient, SHFL_FN_WAIT_FOR_MAPPINGS_CHANGES, 1);
|
---|
341 | VbglHGCMParmUInt32Set(&Msg.version, uPrevVersion);
|
---|
342 |
|
---|
343 | int rc = VbglR3HGCMCall(&Msg.callInfo, sizeof(Msg));
|
---|
344 |
|
---|
345 | *puCurVersion = Msg.version.u.value32;
|
---|
346 | return rc;
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Cancels all threads currently waiting for changes for this client.
|
---|
352 | *
|
---|
353 | * @returns VBox status code.
|
---|
354 | * @param idClient The client ID.
|
---|
355 | */
|
---|
356 | VBGLR3DECL(int) VbglR3SharedFolderCancelMappingsChangesWaits(HGCMCLIENTID idClient)
|
---|
357 | {
|
---|
358 | VBGLIOCHGCMCALL CallInfo;
|
---|
359 | VBGL_HGCM_HDR_INIT(&CallInfo, idClient, SHFL_FN_CANCEL_MAPPINGS_CHANGES_WAITS, 0);
|
---|
360 |
|
---|
361 | return VbglR3HGCMCall(&CallInfo, sizeof(CallInfo));
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Retrieves the prefix for a shared folder mount point. If no prefix
|
---|
367 | * is set in the guest properties "sf_" is returned.
|
---|
368 | *
|
---|
369 | * @returns VBox status code.
|
---|
370 | * @param ppszPrefix Where to return the prefix string. This shall be
|
---|
371 | * freed by calling RTStrFree.
|
---|
372 | */
|
---|
373 | VBGLR3DECL(int) VbglR3SharedFolderGetMountPrefix(char **ppszPrefix)
|
---|
374 | {
|
---|
375 | AssertPtrReturn(ppszPrefix, VERR_INVALID_POINTER);
|
---|
376 | int rc;
|
---|
377 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
378 | HGCMCLIENTID idClientGuestProp;
|
---|
379 | rc = VbglR3GuestPropConnect(&idClientGuestProp);
|
---|
380 | if (RT_SUCCESS(rc))
|
---|
381 | {
|
---|
382 | rc = VbglR3GuestPropReadValueAlloc(idClientGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountPrefix", ppszPrefix);
|
---|
383 | if (rc == VERR_NOT_FOUND) /* No prefix set? Then set the default. */
|
---|
384 | {
|
---|
385 | #endif
|
---|
386 | /** @todo r=bird: Inconsistent! VbglR3SharedFolderGetMountDir does not return a default. */
|
---|
387 | rc = RTStrDupEx(ppszPrefix, "sf_");
|
---|
388 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
389 | }
|
---|
390 | VbglR3GuestPropDisconnect(idClientGuestProp);
|
---|
391 | }
|
---|
392 | #endif
|
---|
393 | return rc;
|
---|
394 | }
|
---|
395 |
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Retrieves the mount root directory for auto-mounted shared
|
---|
399 | * folders. mount point. If no string is set (VERR_NOT_FOUND)
|
---|
400 | * it's up on the caller (guest) to decide where to mount.
|
---|
401 | *
|
---|
402 | * @returns VBox status code.
|
---|
403 | * @param ppszDir Where to return the directory
|
---|
404 | * string. This shall be freed by
|
---|
405 | * calling RTStrFree.
|
---|
406 | */
|
---|
407 | VBGLR3DECL(int) VbglR3SharedFolderGetMountDir(char **ppszDir)
|
---|
408 | {
|
---|
409 | AssertPtrReturn(ppszDir, VERR_INVALID_POINTER);
|
---|
410 | int rc = VERR_NOT_FOUND;
|
---|
411 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
412 | HGCMCLIENTID idClientGuestProp;
|
---|
413 | rc = VbglR3GuestPropConnect(&idClientGuestProp);
|
---|
414 | if (RT_SUCCESS(rc))
|
---|
415 | {
|
---|
416 | rc = VbglR3GuestPropReadValueAlloc(idClientGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountDir", ppszDir);
|
---|
417 | VbglR3GuestPropDisconnect(idClientGuestProp);
|
---|
418 | }
|
---|
419 | #endif
|
---|
420 | return rc;
|
---|
421 | }
|
---|
422 |
|
---|