VirtualBox

Ignore:
Timestamp:
Oct 8, 2019 11:13:29 AM (5 years ago)
Author:
vboxsync
Message:

Guest Control/VBoxManage: Added support for renaming (mv) multiple sources to a (directory) destination, cleaned up that code area and print errors by default.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestCtrl.cpp

    r79257 r81143  
    22982298    HRESULT rc = S_OK;
    22992299
     2300    /* Destination must be a directory when specifying multiple sources. */
    23002301    if (cSources > 1)
    23012302    {
    2302         BOOL fExists = FALSE;
    2303         rc = pCtx->pGuestSession->DirectoryExists(Bstr(pszDst).raw(), FALSE /*followSymlinks*/, &fExists);
    2304         if (FAILED(rc) || !fExists)
    2305             return RTMsgErrorExit(RTEXITCODE_FAILURE, "Destination must be a directory when specifying multiple sources\n");
     2303        ComPtr<IGuestFsObjInfo> pFsObjInfo;
     2304        rc = pCtx->pGuestSession->FsObjQueryInfo(Bstr(pszDst).raw(), FALSE /*followSymlinks*/, pFsObjInfo.asOutParam());
     2305        if (FAILED(rc))
     2306        {
     2307            return RTMsgErrorExit(RTEXITCODE_FAILURE, "Destination does not exist\n");
     2308        }
     2309        else
     2310        {
     2311            FsObjType_T enmObjType = FsObjType_Unknown; /* Shut up MSC */
     2312            rc = pFsObjInfo->COMGETTER(Type)(&enmObjType);
     2313            if (SUCCEEDED(rc))
     2314            {
     2315                if (enmObjType != FsObjType_Directory)
     2316                    return RTMsgErrorExit(RTEXITCODE_FAILURE, "Destination must be a directory when specifying multiple sources\n");
     2317            }
     2318            else
     2319                return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unable to determine destination type: %Rhrc\n", rc);
     2320        }
    23062321    }
    23072322
     
    23102325     */
    23112326    if (pCtx->cVerbose)
    2312         RTPrintf("Renaming %RU32 %s ...\n", cSources, cSources > 1 ? "entries" : "entry");
     2327        RTPrintf("Renaming %RU32 %s ...\n", cSources, cSources > 1 ? "sources" : "source");
    23132328
    23142329    std::vector< Utf8Str >::iterator it = vecSources.begin();
     
    23162331           && !g_fGuestCtrlCanceled)
    23172332    {
    2318         Utf8Str strCurSource = (*it);
     2333        Utf8Str strSrcCur = (*it);
    23192334
    23202335        ComPtr<IGuestFsObjInfo> pFsObjInfo;
    23212336        FsObjType_T enmObjType = FsObjType_Unknown; /* Shut up MSC */
    2322         rc = pCtx->pGuestSession->FsObjQueryInfo(Bstr(strCurSource).raw(), FALSE /*followSymlinks*/, pFsObjInfo.asOutParam());
     2337        rc = pCtx->pGuestSession->FsObjQueryInfo(Bstr(strSrcCur).raw(), FALSE /*followSymlinks*/, pFsObjInfo.asOutParam());
    23232338        if (SUCCEEDED(rc))
    23242339            rc = pFsObjInfo->COMGETTER(Type)(&enmObjType);
    23252340        if (FAILED(rc))
    23262341        {
    2327             if (pCtx->cVerbose)
    2328                 RTPrintf("Warning: Cannot stat for element \"%s\": No such file or directory\n", strCurSource.c_str());
     2342            RTPrintf("Cannot stat \"%s\": No such file or directory\n", strSrcCur.c_str());
    23292343            ++it;
    23302344            continue; /* Skip. */
    23312345        }
     2346
     2347        char *pszDstCur = NULL;
     2348
     2349        if (cSources > 1)
     2350        {
     2351            pszDstCur = RTPathJoinA(pszDst, RTPathFilename(strSrcCur.c_str()));
     2352        }
     2353        else
     2354            pszDstCur = RTStrDup(pszDst);
     2355
     2356        AssertPtrBreakStmt(pszDstCur, VERR_NO_MEMORY);
    23322357
    23332358        if (pCtx->cVerbose)
    23342359            RTPrintf("Renaming %s \"%s\" to \"%s\" ...\n",
    23352360                     enmObjType == FsObjType_Directory ? "directory" : "file",
    2336                      strCurSource.c_str(), pszDst);
     2361                     strSrcCur.c_str(), pszDstCur);
    23372362
    23382363        if (!fDryrun)
    23392364        {
    2340             if (enmObjType == FsObjType_Directory)
    2341             {
    2342                 CHECK_ERROR_BREAK(pCtx->pGuestSession, FsObjRename(Bstr(strCurSource).raw(),
    2343                                                                    Bstr(pszDst).raw(),
    2344                                                                    ComSafeArrayAsInParam(aRenameFlags)));
    2345 
    2346                 /* Break here, since it makes no sense to rename mroe than one source to
    2347                  * the same directory. */
    2348 /** @todo r=bird: You are being kind of windowsy (or just DOSish) about the 'sense' part here,
    2349  * while being totaly buggy about the behavior. 'VBoxManage guestcontrol ren dir1 dir2 dstdir' will
    2350  * stop after 'dir1' and SILENTLY ignore dir2.  If you tried this on Windows, you'd see an error
    2351  * being displayed.  If you 'man mv' on a nearby unixy system, you'd see that they've made perfect
    2352  * sense out of any situation with more than one source. */
    2353                 it = vecSources.end();
    2354                 break;
    2355             }
    2356             else
    2357                 CHECK_ERROR_BREAK(pCtx->pGuestSession, FsObjRename(Bstr(strCurSource).raw(),
    2358                                                                    Bstr(pszDst).raw(),
    2359                                                                    ComSafeArrayAsInParam(aRenameFlags)));
    2360         }
     2365            CHECK_ERROR(pCtx->pGuestSession, FsObjRename(Bstr(strSrcCur).raw(),
     2366                                                         Bstr(pszDstCur).raw(),
     2367                                                         ComSafeArrayAsInParam(aRenameFlags)));
     2368            /* Keep going with next item in case of errors. */
     2369        }
     2370
     2371        RTStrFree(pszDstCur);
    23612372
    23622373        ++it;
Note: See TracChangeset for help on using the changeset viewer.

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