1 | /** @file
|
---|
2 | * Shared Folders: Mappings support.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
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 | #ifdef UNITTEST
|
---|
18 | # include "testcase/tstSharedFolderService.h"
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | #include "mappings.h"
|
---|
22 | #include "vbsfpath.h"
|
---|
23 | #include <iprt/alloc.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/path.h>
|
---|
26 | #include <iprt/string.h>
|
---|
27 |
|
---|
28 | #ifdef UNITTEST
|
---|
29 | # include "teststubs.h"
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | /* Shared folders order in the saved state and in the FolderMapping can differ.
|
---|
33 | * So a translation array of root handle is needed.
|
---|
34 | */
|
---|
35 |
|
---|
36 | static MAPPING FolderMapping[SHFL_MAX_MAPPINGS];
|
---|
37 | static SHFLROOT aIndexFromRoot[SHFL_MAX_MAPPINGS];
|
---|
38 |
|
---|
39 | void vbsfMappingInit(void)
|
---|
40 | {
|
---|
41 | unsigned root;
|
---|
42 |
|
---|
43 | for (root = 0; root < RT_ELEMENTS(aIndexFromRoot); root++)
|
---|
44 | {
|
---|
45 | aIndexFromRoot[root] = SHFL_ROOT_NIL;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | int vbsfMappingLoaded(const PMAPPING pLoadedMapping, SHFLROOT root)
|
---|
50 | {
|
---|
51 | /* Mapping loaded from the saved state with the index. Which means
|
---|
52 | * the guest uses the iMapping as root handle for this folder.
|
---|
53 | * Check whether there is the same mapping in FolderMapping and
|
---|
54 | * update the aIndexFromRoot.
|
---|
55 | *
|
---|
56 | * Also update the mapping properties, which were lost: cMappings.
|
---|
57 | */
|
---|
58 | if (root >= SHFL_MAX_MAPPINGS)
|
---|
59 | {
|
---|
60 | return VERR_INVALID_PARAMETER;
|
---|
61 | }
|
---|
62 |
|
---|
63 | SHFLROOT i;
|
---|
64 | for (i = 0; i < RT_ELEMENTS(FolderMapping); i++)
|
---|
65 | {
|
---|
66 | MAPPING *pMapping = &FolderMapping[i];
|
---|
67 |
|
---|
68 | /* Equal? */
|
---|
69 | if ( pLoadedMapping->fValid == pMapping->fValid
|
---|
70 | && ShflStringSizeOfBuffer(pLoadedMapping->pMapName) == ShflStringSizeOfBuffer(pMapping->pMapName)
|
---|
71 | && memcmp(pLoadedMapping->pMapName, pMapping->pMapName, ShflStringSizeOfBuffer(pMapping->pMapName)) == 0)
|
---|
72 | {
|
---|
73 | /* Actual index is i. */
|
---|
74 | aIndexFromRoot[root] = i;
|
---|
75 |
|
---|
76 | /* Update the mapping properties. */
|
---|
77 | pMapping->cMappings = pLoadedMapping->cMappings;
|
---|
78 |
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* No corresponding mapping on the host but the guest still uses it.
|
---|
84 | * Add a 'placeholder' mapping.
|
---|
85 | */
|
---|
86 | LogRel2(("SharedFolders: mapping a placeholder for '%ls' -> '%s'\n",
|
---|
87 | pLoadedMapping->pMapName->String.ucs2, pLoadedMapping->pszFolderName));
|
---|
88 | return vbsfMappingsAdd(pLoadedMapping->pszFolderName, pLoadedMapping->pMapName,
|
---|
89 | pLoadedMapping->fWritable, pLoadedMapping->fAutoMount,
|
---|
90 | pLoadedMapping->fSymlinksCreate, /* fMissing = */ true, /* fPlaceholder = */ true);
|
---|
91 | }
|
---|
92 |
|
---|
93 | MAPPING *vbsfMappingGetByRoot(SHFLROOT root)
|
---|
94 | {
|
---|
95 | if (root < RT_ELEMENTS(aIndexFromRoot))
|
---|
96 | {
|
---|
97 | SHFLROOT iMapping = aIndexFromRoot[root];
|
---|
98 |
|
---|
99 | if ( iMapping != SHFL_ROOT_NIL
|
---|
100 | && iMapping < RT_ELEMENTS(FolderMapping))
|
---|
101 | {
|
---|
102 | return &FolderMapping[iMapping];
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | return NULL;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static SHFLROOT vbsfMappingGetRootFromIndex(SHFLROOT iMapping)
|
---|
110 | {
|
---|
111 | unsigned root;
|
---|
112 |
|
---|
113 | for (root = 0; root < RT_ELEMENTS(aIndexFromRoot); root++)
|
---|
114 | {
|
---|
115 | if (iMapping == aIndexFromRoot[root])
|
---|
116 | {
|
---|
117 | return root;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | return SHFL_ROOT_NIL;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static MAPPING *vbsfMappingGetByName (PRTUTF16 pwszName, SHFLROOT *pRoot)
|
---|
125 | {
|
---|
126 | unsigned i;
|
---|
127 |
|
---|
128 | for (i=0; i<SHFL_MAX_MAPPINGS; i++)
|
---|
129 | {
|
---|
130 | if (FolderMapping[i].fValid == true)
|
---|
131 | {
|
---|
132 | if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, pwszName))
|
---|
133 | {
|
---|
134 | SHFLROOT root = vbsfMappingGetRootFromIndex(i);
|
---|
135 |
|
---|
136 | if (root != SHFL_ROOT_NIL)
|
---|
137 | {
|
---|
138 | if (pRoot)
|
---|
139 | {
|
---|
140 | *pRoot = root;
|
---|
141 | }
|
---|
142 | return &FolderMapping[i];
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | AssertFailed();
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | return NULL;
|
---|
153 | }
|
---|
154 |
|
---|
155 | static void vbsfRootHandleAdd(SHFLROOT iMapping)
|
---|
156 | {
|
---|
157 | unsigned root;
|
---|
158 |
|
---|
159 | for (root = 0; root < RT_ELEMENTS(aIndexFromRoot); root++)
|
---|
160 | {
|
---|
161 | if (aIndexFromRoot[root] == SHFL_ROOT_NIL)
|
---|
162 | {
|
---|
163 | aIndexFromRoot[root] = iMapping;
|
---|
164 | return;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | AssertFailed();
|
---|
169 | }
|
---|
170 |
|
---|
171 | static void vbsfRootHandleRemove(SHFLROOT iMapping)
|
---|
172 | {
|
---|
173 | unsigned root;
|
---|
174 |
|
---|
175 | for (root = 0; root < RT_ELEMENTS(aIndexFromRoot); root++)
|
---|
176 | {
|
---|
177 | if (aIndexFromRoot[root] == iMapping)
|
---|
178 | {
|
---|
179 | aIndexFromRoot[root] = SHFL_ROOT_NIL;
|
---|
180 | return;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | AssertFailed();
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 |
|
---|
189 | #ifdef UNITTEST
|
---|
190 | /** Unit test the SHFL_FN_ADD_MAPPING API. Located here as a form of API
|
---|
191 | * documentation. */
|
---|
192 | void testMappingsAdd(RTTEST hTest)
|
---|
193 | {
|
---|
194 | /* If the number or types of parameters are wrong the API should fail. */
|
---|
195 | testMappingsAddBadParameters(hTest);
|
---|
196 | /* Add tests as required... */
|
---|
197 | }
|
---|
198 | #endif
|
---|
199 | /*
|
---|
200 | * We are always executed from one specific HGCM thread. So thread safe.
|
---|
201 | */
|
---|
202 | int vbsfMappingsAdd(const char *pszFolderName, PSHFLSTRING pMapName,
|
---|
203 | bool fWritable, bool fAutoMount, bool fSymlinksCreate, bool fMissing, bool fPlaceholder)
|
---|
204 | {
|
---|
205 | unsigned i;
|
---|
206 |
|
---|
207 | Assert(pszFolderName && pMapName);
|
---|
208 |
|
---|
209 | Log(("vbsfMappingsAdd %ls\n", pMapName->String.ucs2));
|
---|
210 |
|
---|
211 | /* check for duplicates */
|
---|
212 | for (i=0; i<SHFL_MAX_MAPPINGS; i++)
|
---|
213 | {
|
---|
214 | if (FolderMapping[i].fValid == true)
|
---|
215 | {
|
---|
216 | if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2))
|
---|
217 | {
|
---|
218 | AssertMsgFailed(("vbsfMappingsAdd: %ls mapping already exists!!\n", pMapName->String.ucs2));
|
---|
219 | return VERR_ALREADY_EXISTS;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | for (i=0; i<SHFL_MAX_MAPPINGS; i++)
|
---|
225 | {
|
---|
226 | if (FolderMapping[i].fValid == false)
|
---|
227 | {
|
---|
228 | /* Make sure the folder name is an absolute path, otherwise we're
|
---|
229 | likely to get into trouble with buffer sizes in vbsfPathGuestToHost. */
|
---|
230 | char szAbsFolderName[RTPATH_MAX];
|
---|
231 | int rc = vbsfPathAbs(NULL, pszFolderName, szAbsFolderName, sizeof(szAbsFolderName));
|
---|
232 | AssertRCReturn(rc, rc);
|
---|
233 |
|
---|
234 | FolderMapping[i].pszFolderName = RTStrDup(szAbsFolderName);
|
---|
235 | if (!FolderMapping[i].pszFolderName)
|
---|
236 | {
|
---|
237 | return VERR_NO_MEMORY;
|
---|
238 | }
|
---|
239 |
|
---|
240 | FolderMapping[i].pMapName = (PSHFLSTRING)RTMemAlloc(ShflStringSizeOfBuffer(pMapName));
|
---|
241 | if (!FolderMapping[i].pMapName)
|
---|
242 | {
|
---|
243 | RTStrFree(FolderMapping[i].pszFolderName);
|
---|
244 | AssertFailed();
|
---|
245 | return VERR_NO_MEMORY;
|
---|
246 | }
|
---|
247 |
|
---|
248 | FolderMapping[i].pMapName->u16Length = pMapName->u16Length;
|
---|
249 | FolderMapping[i].pMapName->u16Size = pMapName->u16Size;
|
---|
250 | memcpy(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2, pMapName->u16Size);
|
---|
251 |
|
---|
252 | FolderMapping[i].fValid = true;
|
---|
253 | FolderMapping[i].cMappings = 0;
|
---|
254 | FolderMapping[i].fWritable = fWritable;
|
---|
255 | FolderMapping[i].fAutoMount = fAutoMount;
|
---|
256 | FolderMapping[i].fSymlinksCreate = fSymlinksCreate;
|
---|
257 | FolderMapping[i].fMissing = fMissing;
|
---|
258 | FolderMapping[i].fPlaceholder = fPlaceholder;
|
---|
259 |
|
---|
260 | /* Check if the host file system is case sensitive */
|
---|
261 | RTFSPROPERTIES prop;
|
---|
262 | prop.fCaseSensitive = false; /* Shut up MSC. */
|
---|
263 | rc = RTFsQueryProperties(FolderMapping[i].pszFolderName, &prop);
|
---|
264 | AssertRC(rc);
|
---|
265 | FolderMapping[i].fHostCaseSensitive = RT_SUCCESS(rc) ? prop.fCaseSensitive : false;
|
---|
266 | vbsfRootHandleAdd(i);
|
---|
267 | break;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | if (i == SHFL_MAX_MAPPINGS)
|
---|
271 | {
|
---|
272 | AssertLogRelMsgFailed(("vbsfMappingsAdd: no more room to add mapping %s to %ls!!\n", pszFolderName, pMapName->String.ucs2));
|
---|
273 | return VERR_TOO_MUCH_DATA;
|
---|
274 | }
|
---|
275 |
|
---|
276 | Log(("vbsfMappingsAdd: added mapping %s to %ls\n", pszFolderName, pMapName->String.ucs2));
|
---|
277 | return VINF_SUCCESS;
|
---|
278 | }
|
---|
279 |
|
---|
280 | #ifdef UNITTEST
|
---|
281 | /** Unit test the SHFL_FN_REMOVE_MAPPING API. Located here as a form of API
|
---|
282 | * documentation. */
|
---|
283 | void testMappingsRemove(RTTEST hTest)
|
---|
284 | {
|
---|
285 | /* If the number or types of parameters are wrong the API should fail. */
|
---|
286 | testMappingsRemoveBadParameters(hTest);
|
---|
287 | /* Add tests as required... */
|
---|
288 | }
|
---|
289 | #endif
|
---|
290 | int vbsfMappingsRemove(PSHFLSTRING pMapName)
|
---|
291 | {
|
---|
292 | unsigned i;
|
---|
293 |
|
---|
294 | Assert(pMapName);
|
---|
295 |
|
---|
296 | Log(("vbsfMappingsRemove %ls\n", pMapName->String.ucs2));
|
---|
297 | for (i=0; i<SHFL_MAX_MAPPINGS; i++)
|
---|
298 | {
|
---|
299 | if (FolderMapping[i].fValid == true)
|
---|
300 | {
|
---|
301 | if (!RTUtf16LocaleICmp(FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2))
|
---|
302 | {
|
---|
303 | if (FolderMapping[i].cMappings != 0)
|
---|
304 | {
|
---|
305 | LogRel2(("SharedFolders: removing '%ls' -> '%s', which is still used by the guest\n",
|
---|
306 | pMapName->String.ucs2, FolderMapping[i].pszFolderName));
|
---|
307 | FolderMapping[i].fMissing = true;
|
---|
308 | FolderMapping[i].fPlaceholder = true;
|
---|
309 | return VINF_PERMISSION_DENIED;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /* pMapName can be the same as FolderMapping[i].pMapName,
|
---|
313 | * log it before deallocating the memory.
|
---|
314 | */
|
---|
315 | Log(("vbsfMappingsRemove: mapping %ls removed\n", pMapName->String.ucs2));
|
---|
316 |
|
---|
317 | RTStrFree(FolderMapping[i].pszFolderName);
|
---|
318 | RTMemFree(FolderMapping[i].pMapName);
|
---|
319 | FolderMapping[i].pszFolderName = NULL;
|
---|
320 | FolderMapping[i].pMapName = NULL;
|
---|
321 | FolderMapping[i].fValid = false;
|
---|
322 | vbsfRootHandleRemove(i);
|
---|
323 | return VINF_SUCCESS;
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | AssertMsgFailed(("vbsfMappingsRemove: mapping %ls not found!!!!\n", pMapName->String.ucs2));
|
---|
329 | return VERR_FILE_NOT_FOUND;
|
---|
330 | }
|
---|
331 |
|
---|
332 | const char* vbsfMappingsQueryHostRoot(SHFLROOT root)
|
---|
333 | {
|
---|
334 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
|
---|
335 | AssertReturn(pFolderMapping, NULL);
|
---|
336 | if (pFolderMapping->fMissing)
|
---|
337 | return NULL;
|
---|
338 | return pFolderMapping->pszFolderName;
|
---|
339 | }
|
---|
340 |
|
---|
341 | int vbsfMappingsQueryHostRootEx(SHFLROOT hRoot, const char **ppszRoot, uint32_t *pcbRootLen)
|
---|
342 | {
|
---|
343 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(hRoot);
|
---|
344 | AssertReturn(pFolderMapping, VERR_INVALID_PARAMETER);
|
---|
345 | if (pFolderMapping->fMissing)
|
---|
346 | return VERR_NOT_FOUND;
|
---|
347 | if ( pFolderMapping->pszFolderName == NULL
|
---|
348 | || pFolderMapping->pszFolderName[0] == 0)
|
---|
349 | return VERR_NOT_FOUND;
|
---|
350 | *ppszRoot = pFolderMapping->pszFolderName;
|
---|
351 | *pcbRootLen = (uint32_t)strlen(pFolderMapping->pszFolderName);
|
---|
352 | return VINF_SUCCESS;
|
---|
353 | }
|
---|
354 |
|
---|
355 | bool vbsfIsGuestMappingCaseSensitive(SHFLROOT root)
|
---|
356 | {
|
---|
357 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
|
---|
358 | AssertReturn(pFolderMapping, false);
|
---|
359 | return pFolderMapping->fGuestCaseSensitive;
|
---|
360 | }
|
---|
361 |
|
---|
362 | bool vbsfIsHostMappingCaseSensitive(SHFLROOT root)
|
---|
363 | {
|
---|
364 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
|
---|
365 | AssertReturn(pFolderMapping, false);
|
---|
366 | return pFolderMapping->fHostCaseSensitive;
|
---|
367 | }
|
---|
368 |
|
---|
369 | #ifdef UNITTEST
|
---|
370 | /** Unit test the SHFL_FN_QUERY_MAPPINGS API. Located here as a form of API
|
---|
371 | * documentation (or should it better be inline in include/VBox/shflsvc.h?) */
|
---|
372 | void testMappingsQuery(RTTEST hTest)
|
---|
373 | {
|
---|
374 | /* The API should return all mappings if we provide enough buffers. */
|
---|
375 | testMappingsQuerySimple(hTest);
|
---|
376 | /* If we provide too few buffers that should be signalled correctly. */
|
---|
377 | testMappingsQueryTooFewBuffers(hTest);
|
---|
378 | /* The SHFL_MF_AUTOMOUNT flag means return only auto-mounted mappings. */
|
---|
379 | testMappingsQueryAutoMount(hTest);
|
---|
380 | /* The mappings return array must have numberOfMappings entries. */
|
---|
381 | testMappingsQueryArrayWrongSize(hTest);
|
---|
382 | }
|
---|
383 | #endif
|
---|
384 | /**
|
---|
385 | * Note: If pMappings / *pcMappings is smaller than the actual amount of mappings
|
---|
386 | * that *could* have been returned *pcMappings contains the required buffer size
|
---|
387 | * so that the caller can retry the operation if wanted.
|
---|
388 | */
|
---|
389 | int vbsfMappingsQuery(PSHFLCLIENTDATA pClient, PSHFLMAPPING pMappings, uint32_t *pcMappings)
|
---|
390 | {
|
---|
391 | int rc = VINF_SUCCESS;
|
---|
392 |
|
---|
393 | uint32_t cMappings = 0; /* Will contain actual valid mappings. */
|
---|
394 | uint32_t idx = 0; /* Current index in mappings buffer. */
|
---|
395 |
|
---|
396 | LogFlow(("vbsfMappingsQuery: pClient = %p, pMappings = %p, pcMappings = %p, *pcMappings = %d\n",
|
---|
397 | pClient, pMappings, pcMappings, *pcMappings));
|
---|
398 |
|
---|
399 | for (uint32_t i = 0; i < SHFL_MAX_MAPPINGS; i++)
|
---|
400 | {
|
---|
401 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(i);
|
---|
402 | if ( pFolderMapping != NULL
|
---|
403 | && pFolderMapping->fValid == true)
|
---|
404 | {
|
---|
405 | if (idx < *pcMappings)
|
---|
406 | {
|
---|
407 | /* Skip mappings which are not marked for auto-mounting if
|
---|
408 | * the SHFL_MF_AUTOMOUNT flag ist set. */
|
---|
409 | if ( (pClient->fu32Flags & SHFL_MF_AUTOMOUNT)
|
---|
410 | && !pFolderMapping->fAutoMount)
|
---|
411 | continue;
|
---|
412 |
|
---|
413 | pMappings[idx].u32Status = SHFL_MS_NEW;
|
---|
414 | pMappings[idx].root = i;
|
---|
415 | idx++;
|
---|
416 | }
|
---|
417 | cMappings++;
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | /* Return actual number of mappings, regardless whether the handed in
|
---|
422 | * mapping buffer was big enough. */
|
---|
423 | *pcMappings = cMappings;
|
---|
424 |
|
---|
425 | LogFlow(("vbsfMappingsQuery: return rc = %Rrc\n", rc));
|
---|
426 | return rc;
|
---|
427 | }
|
---|
428 |
|
---|
429 | #ifdef UNITTEST
|
---|
430 | /** Unit test the SHFL_FN_QUERY_MAP_NAME API. Located here as a form of API
|
---|
431 | * documentation. */
|
---|
432 | void testMappingsQueryName(RTTEST hTest)
|
---|
433 | {
|
---|
434 | /* If we query an valid mapping it should be returned. */
|
---|
435 | testMappingsQueryNameValid(hTest);
|
---|
436 | /* If we query an invalid mapping that should be signalled. */
|
---|
437 | testMappingsQueryNameInvalid(hTest);
|
---|
438 | /* If we pass in a bad string buffer that should be detected. */
|
---|
439 | testMappingsQueryNameBadBuffer(hTest);
|
---|
440 | }
|
---|
441 | #endif
|
---|
442 | int vbsfMappingsQueryName(PSHFLCLIENTDATA pClient, SHFLROOT root, SHFLSTRING *pString)
|
---|
443 | {
|
---|
444 | int rc = VINF_SUCCESS;
|
---|
445 |
|
---|
446 | LogFlow(("vbsfMappingsQuery: pClient = %p, root = %d, *pString = %p\n",
|
---|
447 | pClient, root, pString));
|
---|
448 |
|
---|
449 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
|
---|
450 | if (pFolderMapping == NULL)
|
---|
451 | {
|
---|
452 | return VERR_INVALID_PARAMETER;
|
---|
453 | }
|
---|
454 |
|
---|
455 | if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
|
---|
456 | {
|
---|
457 | /* Not implemented. */
|
---|
458 | AssertFailed();
|
---|
459 | return VERR_INVALID_PARAMETER;
|
---|
460 | }
|
---|
461 |
|
---|
462 | if (pFolderMapping->fValid == true)
|
---|
463 | {
|
---|
464 | if (pString->u16Size < pFolderMapping->pMapName->u16Size)
|
---|
465 | {
|
---|
466 | Log(("vbsfMappingsQuery: passed string too short (%d < %d bytes)!\n",
|
---|
467 | pString->u16Size, pFolderMapping->pMapName->u16Size));
|
---|
468 | rc = VERR_INVALID_PARAMETER;
|
---|
469 | }
|
---|
470 | else
|
---|
471 | {
|
---|
472 | pString->u16Length = pFolderMapping->pMapName->u16Length;
|
---|
473 | memcpy(pString->String.ucs2, pFolderMapping->pMapName->String.ucs2,
|
---|
474 | pFolderMapping->pMapName->u16Size);
|
---|
475 | }
|
---|
476 | }
|
---|
477 | else
|
---|
478 | rc = VERR_FILE_NOT_FOUND;
|
---|
479 |
|
---|
480 | LogFlow(("vbsfMappingsQuery:Name return rc = %Rrc\n", rc));
|
---|
481 |
|
---|
482 | return rc;
|
---|
483 | }
|
---|
484 |
|
---|
485 | /** Queries fWritable flag for the given root. Returns error if the root is not accessible.
|
---|
486 | */
|
---|
487 | int vbsfMappingsQueryWritable(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fWritable)
|
---|
488 | {
|
---|
489 | RT_NOREF1(pClient);
|
---|
490 | int rc = VINF_SUCCESS;
|
---|
491 |
|
---|
492 | LogFlow(("vbsfMappingsQueryWritable: pClient = %p, root = %d\n", pClient, root));
|
---|
493 |
|
---|
494 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
|
---|
495 | AssertReturn(pFolderMapping, VERR_INVALID_PARAMETER);
|
---|
496 |
|
---|
497 | if ( pFolderMapping->fValid
|
---|
498 | && !pFolderMapping->fMissing)
|
---|
499 | *fWritable = pFolderMapping->fWritable;
|
---|
500 | else
|
---|
501 | rc = VERR_FILE_NOT_FOUND;
|
---|
502 |
|
---|
503 | LogFlow(("vbsfMappingsQuery:Writable return rc = %Rrc\n", rc));
|
---|
504 |
|
---|
505 | return rc;
|
---|
506 | }
|
---|
507 |
|
---|
508 | int vbsfMappingsQueryAutoMount(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fAutoMount)
|
---|
509 | {
|
---|
510 | RT_NOREF1(pClient);
|
---|
511 | int rc = VINF_SUCCESS;
|
---|
512 |
|
---|
513 | LogFlow(("vbsfMappingsQueryAutoMount: pClient = %p, root = %d\n", pClient, root));
|
---|
514 |
|
---|
515 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
|
---|
516 | AssertReturn(pFolderMapping, VERR_INVALID_PARAMETER);
|
---|
517 |
|
---|
518 | if (pFolderMapping->fValid == true)
|
---|
519 | *fAutoMount = pFolderMapping->fAutoMount;
|
---|
520 | else
|
---|
521 | rc = VERR_FILE_NOT_FOUND;
|
---|
522 |
|
---|
523 | LogFlow(("vbsfMappingsQueryAutoMount:Writable return rc = %Rrc\n", rc));
|
---|
524 |
|
---|
525 | return rc;
|
---|
526 | }
|
---|
527 |
|
---|
528 | int vbsfMappingsQuerySymlinksCreate(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fSymlinksCreate)
|
---|
529 | {
|
---|
530 | RT_NOREF1(pClient);
|
---|
531 | int rc = VINF_SUCCESS;
|
---|
532 |
|
---|
533 | LogFlow(("vbsfMappingsQueryAutoMount: pClient = %p, root = %d\n", pClient, root));
|
---|
534 |
|
---|
535 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
|
---|
536 | AssertReturn(pFolderMapping, VERR_INVALID_PARAMETER);
|
---|
537 |
|
---|
538 | if (pFolderMapping->fValid == true)
|
---|
539 | *fSymlinksCreate = pFolderMapping->fSymlinksCreate;
|
---|
540 | else
|
---|
541 | rc = VERR_FILE_NOT_FOUND;
|
---|
542 |
|
---|
543 | LogFlow(("vbsfMappingsQueryAutoMount:SymlinksCreate return rc = %Rrc\n", rc));
|
---|
544 |
|
---|
545 | return rc;
|
---|
546 | }
|
---|
547 |
|
---|
548 | #ifdef UNITTEST
|
---|
549 | /** Unit test the SHFL_FN_MAP_FOLDER API. Located here as a form of API
|
---|
550 | * documentation. */
|
---|
551 | void testMapFolder(RTTEST hTest)
|
---|
552 | {
|
---|
553 | /* If we try to map a valid name we should get the root. */
|
---|
554 | testMapFolderValid(hTest);
|
---|
555 | /* If we try to map a valid name we should get VERR_FILE_NOT_FOUND. */
|
---|
556 | testMapFolderInvalid(hTest);
|
---|
557 | /* If we map a folder twice we can unmap it twice.
|
---|
558 | * Currently unmapping too often is only asserted but not signalled. */
|
---|
559 | testMapFolderTwice(hTest);
|
---|
560 | /* The delimiter should be converted in e.g. file delete operations. */
|
---|
561 | testMapFolderDelimiter(hTest);
|
---|
562 | /* Test case sensitive mapping by opening a file with the wrong case. */
|
---|
563 | testMapFolderCaseSensitive(hTest);
|
---|
564 | /* Test case insensitive mapping by opening a file with the wrong case. */
|
---|
565 | testMapFolderCaseInsensitive(hTest);
|
---|
566 | /* If the number or types of parameters are wrong the API should fail. */
|
---|
567 | testMapFolderBadParameters(hTest);
|
---|
568 | }
|
---|
569 | #endif
|
---|
570 | int vbsfMapFolder(PSHFLCLIENTDATA pClient, PSHFLSTRING pszMapName,
|
---|
571 | RTUTF16 wcDelimiter, bool fCaseSensitive, SHFLROOT *pRoot)
|
---|
572 | {
|
---|
573 | MAPPING *pFolderMapping = NULL;
|
---|
574 |
|
---|
575 | if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
|
---|
576 | {
|
---|
577 | Log(("vbsfMapFolder %s\n", pszMapName->String.utf8));
|
---|
578 | }
|
---|
579 | else
|
---|
580 | {
|
---|
581 | Log(("vbsfMapFolder %ls\n", pszMapName->String.ucs2));
|
---|
582 | }
|
---|
583 |
|
---|
584 | AssertMsgReturn(wcDelimiter == '/' || wcDelimiter == '\\',
|
---|
585 | ("Invalid path delimiter: %#x\n", wcDelimiter),
|
---|
586 | VERR_INVALID_PARAMETER);
|
---|
587 | if (pClient->PathDelimiter == 0)
|
---|
588 | {
|
---|
589 | pClient->PathDelimiter = wcDelimiter;
|
---|
590 | }
|
---|
591 | else
|
---|
592 | {
|
---|
593 | AssertMsgReturn(wcDelimiter == pClient->PathDelimiter,
|
---|
594 | ("wcDelimiter=%#x PathDelimiter=%#x", wcDelimiter, pClient->PathDelimiter),
|
---|
595 | VERR_INVALID_PARAMETER);
|
---|
596 | }
|
---|
597 |
|
---|
598 | if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
|
---|
599 | {
|
---|
600 | int rc;
|
---|
601 | PRTUTF16 utf16Name;
|
---|
602 |
|
---|
603 | rc = RTStrToUtf16 ((const char *) pszMapName->String.utf8, &utf16Name);
|
---|
604 | if (RT_FAILURE (rc))
|
---|
605 | return rc;
|
---|
606 |
|
---|
607 | pFolderMapping = vbsfMappingGetByName(utf16Name, pRoot);
|
---|
608 | RTUtf16Free (utf16Name);
|
---|
609 | }
|
---|
610 | else
|
---|
611 | {
|
---|
612 | pFolderMapping = vbsfMappingGetByName(pszMapName->String.ucs2, pRoot);
|
---|
613 | }
|
---|
614 |
|
---|
615 | if (!pFolderMapping)
|
---|
616 | {
|
---|
617 | return VERR_FILE_NOT_FOUND;
|
---|
618 | }
|
---|
619 |
|
---|
620 | pFolderMapping->cMappings++;
|
---|
621 | Assert(pFolderMapping->cMappings == 1 || pFolderMapping->fGuestCaseSensitive == fCaseSensitive);
|
---|
622 | pFolderMapping->fGuestCaseSensitive = fCaseSensitive;
|
---|
623 | return VINF_SUCCESS;
|
---|
624 | }
|
---|
625 |
|
---|
626 | #ifdef UNITTEST
|
---|
627 | /** Unit test the SHFL_FN_UNMAP_FOLDER API. Located here as a form of API
|
---|
628 | * documentation. */
|
---|
629 | void testUnmapFolder(RTTEST hTest)
|
---|
630 | {
|
---|
631 | /* Unmapping a mapped folder should succeed.
|
---|
632 | * If the folder is not mapped this is only asserted, not signalled. */
|
---|
633 | testUnmapFolderValid(hTest);
|
---|
634 | /* Unmapping a non-existant root should fail. */
|
---|
635 | testUnmapFolderInvalid(hTest);
|
---|
636 | /* If the number or types of parameters are wrong the API should fail. */
|
---|
637 | testUnmapFolderBadParameters(hTest);
|
---|
638 | }
|
---|
639 | #endif
|
---|
640 | int vbsfUnmapFolder(PSHFLCLIENTDATA pClient, SHFLROOT root)
|
---|
641 | {
|
---|
642 | RT_NOREF1(pClient);
|
---|
643 | int rc = VINF_SUCCESS;
|
---|
644 |
|
---|
645 | MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
|
---|
646 | if (pFolderMapping == NULL)
|
---|
647 | {
|
---|
648 | AssertFailed();
|
---|
649 | return VERR_FILE_NOT_FOUND;
|
---|
650 | }
|
---|
651 |
|
---|
652 | Assert(pFolderMapping->fValid == true && pFolderMapping->cMappings > 0);
|
---|
653 | if (pFolderMapping->cMappings > 0)
|
---|
654 | pFolderMapping->cMappings--;
|
---|
655 |
|
---|
656 | if ( pFolderMapping->cMappings == 0
|
---|
657 | && pFolderMapping->fPlaceholder)
|
---|
658 | {
|
---|
659 | /* Automatically remove, it is not used by the guest anymore. */
|
---|
660 | Assert(pFolderMapping->fMissing);
|
---|
661 | LogRel2(("SharedFolders: unmapping placeholder '%ls' -> '%s'\n",
|
---|
662 | pFolderMapping->pMapName->String.ucs2, pFolderMapping->pszFolderName));
|
---|
663 | vbsfMappingsRemove(pFolderMapping->pMapName);
|
---|
664 | }
|
---|
665 |
|
---|
666 | Log(("vbsfUnmapFolder\n"));
|
---|
667 | return rc;
|
---|
668 | }
|
---|