1 | /* $Id: VBoxGuestR3LibSharedFolders.cpp 31025 2010-07-22 23:43:25Z 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 | */
|
---|
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 valgrid 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 | * Get the list of available shared folders.
|
---|
90 | *
|
---|
91 | * @returns VBox status code.
|
---|
92 | * @param u32ClientId The client id returned by VbglR3InvsSvcConnect().
|
---|
93 | * @param fAutoMountOnly Flag whether only auto-mounted shared folders
|
---|
94 | * should be reported.
|
---|
95 | * @param paMappings Pointer to a preallocated array which will retrieve the mapping info.
|
---|
96 | * @param cbMappings Size (in bytes) of the provided array.
|
---|
97 | * @param pcMappings On input, the size of @a paMappings gives as an
|
---|
98 | * item count. On output, the number of mappings
|
---|
99 | * returned in @a paMappings.
|
---|
100 | *
|
---|
101 | * @todo r=bird: cbMappings and @a *pcMappings overlap. The better API
|
---|
102 | * would be to change cbMappings to cMappings (the entries are fixed
|
---|
103 | * sized) and move the move the input aspect of @a *pcMappings to it.
|
---|
104 | *
|
---|
105 | * However, it would be better if this function would do the array
|
---|
106 | * allocation. This way you could deal with too-much-data conditions
|
---|
107 | * here (or hide the max-number-of-shared-folders-per-vm-define).
|
---|
108 | * Then paMappings would become ppaMappings and cbMappings could be
|
---|
109 | * removed altogether. *pcMappings would only be output. A
|
---|
110 | * corresponding VbglR3SharedFolderFreeMappings would be required for
|
---|
111 | * a 100% clean API (this is an (/going to be) offical API for C/C++
|
---|
112 | * programs).
|
---|
113 | */
|
---|
114 | VBGLR3DECL(int) VbglR3SharedFolderGetMappings(uint32_t u32ClientId, bool fAutoMountOnly,
|
---|
115 | PVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cbMappings,
|
---|
116 | uint32_t *pcMappings)
|
---|
117 | {
|
---|
118 | AssertPtr(pcMappings);
|
---|
119 |
|
---|
120 | VBoxSFQueryMappings Msg;
|
---|
121 |
|
---|
122 | Msg.callInfo.result = VERR_WRONG_ORDER;
|
---|
123 | Msg.callInfo.u32ClientID = u32ClientId;
|
---|
124 | Msg.callInfo.u32Function = SHFL_FN_QUERY_MAPPINGS;
|
---|
125 | Msg.callInfo.cParms = 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 | /* Init the rest of the message. */
|
---|
134 | VbglHGCMParmUInt32Set(&Msg.numberOfMappings, *pcMappings);
|
---|
135 | VbglHGCMParmPtrSet(&Msg.mappings, &paMappings[0], cbMappings);
|
---|
136 |
|
---|
137 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
138 | if (RT_SUCCESS(rc))
|
---|
139 | {
|
---|
140 | VbglHGCMParmUInt32Get(&Msg.numberOfMappings, pcMappings);
|
---|
141 | rc = Msg.callInfo.result;
|
---|
142 | }
|
---|
143 | return rc;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Get the real name of a shared folder.
|
---|
149 | *
|
---|
150 | * @returns VBox status code.
|
---|
151 | * @param u32ClientId The client id returned by VbglR3InvsSvcConnect().
|
---|
152 | * @param u32Root Root ID of shared folder to get the name for.
|
---|
153 | * @param ppszName Where to return the name string. This shall be
|
---|
154 | * freed by calling RTStrFree.
|
---|
155 | */
|
---|
156 | VBGLR3DECL(int) VbglR3SharedFolderGetName(uint32_t u32ClientId, uint32_t u32Root, char **ppszName)
|
---|
157 | {
|
---|
158 | AssertPtr(ppszName);
|
---|
159 |
|
---|
160 | VBoxSFQueryMapName Msg;
|
---|
161 |
|
---|
162 | Msg.callInfo.result = VERR_WRONG_ORDER;
|
---|
163 | Msg.callInfo.u32ClientID = u32ClientId;
|
---|
164 | Msg.callInfo.u32Function = SHFL_FN_QUERY_MAP_NAME;
|
---|
165 | Msg.callInfo.cParms = 2;
|
---|
166 |
|
---|
167 | int rc;
|
---|
168 | uint32_t cbString = sizeof(SHFLSTRING) + SHFL_MAX_LEN;
|
---|
169 | PSHFLSTRING pString = (PSHFLSTRING)RTMemAlloc(cbString);
|
---|
170 | if (pString)
|
---|
171 | {
|
---|
172 | RT_ZERO(*pString);
|
---|
173 | ShflStringInitBuffer(pString, SHFL_MAX_LEN);
|
---|
174 |
|
---|
175 | VbglHGCMParmUInt32Set(&Msg.root, u32Root);
|
---|
176 | VbglHGCMParmPtrSet(&Msg.name, pString, cbString);
|
---|
177 |
|
---|
178 | rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
179 | if (RT_SUCCESS(rc))
|
---|
180 | {
|
---|
181 | *ppszName = NULL;
|
---|
182 | rc = RTUtf16ToUtf8(&pString->String.ucs2[0], ppszName);
|
---|
183 | if (RT_SUCCESS(rc))
|
---|
184 | rc = Msg.callInfo.result; /** @todo r=bird: Shouldn't you check this *before* doing the conversion? */
|
---|
185 | }
|
---|
186 | RTMemFree(pString);
|
---|
187 | }
|
---|
188 | else
|
---|
189 | rc = VERR_INVALID_PARAMETER;
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|