VirtualBox

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

Last change on this file since 78903 was 78903, checked in by vboxsync, 6 years ago

SharedFolderSvc: Fixed shared folder duplication after restoring state with root id gaps. bugref:9402

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.9 KB
Line 
1/* $Id: mappings.cpp 78903 2019-05-31 19:20:16Z vboxsync $ */
2/** @file
3 * Shared Folders Service - Mappings support.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_SHARED_FOLDERS
23#ifdef UNITTEST
24# include "testcase/tstSharedFolderService.h"
25#endif
26
27#include "mappings.h"
28#include "vbsfpath.h"
29#include <iprt/alloc.h>
30#include <iprt/assert.h>
31#include <iprt/list.h>
32#include <iprt/path.h>
33#include <iprt/string.h>
34#include <VBox/AssertGuest.h>
35
36#ifdef UNITTEST
37# include "teststubs.h"
38#endif
39
40
41/*********************************************************************************************************************************
42* Global Variables *
43*********************************************************************************************************************************/
44extern PVBOXHGCMSVCHELPERS g_pHelpers; /* service.cpp */
45
46
47/* Shared folders order in the saved state and in the g_FolderMapping can differ.
48 * So a translation array of root handle is needed.
49 */
50
51static MAPPING g_FolderMapping[SHFL_MAX_MAPPINGS];
52static SHFLROOT g_aIndexFromRoot[SHFL_MAX_MAPPINGS];
53/**< Array running parallel to g_aIndexFromRoot and which entries are increased
54 * as an root handle is added or removed.
55 *
56 * This helps the guest figuring out that a mapping may have been reconfigured
57 * or that saved state has been restored. Entry reuse is very likely given that
58 * vbsfRootHandleAdd() always starts searching at the start for an unused entry.
59 */
60static uint32_t g_auRootHandleVersions[SHFL_MAX_MAPPINGS];
61/** Version number that is increased for every change made.
62 * This is used by the automount guest service to wait for changes.
63 * @note This does not need saving, the guest should be woken up and refresh
64 * its sate when restored. */
65static uint32_t volatile g_uFolderMappingsVersion = 0;
66
67
68/** For recording async vbsfMappingsWaitForChanges calls. */
69typedef struct SHFLMAPPINGSWAIT
70{
71 RTLISTNODE ListEntry; /**< List entry. */
72 PSHFLCLIENTDATA pClient; /**< The client that's waiting. */
73 VBOXHGCMCALLHANDLE hCall; /**< The call handle to signal completion with. */
74 PVBOXHGCMSVCPARM pParm; /**< The 32-bit unsigned parameter to stuff g_uFolderMappingsVersion into. */
75} SHFLMAPPINGSWAIT;
76/** Pointer to async mappings change wait. */
77typedef SHFLMAPPINGSWAIT *PSHFLMAPPINGSWAIT;
78/** List head for clients waiting on mapping changes (SHFLMAPPINGSWAIT). */
79static RTLISTANCHOR g_MappingsChangeWaiters;
80/** Number of clients waiting on mapping changes.
81 * We use this to limit the number of waiting calls the clients can make. */
82static uint32_t g_cMappingChangeWaiters = 0;
83static void vbsfMappingsWakeupAllWaiters(void);
84
85
86void vbsfMappingInit(void)
87{
88 unsigned root;
89
90 for (root = 0; root < RT_ELEMENTS(g_aIndexFromRoot); root++)
91 {
92 g_aIndexFromRoot[root] = SHFL_ROOT_NIL;
93 }
94
95 RTListInit(&g_MappingsChangeWaiters);
96}
97
98/**
99 * Called before loading mappings from saved state to drop the root IDs.
100 */
101void vbsfMappingLoadingStart(void)
102{
103 for (SHFLROOT idRoot = 0; idRoot < RT_ELEMENTS(g_aIndexFromRoot); idRoot++)
104 g_aIndexFromRoot[idRoot] = SHFL_ROOT_NIL;
105
106 for (SHFLROOT i = 0; i < RT_ELEMENTS(g_FolderMapping); i++)
107 g_FolderMapping[i].fLoadedRootId = false;
108}
109
110/**
111 * Called when a mapping is loaded to restore the root ID and make sure it
112 * exists.
113 *
114 * @returns VBox status code.
115 */
116int vbsfMappingLoaded(const MAPPING *pLoadedMapping, SHFLROOT root)
117{
118 /* Mapping loaded from the saved state with the 'root' index. Which means
119 * the guest uses the 'root' as root handle for this folder.
120 * Check whether there is the same mapping in g_FolderMapping and
121 * update the g_aIndexFromRoot.
122 *
123 * Also update the mapping properties, which were lost: cMappings.
124 */
125 if (root >= SHFL_MAX_MAPPINGS)
126 {
127 return VERR_INVALID_PARAMETER;
128 }
129
130 SHFLROOT i;
131 for (i = 0; i < RT_ELEMENTS(g_FolderMapping); i++)
132 {
133 MAPPING *pMapping = &g_FolderMapping[i];
134
135 /* Equal? */
136 if ( pLoadedMapping->fValid == pMapping->fValid
137 && ShflStringSizeOfBuffer(pLoadedMapping->pMapName) == ShflStringSizeOfBuffer(pMapping->pMapName)
138 && memcmp(pLoadedMapping->pMapName, pMapping->pMapName, ShflStringSizeOfBuffer(pMapping->pMapName)) == 0)
139 {
140 Log(("vbsfMappingLoaded: root=%u i=%u (was %u) (%ls)\n",
141 root, i, g_aIndexFromRoot[root], pLoadedMapping->pMapName->String.utf16));
142
143 if (!pMapping->fLoadedRootId)
144 {
145 /* First encounter. */
146 pMapping->fLoadedRootId = true;
147
148 /* Update the mapping properties. */
149 pMapping->cMappings = pLoadedMapping->cMappings;
150 }
151 else
152 {
153 /* When pMapping->fLoadedRootId is already true it means that another HGCM client uses the same mapping. */
154 Assert(pMapping->cMappings > 1);
155 }
156
157 /* Actual index is i. Remember that when the guest uses 'root' it is actually 'i'. */
158 AssertLogRelMsg(g_aIndexFromRoot[root] == SHFL_ROOT_NIL,
159 ("idRoot=%u: current %u ([%s]), new %u (%ls [%s])\n",
160 root, g_aIndexFromRoot[root], g_FolderMapping[g_aIndexFromRoot[root]].pszFolderName,
161 pLoadedMapping->pMapName->String.utf16, pLoadedMapping->pszFolderName));
162 g_aIndexFromRoot[root] = i;
163
164 /* The mapping is known to the host and is used by the guest.
165 * No need for a 'placeholder'.
166 */
167 return VINF_SUCCESS;
168 }
169 }
170
171 /* No corresponding mapping on the host but the guest still uses it.
172 * Add a 'placeholder' mapping.
173 */
174 LogRel2(("SharedFolders: mapping a placeholder for '%ls' -> '%s'\n",
175 pLoadedMapping->pMapName->String.ucs2, pLoadedMapping->pszFolderName));
176 return vbsfMappingsAdd(pLoadedMapping->pszFolderName, pLoadedMapping->pMapName,
177 pLoadedMapping->fWritable, pLoadedMapping->fAutoMount, pLoadedMapping->pAutoMountPoint,
178 pLoadedMapping->fSymlinksCreate, /* fMissing = */ true, /* fPlaceholder = */ true);
179}
180
181/**
182 * Called after loading mappings from saved state to make sure every mapping has
183 * a root ID.
184 */
185void vbsfMappingLoadingDone(void)
186{
187 for (SHFLROOT iMapping = 0; iMapping < RT_ELEMENTS(g_FolderMapping); iMapping++)
188 if (g_FolderMapping[iMapping].fValid)
189 {
190 AssertLogRel(g_FolderMapping[iMapping].pMapName);
191 AssertLogRel(g_FolderMapping[iMapping].pszFolderName);
192
193 SHFLROOT idRoot;
194 for (idRoot = 0; idRoot < RT_ELEMENTS(g_aIndexFromRoot); idRoot++)
195 if (g_aIndexFromRoot[idRoot] == iMapping)
196 break;
197 if (idRoot >= RT_ELEMENTS(g_aIndexFromRoot))
198 {
199 for (idRoot = 0; idRoot < RT_ELEMENTS(g_aIndexFromRoot); idRoot++)
200 if (g_aIndexFromRoot[idRoot] == SHFL_ROOT_NIL)
201 break;
202 if (idRoot < RT_ELEMENTS(g_aIndexFromRoot))
203 g_aIndexFromRoot[idRoot] = iMapping;
204 else
205 LogRel(("SharedFolders: Warning! No free root ID entry for mapping #%u: %ls [%s]\n", iMapping,
206 g_FolderMapping[iMapping].pMapName->String.ucs2, g_FolderMapping[iMapping].pszFolderName));
207 }
208 }
209
210 /* Log the root ID mappings: */
211 if (LogRelIs2Enabled())
212 for (SHFLROOT idRoot = 0; idRoot < RT_ELEMENTS(g_aIndexFromRoot); idRoot++)
213 {
214 SHFLROOT const iMapping = g_aIndexFromRoot[idRoot];
215 if (iMapping != SHFL_ROOT_NIL)
216 LogRel2(("SharedFolders: idRoot %u: iMapping #%u: %ls [%s]\n", idRoot, iMapping,
217 g_FolderMapping[iMapping].pMapName->String.ucs2, g_FolderMapping[iMapping].pszFolderName));
218 }
219}
220
221
222MAPPING *vbsfMappingGetByRoot(SHFLROOT root)
223{
224 if (root < RT_ELEMENTS(g_aIndexFromRoot))
225 {
226 SHFLROOT iMapping = g_aIndexFromRoot[root];
227
228 if ( iMapping != SHFL_ROOT_NIL
229 && iMapping < RT_ELEMENTS(g_FolderMapping))
230 {
231 return &g_FolderMapping[iMapping];
232 }
233 }
234
235 return NULL;
236}
237
238static SHFLROOT vbsfMappingGetRootFromIndex(SHFLROOT iMapping)
239{
240 unsigned root;
241
242 for (root = 0; root < RT_ELEMENTS(g_aIndexFromRoot); root++)
243 {
244 if (iMapping == g_aIndexFromRoot[root])
245 {
246 return root;
247 }
248 }
249
250 return SHFL_ROOT_NIL;
251}
252
253static MAPPING *vbsfMappingGetByName(PRTUTF16 pwszName, SHFLROOT *pRoot)
254{
255 for (unsigned i = 0; i < SHFL_MAX_MAPPINGS; i++)
256 {
257 if ( g_FolderMapping[i].fValid
258 && !g_FolderMapping[i].fPlaceholder) /* Don't allow mapping placeholders. */
259 {
260 if (!RTUtf16LocaleICmp(g_FolderMapping[i].pMapName->String.ucs2, pwszName))
261 {
262 SHFLROOT root = vbsfMappingGetRootFromIndex(i);
263
264 if (root != SHFL_ROOT_NIL)
265 {
266 if (pRoot)
267 {
268 *pRoot = root;
269 }
270 return &g_FolderMapping[i];
271 }
272 AssertFailed();
273 }
274 }
275 }
276 return NULL;
277}
278
279static void vbsfRootHandleAdd(SHFLROOT iMapping)
280{
281 for (unsigned root = 0; root < RT_ELEMENTS(g_aIndexFromRoot); root++)
282 {
283 if (g_aIndexFromRoot[root] == SHFL_ROOT_NIL)
284 {
285 g_aIndexFromRoot[root] = iMapping;
286 g_auRootHandleVersions[root] += 1;
287 return;
288 }
289 }
290
291 AssertFailed();
292}
293
294static void vbsfRootHandleRemove(SHFLROOT iMapping)
295{
296 unsigned cFound = 0;
297
298 for (unsigned root = 0; root < RT_ELEMENTS(g_aIndexFromRoot); root++)
299 {
300 if (g_aIndexFromRoot[root] == iMapping)
301 {
302 g_aIndexFromRoot[root] = SHFL_ROOT_NIL;
303 g_auRootHandleVersions[root] += 1;
304 Log(("vbsfRootHandleRemove: Removed root=%u (iMapping=%u)\n", root, iMapping));
305
306 /* Note! Do not stop here as g_aIndexFromRoot may (at least it could
307 prior to the introduction of fLoadedRootId) contain
308 duplicates after restoring save state. */
309 cFound++;
310 }
311 }
312
313 Assert(cFound > 0); RT_NOREF(cFound);
314}
315
316
317
318#ifdef UNITTEST
319/** Unit test the SHFL_FN_ADD_MAPPING API. Located here as a form of API
320 * documentation. */
321void testMappingsAdd(RTTEST hTest)
322{
323 /* If the number or types of parameters are wrong the API should fail. */
324 testMappingsAddBadParameters(hTest);
325 /* Add tests as required... */
326}
327#endif
328/*
329 * We are always executed from one specific HGCM thread. So thread safe.
330 */
331int vbsfMappingsAdd(const char *pszFolderName, PSHFLSTRING pMapName, bool fWritable,
332 bool fAutoMount, PSHFLSTRING pAutoMountPoint, bool fSymlinksCreate, bool fMissing, bool fPlaceholder)
333{
334 unsigned i;
335
336 Assert(pszFolderName && pMapName);
337
338 Log(("vbsfMappingsAdd %ls\n", pMapName->String.ucs2));
339
340 /* Check for duplicates, ignoring placeholders to give the GUI to change stuff at runtime. */
341 /** @todo bird: Not entirely sure about ignoring placeholders, but you cannot
342 * trigger auto-umounting without ignoring them. */
343 if (!fPlaceholder)
344 {
345 for (i = 0; i < SHFL_MAX_MAPPINGS; i++)
346 {
347 if ( g_FolderMapping[i].fValid
348 && !g_FolderMapping[i].fPlaceholder)
349 {
350 if (!RTUtf16LocaleICmp(g_FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2))
351 {
352 AssertMsgFailed(("vbsfMappingsAdd: %ls mapping already exists!!\n", pMapName->String.ucs2));
353 return VERR_ALREADY_EXISTS;
354 }
355 }
356 }
357 }
358
359 for (i = 0; i < SHFL_MAX_MAPPINGS; i++)
360 {
361 if (g_FolderMapping[i].fValid == false)
362 {
363 /* Make sure the folder name is an absolute path, otherwise we're
364 likely to get into trouble with buffer sizes in vbsfPathGuestToHost. */
365 char szAbsFolderName[RTPATH_MAX];
366 int rc = vbsfPathAbs(NULL, pszFolderName, szAbsFolderName, sizeof(szAbsFolderName));
367 AssertRCReturn(rc, rc);
368
369 g_FolderMapping[i].pszFolderName = RTStrDup(szAbsFolderName);
370 g_FolderMapping[i].pMapName = ShflStringDup(pMapName);
371 g_FolderMapping[i].pAutoMountPoint = ShflStringDup(pAutoMountPoint);
372 if ( !g_FolderMapping[i].pszFolderName
373 || !g_FolderMapping[i].pMapName
374 || !g_FolderMapping[i].pAutoMountPoint)
375 {
376 RTStrFree(g_FolderMapping[i].pszFolderName);
377 RTMemFree(g_FolderMapping[i].pMapName);
378 RTMemFree(g_FolderMapping[i].pAutoMountPoint);
379 return VERR_NO_MEMORY;
380 }
381
382 g_FolderMapping[i].fValid = true;
383 g_FolderMapping[i].cMappings = 0;
384 g_FolderMapping[i].fWritable = fWritable;
385 g_FolderMapping[i].fAutoMount = fAutoMount;
386 g_FolderMapping[i].fSymlinksCreate = fSymlinksCreate;
387 g_FolderMapping[i].fMissing = fMissing;
388 g_FolderMapping[i].fPlaceholder = fPlaceholder;
389 g_FolderMapping[i].fLoadedRootId = false;
390
391 /* Check if the host file system is case sensitive */
392 RTFSPROPERTIES prop;
393 prop.fCaseSensitive = false; /* Shut up MSC. */
394 rc = RTFsQueryProperties(g_FolderMapping[i].pszFolderName, &prop);
395 AssertRC(rc);
396 g_FolderMapping[i].fHostCaseSensitive = RT_SUCCESS(rc) ? prop.fCaseSensitive : false;
397 vbsfRootHandleAdd(i);
398 vbsfMappingsWakeupAllWaiters();
399 break;
400 }
401 }
402 if (i == SHFL_MAX_MAPPINGS)
403 {
404 AssertLogRelMsgFailed(("vbsfMappingsAdd: no more room to add mapping %s to %ls!!\n", pszFolderName, pMapName->String.ucs2));
405 return VERR_TOO_MUCH_DATA;
406 }
407
408 Log(("vbsfMappingsAdd: added mapping %s to %ls (slot %u, root %u)\n",
409 pszFolderName, pMapName->String.ucs2, i, vbsfMappingGetRootFromIndex(i)));
410 return VINF_SUCCESS;
411}
412
413#ifdef UNITTEST
414/** Unit test the SHFL_FN_REMOVE_MAPPING API. Located here as a form of API
415 * documentation. */
416void testMappingsRemove(RTTEST hTest)
417{
418 /* If the number or types of parameters are wrong the API should fail. */
419 testMappingsRemoveBadParameters(hTest);
420 /* Add tests as required... */
421}
422#endif
423int vbsfMappingsRemove(PSHFLSTRING pMapName)
424{
425 Assert(pMapName);
426 Log(("vbsfMappingsRemove %ls\n", pMapName->String.ucs2));
427
428 /*
429 * We must iterate thru the whole table as may have 0+ placeholder entries
430 * and 0-1 regular entries with the same name. Also, it is good to kick
431 * the guest automounter into action wrt to evicting placeholders.
432 */
433 int rc = VERR_FILE_NOT_FOUND;
434 for (unsigned i = 0; i < SHFL_MAX_MAPPINGS; i++)
435 {
436 if (g_FolderMapping[i].fValid == true)
437 {
438 if (!RTUtf16LocaleICmp(g_FolderMapping[i].pMapName->String.ucs2, pMapName->String.ucs2))
439 {
440 if (g_FolderMapping[i].cMappings != 0)
441 {
442 LogRel2(("SharedFolders: removing '%ls' -> '%s'%s, which is still used by the guest\n", pMapName->String.ucs2,
443 g_FolderMapping[i].pszFolderName, g_FolderMapping[i].fPlaceholder ? " (again)" : ""));
444 g_FolderMapping[i].fMissing = true;
445 g_FolderMapping[i].fPlaceholder = true;
446 vbsfMappingsWakeupAllWaiters();
447 rc = VINF_PERMISSION_DENIED;
448 }
449 else
450 {
451 /* pMapName can be the same as g_FolderMapping[i].pMapName when
452 * called from vbsfUnmapFolder, log it before deallocating the memory. */
453 Log(("vbsfMappingsRemove: mapping %ls removed\n", pMapName->String.ucs2));
454 bool fSame = g_FolderMapping[i].pMapName == pMapName;
455
456 RTStrFree(g_FolderMapping[i].pszFolderName);
457 RTMemFree(g_FolderMapping[i].pMapName);
458 g_FolderMapping[i].pszFolderName = NULL;
459 g_FolderMapping[i].pMapName = NULL;
460 g_FolderMapping[i].fValid = false;
461 vbsfRootHandleRemove(i);
462 vbsfMappingsWakeupAllWaiters();
463 if (rc == VERR_FILE_NOT_FOUND)
464 rc = VINF_SUCCESS;
465 if (fSame)
466 break;
467 }
468 }
469 }
470 }
471
472 return rc;
473}
474
475const char* vbsfMappingsQueryHostRoot(SHFLROOT root)
476{
477 MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
478 AssertReturn(pFolderMapping, NULL);
479 if (pFolderMapping->fMissing)
480 return NULL;
481 return pFolderMapping->pszFolderName;
482}
483
484int vbsfMappingsQueryHostRootEx(SHFLROOT hRoot, const char **ppszRoot, uint32_t *pcbRootLen)
485{
486 MAPPING *pFolderMapping = vbsfMappingGetByRoot(hRoot);
487 AssertReturn(pFolderMapping, VERR_INVALID_PARAMETER);
488 if (pFolderMapping->fMissing)
489 return VERR_NOT_FOUND;
490 if ( pFolderMapping->pszFolderName == NULL
491 || pFolderMapping->pszFolderName[0] == 0)
492 return VERR_NOT_FOUND;
493 *ppszRoot = pFolderMapping->pszFolderName;
494 *pcbRootLen = (uint32_t)strlen(pFolderMapping->pszFolderName);
495 return VINF_SUCCESS;
496}
497
498bool vbsfIsGuestMappingCaseSensitive(SHFLROOT root)
499{
500 MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
501 AssertReturn(pFolderMapping, false);
502 return pFolderMapping->fGuestCaseSensitive;
503}
504
505bool vbsfIsHostMappingCaseSensitive(SHFLROOT root)
506{
507 MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
508 AssertReturn(pFolderMapping, false);
509 return pFolderMapping->fHostCaseSensitive;
510}
511
512#ifdef UNITTEST
513/** Unit test the SHFL_FN_QUERY_MAPPINGS API. Located here as a form of API
514 * documentation (or should it better be inline in include/VBox/shflsvc.h?) */
515void testMappingsQuery(RTTEST hTest)
516{
517 /* The API should return all mappings if we provide enough buffers. */
518 testMappingsQuerySimple(hTest);
519 /* If we provide too few buffers that should be signalled correctly. */
520 testMappingsQueryTooFewBuffers(hTest);
521 /* The SHFL_MF_AUTOMOUNT flag means return only auto-mounted mappings. */
522 testMappingsQueryAutoMount(hTest);
523 /* The mappings return array must have numberOfMappings entries. */
524 testMappingsQueryArrayWrongSize(hTest);
525}
526#endif
527/**
528 * @note If pMappings / *pcMappings is smaller than the actual amount of
529 * mappings that *could* have been returned *pcMappings contains the
530 * required buffer size so that the caller can retry the operation if
531 * wanted.
532 */
533int vbsfMappingsQuery(PSHFLCLIENTDATA pClient, bool fOnlyAutoMounts, PSHFLMAPPING pMappings, uint32_t *pcMappings)
534{
535 LogFlow(("vbsfMappingsQuery: pClient = %p, pMappings = %p, pcMappings = %p, *pcMappings = %d\n",
536 pClient, pMappings, pcMappings, *pcMappings));
537
538 uint32_t const cMaxMappings = *pcMappings;
539 uint32_t idx = 0;
540 for (uint32_t i = 0; i < SHFL_MAX_MAPPINGS; i++)
541 {
542 MAPPING *pFolderMapping = vbsfMappingGetByRoot(i);
543 if ( pFolderMapping != NULL
544 && pFolderMapping->fValid
545 && ( !fOnlyAutoMounts
546 || (pFolderMapping->fAutoMount && !pFolderMapping->fPlaceholder)) )
547 {
548 if (idx < cMaxMappings)
549 {
550 pMappings[idx].u32Status = SHFL_MS_NEW;
551 pMappings[idx].root = i;
552 }
553 idx++;
554 }
555 }
556
557 /* Return actual number of mappings, regardless whether the handed in
558 * mapping buffer was big enough. */
559 /** @todo r=bird: This is non-standard interface behaviour. We return
560 * VERR_BUFFER_OVERFLOW or at least a VINF_BUFFER_OVERFLOW here.
561 *
562 * Guess this goes well along with ORing SHFL_MF_AUTOMOUNT into
563 * pClient->fu32Flags rather than passing it as fOnlyAutoMounts...
564 * Not amused by this. */
565 *pcMappings = idx;
566
567 RT_NOREF_PV(pClient);
568 LogFlow(("vbsfMappingsQuery: returns VINF_SUCCESS (idx=%u, cMaxMappings=%u)\n", idx, cMaxMappings));
569 return VINF_SUCCESS;
570}
571
572#ifdef UNITTEST
573/** Unit test the SHFL_FN_QUERY_MAP_NAME API. Located here as a form of API
574 * documentation. */
575void testMappingsQueryName(RTTEST hTest)
576{
577 /* If we query an valid mapping it should be returned. */
578 testMappingsQueryNameValid(hTest);
579 /* If we query an invalid mapping that should be signalled. */
580 testMappingsQueryNameInvalid(hTest);
581 /* If we pass in a bad string buffer that should be detected. */
582 testMappingsQueryNameBadBuffer(hTest);
583}
584#endif
585int vbsfMappingsQueryName(PSHFLCLIENTDATA pClient, SHFLROOT root, SHFLSTRING *pString)
586{
587 LogFlow(("vbsfMappingsQuery: pClient = %p, root = %d, *pString = %p\n", pClient, root, pString));
588
589 int rc;
590 MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
591 if (pFolderMapping)
592 {
593 if (pFolderMapping->fValid)
594 {
595 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
596 rc = ShflStringCopyUtf16BufAsUtf8(pString, pFolderMapping->pMapName);
597 else
598 {
599 /* Not using ShlfStringCopy here as behaviour shouldn't change... */
600 if (pString->u16Size < pFolderMapping->pMapName->u16Size)
601 {
602 Log(("vbsfMappingsQuery: passed string too short (%d < %d bytes)!\n",
603 pString->u16Size, pFolderMapping->pMapName->u16Size));
604 rc = VERR_INVALID_PARAMETER;
605 }
606 else
607 {
608 pString->u16Length = pFolderMapping->pMapName->u16Length;
609 memcpy(pString->String.ucs2, pFolderMapping->pMapName->String.ucs2,
610 pFolderMapping->pMapName->u16Size);
611 rc = VINF_SUCCESS;
612 }
613 }
614 }
615 else
616 rc = VERR_FILE_NOT_FOUND;
617 }
618 else
619 rc = VERR_INVALID_PARAMETER;
620
621 LogFlow(("vbsfMappingsQuery:Name return rc = %Rrc\n", rc));
622 return rc;
623}
624
625/** Queries fWritable flag for the given root. Returns error if the root is not accessible.
626 */
627int vbsfMappingsQueryWritable(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fWritable)
628{
629 RT_NOREF1(pClient);
630 int rc = VINF_SUCCESS;
631
632 LogFlow(("vbsfMappingsQueryWritable: pClient = %p, root = %d\n", pClient, root));
633
634 MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
635 AssertReturn(pFolderMapping, VERR_INVALID_PARAMETER);
636
637 if ( pFolderMapping->fValid
638 && !pFolderMapping->fMissing)
639 *fWritable = pFolderMapping->fWritable;
640 else
641 rc = VERR_FILE_NOT_FOUND;
642
643 LogFlow(("vbsfMappingsQuery:Writable return rc = %Rrc\n", rc));
644
645 return rc;
646}
647
648int vbsfMappingsQueryAutoMount(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fAutoMount)
649{
650 RT_NOREF1(pClient);
651 int rc = VINF_SUCCESS;
652
653 LogFlow(("vbsfMappingsQueryAutoMount: pClient = %p, root = %d\n", pClient, root));
654
655 MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
656 AssertReturn(pFolderMapping, VERR_INVALID_PARAMETER);
657
658 if (pFolderMapping->fValid == true)
659 *fAutoMount = pFolderMapping->fAutoMount;
660 else
661 rc = VERR_FILE_NOT_FOUND;
662
663 LogFlow(("vbsfMappingsQueryAutoMount:Writable return rc = %Rrc\n", rc));
664
665 return rc;
666}
667
668int vbsfMappingsQuerySymlinksCreate(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fSymlinksCreate)
669{
670 RT_NOREF1(pClient);
671 int rc = VINF_SUCCESS;
672
673 LogFlow(("vbsfMappingsQueryAutoMount: pClient = %p, root = %d\n", pClient, root));
674
675 MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
676 AssertReturn(pFolderMapping, VERR_INVALID_PARAMETER);
677
678 if (pFolderMapping->fValid == true)
679 *fSymlinksCreate = pFolderMapping->fSymlinksCreate;
680 else
681 rc = VERR_FILE_NOT_FOUND;
682
683 LogFlow(("vbsfMappingsQueryAutoMount:SymlinksCreate return rc = %Rrc\n", rc));
684
685 return rc;
686}
687
688/**
689 * Implements SHFL_FN_QUERY_MAP_INFO.
690 * @since VBox 6.0
691 */
692int vbsfMappingsQueryInfo(PSHFLCLIENTDATA pClient, SHFLROOT root, PSHFLSTRING pNameBuf, PSHFLSTRING pMntPtBuf,
693 uint64_t *pfFlags, uint32_t *puVersion)
694{
695 LogFlow(("vbsfMappingsQueryInfo: pClient=%p root=%d\n", pClient, root));
696
697 /* Resolve the root handle. */
698 int rc;
699 PMAPPING pFolderMapping = vbsfMappingGetByRoot(root);
700 if (pFolderMapping)
701 {
702 if (pFolderMapping->fValid)
703 {
704 /*
705 * Produce the output.
706 */
707 *puVersion = g_auRootHandleVersions[root];
708
709 *pfFlags = 0;
710 if (pFolderMapping->fWritable)
711 *pfFlags |= SHFL_MIF_WRITABLE;
712 if (pFolderMapping->fAutoMount)
713 *pfFlags |= SHFL_MIF_AUTO_MOUNT;
714 if (pFolderMapping->fHostCaseSensitive)
715 *pfFlags |= SHFL_MIF_HOST_ICASE;
716 if (pFolderMapping->fGuestCaseSensitive)
717 *pfFlags |= SHFL_MIF_GUEST_ICASE;
718 if (pFolderMapping->fSymlinksCreate)
719 *pfFlags |= SHFL_MIF_SYMLINK_CREATION;
720
721 int rc2;
722 if (pClient->fu32Flags & SHFL_CF_UTF8)
723 {
724 rc = ShflStringCopyUtf16BufAsUtf8(pNameBuf, pFolderMapping->pMapName);
725 rc2 = ShflStringCopyUtf16BufAsUtf8(pMntPtBuf, pFolderMapping->pAutoMountPoint);
726 }
727 else
728 {
729 rc = ShflStringCopy(pNameBuf, pFolderMapping->pMapName, sizeof(RTUTF16));
730 rc2 = ShflStringCopy(pMntPtBuf, pFolderMapping->pAutoMountPoint, sizeof(RTUTF16));
731 }
732 if (RT_SUCCESS(rc))
733 rc = rc2;
734 }
735 else
736 rc = VERR_FILE_NOT_FOUND;
737 }
738 else
739 rc = VERR_INVALID_PARAMETER;
740 LogFlow(("vbsfMappingsQueryInfo: returns %Rrc\n", rc));
741 return rc;
742}
743
744
745
746#ifdef UNITTEST
747/** Unit test the SHFL_FN_MAP_FOLDER API. Located here as a form of API
748 * documentation. */
749void testMapFolder(RTTEST hTest)
750{
751 /* If we try to map a valid name we should get the root. */
752 testMapFolderValid(hTest);
753 /* If we try to map a valid name we should get VERR_FILE_NOT_FOUND. */
754 testMapFolderInvalid(hTest);
755 /* If we map a folder twice we can unmap it twice.
756 * Currently unmapping too often is only asserted but not signalled. */
757 testMapFolderTwice(hTest);
758 /* The delimiter should be converted in e.g. file delete operations. */
759 testMapFolderDelimiter(hTest);
760 /* Test case sensitive mapping by opening a file with the wrong case. */
761 testMapFolderCaseSensitive(hTest);
762 /* Test case insensitive mapping by opening a file with the wrong case. */
763 testMapFolderCaseInsensitive(hTest);
764 /* If the number or types of parameters are wrong the API should fail. */
765 testMapFolderBadParameters(hTest);
766}
767#endif
768int vbsfMapFolder(PSHFLCLIENTDATA pClient, PSHFLSTRING pszMapName,
769 RTUTF16 wcDelimiter, bool fCaseSensitive, SHFLROOT *pRoot)
770{
771 MAPPING *pFolderMapping = NULL;
772
773 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
774 {
775 Log(("vbsfMapFolder %s\n", pszMapName->String.utf8));
776 }
777 else
778 {
779 Log(("vbsfMapFolder %ls\n", pszMapName->String.ucs2));
780 }
781
782 AssertMsgReturn(wcDelimiter == '/' || wcDelimiter == '\\',
783 ("Invalid path delimiter: %#x\n", wcDelimiter),
784 VERR_INVALID_PARAMETER);
785 if (pClient->PathDelimiter == 0)
786 {
787 pClient->PathDelimiter = wcDelimiter;
788 }
789 else
790 {
791 AssertMsgReturn(wcDelimiter == pClient->PathDelimiter,
792 ("wcDelimiter=%#x PathDelimiter=%#x", wcDelimiter, pClient->PathDelimiter),
793 VERR_INVALID_PARAMETER);
794 }
795
796 SHFLROOT RootTmp;
797 if (!pRoot)
798 pRoot = &RootTmp;
799 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
800 {
801 int rc;
802 PRTUTF16 utf16Name;
803
804 rc = RTStrToUtf16((const char *) pszMapName->String.utf8, &utf16Name);
805 if (RT_FAILURE (rc))
806 return rc;
807
808 pFolderMapping = vbsfMappingGetByName(utf16Name, pRoot);
809 RTUtf16Free(utf16Name);
810 }
811 else
812 {
813 pFolderMapping = vbsfMappingGetByName(pszMapName->String.ucs2, pRoot);
814 }
815
816 if (!pFolderMapping)
817 {
818 return VERR_FILE_NOT_FOUND;
819 }
820
821 /*
822 * Check for reference count overflows and settings compatibility.
823 * For paranoid reasons, we don't allow modifying the case sensitivity
824 * setting while there are other mappings of a folder.
825 */
826 AssertLogRelReturn(*pRoot < RT_ELEMENTS(pClient->acMappings), VERR_INTERNAL_ERROR);
827 AssertLogRelReturn(!pClient->fHasMappingCounts || pClient->acMappings[*pRoot] < _32K, VERR_TOO_MANY_OPENS);
828 ASSERT_GUEST_LOGREL_MSG_RETURN( pFolderMapping->cMappings == 0
829 || pFolderMapping->fGuestCaseSensitive == fCaseSensitive,
830 ("Incompatible case sensitivity setting: %s: %u mappings, %ssenitive, requested %ssenitive!\n",
831 pFolderMapping->pszFolderName, pFolderMapping->cMappings,
832 pFolderMapping->fGuestCaseSensitive ? "" : "in", fCaseSensitive ? "" : "in"),
833 VERR_INCOMPATIBLE_CONFIG);
834
835 /*
836 * Go ahead and map it.
837 */
838 if (pClient->fHasMappingCounts)
839 pClient->acMappings[*pRoot] += 1;
840 pFolderMapping->cMappings++;
841 pFolderMapping->fGuestCaseSensitive = fCaseSensitive;
842 Log(("vbsfMmapFolder (cMappings=%u, acMappings[%u]=%u)\n", pFolderMapping->cMappings, *pRoot, pClient->acMappings[*pRoot]));
843 return VINF_SUCCESS;
844}
845
846#ifdef UNITTEST
847/** Unit test the SHFL_FN_UNMAP_FOLDER API. Located here as a form of API
848 * documentation. */
849void testUnmapFolder(RTTEST hTest)
850{
851 /* Unmapping a mapped folder should succeed.
852 * If the folder is not mapped this is only asserted, not signalled. */
853 testUnmapFolderValid(hTest);
854 /* Unmapping a non-existant root should fail. */
855 testUnmapFolderInvalid(hTest);
856 /* If the number or types of parameters are wrong the API should fail. */
857 testUnmapFolderBadParameters(hTest);
858}
859#endif
860int vbsfUnmapFolder(PSHFLCLIENTDATA pClient, SHFLROOT root)
861{
862 RT_NOREF1(pClient);
863 int rc = VINF_SUCCESS;
864
865 MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
866 if (pFolderMapping == NULL)
867 {
868 AssertFailed();
869 return VERR_FILE_NOT_FOUND;
870 }
871 Assert(pFolderMapping->fValid == true && pFolderMapping->cMappings > 0);
872
873 AssertLogRelReturn(root < RT_ELEMENTS(pClient->acMappings), VERR_INTERNAL_ERROR);
874 AssertLogRelReturn(!pClient->fHasMappingCounts || pClient->acMappings[root] > 0, VERR_INVALID_HANDLE);
875
876 if (pClient->fHasMappingCounts)
877 pClient->acMappings[root] -= 1;
878
879 if (pFolderMapping->cMappings > 0)
880 pFolderMapping->cMappings--;
881
882 uint32_t const cMappings = pFolderMapping->cMappings;
883 if ( cMappings == 0
884 && pFolderMapping->fPlaceholder)
885 {
886 /* Automatically remove, it is not used by the guest anymore. */
887 Assert(pFolderMapping->fMissing);
888 LogRel2(("SharedFolders: unmapping placeholder '%ls' -> '%s'\n",
889 pFolderMapping->pMapName->String.ucs2, pFolderMapping->pszFolderName));
890 vbsfMappingsRemove(pFolderMapping->pMapName);
891 }
892
893 Log(("vbsfUnmapFolder (cMappings=%u, acMappings[%u]=%u)\n", cMappings, root, pClient->acMappings[root]));
894 return rc;
895}
896
897/**
898 * SHFL_FN_WAIT_FOR_MAPPINGS_CHANGES implementation.
899 *
900 * @returns VBox status code.
901 * @retval VINF_SUCCESS on change.
902 * @retval VINF_TRY_AGAIN on resume.
903 * @retval VINF_HGCM_ASYNC_EXECUTE if waiting.
904 * @retval VERR_CANCELLED if cancelled.
905 * @retval VERR_OUT_OF_RESOURCES if there are too many pending waits.
906 *
907 * @param pClient The calling client.
908 * @param hCall The call handle.
909 * @param pParm The parameter (32-bit).
910 * @param fRestored Set if this is a call restored & resubmitted from saved
911 * state.
912 * @since VBox 6.0
913 */
914int vbsfMappingsWaitForChanges(PSHFLCLIENTDATA pClient, VBOXHGCMCALLHANDLE hCall, PVBOXHGCMSVCPARM pParm, bool fRestored)
915{
916 /*
917 * Return immediately if the fodler mappings have changed since last call
918 * or if we got restored from saved state (adding of global folders, etc).
919 */
920 uint32_t uCurVersion = g_uFolderMappingsVersion;
921 if ( pParm->u.uint32 != uCurVersion
922 || fRestored
923 || (pClient->fu32Flags & SHFL_CF_CANCEL_NEXT_WAIT) )
924 {
925 int rc = VINF_SUCCESS;
926 if (pClient->fu32Flags & SHFL_CF_CANCEL_NEXT_WAIT)
927 {
928 pClient->fu32Flags &= ~SHFL_CF_CANCEL_NEXT_WAIT;
929 rc = VERR_CANCELLED;
930 }
931 else if (fRestored)
932 {
933 rc = VINF_TRY_AGAIN;
934 if (pParm->u.uint32 == uCurVersion)
935 uCurVersion = uCurVersion != UINT32_C(0x55555555) ? UINT32_C(0x55555555) : UINT32_C(0x99999999);
936 }
937 Log(("vbsfMappingsWaitForChanges: Version %#x -> %#x, returning %Rrc immediately.\n", pParm->u.uint32, uCurVersion, rc));
938 pParm->u.uint32 = uCurVersion;
939 return rc;
940 }
941
942 /*
943 * Setup a wait if we can.
944 */
945 if (g_cMappingChangeWaiters < 64)
946 {
947 PSHFLMAPPINGSWAIT pWait = (PSHFLMAPPINGSWAIT)RTMemAlloc(sizeof(*pWait));
948 if (pWait)
949 {
950 pWait->pClient = pClient;
951 pWait->hCall = hCall;
952 pWait->pParm = pParm;
953
954 RTListAppend(&g_MappingsChangeWaiters, &pWait->ListEntry);
955 g_cMappingChangeWaiters += 1;
956 return VINF_HGCM_ASYNC_EXECUTE;
957 }
958 return VERR_NO_MEMORY;
959 }
960 LogRelMax(32, ("vbsfMappingsWaitForChanges: Too many threads waiting for changes!\n"));
961 return VERR_OUT_OF_RESOURCES;
962}
963
964/**
965 * SHFL_FN_CANCEL_MAPPINGS_CHANGES_WAITS implementation.
966 *
967 * @returns VINF_SUCCESS
968 * @param pClient The calling client to cancel all waits for.
969 * @since VBox 6.0
970 */
971int vbsfMappingsCancelChangesWaits(PSHFLCLIENTDATA pClient)
972{
973 uint32_t const uCurVersion = g_uFolderMappingsVersion;
974
975 PSHFLMAPPINGSWAIT pCur, pNext;
976 RTListForEachSafe(&g_MappingsChangeWaiters, pCur, pNext, SHFLMAPPINGSWAIT, ListEntry)
977 {
978 if (pCur->pClient == pClient)
979 {
980 RTListNodeRemove(&pCur->ListEntry);
981 pCur->pParm->u.uint32 = uCurVersion;
982 g_pHelpers->pfnCallComplete(pCur->hCall, VERR_CANCELLED);
983 RTMemFree(pCur);
984 }
985 }
986
987 /* Set a flag to make sure the next SHFL_FN_WAIT_FOR_MAPPINGS_CHANGES doesn't block.
988 This should help deal with races between this call and a thread about to do a wait. */
989 pClient->fu32Flags |= SHFL_CF_CANCEL_NEXT_WAIT;
990
991 return VINF_SUCCESS;
992}
993
994/**
995 * Wakes up all clients waiting on
996 */
997static void vbsfMappingsWakeupAllWaiters(void)
998{
999 uint32_t const uCurVersion = ++g_uFolderMappingsVersion;
1000
1001 PSHFLMAPPINGSWAIT pCur, pNext;
1002 RTListForEachSafe(&g_MappingsChangeWaiters, pCur, pNext, SHFLMAPPINGSWAIT, ListEntry)
1003 {
1004 RTListNodeRemove(&pCur->ListEntry);
1005 pCur->pParm->u.uint32 = uCurVersion;
1006 g_pHelpers->pfnCallComplete(pCur->hCall, VERR_CANCELLED);
1007 RTMemFree(pCur);
1008 }
1009}
1010
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