VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibSharedFolders.cpp@ 43898

Last change on this file since 43898 was 41443, checked in by vboxsync, 13 years ago

Additions/SharedFolders: better error handling of VbglR3SharedFolderGetMappings()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/* $Id: VBoxGuestR3LibSharedFolders.cpp 41443 2012-05-25 07:52:59Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, shared folders.
4 */
5
6/*
7 * Copyright (C) 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#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 "VBGLR3Internal.h"
40
41
42/**
43 * Connects to the shared folder service.
44 *
45 * @returns VBox status code
46 * @param pu32ClientId Where to put the client id on success. The client id
47 * must be passed to all the other calls to the service.
48 */
49VBGLR3DECL(int) VbglR3SharedFolderConnect(uint32_t *pu32ClientId)
50{
51 VBoxGuestHGCMConnectInfo Info;
52 Info.result = VERR_WRONG_ORDER;
53 Info.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
54 RT_ZERO(Info.Loc.u);
55 strcpy(Info.Loc.u.host.achName, "VBoxSharedFolders");
56 Info.u32ClientID = UINT32_MAX; /* try make valgrind shut up. */
57
58 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CONNECT, &Info, sizeof(Info));
59 if (RT_SUCCESS(rc))
60 {
61 rc = Info.result;
62 if (RT_SUCCESS(rc))
63 *pu32ClientId = Info.u32ClientID;
64 }
65 return rc;
66}
67
68
69/**
70 * Disconnect from the shared folder service.
71 *
72 * @returns VBox status code.
73 * @param u32ClientId The client id returned by VbglR3InfoSvcConnect().
74 */
75VBGLR3DECL(int) VbglR3SharedFolderDisconnect(uint32_t u32ClientId)
76{
77 VBoxGuestHGCMDisconnectInfo Info;
78 Info.result = VERR_WRONG_ORDER;
79 Info.u32ClientID = u32ClientId;
80
81 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_DISCONNECT, &Info, sizeof(Info));
82 if (RT_SUCCESS(rc))
83 rc = Info.result;
84 return rc;
85}
86
87
88/**
89 * Checks whether a shared folder share exists or not.
90 *
91 * @returns True if shared folder exists, false if not.
92 * @param u32ClientId The client id returned by VbglR3InfoSvcConnect().
93 * @param pszShareName Shared folder name to check.
94 */
95VBGLR3DECL(bool) VbglR3SharedFolderExists(uint32_t u32ClientId, const char *pszShareName)
96{
97 AssertPtr(pszShareName);
98
99 uint32_t cMappings;
100 VBGLR3SHAREDFOLDERMAPPING *paMappings;
101
102 /** @todo Use some caching here? */
103 bool fFound = false;
104 int rc = VbglR3SharedFolderGetMappings(u32ClientId, true /* Only process auto-mounted folders */,
105 &paMappings, &cMappings);
106 if (RT_SUCCESS(rc))
107 {
108 for (uint32_t i = 0; i < cMappings && !fFound; i++)
109 {
110 char *pszName = NULL;
111 rc = VbglR3SharedFolderGetName(u32ClientId, paMappings[i].u32Root, &pszName);
112 if ( RT_SUCCESS(rc)
113 && *pszName)
114 {
115 if (RTStrICmp(pszName, pszShareName) == 0)
116 fFound = true;
117 RTStrFree(pszName);
118 }
119 }
120 VbglR3SharedFolderFreeMappings(paMappings);
121 }
122 return fFound;
123}
124
125
126/**
127 * Get the list of available shared folders.
128 *
129 * @returns VBox status code.
130 * @param u32ClientId The client id returned by VbglR3SharedFolderConnect().
131 * @param fAutoMountOnly Flag whether only auto-mounted shared folders
132 * should be reported.
133 * @param ppaMappings Allocated array which will retrieve the mapping info. Needs
134 * to be freed with VbglR3SharedFolderFreeMappings() later.
135 * @param pcMappings The number of mappings returned in @a ppaMappings.
136 */
137VBGLR3DECL(int) VbglR3SharedFolderGetMappings(uint32_t u32ClientId, bool fAutoMountOnly,
138 PVBGLR3SHAREDFOLDERMAPPING *ppaMappings, uint32_t *pcMappings)
139{
140 AssertPtrReturn(pcMappings, VERR_INVALID_PARAMETER);
141 AssertPtrReturn(ppaMappings, VERR_INVALID_PARAMETER);
142
143 *pcMappings = 0;
144 *ppaMappings = NULL;
145
146 VBoxSFQueryMappings Msg;
147
148 Msg.callInfo.result = VERR_WRONG_ORDER;
149 Msg.callInfo.u32ClientID = u32ClientId;
150 Msg.callInfo.u32Function = SHFL_FN_QUERY_MAPPINGS;
151 Msg.callInfo.cParms = 3;
152
153 /* Set the mapping flags. */
154 uint32_t u32Flags = 0; /** @todo SHFL_MF_UTF8 is not implemented yet. */
155 if (fAutoMountOnly) /* We only want the mappings which get auto-mounted. */
156 u32Flags |= SHFL_MF_AUTOMOUNT;
157 VbglHGCMParmUInt32Set(&Msg.flags, u32Flags);
158
159 /*
160 * Prepare and get the actual mappings from the host service.
161 */
162 int rc = VINF_SUCCESS;
163 uint32_t cMappings = 8; /* Should be a good default value. */
164 uint32_t cbSize = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
165 VBGLR3SHAREDFOLDERMAPPING *ppaMappingsTemp = (PVBGLR3SHAREDFOLDERMAPPING)RTMemAllocZ(cbSize);
166 if (!ppaMappingsTemp)
167 return VERR_NO_MEMORY;
168
169 do
170 {
171 VbglHGCMParmUInt32Set(&Msg.numberOfMappings, cMappings);
172 VbglHGCMParmPtrSet(&Msg.mappings, ppaMappingsTemp, cbSize);
173
174 rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
175 if (RT_SUCCESS(rc))
176 {
177 rc = Msg.callInfo.result;
178 if (RT_SUCCESS(rc))
179 {
180 VbglHGCMParmUInt32Get(&Msg.numberOfMappings, pcMappings);
181
182 /* Do we have more mappings than we have allocated space for? */
183 if (rc == VINF_BUFFER_OVERFLOW)
184 {
185 cMappings = *pcMappings;
186 cbSize = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
187 void *pvNew = RTMemRealloc(ppaMappingsTemp, cbSize);
188 AssertPtrBreakStmt(pvNew, rc = VERR_NO_MEMORY);
189 ppaMappingsTemp = (PVBGLR3SHAREDFOLDERMAPPING)pvNew;
190 }
191 }
192 }
193 } while (rc == VINF_BUFFER_OVERFLOW);
194
195 if ( RT_FAILURE(rc)
196 || !*pcMappings)
197 {
198 RTMemFree(ppaMappingsTemp);
199 ppaMappingsTemp = NULL;
200 }
201
202 *ppaMappings = ppaMappingsTemp;
203
204 return rc;
205}
206
207
208/**
209 * Frees the shared folder mappings allocated by
210 * VbglR3SharedFolderGetMappings() before.
211 *
212 * @param paMappings What
213 */
214VBGLR3DECL(void) VbglR3SharedFolderFreeMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings)
215{
216 if (paMappings)
217 RTMemFree(paMappings);
218}
219
220
221/**
222 * Get the real name of a shared folder.
223 *
224 * @returns VBox status code.
225 * @param u32ClientId The client id returned by VbglR3InvsSvcConnect().
226 * @param u32Root Root ID of shared folder to get the name for.
227 * @param ppszName Where to return the name string. This shall be
228 * freed by calling RTStrFree.
229 */
230VBGLR3DECL(int) VbglR3SharedFolderGetName(uint32_t u32ClientId, uint32_t u32Root, char **ppszName)
231{
232 AssertPtr(ppszName);
233
234 VBoxSFQueryMapName Msg;
235
236 Msg.callInfo.result = VERR_WRONG_ORDER;
237 Msg.callInfo.u32ClientID = u32ClientId;
238 Msg.callInfo.u32Function = SHFL_FN_QUERY_MAP_NAME;
239 Msg.callInfo.cParms = 2;
240
241 int rc;
242 uint32_t cbString = sizeof(SHFLSTRING) + SHFL_MAX_LEN;
243 PSHFLSTRING pString = (PSHFLSTRING)RTMemAlloc(cbString);
244 if (pString)
245 {
246 RT_ZERO(*pString);
247 ShflStringInitBuffer(pString, SHFL_MAX_LEN);
248
249 VbglHGCMParmUInt32Set(&Msg.root, u32Root);
250 VbglHGCMParmPtrSet(&Msg.name, pString, cbString);
251
252 rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
253 if (RT_SUCCESS(rc))
254 {
255 rc = Msg.callInfo.result;
256 if (RT_SUCCESS(rc))
257 {
258 *ppszName = NULL;
259 rc = RTUtf16ToUtf8(&pString->String.ucs2[0], ppszName);
260 }
261 }
262 RTMemFree(pString);
263 }
264 else
265 rc = VERR_INVALID_PARAMETER;
266 return rc;
267}
268
269/**
270 * Retrieves the prefix for a shared folder mount point. If no prefix
271 * is set in the guest properties "sf_" is returned.
272 *
273 * @returns VBox status code.
274 * @param ppszPrefix Where to return the prefix string. This shall be
275 * freed by calling RTStrFree.
276 */
277VBGLR3DECL(int) VbglR3SharedFolderGetMountPrefix(char **ppszPrefix)
278{
279 AssertPtrReturn(ppszPrefix, VERR_INVALID_POINTER);
280 int rc;
281#ifdef VBOX_WITH_GUEST_PROPS
282 uint32_t u32ClientIdGuestProp;
283 rc = VbglR3GuestPropConnect(&u32ClientIdGuestProp);
284 if (RT_SUCCESS(rc))
285 {
286 rc = VbglR3GuestPropReadValueAlloc(u32ClientIdGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountPrefix", ppszPrefix);
287 if (rc == VERR_NOT_FOUND) /* No prefix set? Then set the default. */
288 {
289#endif
290 rc = RTStrDupEx(ppszPrefix, "sf_");
291#ifdef VBOX_WITH_GUEST_PROPS
292 }
293 VbglR3GuestPropDisconnect(u32ClientIdGuestProp);
294 }
295#endif
296 return rc;
297}
298
299/**
300 * Retrieves the mount root directory for auto-mounted shared
301 * folders. mount point. If no string is set (VERR_NOT_FOUND)
302 * it's up on the caller (guest) to decide where to mount.
303 *
304 * @returns VBox status code.
305 * @param ppszDir Where to return the directory
306 * string. This shall be freed by
307 * calling RTStrFree.
308 */
309VBGLR3DECL(int) VbglR3SharedFolderGetMountDir(char **ppszDir)
310{
311 AssertPtrReturn(ppszDir, VERR_INVALID_POINTER);
312 int rc;
313#ifdef VBOX_WITH_GUEST_PROPS
314 uint32_t u32ClientIdGuestProp;
315 rc = VbglR3GuestPropConnect(&u32ClientIdGuestProp);
316 if (RT_SUCCESS(rc))
317 {
318 rc = VbglR3GuestPropReadValueAlloc(u32ClientIdGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountDir", ppszDir);
319 VbglR3GuestPropDisconnect(u32ClientIdGuestProp);
320 }
321#endif
322 return rc;
323}
324
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