VirtualBox

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

Last change on this file since 4994 was 4071, checked in by vboxsync, 18 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 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 as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * 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)
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
76 /* Check if the host file system is case sensitive */
77 RTFSPROPERTIES prop;
78 char *utf8Root, *asciiroot;
79
80 int rc = RTUtf16ToUtf8(FolderMapping[i].pFolderName->String.ucs2, &utf8Root);
81 AssertRC(rc);
82
83 if (VBOX_SUCCESS(rc))
84 {
85 rc = RTStrUtf8ToCurrentCP(&asciiroot, utf8Root);
86 if (VBOX_SUCCESS(rc))
87 {
88 rc = RTFsQueryProperties(asciiroot, &prop);
89 AssertRC(rc);
90 RTStrFree(asciiroot);
91 }
92 RTStrFree(utf8Root);
93 }
94 FolderMapping[i].fHostCaseSensitive = VBOX_SUCCESS(rc) ? prop.fCaseSensitive : false;
95 break;
96 }
97 }
98 if (i == SHFL_MAX_MAPPINGS)
99 {
100 AssertMsgFailed(("vbsfMappingsAdd: no more room to add mapping %ls to %ls!!\n", pFolderName->String.ucs2, pMapName->String.ucs2));
101 return VERR_TOO_MUCH_DATA;
102 }
103
104 Log(("vbsfMappingsAdd: added mapping %ls to %ls\n", pFolderName->String.ucs2, pMapName->String.ucs2));
105 return VINF_SUCCESS;
106}
107
108int vbsfMappingsRemove (PSHFLSTRING pMapName)
109{
110 int i;
111
112 Assert(pMapName);
113
114 Log(("vbsfMappingsRemove %ls\n", pMapName->String.ucs2));
115 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
116 {
117 if (FolderMapping[i].fValid == true)
118 {
119 if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2))
120 {
121 if (FolderMapping[i].cMappings != 0)
122 {
123 Log(("vbsfMappingsRemove: trying to remove active share %ls\n", pMapName->String.ucs2));
124 return VERR_PERMISSION_DENIED;
125 }
126
127 RTMemFree(FolderMapping[i].pFolderName);
128 RTMemFree(FolderMapping[i].pMapName);
129 FolderMapping[i].pFolderName = NULL;
130 FolderMapping[i].pMapName = NULL;
131 FolderMapping[i].fValid = false;
132 break;
133 }
134 }
135 }
136
137 if (i == SHFL_MAX_MAPPINGS)
138 {
139 AssertMsgFailed(("vbsfMappingsRemove: mapping %ls not found!!!!\n", pMapName->String.ucs2));
140 return VERR_FILE_NOT_FOUND;
141 }
142 Log(("vbsfMappingsRemove: mapping %ls removed\n", pMapName->String.ucs2));
143 return VINF_SUCCESS;
144}
145
146const RTUCS2 *vbsfMappingsQueryHostRoot (SHFLROOT root, uint32_t *pcbRoot)
147{
148 if (root > SHFL_MAX_MAPPINGS)
149 {
150 AssertFailed();
151 return NULL;
152 }
153
154 *pcbRoot = FolderMapping[root].pFolderName->u16Size;
155 return &FolderMapping[root].pFolderName->String.ucs2[0];
156}
157
158bool vbsfIsGuestMappingCaseSensitive (SHFLROOT root)
159{
160 if (root > SHFL_MAX_MAPPINGS)
161 {
162 AssertFailed();
163 return false;
164 }
165
166 return FolderMapping[root].fGuestCaseSensitive;
167}
168
169bool vbsfIsHostMappingCaseSensitive (SHFLROOT root)
170{
171 if (root > SHFL_MAX_MAPPINGS)
172 {
173 AssertFailed();
174 return false;
175 }
176
177 return FolderMapping[root].fHostCaseSensitive;
178}
179
180int vbsfMappingsQuery (SHFLCLIENTDATA *pClient, SHFLMAPPING *pMappings, uint32_t *pcMappings)
181{
182 int rc = VINF_SUCCESS;
183 uint32_t cMaxMappings = RT_MIN(*pcMappings, SHFL_MAX_MAPPINGS);
184
185 LogFlow(("vbsfMappingsQuery: pClient = %p, pMappings = %p, pcMappings = %p, *pcMappings = %d\n",
186 pClient, pMappings, pcMappings, *pcMappings));
187
188 *pcMappings = 0;
189 for (uint32_t i=0;i<cMaxMappings;i++)
190 {
191 if (FolderMapping[i].fValid == true)
192 {
193 pMappings[*pcMappings].u32Status = SHFL_MS_NEW;
194 pMappings[*pcMappings].root = i;
195 *pcMappings = *pcMappings + 1;
196 }
197 }
198
199 LogFlow(("vbsfMappingsQuery: return rc = %Vrc\n", rc));
200
201 return rc;
202}
203
204int vbsfMappingsQueryName (SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pString)
205{
206 int rc = VINF_SUCCESS;
207
208 LogFlow(("vbsfMappingsQuery: pClient = %p, root = %d, *pString = %p\n",
209 pClient, root, pString));
210
211 if (root >= SHFL_MAX_MAPPINGS)
212 return VERR_INVALID_PARAMETER;
213
214 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
215 {
216 /* not implemented */
217 AssertFailed();
218 return VERR_INVALID_PARAMETER;
219 }
220
221 if (FolderMapping[root].fValid == true)
222 {
223 pString->u16Length = FolderMapping[root].pMapName->u16Length;
224 memcpy(pString->String.ucs2, FolderMapping[root].pMapName->String.ucs2, pString->u16Size);
225 }
226 else
227 rc = VERR_FILE_NOT_FOUND;
228
229 LogFlow(("vbsfMappingsQuery:Name return rc = %Vrc\n", rc));
230
231 return rc;
232}
233
234static int vbsfQueryMappingIndex (PRTUTF16 utf16Name, size_t *pIndex)
235{
236 size_t i;
237
238 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
239 {
240 if (FolderMapping[i].fValid == true)
241 {
242 if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, utf16Name))
243 {
244 *pIndex = i;
245 return 0;
246 }
247 }
248 }
249 return -1;
250}
251
252int vbsfMapFolder (SHFLCLIENTDATA *pClient, PSHFLSTRING pszMapName, RTUCS2 delimiter, bool fCaseSensitive, SHFLROOT *pRoot)
253{
254 size_t index;
255
256 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
257 {
258 Log(("vbsfMapFolder %s\n", pszMapName->String.utf8));
259 }
260 else
261 {
262 Log(("vbsfMapFolder %ls\n", pszMapName->String.ucs2));
263 }
264
265 if (pClient->PathDelimiter == 0)
266 {
267 pClient->PathDelimiter = delimiter;
268 }
269 else
270 {
271 Assert(delimiter == pClient->PathDelimiter);
272 }
273
274 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
275 {
276 int rc;
277 PRTUTF16 utf16Name;
278
279 rc = RTStrToUtf16 ((const char *) pszMapName->String.utf8, &utf16Name);
280 if (VBOX_FAILURE (rc))
281 return rc;
282
283 rc = vbsfQueryMappingIndex (utf16Name, &index);
284 RTUtf16Free (utf16Name);
285
286 if (rc)
287 {
288 // AssertMsgFailed(("vbsfMapFolder: map %s not found!!\n",
289 // pszMapName->String.utf8));
290 return VERR_FILE_NOT_FOUND;
291 }
292 }
293 else
294 {
295 if (vbsfQueryMappingIndex (pszMapName->String.ucs2, &index))
296 {
297 // AssertMsgFailed(("vbsfMapFolder: map %ls not found!!\n",
298 // pszMapName->String.ucs2));
299 return VERR_FILE_NOT_FOUND;
300 }
301 }
302
303 FolderMapping[index].cMappings++;
304 Assert(FolderMapping[index].cMappings == 1 || FolderMapping[index].fGuestCaseSensitive == fCaseSensitive);
305 FolderMapping[index].fGuestCaseSensitive = fCaseSensitive;
306 *pRoot = index;
307 return VINF_SUCCESS;
308}
309
310int vbsfUnmapFolder (SHFLCLIENTDATA *pClient, SHFLROOT root)
311{
312 int rc = VINF_SUCCESS;
313
314 if (root > SHFL_MAX_MAPPINGS)
315 {
316 AssertFailed();
317 return VERR_FILE_NOT_FOUND;
318 }
319
320 Assert(FolderMapping[root].fValid == true && FolderMapping[root].cMappings > 0);
321 if (FolderMapping[root].cMappings > 0)
322 FolderMapping[root].cMappings--;
323
324 Log(("vbsfUnmapFolder\n"));
325 return rc;
326}
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