1 | /* $Id: VBoxGuestR3LibSharedFolders.cpp 51997 2014-07-11 21:04:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, shared folders.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2012 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 | */
|
---|
49 | VBGLR3DECL(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 | */
|
---|
75 | VBGLR3DECL(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 | */
|
---|
95 | VBGLR3DECL(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 | */
|
---|
137 | VBGLR3DECL(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 | /* In this case, just return success with 0 mappings */
|
---|
203 | if ( rc == VERR_INVALID_PARAMETER
|
---|
204 | && fAutoMountOnly)
|
---|
205 | rc = VINF_SUCCESS;
|
---|
206 |
|
---|
207 | *ppaMappings = ppaMappingsTemp;
|
---|
208 |
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Frees the shared folder mappings allocated by
|
---|
215 | * VbglR3SharedFolderGetMappings() before.
|
---|
216 | *
|
---|
217 | * @param paMappings What
|
---|
218 | */
|
---|
219 | VBGLR3DECL(void) VbglR3SharedFolderFreeMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings)
|
---|
220 | {
|
---|
221 | if (paMappings)
|
---|
222 | RTMemFree(paMappings);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Get the real name of a shared folder.
|
---|
228 | *
|
---|
229 | * @returns VBox status code.
|
---|
230 | * @param u32ClientId The client id returned by VbglR3InvsSvcConnect().
|
---|
231 | * @param u32Root Root ID of shared folder to get the name for.
|
---|
232 | * @param ppszName Where to return the name string. This shall be
|
---|
233 | * freed by calling RTStrFree.
|
---|
234 | */
|
---|
235 | VBGLR3DECL(int) VbglR3SharedFolderGetName(uint32_t u32ClientId, uint32_t u32Root, char **ppszName)
|
---|
236 | {
|
---|
237 | AssertPtr(ppszName);
|
---|
238 |
|
---|
239 | VBoxSFQueryMapName Msg;
|
---|
240 |
|
---|
241 | Msg.callInfo.result = VERR_WRONG_ORDER;
|
---|
242 | Msg.callInfo.u32ClientID = u32ClientId;
|
---|
243 | Msg.callInfo.u32Function = SHFL_FN_QUERY_MAP_NAME;
|
---|
244 | Msg.callInfo.cParms = 2;
|
---|
245 |
|
---|
246 | int rc;
|
---|
247 | uint32_t cbString = SHFLSTRING_HEADER_SIZE + SHFL_MAX_LEN;
|
---|
248 | PSHFLSTRING pString = (PSHFLSTRING)RTMemAlloc(cbString);
|
---|
249 | if (pString)
|
---|
250 | {
|
---|
251 | if (!ShflStringInitBuffer(pString, cbString))
|
---|
252 | {
|
---|
253 | RTMemFree(pString);
|
---|
254 | return VERR_INVALID_PARAMETER;
|
---|
255 | }
|
---|
256 |
|
---|
257 | VbglHGCMParmUInt32Set(&Msg.root, u32Root);
|
---|
258 | VbglHGCMParmPtrSet(&Msg.name, pString, cbString);
|
---|
259 |
|
---|
260 | rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
261 | if (RT_SUCCESS(rc))
|
---|
262 | {
|
---|
263 | rc = Msg.callInfo.result;
|
---|
264 | if (RT_SUCCESS(rc))
|
---|
265 | {
|
---|
266 | *ppszName = NULL;
|
---|
267 | rc = RTUtf16ToUtf8(&pString->String.ucs2[0], ppszName);
|
---|
268 | }
|
---|
269 | }
|
---|
270 | RTMemFree(pString);
|
---|
271 | }
|
---|
272 | else
|
---|
273 | rc = VERR_INVALID_PARAMETER;
|
---|
274 | return rc;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Retrieves the prefix for a shared folder mount point. If no prefix
|
---|
279 | * is set in the guest properties "sf_" is returned.
|
---|
280 | *
|
---|
281 | * @returns VBox status code.
|
---|
282 | * @param ppszPrefix Where to return the prefix string. This shall be
|
---|
283 | * freed by calling RTStrFree.
|
---|
284 | */
|
---|
285 | VBGLR3DECL(int) VbglR3SharedFolderGetMountPrefix(char **ppszPrefix)
|
---|
286 | {
|
---|
287 | AssertPtrReturn(ppszPrefix, VERR_INVALID_POINTER);
|
---|
288 | int rc;
|
---|
289 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
290 | uint32_t u32ClientIdGuestProp;
|
---|
291 | rc = VbglR3GuestPropConnect(&u32ClientIdGuestProp);
|
---|
292 | if (RT_SUCCESS(rc))
|
---|
293 | {
|
---|
294 | rc = VbglR3GuestPropReadValueAlloc(u32ClientIdGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountPrefix", ppszPrefix);
|
---|
295 | if (rc == VERR_NOT_FOUND) /* No prefix set? Then set the default. */
|
---|
296 | {
|
---|
297 | #endif
|
---|
298 | rc = RTStrDupEx(ppszPrefix, "sf_");
|
---|
299 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
300 | }
|
---|
301 | VbglR3GuestPropDisconnect(u32ClientIdGuestProp);
|
---|
302 | }
|
---|
303 | #endif
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Retrieves the mount root directory for auto-mounted shared
|
---|
309 | * folders. mount point. If no string is set (VERR_NOT_FOUND)
|
---|
310 | * it's up on the caller (guest) to decide where to mount.
|
---|
311 | *
|
---|
312 | * @returns VBox status code.
|
---|
313 | * @param ppszDir Where to return the directory
|
---|
314 | * string. This shall be freed by
|
---|
315 | * calling RTStrFree.
|
---|
316 | */
|
---|
317 | VBGLR3DECL(int) VbglR3SharedFolderGetMountDir(char **ppszDir)
|
---|
318 | {
|
---|
319 | AssertPtrReturn(ppszDir, VERR_INVALID_POINTER);
|
---|
320 | int rc;
|
---|
321 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
322 | uint32_t u32ClientIdGuestProp;
|
---|
323 | rc = VbglR3GuestPropConnect(&u32ClientIdGuestProp);
|
---|
324 | if (RT_SUCCESS(rc))
|
---|
325 | {
|
---|
326 | rc = VbglR3GuestPropReadValueAlloc(u32ClientIdGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountDir", ppszDir);
|
---|
327 | VbglR3GuestPropDisconnect(u32ClientIdGuestProp);
|
---|
328 | }
|
---|
329 | #endif
|
---|
330 | return rc;
|
---|
331 | }
|
---|
332 |
|
---|