VirtualBox

Changeset 81440 in vbox


Ignore:
Timestamp:
Oct 22, 2019 8:09:05 AM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
134158
Message:

Shared Clipboard/Transfers: Various fixes to make the new transfers testcase pass.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/GuestHost/SharedClipboard/clipboard-transfers.cpp

    r81395 r81440  
    801801    if (pTransfer->State.enmSource == SHCLSOURCE_LOCAL)
    802802    {
    803         PSHCLOBJHANDLEINFO pInfo
    804             = (PSHCLOBJHANDLEINFO)RTMemAllocZ(sizeof(SHCLOBJHANDLEINFO));
    805         if (pInfo)
    806         {
    807             rc = ShClTransferObjHandleInfoInit(pInfo);
    808             if (RT_SUCCESS(rc))
     803        /*
     804         * Make sure the transfer direction matches the open/create parameters.
     805         */
     806        if (pTransfer->State.enmDir == SHCLTRANSFERDIR_READ)
     807        {
     808            if (pOpenCreateParms->fCreate & SHCL_OBJ_CF_ACCESS_READ) /* Read access wanted? */
    809809            {
    810                 /*
    811                  * Make sure the transfer direction matches the open/create parameters.
    812                  */
    813                 if (pTransfer->State.enmDir == SHCLTRANSFERDIR_READ)
    814                 {
    815                     if (pOpenCreateParms->fCreate & SHCL_OBJ_CF_ACCESS_READ) /* Read access wanted? */
    816                     {
    817                         AssertMsgFailed(("Is not a write transfer, but object open flags are set to read access (0x%x)\n",
    818                                          pOpenCreateParms->fCreate)); /* Should never happen. */
    819                         rc = VERR_INVALID_PARAMETER;
    820                     }
    821                 }
    822                 else if (pTransfer->State.enmDir == SHCLTRANSFERDIR_WRITE)
    823                 {
    824                     if (pOpenCreateParms->fCreate & SHCL_OBJ_CF_ACCESS_WRITE) /* Write access wanted? */
    825                     {
    826                         AssertMsgFailed(("Is not a read transfer, but object open flags are set to write access (0x%x)\n",
    827                                          pOpenCreateParms->fCreate)); /* Should never happen. */
    828                         rc = VERR_INVALID_PARAMETER;
    829                     }
    830                 }
    831                 else
    832                 {
    833                     AssertFailed();
    834                     rc = VERR_NOT_SUPPORTED;
    835                 }
    836 
     810                AssertMsgFailed(("Is not a write transfer, but object open flags are set to read access (0x%x)\n",
     811                                 pOpenCreateParms->fCreate)); /* Should never happen. */
     812                rc = VERR_INVALID_PARAMETER;
     813            }
     814        }
     815        else if (pTransfer->State.enmDir == SHCLTRANSFERDIR_WRITE)
     816        {
     817            if (pOpenCreateParms->fCreate & SHCL_OBJ_CF_ACCESS_WRITE) /* Write access wanted? */
     818            {
     819                AssertMsgFailed(("Is not a read transfer, but object open flags are set to write access (0x%x)\n",
     820                                 pOpenCreateParms->fCreate)); /* Should never happen. */
     821                rc = VERR_INVALID_PARAMETER;
     822            }
     823        }
     824        else
     825        {
     826            AssertFailed();
     827            rc = VERR_NOT_SUPPORTED;
     828        }
     829
     830        if (RT_SUCCESS(rc))
     831        {
     832            PSHCLOBJHANDLEINFO pInfo
     833                = (PSHCLOBJHANDLEINFO)RTMemAllocZ(sizeof(SHCLOBJHANDLEINFO));
     834            if (pInfo)
     835            {
     836                rc = ShClTransferObjHandleInfoInit(pInfo);
    837837                if (RT_SUCCESS(rc))
    838838                {
     839
    839840                    /* Only if this is a read transfer (locally) we're able to actually write to files
    840841                     * (we're reading from the source). */
     
    847848                    if (RT_SUCCESS(rc))
    848849                    {
    849                         rc = shClTransferResolvePathAbs(pTransfer, pOpenCreateParms->pszPath, 0 /* fFlags */, &pInfo->pszPathLocalAbs);
     850                        rc = shClTransferResolvePathAbs(pTransfer, pOpenCreateParms->pszPath, 0 /* fFlags */,
     851                                                        &pInfo->pszPathLocalAbs);
    850852                        if (RT_SUCCESS(rc))
    851853                        {
     
    860862                    }
    861863                }
    862             }
    863 
    864             if (RT_SUCCESS(rc))
    865             {
    866                 pInfo->hObj    = pTransfer->uObjHandleNext++;
    867                 pInfo->enmType = SHCLOBJTYPE_FILE;
    868 
    869                 RTListAppend(&pTransfer->lstObj, &pInfo->Node);
    870                 pTransfer->cObjHandles++;
    871 
    872                 LogFlowFunc(("cObjHandles=%RU32\n", pTransfer->cObjHandles));
    873 
    874                 *phObj = pInfo->hObj;
     864
     865                if (RT_SUCCESS(rc))
     866                {
     867                    pInfo->hObj    = pTransfer->uObjHandleNext++;
     868                    pInfo->enmType = SHCLOBJTYPE_FILE;
     869
     870                    RTListAppend(&pTransfer->lstObj, &pInfo->Node);
     871                    pTransfer->cObjHandles++;
     872
     873                    LogFlowFunc(("cObjHandles=%RU32\n", pTransfer->cObjHandles));
     874
     875                    *phObj = pInfo->hObj;
     876                }
     877                else
     878                {
     879                    ShClTransferObjHandleInfoDestroy(pInfo);
     880                    RTMemFree(pInfo);
     881                }
    875882            }
    876883            else
    877             {
    878                 ShClTransferObjHandleInfoDestroy(pInfo);
    879                 RTMemFree(pInfo);
    880             }
    881         }
    882         else
    883             rc = VERR_NO_MEMORY;
     884                rc = VERR_NO_MEMORY;
     885        }
    884886    }
    885887    else if (pTransfer->State.enmSource == SHCLSOURCE_REMOTE)
     
    892894            rc = VERR_NOT_SUPPORTED;
    893895    }
     896    else
     897        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
    894898
    895899    LogFlowFuncLeaveRC(rc);
     
    972976            rc = VERR_NOT_SUPPORTED;
    973977    }
     978    else
     979        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
    974980
    975981    LogFlowFuncLeaveRC(rc);
     
    10341040            rc = VERR_NOT_SUPPORTED;
    10351041    }
     1042    else
     1043        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
    10361044
    10371045    LogFlowFuncLeaveRC(rc);
     
    10901098            rc = VERR_NOT_SUPPORTED;
    10911099    }
     1100    else
     1101        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
    10921102
    10931103    LogFlowFuncLeaveRC(rc);
     
    13691379
    13701380/**
     1381 * Valides whether a given path matches our set of rules or not.
     1382 *
     1383 * @returns VBox status code.
     1384 * @param   pszPath             Path to validate.
     1385 * @param   fMustExist          Whether the path to validate also must exist.
     1386 */
     1387static int shClTransferValidatePath(const char *pszPath, bool fMustExist)
     1388{
     1389    int rc = VINF_SUCCESS;
     1390
     1391    if (!strlen(pszPath))
     1392        rc = VERR_INVALID_PARAMETER;
     1393
     1394    if (   RT_SUCCESS(rc)
     1395        && !RTStrIsValidEncoding(pszPath))
     1396    {
     1397        rc = VERR_INVALID_UTF8_ENCODING;
     1398    }
     1399
     1400    if (   RT_SUCCESS(rc)
     1401        && RTStrStr(pszPath, ".."))
     1402    {
     1403        rc = VERR_INVALID_PARAMETER;
     1404    }
     1405
     1406    if (   RT_SUCCESS(rc)
     1407        && fMustExist)
     1408    {
     1409        RTFSOBJINFO objInfo;
     1410        rc = RTPathQueryInfo(pszPath, &objInfo, RTFSOBJATTRADD_NOTHING);
     1411        if (RT_SUCCESS(rc))
     1412        {
     1413            if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
     1414            {
     1415                if (!RTDirExists(pszPath)) /* Path must exist. */
     1416                    rc = VERR_PATH_NOT_FOUND;
     1417            }
     1418            else if (RTFS_IS_FILE(objInfo.Attr.fMode))
     1419            {
     1420                if (!RTFileExists(pszPath)) /* File must exist. */
     1421                    rc = VERR_FILE_NOT_FOUND;
     1422            }
     1423            else /* Everything else (e.g. symbolic links) are not supported. */
     1424                rc = VERR_NOT_SUPPORTED;
     1425        }
     1426    }
     1427
     1428    LogFlowFuncLeaveRC(rc);
     1429    return rc;
     1430}
     1431
     1432/**
    13711433 * Resolves a relative path of a specific transfer to its absolute path.
    13721434 *
     
    13861448    LogFlowFunc(("pszPathRootAbs=%s, pszPath=%s\n", pTransfer->pszPathRootAbs, pszPath));
    13871449
    1388     /* Paranoia. */
    1389     if (   !strlen(pszPath)
    1390         || !RTStrIsValidEncoding(pszPath)
    1391         ||  RTStrStr(pszPath, ".."))
    1392     {
    1393         LogRel(("Shared Clipboard: Resolving absolute path '%s' failed, invalid\n", pszPath));
    1394         return VERR_INVALID_PARAMETER;
    1395     }
    1396 
    1397     int rc;
    1398 
    1399     char *pszResolved = RTPathJoinA(pTransfer->pszPathRootAbs, pszPath);
    1400     if (pszResolved)
    1401     {
    1402         LogFlowFunc(("Resolved to: %s\n", pszResolved));
    1403 
    1404         *ppszResolved = pszResolved;
    1405 
    1406         rc = VINF_SUCCESS;
    1407     }
    1408     else
    1409         rc = VERR_NO_MEMORY;
     1450    int rc = shClTransferValidatePath(pszPath, false /* fMustExist */);
     1451    if (RT_SUCCESS(rc))
     1452    {
     1453        char *pszPathAbs = RTPathJoinA(pTransfer->pszPathRootAbs, pszPath);
     1454        if (pszPathAbs)
     1455        {
     1456            char   szResolved[RTPATH_MAX + 1];
     1457            size_t cbResolved = sizeof(szResolved);
     1458            rc = RTPathAbsEx(pTransfer->pszPathRootAbs, pszPathAbs, RTPATH_STR_F_STYLE_HOST, szResolved, &cbResolved);
     1459
     1460            RTStrFree(pszPathAbs);
     1461
     1462            if (RT_SUCCESS(rc))
     1463            {
     1464                LogFlowFunc(("pszResolved=%s\n", szResolved));
     1465
     1466                rc = VERR_PATH_NOT_FOUND; /* Play safe by default. */
     1467
     1468                /* Make sure the resolved path is part of the set of root entries. */
     1469                PSHCLLISTROOT pListRoot;
     1470                RTListForEach(&pTransfer->lstRoots, pListRoot, SHCLLISTROOT, Node)
     1471                {
     1472                    if (RTPathStartsWith(szResolved, pListRoot->pszPathAbs))
     1473                    {
     1474                        rc = VINF_SUCCESS;
     1475                        break;
     1476                    }
     1477                }
     1478
     1479                if (RT_SUCCESS(rc))
     1480                    *ppszResolved = RTStrDup(szResolved);
     1481            }
     1482        }
     1483        else
     1484            rc = VERR_NO_MEMORY;
     1485    }
     1486
     1487    if (RT_FAILURE(rc))
     1488        LogRel(("Shared Clipboard: Resolving absolute path '%s' failed, rc=%Rrc\n", pszPath, rc));
    14101489
    14111490    LogFlowFuncLeaveRC(rc);
     
    14411520        if (pInfo)
    14421521        {
    1443             rc = shClTransferResolvePathAbs(pTransfer, pOpenParms->pszPath, 0 /* fFlags */, &pInfo->pszPathLocalAbs);
     1522            rc = ShClTransferListHandleInfoInit(pInfo);
    14441523            if (RT_SUCCESS(rc))
    14451524            {
    1446                 rc = ShClTransferListHandleInfoInit(pInfo);
     1525                rc = shClTransferResolvePathAbs(pTransfer, pOpenParms->pszPath, 0 /* fFlags */, &pInfo->pszPathLocalAbs);
    14471526                if (RT_SUCCESS(rc))
    14481527                {
     
    14861565                            RTListAppend(&pTransfer->lstList, &pInfo->Node);
    14871566                            pTransfer->cListHandles++;
     1567
     1568                            if (phList)
     1569                                *phList = pInfo->hList;
    14881570
    14891571                            LogFlowFunc(("pszPathLocalAbs=%s, hList=%RU64, cListHandles=%RU32\n",
     
    19472029            rc = VERR_NOT_SUPPORTED;
    19482030    }
     2031    else
     2032        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
    19492033
    19502034    LogFlowFuncLeaveRC(rc);
     
    19872071        AssertFailed(); /** @todo Implement. */
    19882072    }
     2073    else
     2074        AssertFailedStmt(fIsValid = false);
    19892075
    19902076    return fIsValid;
     
    22462332            rc = VERR_NOT_SUPPORTED;
    22472333    }
     2334    else
     2335        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
    22482336
    22492337    LogFlowFuncLeaveRC(rc);
    22502338    return rc;
    22512339}
    2252 
    22532340
    22542341/**
     
    22822369        AssertPtrBreakStmt(pListRoot, rc = VERR_NO_MEMORY);
    22832370
    2284         pListRoot->pszPathAbs = RTStrDup(lstRootEntries.at(i).c_str());
    2285         AssertPtrBreakStmt(pListRoot->pszPathAbs, rc = VERR_NO_MEMORY);
    2286 
     2371        const char *pszPathCur = RTStrDup(lstRootEntries.at(i).c_str());
     2372
     2373        LogFlowFunc(("pszPathCur=%s\n", pszPathCur));
     2374
     2375        /* No root path determined yet? */
    22872376        if (!pszPathRootAbs)
    22882377        {
    2289             pszPathRootAbs = RTStrDup(pListRoot->pszPathAbs);
     2378            pszPathRootAbs = RTStrDup(pszPathCur);
    22902379            if (pszPathRootAbs)
    22912380            {
    22922381                RTPathStripFilename(pszPathRootAbs);
     2382
    22932383                LogFlowFunc(("pszPathRootAbs=%s\n", pszPathRootAbs));
     2384
     2385                /* We don't want to have a relative directory here. */
     2386                if (RTPathStartsWithRoot(pszPathRootAbs))
     2387                {
     2388                    rc = shClTransferValidatePath(pszPathRootAbs, true /* Path must exist */);
     2389                }
     2390                else
     2391                    rc = VERR_INVALID_PARAMETER;
    22942392            }
    22952393            else
     
    23002398            break;
    23012399
    2302         /* Make sure all entries have the same root path. */
    2303         if (!RTStrStartsWith(pListRoot->pszPathAbs, pszPathRootAbs))
    2304         {
    2305             rc = VERR_INVALID_PARAMETER;
     2400        pListRoot->pszPathAbs = RTStrDup(pszPathCur);
     2401        if (!pListRoot->pszPathAbs)
     2402        {
     2403            rc = VERR_NO_MEMORY;
    23062404            break;
    23072405        }
     
    23102408
    23112409        pTransfer->cRoots++;
     2410    }
     2411
     2412    /* No (valid) root directory found? Bail out early. */
     2413    if (!pszPathRootAbs)
     2414        rc = VERR_PATH_NOT_FOUND;
     2415
     2416    if (RT_SUCCESS(rc))
     2417    {
     2418        /*
     2419         * Step 2:
     2420         * Go through the created list and make sure all entries have the same root path.
     2421         */
     2422        PSHCLLISTROOT pListRoot;
     2423        RTListForEach(&pTransfer->lstRoots, pListRoot, SHCLLISTROOT, Node)
     2424        {
     2425            if (!RTStrStartsWith(pListRoot->pszPathAbs, pszPathRootAbs))
     2426            {
     2427                rc = VERR_INVALID_PARAMETER;
     2428                break;
     2429            }
     2430
     2431            rc = shClTransferValidatePath(pListRoot->pszPathAbs, true /* Path must exist */);
     2432            if (RT_FAILURE(rc))
     2433                break;
     2434        }
    23122435    }
    23132436
     
    23222445    }
    23232446    else
     2447    {
    23242448        LogRel(("Shared Clipboard: Unable to set roots for transfer, rc=%Rrc\n", rc));
     2449        RTStrFree(pszPathRootAbs);
     2450    }
    23252451
    23262452    LogFlowFuncLeaveRC(rc);
Note: See TracChangeset for help on using the changeset viewer.

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