VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedFolders/mappings.cpp@ 17822

Last change on this file since 17822 was 14215, checked in by vboxsync, 16 years ago

Fixed some warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/** @file
2 * Shared Folders: Mappings support.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
17 * Clara, CA 95054 USA or visit http://www.sun.com if you need
18 * additional information or have any questions.
19 */
20
21#include "mappings.h"
22#include <iprt/alloc.h>
23#include <iprt/assert.h>
24#include <iprt/string.h>
25
26MAPPING FolderMapping[SHFL_MAX_MAPPINGS];
27
28
29/*
30 *
31 * We are always executed from one specific HGCM thread. So thread safe.
32 *
33 */
34int vbsfMappingsAdd (PSHFLSTRING pFolderName, PSHFLSTRING pMapName, uint32_t fWritable)
35{
36 int i;
37
38 Assert(pFolderName && pMapName);
39
40 Log(("vbsfMappingsAdd %ls\n", pMapName->String.ucs2));
41
42 /* check for duplicates */
43 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
44 {
45 if (FolderMapping[i].fValid == true)
46 {
47 if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2))
48 {
49 AssertMsgFailed(("vbsfMappingsAdd: %ls mapping already exists!!\n", pMapName->String.ucs2));
50 return VERR_ALREADY_EXISTS;
51 }
52 }
53 }
54
55 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
56 {
57 if (FolderMapping[i].fValid == false)
58 {
59 FolderMapping[i].pFolderName = (PSHFLSTRING)RTMemAlloc(ShflStringSizeOfBuffer(pFolderName));
60 Assert(FolderMapping[i].pFolderName);
61 if (FolderMapping[i].pFolderName == NULL)
62 return VERR_NO_MEMORY;
63
64 FolderMapping[i].pFolderName->u16Length = pFolderName->u16Length;
65 FolderMapping[i].pFolderName->u16Size = pFolderName->u16Size;
66 memcpy(FolderMapping[i].pFolderName->String.ucs2, pFolderName->String.ucs2, pFolderName->u16Size);
67
68 FolderMapping[i].pMapName = (PSHFLSTRING)RTMemAlloc(ShflStringSizeOfBuffer(pMapName));
69 Assert(FolderMapping[i].pMapName);
70 if (FolderMapping[i].pMapName == NULL)
71 return VERR_NO_MEMORY;
72
73 FolderMapping[i].pMapName->u16Length = pMapName->u16Length;
74 FolderMapping[i].pMapName->u16Size = pMapName->u16Size;
75 memcpy(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2, pMapName->u16Size);
76
77 FolderMapping[i].fValid = true;
78 FolderMapping[i].cMappings = 0;
79 FolderMapping[i].fWritable = !!fWritable;
80
81 /* Check if the host file system is case sensitive */
82 RTFSPROPERTIES prop;
83 char *utf8Root, *asciiroot;
84
85 int rc = RTUtf16ToUtf8(FolderMapping[i].pFolderName->String.ucs2, &utf8Root);
86 AssertRC(rc);
87
88 if (RT_SUCCESS(rc))
89 {
90 rc = RTStrUtf8ToCurrentCP(&asciiroot, utf8Root);
91 if (RT_SUCCESS(rc))
92 {
93 rc = RTFsQueryProperties(asciiroot, &prop);
94 AssertRC(rc);
95 RTStrFree(asciiroot);
96 }
97 RTStrFree(utf8Root);
98 }
99 FolderMapping[i].fHostCaseSensitive = RT_SUCCESS(rc) ? prop.fCaseSensitive : false;
100 break;
101 }
102 }
103 if (i == SHFL_MAX_MAPPINGS)
104 {
105 AssertMsgFailed(("vbsfMappingsAdd: no more room to add mapping %ls to %ls!!\n", pFolderName->String.ucs2, pMapName->String.ucs2));
106 return VERR_TOO_MUCH_DATA;
107 }
108
109 Log(("vbsfMappingsAdd: added mapping %ls to %ls\n", pFolderName->String.ucs2, pMapName->String.ucs2));
110 return VINF_SUCCESS;
111}
112
113int vbsfMappingsRemove (PSHFLSTRING pMapName)
114{
115 int i;
116
117 Assert(pMapName);
118
119 Log(("vbsfMappingsRemove %ls\n", pMapName->String.ucs2));
120 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
121 {
122 if (FolderMapping[i].fValid == true)
123 {
124 if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2))
125 {
126 if (FolderMapping[i].cMappings != 0)
127 {
128 Log(("vbsfMappingsRemove: trying to remove active share %ls\n", pMapName->String.ucs2));
129 return VERR_PERMISSION_DENIED;
130 }
131
132 RTMemFree(FolderMapping[i].pFolderName);
133 RTMemFree(FolderMapping[i].pMapName);
134 FolderMapping[i].pFolderName = NULL;
135 FolderMapping[i].pMapName = NULL;
136 FolderMapping[i].fValid = false;
137 break;
138 }
139 }
140 }
141
142 if (i == SHFL_MAX_MAPPINGS)
143 {
144 AssertMsgFailed(("vbsfMappingsRemove: mapping %ls not found!!!!\n", pMapName->String.ucs2));
145 return VERR_FILE_NOT_FOUND;
146 }
147 Log(("vbsfMappingsRemove: mapping %ls removed\n", pMapName->String.ucs2));
148 return VINF_SUCCESS;
149}
150
151PCRTUTF16 vbsfMappingsQueryHostRoot (SHFLROOT root, uint32_t *pcbRoot)
152{
153 if (root > SHFL_MAX_MAPPINGS)
154 {
155 AssertFailed();
156 return NULL;
157 }
158
159 *pcbRoot = FolderMapping[root].pFolderName->u16Size;
160 return &FolderMapping[root].pFolderName->String.ucs2[0];
161}
162
163bool vbsfIsGuestMappingCaseSensitive (SHFLROOT root)
164{
165 if (root > SHFL_MAX_MAPPINGS)
166 {
167 AssertFailed();
168 return false;
169 }
170
171 return FolderMapping[root].fGuestCaseSensitive;
172}
173
174bool vbsfIsHostMappingCaseSensitive (SHFLROOT root)
175{
176 if (root > SHFL_MAX_MAPPINGS)
177 {
178 AssertFailed();
179 return false;
180 }
181
182 return FolderMapping[root].fHostCaseSensitive;
183}
184
185int vbsfMappingsQuery (SHFLCLIENTDATA *pClient, SHFLMAPPING *pMappings, uint32_t *pcMappings)
186{
187 int rc = VINF_SUCCESS;
188 uint32_t cMaxMappings = RT_MIN(*pcMappings, SHFL_MAX_MAPPINGS);
189
190 LogFlow(("vbsfMappingsQuery: pClient = %p, pMappings = %p, pcMappings = %p, *pcMappings = %d\n",
191 pClient, pMappings, pcMappings, *pcMappings));
192
193 *pcMappings = 0;
194 for (uint32_t i=0;i<cMaxMappings;i++)
195 {
196 if (FolderMapping[i].fValid == true)
197 {
198 pMappings[*pcMappings].u32Status = SHFL_MS_NEW;
199 pMappings[*pcMappings].root = i;
200 *pcMappings = *pcMappings + 1;
201 }
202 }
203
204 LogFlow(("vbsfMappingsQuery: return rc = %Rrc\n", rc));
205
206 return rc;
207}
208
209int vbsfMappingsQueryName (SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pString)
210{
211 int rc = VINF_SUCCESS;
212
213 LogFlow(("vbsfMappingsQuery: pClient = %p, root = %d, *pString = %p\n",
214 pClient, root, pString));
215
216 if (root >= SHFL_MAX_MAPPINGS)
217 return VERR_INVALID_PARAMETER;
218
219 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
220 {
221 /* not implemented */
222 AssertFailed();
223 return VERR_INVALID_PARAMETER;
224 }
225
226 if (FolderMapping[root].fValid == true)
227 {
228 pString->u16Length = FolderMapping[root].pMapName->u16Length;
229 memcpy(pString->String.ucs2, FolderMapping[root].pMapName->String.ucs2, pString->u16Size);
230 }
231 else
232 rc = VERR_FILE_NOT_FOUND;
233
234 LogFlow(("vbsfMappingsQuery:Name return rc = %Rrc\n", rc));
235
236 return rc;
237}
238
239int vbsfMappingsQueryWritable (SHFLCLIENTDATA *pClient, SHFLROOT root, bool *fWritable)
240{
241 int rc = VINF_SUCCESS;
242
243 LogFlow(("vbsfMappingsQueryWritable: pClient = %p, root = %d\n",
244 pClient, root));
245
246 if (root >= SHFL_MAX_MAPPINGS)
247 return VERR_INVALID_PARAMETER;
248
249 if (FolderMapping[root].fValid == true)
250 *fWritable = FolderMapping[root].fWritable;
251 else
252 rc = VERR_FILE_NOT_FOUND;
253
254 LogFlow(("vbsfMappingsQuery:Writable return rc = %Rrc\n", rc));
255
256 return rc;
257}
258
259static int vbsfQueryMappingIndex (PRTUTF16 utf16Name, size_t *pIndex)
260{
261 size_t i;
262
263 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
264 {
265 if (FolderMapping[i].fValid == true)
266 {
267 if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, utf16Name))
268 {
269 *pIndex = i;
270 return 0;
271 }
272 }
273 }
274 return -1;
275}
276
277int vbsfMapFolder (SHFLCLIENTDATA *pClient, PSHFLSTRING pszMapName, RTUTF16 delimiter, bool fCaseSensitive, SHFLROOT *pRoot)
278{
279 size_t index;
280
281 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
282 {
283 Log(("vbsfMapFolder %s\n", pszMapName->String.utf8));
284 }
285 else
286 {
287 Log(("vbsfMapFolder %ls\n", pszMapName->String.ucs2));
288 }
289
290 if (pClient->PathDelimiter == 0)
291 {
292 pClient->PathDelimiter = delimiter;
293 }
294 else
295 {
296 Assert(delimiter == pClient->PathDelimiter);
297 }
298
299 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
300 {
301 int rc;
302 PRTUTF16 utf16Name;
303
304 rc = RTStrToUtf16 ((const char *) pszMapName->String.utf8, &utf16Name);
305 if (RT_FAILURE (rc))
306 return rc;
307
308 rc = vbsfQueryMappingIndex (utf16Name, &index);
309 RTUtf16Free (utf16Name);
310
311 if (rc)
312 {
313 // AssertMsgFailed(("vbsfMapFolder: map %s not found!!\n",
314 // pszMapName->String.utf8));
315 return VERR_FILE_NOT_FOUND;
316 }
317 }
318 else
319 {
320 if (vbsfQueryMappingIndex (pszMapName->String.ucs2, &index))
321 {
322 // AssertMsgFailed(("vbsfMapFolder: map %ls not found!!\n",
323 // pszMapName->String.ucs2));
324 return VERR_FILE_NOT_FOUND;
325 }
326 }
327
328 FolderMapping[index].cMappings++;
329 Assert(FolderMapping[index].cMappings == 1 || FolderMapping[index].fGuestCaseSensitive == fCaseSensitive);
330 FolderMapping[index].fGuestCaseSensitive = fCaseSensitive;
331 *pRoot = (SHFLROOT)index;
332 return VINF_SUCCESS;
333}
334
335int vbsfUnmapFolder (SHFLCLIENTDATA *pClient, SHFLROOT root)
336{
337 int rc = VINF_SUCCESS;
338
339 if (root > SHFL_MAX_MAPPINGS)
340 {
341 AssertFailed();
342 return VERR_FILE_NOT_FOUND;
343 }
344
345 Assert(FolderMapping[root].fValid == true && FolderMapping[root].cMappings > 0);
346 if (FolderMapping[root].cMappings > 0)
347 FolderMapping[root].cMappings--;
348
349 Log(("vbsfUnmapFolder\n"));
350 return rc;
351}
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