STDMETHODIMP GuestDirectory::Read(IFsObjInfo **aInfo) { #ifndef VBOX_WITH_GUEST_CONTROL ReturnComNotImplemented(); #else LogFlowThisFuncEnter(); AutoCaller autoCaller(this); if (FAILED(autoCaller.rc())) return autoCaller.rc(); ComObjPtr<GuestProcess> pProcess = mData.mProcess; Assert(!pProcess.isNull()); GuestProcessStreamBlock streamBlock; GuestFsObjData objData; int rc = parseData(streamBlock); if ( RT_FAILURE(rc) || streamBlock.IsEmpty()) /* More data needed. */ { rc = pProcess->waitForStart(30 * 1000 /* 30s timeout */); } if (RT_SUCCESS(rc)) { BYTE byBuf[_64K]; size_t cbRead = 0; /** @todo Merge with GuestSession::queryFileInfoInternal. */ for (;RT_SUCCESS(rc);) { GuestProcessWaitResult waitRes; rc = pProcess->waitFor( ProcessWaitForFlag_Terminate | ProcessWaitForFlag_StdOut, 30 * 1000 /* Timeout */, waitRes); if ( RT_FAILURE(rc) || waitRes.mResult == ProcessWaitResult_Terminate || waitRes.mResult == ProcessWaitResult_Error || waitRes.mResult == ProcessWaitResult_Timeout) { if (RT_FAILURE(waitRes.mRC)) rc = waitRes.mRC; break; } rc = pProcess->readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf), 30 * 1000 /* Timeout */, byBuf, sizeof(byBuf), &cbRead); if (RT_FAILURE(rc)) break; if (cbRead) { AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); rc = mData.mStream.AddData(byBuf, cbRead); if (RT_FAILURE(rc)) break; LogFlowThisFunc(("rc=%Rrc, cbRead=%RU64, cbStreamOut=%RU32\n", rc, cbRead, mData.mStream.GetSize())); rc = parseData(streamBlock); if (RT_SUCCESS(rc)) { /* Parsing the current stream block succeeded so * we don't need more at the moment. */ break; } } } LogFlowThisFunc(("Reading done with rc=%Rrc, cbRead=%RU64, cbStream=%RU32\n", rc, cbRead, mData.mStream.GetSize())); if (RT_SUCCESS(rc)) { rc = parseData(streamBlock); if (rc == VERR_NO_DATA) /* Since this is the last parsing call, this is ok. */ rc = VINF_SUCCESS; } /* * Note: The guest process can still be around to serve the next * upcoming stream block next time. */ if (RT_SUCCESS(rc)) { /** @todo Move into common function. */ ProcessStatus_T procStatus = ProcessStatus_Undefined; LONG exitCode = 0; HRESULT hr2 = pProcess->COMGETTER(Status(&procStatus)); ComAssertComRC(hr2); hr2 = pProcess->COMGETTER(ExitCode(&exitCode)); ComAssertComRC(hr2); if ( ( procStatus != ProcessStatus_Started && procStatus != ProcessStatus_Paused && procStatus != ProcessStatus_Terminating ) && exitCode != 0) { rc = VERR_ACCESS_DENIED; } } } if (RT_SUCCESS(rc)) { if (streamBlock.GetCount()) /* Did we get content? */ { rc = objData.FromLs(streamBlock); if (RT_FAILURE(rc)) rc = VERR_PATH_NOT_FOUND; if (RT_SUCCESS(rc)) { /* Create the object. */ ComObjPtr<GuestFsObjInfo> pFsObjInfo; HRESULT hr2 = pFsObjInfo.createObject(); if (FAILED(hr2)) rc = VERR_COM_UNEXPECTED; if (RT_SUCCESS(rc)) rc = pFsObjInfo->init(objData); if (RT_SUCCESS(rc)) { /* Return info object to the caller. */ hr2 = pFsObjInfo.queryInterfaceTo(aInfo); if (FAILED(hr2)) rc = VERR_COM_UNEXPECTED; } } } else { /* Nothing to read anymore. Tell the caller. */ rc = VERR_NO_MORE_FILES; } } HRESULT hr = S_OK; if (RT_FAILURE(rc)) /** @todo Add more errors here. */ { switch (rc) { case VERR_ACCESS_DENIED: hr = setError(VBOX_E_IPRT_ERROR, tr("Reading directory \"%s\" failed: Unable to read / access denied"), mData.mName.c_str()); break; case VERR_PATH_NOT_FOUND: hr = setError(VBOX_E_IPRT_ERROR, tr("Reading directory \"%s\" failed: Path not found"), mData.mName.c_str()); break; case VERR_NO_MORE_FILES: hr = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No more entries for directory \"%s\""), mData.mName.c_str()); break; default: hr = setError(VBOX_E_IPRT_ERROR, tr("Error while reading directory \"%s\": %Rrc\n"), mData.mName.c_str(), rc); break; } } LogFlowFuncLeaveRC(rc); return hr; #endif /* VBOX_WITH_GUEST_CONTROL */ }
int SessionTaskCopyFrom::Run(void) { LogFlowThisFuncEnter(); ComObjPtr<GuestSession> pSession = mSession; Assert(!pSession.isNull()); AutoCaller autoCaller(pSession); if (FAILED(autoCaller.rc())) return autoCaller.rc(); /* * Note: There will be races between querying file size + reading the guest file's * content because we currently *do not* lock down the guest file when doing the * actual operations. ** @todo Implement guest file locking! */ GuestFsObjData objData; int rc = pSession->fileQueryInfoInternal(Utf8Str(mSource), objData); if (RT_FAILURE(rc)) { setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Querying guest file information for \"%s\" failed: %Rrc"), mSource.c_str(), rc)); } else if (objData.mType != FsObjType_File) /* Only single files are supported at the moment. */ { rc = setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Object \"%s\" on the guest is not a file"), mSource.c_str())); } if (RT_SUCCESS(rc)) { RTFILE fileDest; rc = RTFileOpen(&fileDest, mDest.c_str(), RTFILE_O_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE); /** @todo Use the correct open modes! */ if (RT_FAILURE(rc)) { setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Error opening destination file \"%s\": %Rrc"), mDest.c_str(), rc)); } else { GuestProcessStartupInfo procInfo; procInfo.mName = Utf8StrFmt(GuestSession::tr("Copying file \"%s\" from guest to the host to \"%s\" (%RI64 bytes)"), mSource.c_str(), mDest.c_str(), objData.mObjectSize); procInfo.mCommand = Utf8Str(VBOXSERVICE_TOOL_CAT); procInfo.mFlags = ProcessCreateFlag_Hidden | ProcessCreateFlag_WaitForStdOut; /* Set arguments.*/ procInfo.mArguments.push_back(mSource); /* Which file to output? */ /* Startup process. */ ComObjPtr<GuestProcess> pProcess; rc = pSession->processCreateExInteral(procInfo, pProcess); if (RT_SUCCESS(rc)) rc = pProcess->startProcess(); if (RT_FAILURE(rc)) { setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Unable to start guest process for copying data from guest to host: %Rrc"), rc)); } else { GuestProcessWaitResult waitRes; BYTE byBuf[_64K]; BOOL fCanceled = FALSE; uint64_t cbWrittenTotal = 0; uint64_t cbToRead = objData.mObjectSize; for (;;) { rc = pProcess->waitFor(ProcessWaitForFlag_StdOut, 30 * 1000 /* Timeout */, waitRes); if ( waitRes.mResult == ProcessWaitResult_StdOut || waitRes.mResult == ProcessWaitResult_WaitFlagNotSupported) { /* If the guest does not support waiting for stdin, we now yield in * order to reduce the CPU load due to busy waiting. */ if (waitRes.mResult == ProcessWaitResult_WaitFlagNotSupported) RTThreadYield(); /* Optional, don't check rc. */ size_t cbRead; rc = pProcess->readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf), 30 * 1000 /* Timeout */, byBuf, sizeof(byBuf), &cbRead); if (RT_FAILURE(rc)) { setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Reading from file \"%s\" (offset %RU64) failed: %Rrc"), mSource.c_str(), cbWrittenTotal, rc)); break; } if (cbRead) { rc = RTFileWrite(fileDest, byBuf, cbRead, NULL /* No partial writes */); if (RT_FAILURE(rc)) { setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Error writing to file \"%s\" (%RU64 bytes left): %Rrc"), mDest.c_str(), cbToRead, rc)); break; } /* Only subtract bytes reported written by the guest. */ Assert(cbToRead >= cbRead); cbToRead -= cbRead; /* Update total bytes written to the guest. */ cbWrittenTotal += cbRead; Assert(cbWrittenTotal <= (uint64_t)objData.mObjectSize); /* Did the user cancel the operation above? */ if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled))) && fCanceled) break; rc = setProgress((ULONG)(cbWrittenTotal / ((uint64_t)objData.mObjectSize / 100.0))); if (RT_FAILURE(rc)) break; } } else if ( RT_FAILURE(rc) || waitRes.mResult == ProcessWaitResult_Terminate || waitRes.mResult == ProcessWaitResult_Error || waitRes.mResult == ProcessWaitResult_Timeout) { if (RT_FAILURE(waitRes.mRC)) rc = waitRes.mRC; break; } } /* for */ LogFlowThisFunc(("rc=%Rrc, cbWrittenTotal=%RU64, cbSize=%RI64, cbToRead=%RU64\n", rc, cbWrittenTotal, objData.mObjectSize, cbToRead)); if ( !fCanceled || RT_SUCCESS(rc)) { /* * Even if we succeeded until here make sure to check whether we really transfered * everything. */ if ( objData.mObjectSize > 0 && cbWrittenTotal == 0) { /* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write * to the destination -> access denied. */ setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Access denied when copying file \"%s\" to \"%s\""), mSource.c_str(), mDest.c_str())); rc = VERR_GENERAL_FAILURE; /* Fudge. */ } else if (cbWrittenTotal < (uint64_t)objData.mObjectSize) { /* If we did not copy all let the user know. */ setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Copying file \"%s\" failed (%RU64/%RI64 bytes transfered)"), mSource.c_str(), cbWrittenTotal, objData.mObjectSize)); rc = VERR_GENERAL_FAILURE; /* Fudge. */ } else { ProcessStatus_T procStatus; LONG exitCode; if ( ( SUCCEEDED(pProcess->COMGETTER(Status(&procStatus))) && procStatus != ProcessStatus_TerminatedNormally) || ( SUCCEEDED(pProcess->COMGETTER(ExitCode(&exitCode))) && exitCode != 0) ) { setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("Copying file \"%s\" failed with status %ld, exit code %d"), mSource.c_str(), procStatus, exitCode)); /**@todo Add stringify methods! */ rc = VERR_GENERAL_FAILURE; /* Fudge. */ } else /* Yay, success! */ rc = setProgressSuccess(); } } if (!pProcess.isNull()) pProcess->uninit(); } RTFileClose(fileDest); } } LogFlowFuncLeaveRC(rc); return rc; }