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