VirtualBox

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

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

Refuse to delete folders that are in use. (VERR_PERMISSION_DENIED)

  • 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 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
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)
35{
36 int i;
37
38 Assert(pFolderName && pMapName);
39
40 /* check for duplicates */
41 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
42 {
43 if (FolderMapping[i].fValid == true)
44 {
45 if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2))
46 {
47 AssertMsgFailed(("vbsfMappingsAdd: %ls mapping already exists!!\n", pMapName->String.ucs2));
48 return VERR_ALREADY_EXISTS;
49 }
50 }
51 }
52
53 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
54 {
55 if (FolderMapping[i].fValid == false)
56 {
57 FolderMapping[i].pFolderName = (PSHFLSTRING)RTMemAlloc(ShflStringSizeOfBuffer(pFolderName));
58 Assert(FolderMapping[i].pFolderName);
59 if (FolderMapping[i].pFolderName == NULL)
60 return VERR_NO_MEMORY;
61
62 FolderMapping[i].pFolderName->u16Length = pFolderName->u16Length;
63 FolderMapping[i].pFolderName->u16Size = pFolderName->u16Size;
64 memcpy(FolderMapping[i].pFolderName->String.ucs2, pFolderName->String.ucs2, pFolderName->u16Size);
65
66 FolderMapping[i].pMapName = (PSHFLSTRING)RTMemAlloc(ShflStringSizeOfBuffer(pMapName));
67 Assert(FolderMapping[i].pMapName);
68 if (FolderMapping[i].pMapName == NULL)
69 return VERR_NO_MEMORY;
70
71 FolderMapping[i].pMapName->u16Length = pMapName->u16Length;
72 FolderMapping[i].pMapName->u16Size = pMapName->u16Size;
73 memcpy(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2, pMapName->u16Size);
74
75 FolderMapping[i].fValid = true;
76 FolderMapping[i].cMappings = 0;
77
78 /* Check if the host file system is case sensitive */
79 RTFSPROPERTIES prop;
80 char *utf8Root, *asciiroot;
81
82 int rc = RTUtf16ToUtf8(FolderMapping[i].pFolderName->String.ucs2, &utf8Root);
83 AssertRC(rc);
84
85 if (VBOX_SUCCESS(rc))
86 {
87 rc = RTStrUtf8ToCurrentCP(&asciiroot, utf8Root);
88 if (VBOX_SUCCESS(rc))
89 {
90 rc = RTFsQueryProperties(asciiroot, &prop);
91 AssertRC(rc);
92 RTStrFree(asciiroot);
93 }
94 RTStrFree(utf8Root);
95 }
96 FolderMapping[i].fHostCaseSensitive = VBOX_SUCCESS(rc) ? prop.fCaseSensitive : false;
97 break;
98 }
99 }
100 if (i == SHFL_MAX_MAPPINGS)
101 {
102 AssertMsgFailed(("vbsfMappingsAdd: no more room to add mapping %ls to %ls!!\n", pFolderName->String.ucs2, pMapName->String.ucs2));
103 return VERR_TOO_MUCH_DATA;
104 }
105
106 Log(("vbsfMappingsAdd: added mapping %ls to %ls\n", pFolderName->String.ucs2, pMapName->String.ucs2));
107 return VINF_SUCCESS;
108}
109
110int vbsfMappingsRemove (PSHFLSTRING pMapName)
111{
112 int i;
113
114 Assert(pMapName);
115
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));
141 return VERR_FILE_NOT_FOUND;
142 }
143 Log(("vbsfMappingsRemove: mapping %ls removed\n", pMapName->String));
144 return VINF_SUCCESS;
145}
146
147const RTUCS2 *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
235static int vbsfQueryMappingIndex (PRTUTF16 utf16Name, size_t *pIndex)
236{
237 size_t i;
238
239 for (i=0;i<SHFL_MAX_MAPPINGS;i++)
240 {
241 if (FolderMapping[i].fValid == true)
242 {
243 if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, utf16Name))
244 {
245 *pIndex = i;
246 return 0;
247 }
248 }
249 }
250 return -1;
251}
252
253int vbsfMapFolder (SHFLCLIENTDATA *pClient, PSHFLSTRING pszMapName, RTUCS2 delimiter, bool fCaseSensitive, SHFLROOT *pRoot)
254{
255 size_t index;
256
257 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
258 {
259 Log(("vbsfMapFolder %s\n", pszMapName->String.utf8));
260 }
261 else
262 {
263 Log(("vbsfMapFolder %ls\n", pszMapName->String.ucs2));
264 }
265
266 if (pClient->PathDelimiter == 0)
267 {
268 pClient->PathDelimiter = delimiter;
269 }
270 else
271 {
272 Assert(delimiter == pClient->PathDelimiter);
273 }
274
275 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
276 {
277 int rc;
278 PRTUTF16 utf16Name;
279
280 rc = RTStrToUtf16 ((const char *) pszMapName->String.utf8, &utf16Name);
281 if (VBOX_FAILURE (rc))
282 return rc;
283
284 rc = vbsfQueryMappingIndex (utf16Name, &index);
285 RTUtf16Free (utf16Name);
286
287 if (rc)
288 {
289 // AssertMsgFailed(("vbsfMapFolder: map %s not found!!\n",
290 // pszMapName->String.utf8));
291 return VERR_FILE_NOT_FOUND;
292 }
293 }
294 else
295 {
296 if (vbsfQueryMappingIndex (pszMapName->String.ucs2, &index))
297 {
298 // AssertMsgFailed(("vbsfMapFolder: map %ls not found!!\n",
299 // pszMapName->String.ucs2));
300 return VERR_FILE_NOT_FOUND;
301 }
302 }
303
304 FolderMapping[index].cMappings++;
305 Assert(FolderMapping[index].cMappings == 1 || FolderMapping[index].fGuestCaseSensitive == fCaseSensitive);
306 FolderMapping[index].fGuestCaseSensitive = fCaseSensitive;
307 *pRoot = index;
308 return VINF_SUCCESS;
309}
310
311int vbsfUnmapFolder (SHFLCLIENTDATA *pClient, SHFLROOT root)
312{
313 int rc = VINF_SUCCESS;
314
315 if (root > SHFL_MAX_MAPPINGS)
316 {
317 AssertFailed();
318 return VERR_FILE_NOT_FOUND;
319 }
320
321 Assert(FolderMapping[root].fValid == true && FolderMapping[root].cMappings > 0);
322 if (FolderMapping[root].cMappings > 0)
323 FolderMapping[root].cMappings--;
324
325 Log(("vbsfUnmapFolder\n"));
326 return rc;
327}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette