VirtualBox

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

Last change on this file since 7449 was 7429, checked in by vboxsync, 17 years ago

RTUCS2 -> RTUTF16.

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