VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3LibSharedFolders.cpp@ 68654

Last change on this file since 68654 was 68650, checked in by vboxsync, 7 years ago

VBGLR3Internal.h -> VBoxGuestR3LibInternal.h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1/* $Id: VBoxGuestR3LibSharedFolders.cpp 68650 2017-09-05 14:34:23Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, shared folders.
4 */
5
6/*
7 * Copyright (C) 2010-2016 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 */
49VBGLR3DECL(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 */
61VBGLR3DECL(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 */
74VBGLR3DECL(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 */
115VBGLR3DECL(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);
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 */
189VBGLR3DECL(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 */
205VBGLR3DECL(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;
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_INVALID_PARAMETER;
236 return rc;
237}
238
239/**
240 * Retrieves the prefix for a shared folder mount point. If no prefix
241 * is set in the guest properties "sf_" is returned.
242 *
243 * @returns VBox status code.
244 * @param ppszPrefix Where to return the prefix string. This shall be
245 * freed by calling RTStrFree.
246 */
247VBGLR3DECL(int) VbglR3SharedFolderGetMountPrefix(char **ppszPrefix)
248{
249 AssertPtrReturn(ppszPrefix, VERR_INVALID_POINTER);
250 int rc;
251#ifdef VBOX_WITH_GUEST_PROPS
252 HGCMCLIENTID idClientGuestProp;
253 rc = VbglR3GuestPropConnect(&idClientGuestProp);
254 if (RT_SUCCESS(rc))
255 {
256 rc = VbglR3GuestPropReadValueAlloc(idClientGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountPrefix", ppszPrefix);
257 if (rc == VERR_NOT_FOUND) /* No prefix set? Then set the default. */
258 {
259#endif
260 rc = RTStrDupEx(ppszPrefix, "sf_");
261#ifdef VBOX_WITH_GUEST_PROPS
262 }
263 VbglR3GuestPropDisconnect(idClientGuestProp);
264 }
265#endif
266 return rc;
267}
268
269/**
270 * Retrieves the mount root directory for auto-mounted shared
271 * folders. mount point. If no string is set (VERR_NOT_FOUND)
272 * it's up on the caller (guest) to decide where to mount.
273 *
274 * @returns VBox status code.
275 * @param ppszDir Where to return the directory
276 * string. This shall be freed by
277 * calling RTStrFree.
278 */
279VBGLR3DECL(int) VbglR3SharedFolderGetMountDir(char **ppszDir)
280{
281 AssertPtrReturn(ppszDir, VERR_INVALID_POINTER);
282 int rc = VERR_NOT_FOUND;
283#ifdef VBOX_WITH_GUEST_PROPS
284 HGCMCLIENTID idClientGuestProp;
285 rc = VbglR3GuestPropConnect(&idClientGuestProp);
286 if (RT_SUCCESS(rc))
287 {
288 rc = VbglR3GuestPropReadValueAlloc(idClientGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountDir", ppszDir);
289 VbglR3GuestPropDisconnect(idClientGuestProp);
290 }
291#endif
292 return rc;
293}
294
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