VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestFileImpl.cpp@ 86728

Last change on this file since 86728 was 85307, checked in by vboxsync, 4 years ago

Main/comimpl.xsl,++: Try use the Utf8Str versions of the event stuff where possible. bugref:9790

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 52.9 KB
Line 
1/* $Id: GuestFileImpl.cpp 85307 2020-07-13 12:38:15Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest file handling.
4 */
5
6/*
7 * Copyright (C) 2012-2020 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_MAIN_GUESTFILE
23#include "LoggingNew.h"
24
25#ifndef VBOX_WITH_GUEST_CONTROL
26# error "VBOX_WITH_GUEST_CONTROL must defined in this file"
27#endif
28#include "GuestFileImpl.h"
29#include "GuestSessionImpl.h"
30#include "GuestCtrlImplPrivate.h"
31#include "ConsoleImpl.h"
32#include "VirtualBoxErrorInfoImpl.h"
33
34#include "Global.h"
35#include "AutoCaller.h"
36#include "VBoxEvents.h"
37
38#include <iprt/cpp/utils.h> /* For unconst(). */
39#include <iprt/file.h>
40
41#include <VBox/com/array.h>
42#include <VBox/com/listeners.h>
43#include <VBox/AssertGuest.h>
44
45
46/**
47 * Internal listener class to serve events in an
48 * active manner, e.g. without polling delays.
49 */
50class GuestFileListener
51{
52public:
53
54 GuestFileListener(void)
55 {
56 }
57
58 virtual ~GuestFileListener()
59 {
60 }
61
62 HRESULT init(GuestFile *pFile)
63 {
64 AssertPtrReturn(pFile, E_POINTER);
65 mFile = pFile;
66 return S_OK;
67 }
68
69 void uninit(void)
70 {
71 mFile = NULL;
72 }
73
74 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
75 {
76 switch (aType)
77 {
78 case VBoxEventType_OnGuestFileStateChanged:
79 case VBoxEventType_OnGuestFileOffsetChanged:
80 case VBoxEventType_OnGuestFileRead:
81 case VBoxEventType_OnGuestFileWrite:
82 {
83 AssertPtrReturn(mFile, E_POINTER);
84 int rc2 = mFile->signalWaitEvent(aType, aEvent);
85 RT_NOREF(rc2);
86#ifdef DEBUG_andy
87 LogFlowFunc(("Signalling events of type=%RU32, file=%p resulted in rc=%Rrc\n",
88 aType, mFile, rc2));
89#endif
90 break;
91 }
92
93 default:
94 AssertMsgFailed(("Unhandled event %RU32\n", aType));
95 break;
96 }
97
98 return S_OK;
99 }
100
101private:
102
103 GuestFile *mFile;
104};
105typedef ListenerImpl<GuestFileListener, GuestFile*> GuestFileListenerImpl;
106
107VBOX_LISTENER_DECLARE(GuestFileListenerImpl)
108
109// constructor / destructor
110/////////////////////////////////////////////////////////////////////////////
111
112DEFINE_EMPTY_CTOR_DTOR(GuestFile)
113
114HRESULT GuestFile::FinalConstruct(void)
115{
116 LogFlowThisFuncEnter();
117 return BaseFinalConstruct();
118}
119
120void GuestFile::FinalRelease(void)
121{
122 LogFlowThisFuncEnter();
123 uninit();
124 BaseFinalRelease();
125 LogFlowThisFuncLeave();
126}
127
128// public initializer/uninitializer for internal purposes only
129/////////////////////////////////////////////////////////////////////////////
130
131/**
132 * Initializes a file object but does *not* open the file on the guest
133 * yet. This is done in the dedidcated openFile call.
134 *
135 * @return IPRT status code.
136 * @param pConsole Pointer to console object.
137 * @param pSession Pointer to session object.
138 * @param aObjectID The object's ID.
139 * @param openInfo File opening information.
140 */
141int GuestFile::init(Console *pConsole, GuestSession *pSession,
142 ULONG aObjectID, const GuestFileOpenInfo &openInfo)
143{
144 LogFlowThisFunc(("pConsole=%p, pSession=%p, aObjectID=%RU32, strPath=%s\n",
145 pConsole, pSession, aObjectID, openInfo.mFilename.c_str()));
146
147 AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
148 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
149
150 /* Enclose the state transition NotReady->InInit->Ready. */
151 AutoInitSpan autoInitSpan(this);
152 AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
153
154 int vrc = bindToSession(pConsole, pSession, aObjectID);
155 if (RT_SUCCESS(vrc))
156 {
157 mSession = pSession;
158
159 mData.mOpenInfo = openInfo;
160 mData.mInitialSize = 0;
161 mData.mStatus = FileStatus_Undefined;
162 mData.mLastError = VINF_SUCCESS;
163 mData.mOffCurrent = 0;
164
165 unconst(mEventSource).createObject();
166 HRESULT hr = mEventSource->init();
167 if (FAILED(hr))
168 vrc = VERR_COM_UNEXPECTED;
169 }
170
171 if (RT_SUCCESS(vrc))
172 {
173 try
174 {
175 GuestFileListener *pListener = new GuestFileListener();
176 ComObjPtr<GuestFileListenerImpl> thisListener;
177 HRESULT hr = thisListener.createObject();
178 if (SUCCEEDED(hr))
179 hr = thisListener->init(pListener, this);
180
181 if (SUCCEEDED(hr))
182 {
183 com::SafeArray <VBoxEventType_T> eventTypes;
184 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
185 eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
186 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
187 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
188 hr = mEventSource->RegisterListener(thisListener,
189 ComSafeArrayAsInParam(eventTypes),
190 TRUE /* Active listener */);
191 if (SUCCEEDED(hr))
192 {
193 vrc = baseInit();
194 if (RT_SUCCESS(vrc))
195 {
196 mLocalListener = thisListener;
197 }
198 }
199 else
200 vrc = VERR_COM_UNEXPECTED;
201 }
202 else
203 vrc = VERR_COM_UNEXPECTED;
204 }
205 catch(std::bad_alloc &)
206 {
207 vrc = VERR_NO_MEMORY;
208 }
209 }
210
211 if (RT_SUCCESS(vrc))
212 {
213 /* Confirm a successful initialization when it's the case. */
214 autoInitSpan.setSucceeded();
215 }
216 else
217 autoInitSpan.setFailed();
218
219 LogFlowFuncLeaveRC(vrc);
220 return vrc;
221}
222
223/**
224 * Uninitializes the instance.
225 * Called from FinalRelease().
226 */
227void GuestFile::uninit(void)
228{
229 /* Enclose the state transition Ready->InUninit->NotReady. */
230 AutoUninitSpan autoUninitSpan(this);
231 if (autoUninitSpan.uninitDone())
232 return;
233
234 LogFlowThisFuncEnter();
235
236 baseUninit();
237 LogFlowThisFuncLeave();
238}
239
240// implementation of public getters/setters for attributes
241/////////////////////////////////////////////////////////////////////////////
242
243HRESULT GuestFile::getCreationMode(ULONG *aCreationMode)
244{
245 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
246
247 *aCreationMode = mData.mOpenInfo.mCreationMode;
248
249 return S_OK;
250}
251
252HRESULT GuestFile::getOpenAction(FileOpenAction_T *aOpenAction)
253{
254 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
255
256 *aOpenAction = mData.mOpenInfo.mOpenAction;
257
258 return S_OK;
259}
260
261HRESULT GuestFile::getEventSource(ComPtr<IEventSource> &aEventSource)
262{
263 /* No need to lock - lifetime constant. */
264 mEventSource.queryInterfaceTo(aEventSource.asOutParam());
265
266 return S_OK;
267}
268
269HRESULT GuestFile::getFilename(com::Utf8Str &aFilename)
270{
271 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
272
273 aFilename = mData.mOpenInfo.mFilename;
274
275 return S_OK;
276}
277
278HRESULT GuestFile::getId(ULONG *aId)
279{
280 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
281
282 *aId = mObjectID;
283
284 return S_OK;
285}
286
287HRESULT GuestFile::getInitialSize(LONG64 *aInitialSize)
288{
289 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
290
291 *aInitialSize = mData.mInitialSize;
292
293 return S_OK;
294}
295
296HRESULT GuestFile::getOffset(LONG64 *aOffset)
297{
298 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
299
300 /*
301 * This is updated by GuestFile::i_onFileNotify() when read, write and seek
302 * confirmation messages are recevied.
303 *
304 * Note! This will not be accurate with older (< 5.2.32, 6.0.0 - 6.0.9)
305 * Guest Additions when using writeAt, readAt or writing to a file
306 * opened in append mode.
307 */
308 *aOffset = mData.mOffCurrent;
309
310 return S_OK;
311}
312
313HRESULT GuestFile::getAccessMode(FileAccessMode_T *aAccessMode)
314{
315 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
316
317 *aAccessMode = mData.mOpenInfo.mAccessMode;
318
319 return S_OK;
320}
321
322HRESULT GuestFile::getStatus(FileStatus_T *aStatus)
323{
324 LogFlowThisFuncEnter();
325
326 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
327
328 *aStatus = mData.mStatus;
329
330 return S_OK;
331}
332
333// private methods
334/////////////////////////////////////////////////////////////////////////////
335
336int GuestFile::i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
337{
338 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
339 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
340
341 LogFlowThisFunc(("strName=%s, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
342 mData.mOpenInfo.mFilename.c_str(), pCbCtx->uContextID, pCbCtx->uMessage, pSvcCb));
343
344 int vrc;
345 switch (pCbCtx->uMessage)
346 {
347 case GUEST_MSG_DISCONNECTED:
348 vrc = i_onGuestDisconnected(pCbCtx, pSvcCb);
349 break;
350
351 case GUEST_MSG_FILE_NOTIFY:
352 vrc = i_onFileNotify(pCbCtx, pSvcCb);
353 break;
354
355 default:
356 /* Silently ignore not implemented functions. */
357 vrc = VERR_NOT_SUPPORTED;
358 break;
359 }
360
361#ifdef DEBUG
362 LogFlowFuncLeaveRC(vrc);
363#endif
364 return vrc;
365}
366
367int GuestFile::i_closeFile(int *prcGuest)
368{
369 LogFlowThisFunc(("strFile=%s\n", mData.mOpenInfo.mFilename.c_str()));
370
371 int vrc;
372
373 GuestWaitEvent *pEvent = NULL;
374 GuestEventTypes eventTypes;
375 try
376 {
377 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
378
379 vrc = registerWaitEvent(eventTypes, &pEvent);
380 }
381 catch (std::bad_alloc &)
382 {
383 vrc = VERR_NO_MEMORY;
384 }
385
386 if (RT_FAILURE(vrc))
387 return vrc;
388
389 /* Prepare HGCM call. */
390 VBOXHGCMSVCPARM paParms[4];
391 int i = 0;
392 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
393 HGCMSvcSetU32(&paParms[i++], mObjectID /* Guest file ID */);
394
395 vrc = sendMessage(HOST_MSG_FILE_CLOSE, i, paParms);
396 if (RT_SUCCESS(vrc))
397 vrc = i_waitForStatusChange(pEvent, 30 * 1000 /* Timeout in ms */,
398 NULL /* FileStatus */, prcGuest);
399 unregisterWaitEvent(pEvent);
400
401 LogFlowFuncLeaveRC(vrc);
402 return vrc;
403}
404
405/**
406 * Converts a given guest file error to a string.
407 *
408 * @returns Error string.
409 * @param rcGuest Guest file error to return string for.
410 * @param pcszWhat Hint of what was involved when the error occurred.
411 */
412/* static */
413Utf8Str GuestFile::i_guestErrorToString(int rcGuest, const char *pcszWhat)
414{
415 AssertPtrReturn(pcszWhat, "");
416
417 Utf8Str strErr;
418
419#define CASE_MSG(a_iRc, ...) \
420 case a_iRc: strErr = Utf8StrFmt(__VA_ARGS__); break;
421
422 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
423 switch (rcGuest)
424 {
425 CASE_MSG(VERR_ACCESS_DENIED , tr("Access to guest file \"%s\" denied"), pcszWhat);
426 CASE_MSG(VERR_ALREADY_EXISTS , tr("Guest file \"%s\" already exists"), pcszWhat);
427 CASE_MSG(VERR_FILE_NOT_FOUND , tr("Guest file \"%s\" not found"), pcszWhat);
428 CASE_MSG(VERR_NET_HOST_NOT_FOUND, tr("Host name \"%s\", not found"), pcszWhat);
429 CASE_MSG(VERR_SHARING_VIOLATION , tr("Sharing violation for guest file \"%s\""), pcszWhat);
430 default:
431 {
432 char szDefine[80];
433 RTErrQueryDefine(rcGuest, szDefine, sizeof(szDefine), false /*fFailIfUnknown*/);
434 strErr = Utf8StrFmt(tr("Error %s for guest file \"%s\" occurred\n"), szDefine, pcszWhat);
435 break;
436 }
437 }
438
439#undef CASE_MSG
440
441 return strErr;
442}
443
444int GuestFile::i_onFileNotify(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
445{
446 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
447 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
448
449 LogFlowThisFuncEnter();
450
451 if (pSvcCbData->mParms < 3)
452 return VERR_INVALID_PARAMETER;
453
454 int idx = 1; /* Current parameter index. */
455 CALLBACKDATA_FILE_NOTIFY dataCb;
456 RT_ZERO(dataCb);
457 /* pSvcCb->mpaParms[0] always contains the context ID. */
458 HGCMSvcGetU32(&pSvcCbData->mpaParms[idx++], &dataCb.uType);
459 HGCMSvcGetU32(&pSvcCbData->mpaParms[idx++], &dataCb.rc);
460
461 int rcGuest = (int)dataCb.rc; /* uint32_t vs. int. */
462
463 LogFlowThisFunc(("uType=%RU32, rcGuest=%Rrc\n", dataCb.uType, rcGuest));
464
465 if (RT_FAILURE(rcGuest))
466 {
467 int rc2 = i_setFileStatus(FileStatus_Error, rcGuest);
468 AssertRC(rc2);
469
470 /* Ignore rc, as the event to signal might not be there (anymore). */
471 signalWaitEventInternal(pCbCtx, rcGuest, NULL /* pPayload */);
472 return VINF_SUCCESS; /* Report to the guest. */
473 }
474
475 AssertMsg(mObjectID == VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCbCtx->uContextID),
476 ("File ID %RU32 does not match object ID %RU32\n", mObjectID,
477 VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCbCtx->uContextID)));
478
479 int rc = VERR_NOT_SUPPORTED; /* Play safe by default. */
480
481 switch (dataCb.uType)
482 {
483 case GUEST_FILE_NOTIFYTYPE_ERROR:
484 {
485 rc = i_setFileStatus(FileStatus_Error, rcGuest);
486 break;
487 }
488
489 case GUEST_FILE_NOTIFYTYPE_OPEN:
490 {
491 if (pSvcCbData->mParms == 4)
492 {
493 rc = HGCMSvcGetU32(&pSvcCbData->mpaParms[idx++], &dataCb.u.open.uHandle);
494 if (RT_FAILURE(rc))
495 break;
496
497 /* Set the process status. */
498 rc = i_setFileStatus(FileStatus_Open, rcGuest);
499 }
500 break;
501 }
502
503 case GUEST_FILE_NOTIFYTYPE_CLOSE:
504 {
505 rc = i_setFileStatus(FileStatus_Closed, rcGuest);
506 break;
507 }
508
509 case GUEST_FILE_NOTIFYTYPE_READ:
510 {
511 if (pSvcCbData->mParms == 4)
512 {
513 rc = HGCMSvcGetPv(&pSvcCbData->mpaParms[idx++], &dataCb.u.read.pvData,
514 &dataCb.u.read.cbData);
515 if (RT_FAILURE(rc))
516 break;
517
518 const uint32_t cbRead = dataCb.u.read.cbData;
519
520 Log3ThisFunc(("cbRead=%RU32\n", cbRead));
521
522 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
523
524 mData.mOffCurrent += cbRead; /* Bogus for readAt, which is why we've got GUEST_FILE_NOTIFYTYPE_READ_OFFSET. */
525
526 alock.release();
527
528 com::SafeArray<BYTE> data((size_t)cbRead);
529 data.initFrom((BYTE *)dataCb.u.read.pvData, cbRead);
530
531 ::FireGuestFileReadEvent(mEventSource, mSession, this, mData.mOffCurrent, cbRead, ComSafeArrayAsInParam(data));
532 }
533 break;
534 }
535
536 case GUEST_FILE_NOTIFYTYPE_READ_OFFSET:
537 {
538 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 5, ("mParms=%u\n", pSvcCbData->mParms),
539 rc = VERR_WRONG_PARAMETER_COUNT);
540 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_PTR,
541 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
542 rc = VERR_WRONG_PARAMETER_TYPE);
543 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx + 1].type == VBOX_HGCM_SVC_PARM_64BIT,
544 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
545 rc = VERR_WRONG_PARAMETER_TYPE);
546 BYTE const * const pbData = (BYTE const *)pSvcCbData->mpaParms[idx].u.pointer.addr;
547 uint32_t const cbRead = pSvcCbData->mpaParms[idx].u.pointer.size;
548 int64_t offNew = (int64_t)pSvcCbData->mpaParms[idx + 1].u.uint64;
549 Log3ThisFunc(("cbRead=%RU32 offNew=%RI64 (%#RX64)\n", cbRead, offNew, offNew));
550
551 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
552 if (offNew < 0) /* non-seekable */
553 offNew = mData.mOffCurrent + cbRead;
554 mData.mOffCurrent = offNew;
555 alock.release();
556
557 try
558 {
559 com::SafeArray<BYTE> data((size_t)cbRead);
560 data.initFrom(pbData, cbRead);
561 ::FireGuestFileReadEvent(mEventSource, mSession, this, offNew, cbRead, ComSafeArrayAsInParam(data));
562 rc = VINF_SUCCESS;
563 }
564 catch (std::bad_alloc &)
565 {
566 rc = VERR_NO_MEMORY;
567 }
568 break;
569 }
570
571 case GUEST_FILE_NOTIFYTYPE_WRITE:
572 {
573 if (pSvcCbData->mParms == 4)
574 {
575 rc = HGCMSvcGetU32(&pSvcCbData->mpaParms[idx++], &dataCb.u.write.cbWritten);
576 if (RT_FAILURE(rc))
577 break;
578
579 const uint32_t cbWritten = dataCb.u.write.cbWritten;
580
581 Log3ThisFunc(("cbWritten=%RU32\n", cbWritten));
582
583 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
584
585 mData.mOffCurrent += cbWritten; /* Bogus for writeAt and append mode, thus GUEST_FILE_NOTIFYTYPE_WRITE_OFFSET. */
586
587 alock.release();
588
589 ::FireGuestFileWriteEvent(mEventSource, mSession, this, mData.mOffCurrent, cbWritten);
590 }
591 break;
592 }
593
594 case GUEST_FILE_NOTIFYTYPE_WRITE_OFFSET:
595 {
596 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 5, ("mParms=%u\n", pSvcCbData->mParms),
597 rc = VERR_WRONG_PARAMETER_COUNT);
598 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_32BIT,
599 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
600 rc = VERR_WRONG_PARAMETER_TYPE);
601 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx + 1].type == VBOX_HGCM_SVC_PARM_64BIT,
602 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
603 rc = VERR_WRONG_PARAMETER_TYPE);
604 uint32_t const cbWritten = pSvcCbData->mpaParms[idx].u.uint32;
605 int64_t offNew = (int64_t)pSvcCbData->mpaParms[idx + 1].u.uint64;
606 Log3ThisFunc(("cbWritten=%RU32 offNew=%RI64 (%#RX64)\n", cbWritten, offNew, offNew));
607
608 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
609 if (offNew < 0) /* non-seekable */
610 offNew = mData.mOffCurrent + cbWritten;
611 mData.mOffCurrent = offNew;
612 alock.release();
613
614 HRESULT hrc2 = ::FireGuestFileWriteEvent(mEventSource, mSession, this, offNew, cbWritten);
615 rc = SUCCEEDED(hrc2) ? VINF_SUCCESS : Global::vboxStatusCodeFromCOM(hrc2);
616 break;
617 }
618
619 case GUEST_FILE_NOTIFYTYPE_SEEK:
620 {
621 if (pSvcCbData->mParms == 4)
622 {
623 rc = HGCMSvcGetU64(&pSvcCbData->mpaParms[idx++], &dataCb.u.seek.uOffActual);
624 if (RT_FAILURE(rc))
625 break;
626
627 Log3ThisFunc(("uOffActual=%RU64\n", dataCb.u.seek.uOffActual));
628
629 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
630
631 mData.mOffCurrent = dataCb.u.seek.uOffActual;
632
633 alock.release();
634
635 ::FireGuestFileOffsetChangedEvent(mEventSource, mSession, this, dataCb.u.seek.uOffActual, 0 /* Processed */);
636 }
637 break;
638 }
639
640 case GUEST_FILE_NOTIFYTYPE_TELL:
641 /* We don't issue any HOST_MSG_FILE_TELL, so we shouldn't get these notifications! */
642 AssertFailed();
643 break;
644
645 case GUEST_FILE_NOTIFYTYPE_SET_SIZE:
646 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 4, ("mParms=%u\n", pSvcCbData->mParms),
647 rc = VERR_WRONG_PARAMETER_COUNT);
648 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_64BIT,
649 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
650 rc = VERR_WRONG_PARAMETER_TYPE);
651 dataCb.u.SetSize.cbSize = pSvcCbData->mpaParms[idx].u.uint64;
652 Log3ThisFunc(("cbSize=%RU64\n", dataCb.u.SetSize.cbSize));
653
654 ::FireGuestFileSizeChangedEvent(mEventSource, mSession, this, dataCb.u.SetSize.cbSize);
655 rc = VINF_SUCCESS;
656 break;
657
658 default:
659 break;
660 }
661
662 if (RT_SUCCESS(rc))
663 {
664 try
665 {
666 GuestWaitEventPayload payload(dataCb.uType, &dataCb, sizeof(dataCb));
667
668 /* Ignore rc, as the event to signal might not be there (anymore). */
669 signalWaitEventInternal(pCbCtx, rcGuest, &payload);
670 }
671 catch (int rcEx) /* Thrown by GuestWaitEventPayload constructor. */
672 {
673 rc = rcEx;
674 }
675 }
676
677 LogFlowThisFunc(("uType=%RU32, rcGuest=%Rrc, rc=%Rrc\n", dataCb.uType, rcGuest, rc));
678 return rc;
679}
680
681int GuestFile::i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
682{
683 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
684 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
685
686 int vrc = i_setFileStatus(FileStatus_Down, VINF_SUCCESS);
687
688 LogFlowFuncLeaveRC(vrc);
689 return vrc;
690}
691
692/**
693 * @copydoc GuestObject::i_onUnregister
694 */
695int GuestFile::i_onUnregister(void)
696{
697 LogFlowThisFuncEnter();
698
699 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
700
701 int vrc = VINF_SUCCESS;
702
703 /*
704 * Note: The event source stuff holds references to this object,
705 * so make sure that this is cleaned up *before* calling uninit().
706 */
707 if (!mEventSource.isNull())
708 {
709 mEventSource->UnregisterListener(mLocalListener);
710
711 mLocalListener.setNull();
712 unconst(mEventSource).setNull();
713 }
714
715 LogFlowFuncLeaveRC(vrc);
716 return vrc;
717}
718
719/**
720 * @copydoc GuestObject::i_onSessionStatusChange
721 */
722int GuestFile::i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus)
723{
724 LogFlowThisFuncEnter();
725
726 int vrc = VINF_SUCCESS;
727
728 /* If the session now is in a terminated state, set the file status
729 * to "down", as there is not much else we can do now. */
730 if (GuestSession::i_isTerminated(enmSessionStatus))
731 vrc = i_setFileStatus(FileStatus_Down, 0 /* fileRc, ignored */);
732
733 LogFlowFuncLeaveRC(vrc);
734 return vrc;
735}
736
737int GuestFile::i_openFile(uint32_t uTimeoutMS, int *prcGuest)
738{
739 AssertReturn(mData.mOpenInfo.mFilename.isNotEmpty(), VERR_INVALID_PARAMETER);
740
741 LogFlowThisFuncEnter();
742
743 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
744
745 LogFlowThisFunc(("strFile=%s, enmAccessMode=%d, enmOpenAction=%d, uCreationMode=%o, mfOpenEx=%#x\n",
746 mData.mOpenInfo.mFilename.c_str(), mData.mOpenInfo.mAccessMode, mData.mOpenInfo.mOpenAction,
747 mData.mOpenInfo.mCreationMode, mData.mOpenInfo.mfOpenEx));
748
749 /* Validate and translate open action. */
750 const char *pszOpenAction = NULL;
751 switch (mData.mOpenInfo.mOpenAction)
752 {
753 case FileOpenAction_OpenExisting: pszOpenAction = "oe"; break;
754 case FileOpenAction_OpenOrCreate: pszOpenAction = "oc"; break;
755 case FileOpenAction_CreateNew: pszOpenAction = "ce"; break;
756 case FileOpenAction_CreateOrReplace: pszOpenAction = "ca"; break;
757 case FileOpenAction_OpenExistingTruncated: pszOpenAction = "ot"; break;
758 case FileOpenAction_AppendOrCreate:
759 pszOpenAction = "oa"; /** @todo get rid of this one and implement AppendOnly/AppendRead. */
760 break;
761 default:
762 return VERR_INVALID_PARAMETER;
763 }
764
765 /* Validate and translate access mode. */
766 const char *pszAccessMode = NULL;
767 switch (mData.mOpenInfo.mAccessMode)
768 {
769 case FileAccessMode_ReadOnly: pszAccessMode = "r"; break;
770 case FileAccessMode_WriteOnly: pszAccessMode = "w"; break;
771 case FileAccessMode_ReadWrite: pszAccessMode = "r+"; break;
772 case FileAccessMode_AppendOnly: pszAccessMode = "a"; break;
773 case FileAccessMode_AppendRead: pszAccessMode = "a+"; break;
774 default: return VERR_INVALID_PARAMETER;
775 }
776
777 /* Validate and translate sharing mode. */
778 const char *pszSharingMode = NULL;
779 switch (mData.mOpenInfo.mSharingMode)
780 {
781 case FileSharingMode_All: pszSharingMode = ""; break;
782 case FileSharingMode_Read: RT_FALL_THRU();
783 case FileSharingMode_Write: RT_FALL_THRU();
784 case FileSharingMode_ReadWrite: RT_FALL_THRU();
785 case FileSharingMode_Delete: RT_FALL_THRU();
786 case FileSharingMode_ReadDelete: RT_FALL_THRU();
787 case FileSharingMode_WriteDelete: return VERR_NOT_IMPLEMENTED;
788 default: return VERR_INVALID_PARAMETER;
789 }
790
791 int vrc;
792
793 GuestWaitEvent *pEvent = NULL;
794 GuestEventTypes eventTypes;
795 try
796 {
797 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
798
799 vrc = registerWaitEvent(eventTypes, &pEvent);
800 }
801 catch (std::bad_alloc &)
802 {
803 vrc = VERR_NO_MEMORY;
804 }
805
806 if (RT_FAILURE(vrc))
807 return vrc;
808
809 /* Prepare HGCM call. */
810 VBOXHGCMSVCPARM paParms[8];
811 int i = 0;
812 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
813 HGCMSvcSetPv(&paParms[i++], (void*)mData.mOpenInfo.mFilename.c_str(),
814 (ULONG)mData.mOpenInfo.mFilename.length() + 1);
815 HGCMSvcSetStr(&paParms[i++], pszAccessMode);
816 HGCMSvcSetStr(&paParms[i++], pszOpenAction);
817 HGCMSvcSetStr(&paParms[i++], pszSharingMode);
818 HGCMSvcSetU32(&paParms[i++], mData.mOpenInfo.mCreationMode);
819 HGCMSvcSetU64(&paParms[i++], 0 /*unused offset*/);
820 /** @todo Next protocol version: add flags, replace strings, remove initial offset. */
821
822 alock.release(); /* Drop write lock before sending. */
823
824 vrc = sendMessage(HOST_MSG_FILE_OPEN, i, paParms);
825 if (RT_SUCCESS(vrc))
826 vrc = i_waitForStatusChange(pEvent, uTimeoutMS, NULL /* FileStatus */, prcGuest);
827
828 unregisterWaitEvent(pEvent);
829
830 LogFlowFuncLeaveRC(vrc);
831 return vrc;
832}
833
834int GuestFile::i_queryInfo(GuestFsObjData &objData, int *prcGuest)
835{
836 AssertPtr(mSession);
837 return mSession->i_fsQueryInfo(mData.mOpenInfo.mFilename, FALSE /* fFollowSymlinks */, objData, prcGuest);
838}
839
840int GuestFile::i_readData(uint32_t uSize, uint32_t uTimeoutMS,
841 void* pvData, uint32_t cbData, uint32_t* pcbRead)
842{
843 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
844 AssertReturn(cbData, VERR_INVALID_PARAMETER);
845
846 LogFlowThisFunc(("uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
847 uSize, uTimeoutMS, pvData, cbData));
848
849 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
850
851 int vrc;
852
853 GuestWaitEvent *pEvent = NULL;
854 GuestEventTypes eventTypes;
855 try
856 {
857 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
858 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
859
860 vrc = registerWaitEvent(eventTypes, &pEvent);
861 }
862 catch (std::bad_alloc &)
863 {
864 vrc = VERR_NO_MEMORY;
865 }
866
867 if (RT_FAILURE(vrc))
868 return vrc;
869
870 /* Prepare HGCM call. */
871 VBOXHGCMSVCPARM paParms[4];
872 int i = 0;
873 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
874 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
875 HGCMSvcSetU32(&paParms[i++], uSize /* Size (in bytes) to read */);
876
877 alock.release(); /* Drop write lock before sending. */
878
879 vrc = sendMessage(HOST_MSG_FILE_READ, i, paParms);
880 if (RT_SUCCESS(vrc))
881 {
882 uint32_t cbRead = 0;
883 vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
884 if (RT_SUCCESS(vrc))
885 {
886 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
887 if (pcbRead)
888 *pcbRead = cbRead;
889 }
890 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
891 {
892 vrc = pEvent->GetGuestError();
893 }
894 }
895
896 unregisterWaitEvent(pEvent);
897
898 LogFlowFuncLeaveRC(vrc);
899 return vrc;
900}
901
902int GuestFile::i_readDataAt(uint64_t uOffset, uint32_t uSize, uint32_t uTimeoutMS,
903 void* pvData, size_t cbData, size_t* pcbRead)
904{
905 LogFlowThisFunc(("uOffset=%RU64, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
906 uOffset, uSize, uTimeoutMS, pvData, cbData));
907
908 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
909
910 int vrc;
911
912 GuestWaitEvent *pEvent = NULL;
913 GuestEventTypes eventTypes;
914 try
915 {
916 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
917 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
918
919 vrc = registerWaitEvent(eventTypes, &pEvent);
920 }
921 catch (std::bad_alloc &)
922 {
923 vrc = VERR_NO_MEMORY;
924 }
925
926 if (RT_FAILURE(vrc))
927 return vrc;
928
929 /* Prepare HGCM call. */
930 VBOXHGCMSVCPARM paParms[4];
931 int i = 0;
932 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
933 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
934 HGCMSvcSetU64(&paParms[i++], uOffset /* Offset (in bytes) to start reading */);
935 HGCMSvcSetU32(&paParms[i++], uSize /* Size (in bytes) to read */);
936
937 alock.release(); /* Drop write lock before sending. */
938
939 vrc = sendMessage(HOST_MSG_FILE_READ_AT, i, paParms);
940 if (RT_SUCCESS(vrc))
941 {
942 uint32_t cbRead = 0;
943 vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
944 if (RT_SUCCESS(vrc))
945 {
946 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
947
948 if (pcbRead)
949 *pcbRead = cbRead;
950 }
951 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
952 {
953 vrc = pEvent->GetGuestError();
954 }
955 }
956
957 unregisterWaitEvent(pEvent);
958
959 LogFlowFuncLeaveRC(vrc);
960 return vrc;
961}
962
963int GuestFile::i_seekAt(int64_t iOffset, GUEST_FILE_SEEKTYPE eSeekType,
964 uint32_t uTimeoutMS, uint64_t *puOffset)
965{
966 LogFlowThisFunc(("iOffset=%RI64, uTimeoutMS=%RU32\n",
967 iOffset, uTimeoutMS));
968
969 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
970
971 int vrc;
972
973 GuestWaitEvent *pEvent = NULL;
974 GuestEventTypes eventTypes;
975 try
976 {
977 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
978 eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
979
980 vrc = registerWaitEvent(eventTypes, &pEvent);
981 }
982 catch (std::bad_alloc &)
983 {
984 vrc = VERR_NO_MEMORY;
985 }
986
987 if (RT_FAILURE(vrc))
988 return vrc;
989
990 /* Prepare HGCM call. */
991 VBOXHGCMSVCPARM paParms[4];
992 int i = 0;
993 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
994 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
995 HGCMSvcSetU32(&paParms[i++], eSeekType /* Seek method */);
996 /** @todo uint64_t vs. int64_t! */
997 HGCMSvcSetU64(&paParms[i++], (uint64_t)iOffset /* Offset (in bytes) to start reading */);
998
999 alock.release(); /* Drop write lock before sending. */
1000
1001 vrc = sendMessage(HOST_MSG_FILE_SEEK, i, paParms);
1002 if (RT_SUCCESS(vrc))
1003 {
1004 uint64_t uOffset;
1005 vrc = i_waitForOffsetChange(pEvent, uTimeoutMS, &uOffset);
1006 if (RT_SUCCESS(vrc))
1007 {
1008 LogFlowThisFunc(("uOffset=%RU64\n", uOffset));
1009
1010 if (puOffset)
1011 *puOffset = uOffset;
1012 }
1013 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
1014 {
1015 vrc = pEvent->GetGuestError();
1016 }
1017 }
1018
1019 unregisterWaitEvent(pEvent);
1020
1021 LogFlowFuncLeaveRC(vrc);
1022 return vrc;
1023}
1024
1025int GuestFile::i_setFileStatus(FileStatus_T fileStatus, int fileRc)
1026{
1027 LogFlowThisFuncEnter();
1028
1029 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1030
1031 LogFlowThisFunc(("oldStatus=%RU32, newStatus=%RU32, fileRc=%Rrc\n",
1032 mData.mStatus, fileStatus, fileRc));
1033
1034#ifdef VBOX_STRICT
1035 if (fileStatus == FileStatus_Error)
1036 {
1037 AssertMsg(RT_FAILURE(fileRc), ("Guest rc must be an error (%Rrc)\n", fileRc));
1038 }
1039 else
1040 AssertMsg(RT_SUCCESS(fileRc), ("Guest rc must not be an error (%Rrc)\n", fileRc));
1041#endif
1042
1043 if (mData.mStatus != fileStatus)
1044 {
1045 mData.mStatus = fileStatus;
1046 mData.mLastError = fileRc;
1047
1048 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
1049 HRESULT hr = errorInfo.createObject();
1050 ComAssertComRC(hr);
1051 if (RT_FAILURE(fileRc))
1052 {
1053 hr = errorInfo->initEx(VBOX_E_IPRT_ERROR, fileRc,
1054 COM_IIDOF(IGuestFile), getComponentName(),
1055 i_guestErrorToString(fileRc, mData.mOpenInfo.mFilename.c_str()));
1056 ComAssertComRC(hr);
1057 }
1058
1059 alock.release(); /* Release lock before firing off event. */
1060
1061 ::FireGuestFileStateChangedEvent(mEventSource, mSession, this, fileStatus, errorInfo);
1062 }
1063
1064 return VINF_SUCCESS;
1065}
1066
1067int GuestFile::i_waitForOffsetChange(GuestWaitEvent *pEvent,
1068 uint32_t uTimeoutMS, uint64_t *puOffset)
1069{
1070 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1071
1072 VBoxEventType_T evtType;
1073 ComPtr<IEvent> pIEvent;
1074 int vrc = waitForEvent(pEvent, uTimeoutMS,
1075 &evtType, pIEvent.asOutParam());
1076 if (RT_SUCCESS(vrc))
1077 {
1078 if (evtType == VBoxEventType_OnGuestFileOffsetChanged)
1079 {
1080 if (puOffset)
1081 {
1082 ComPtr<IGuestFileOffsetChangedEvent> pFileEvent = pIEvent;
1083 Assert(!pFileEvent.isNull());
1084
1085 HRESULT hr = pFileEvent->COMGETTER(Offset)((LONG64*)puOffset);
1086 ComAssertComRC(hr);
1087 }
1088 }
1089 else
1090 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1091 }
1092
1093 return vrc;
1094}
1095
1096int GuestFile::i_waitForRead(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1097 void *pvData, size_t cbData, uint32_t *pcbRead)
1098{
1099 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1100
1101 VBoxEventType_T evtType;
1102 ComPtr<IEvent> pIEvent;
1103 int vrc = waitForEvent(pEvent, uTimeoutMS,
1104 &evtType, pIEvent.asOutParam());
1105 if (RT_SUCCESS(vrc))
1106 {
1107 if (evtType == VBoxEventType_OnGuestFileRead)
1108 {
1109 vrc = VINF_SUCCESS;
1110
1111 ComPtr<IGuestFileReadEvent> pFileEvent = pIEvent;
1112 Assert(!pFileEvent.isNull());
1113
1114 if (pvData)
1115 {
1116 com::SafeArray <BYTE> data;
1117 HRESULT hrc1 = pFileEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
1118 ComAssertComRC(hrc1);
1119 const size_t cbRead = data.size();
1120 if (cbRead)
1121 {
1122 if (cbRead <= cbData)
1123 memcpy(pvData, data.raw(), cbRead);
1124 else
1125 vrc = VERR_BUFFER_OVERFLOW;
1126 }
1127 /* else: used to be VERR_NO_DATA, but that messes stuff up. */
1128
1129 if (pcbRead)
1130 {
1131 *pcbRead = (uint32_t)cbRead;
1132 Assert(*pcbRead == cbRead);
1133 }
1134 }
1135 else if (pcbRead)
1136 {
1137 *pcbRead = 0;
1138 HRESULT hrc2 = pFileEvent->COMGETTER(Processed)((ULONG *)pcbRead);
1139 ComAssertComRC(hrc2); NOREF(hrc2);
1140 }
1141 }
1142 else
1143 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1144 }
1145
1146 return vrc;
1147}
1148
1149/**
1150 * Undocumented, use with great care.
1151 *
1152 * @note Similar code in GuestProcess::i_waitForStatusChange() and
1153 * GuestSession::i_waitForStatusChange().
1154 */
1155int GuestFile::i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1156 FileStatus_T *pFileStatus, int *prcGuest)
1157{
1158 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1159 /* pFileStatus is optional. */
1160
1161 VBoxEventType_T evtType;
1162 ComPtr<IEvent> pIEvent;
1163 int vrc = waitForEvent(pEvent, uTimeoutMS,
1164 &evtType, pIEvent.asOutParam());
1165 if (RT_SUCCESS(vrc))
1166 {
1167 Assert(evtType == VBoxEventType_OnGuestFileStateChanged);
1168 ComPtr<IGuestFileStateChangedEvent> pFileEvent = pIEvent;
1169 Assert(!pFileEvent.isNull());
1170
1171 HRESULT hr;
1172 if (pFileStatus)
1173 {
1174 hr = pFileEvent->COMGETTER(Status)(pFileStatus);
1175 ComAssertComRC(hr);
1176 }
1177
1178 ComPtr<IVirtualBoxErrorInfo> errorInfo;
1179 hr = pFileEvent->COMGETTER(Error)(errorInfo.asOutParam());
1180 ComAssertComRC(hr);
1181
1182 LONG lGuestRc;
1183 hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
1184 ComAssertComRC(hr);
1185
1186 LogFlowThisFunc(("resultDetail=%RI32 (%Rrc)\n",
1187 lGuestRc, lGuestRc));
1188
1189 if (RT_FAILURE((int)lGuestRc))
1190 vrc = VERR_GSTCTL_GUEST_ERROR;
1191
1192 if (prcGuest)
1193 *prcGuest = (int)lGuestRc;
1194 }
1195 /* waitForEvent may also return VERR_GSTCTL_GUEST_ERROR like we do above, so make prcGuest is set. */
1196 /** @todo r=bird: Andy, you seem to have forgotten this scenario. Showed up occasionally when
1197 * using the wrong password with a copyto command in a debug build on windows, error info
1198 * contained "Unknown Status -858993460 (0xcccccccc)". As you know windows fills the stack frames
1199 * with 0xcccccccc in debug builds to highlight use of uninitialized data, so that's what happened
1200 * here. It's actually good you didn't initialize lGuest, as it would be heck to find otherwise.
1201 *
1202 * I'm still not very impressed with the error managment or the usuefullness of the documentation
1203 * in this code, though the latter is getting better! */
1204 else if (vrc == VERR_GSTCTL_GUEST_ERROR && prcGuest)
1205 *prcGuest = pEvent->GuestResult();
1206 Assert(vrc != VERR_GSTCTL_GUEST_ERROR || !prcGuest || *prcGuest != (int)0xcccccccc);
1207
1208 return vrc;
1209}
1210
1211int GuestFile::i_waitForWrite(GuestWaitEvent *pEvent,
1212 uint32_t uTimeoutMS, uint32_t *pcbWritten)
1213{
1214 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1215
1216 VBoxEventType_T evtType;
1217 ComPtr<IEvent> pIEvent;
1218 int vrc = waitForEvent(pEvent, uTimeoutMS,
1219 &evtType, pIEvent.asOutParam());
1220 if (RT_SUCCESS(vrc))
1221 {
1222 if (evtType == VBoxEventType_OnGuestFileWrite)
1223 {
1224 if (pcbWritten)
1225 {
1226 ComPtr<IGuestFileWriteEvent> pFileEvent = pIEvent;
1227 Assert(!pFileEvent.isNull());
1228
1229 HRESULT hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbWritten);
1230 ComAssertComRC(hr);
1231 }
1232 }
1233 else
1234 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1235 }
1236
1237 return vrc;
1238}
1239
1240int GuestFile::i_writeData(uint32_t uTimeoutMS, const void *pvData, uint32_t cbData,
1241 uint32_t *pcbWritten)
1242{
1243 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1244 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1245
1246 LogFlowThisFunc(("uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1247 uTimeoutMS, pvData, cbData));
1248
1249 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1250
1251 int vrc;
1252
1253 GuestWaitEvent *pEvent = NULL;
1254 GuestEventTypes eventTypes;
1255 try
1256 {
1257 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1258 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1259
1260 vrc = registerWaitEvent(eventTypes, &pEvent);
1261 }
1262 catch (std::bad_alloc &)
1263 {
1264 vrc = VERR_NO_MEMORY;
1265 }
1266
1267 if (RT_FAILURE(vrc))
1268 return vrc;
1269
1270 /* Prepare HGCM call. */
1271 VBOXHGCMSVCPARM paParms[8];
1272 int i = 0;
1273 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1274 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1275 HGCMSvcSetU32(&paParms[i++], cbData /* Size (in bytes) to write */);
1276 HGCMSvcSetPv (&paParms[i++], unconst(pvData), cbData);
1277
1278 alock.release(); /* Drop write lock before sending. */
1279
1280 vrc = sendMessage(HOST_MSG_FILE_WRITE, i, paParms);
1281 if (RT_SUCCESS(vrc))
1282 {
1283 uint32_t cbWritten = 0;
1284 vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1285 if (RT_SUCCESS(vrc))
1286 {
1287 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1288 if (pcbWritten)
1289 *pcbWritten = cbWritten;
1290 }
1291 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
1292 {
1293 vrc = pEvent->GetGuestError();
1294 }
1295 }
1296
1297 unregisterWaitEvent(pEvent);
1298
1299 LogFlowFuncLeaveRC(vrc);
1300 return vrc;
1301}
1302
1303int GuestFile::i_writeDataAt(uint64_t uOffset, uint32_t uTimeoutMS,
1304 const void *pvData, uint32_t cbData, uint32_t *pcbWritten)
1305{
1306 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1307 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1308
1309 LogFlowThisFunc(("uOffset=%RU64, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1310 uOffset, uTimeoutMS, pvData, cbData));
1311
1312 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1313
1314 int vrc;
1315
1316 GuestWaitEvent *pEvent = NULL;
1317 GuestEventTypes eventTypes;
1318 try
1319 {
1320 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1321 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1322
1323 vrc = registerWaitEvent(eventTypes, &pEvent);
1324 }
1325 catch (std::bad_alloc &)
1326 {
1327 vrc = VERR_NO_MEMORY;
1328 }
1329
1330 if (RT_FAILURE(vrc))
1331 return vrc;
1332
1333 /* Prepare HGCM call. */
1334 VBOXHGCMSVCPARM paParms[8];
1335 int i = 0;
1336 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1337 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1338 HGCMSvcSetU64(&paParms[i++], uOffset /* Offset where to starting writing */);
1339 HGCMSvcSetU32(&paParms[i++], cbData /* Size (in bytes) to write */);
1340 HGCMSvcSetPv (&paParms[i++], unconst(pvData), cbData);
1341
1342 alock.release(); /* Drop write lock before sending. */
1343
1344 vrc = sendMessage(HOST_MSG_FILE_WRITE_AT, i, paParms);
1345 if (RT_SUCCESS(vrc))
1346 {
1347 uint32_t cbWritten = 0;
1348 vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1349 if (RT_SUCCESS(vrc))
1350 {
1351 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1352 if (pcbWritten)
1353 *pcbWritten = cbWritten;
1354 }
1355 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
1356 {
1357 vrc = pEvent->GetGuestError();
1358 }
1359 }
1360
1361 unregisterWaitEvent(pEvent);
1362
1363 LogFlowFuncLeaveRC(vrc);
1364 return vrc;
1365}
1366
1367// Wrapped IGuestFile methods
1368/////////////////////////////////////////////////////////////////////////////
1369HRESULT GuestFile::close()
1370{
1371 AutoCaller autoCaller(this);
1372 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1373
1374 LogFlowThisFuncEnter();
1375
1376 /* Close file on guest. */
1377 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1378 int vrc = i_closeFile(&rcGuest);
1379 /* On failure don't return here, instead do all the cleanup
1380 * work first and then return an error. */
1381
1382 AssertPtr(mSession);
1383 int vrc2 = mSession->i_fileUnregister(this);
1384 if (RT_SUCCESS(vrc))
1385 vrc = vrc2;
1386
1387 if (RT_FAILURE(vrc))
1388 {
1389 if (vrc == VERR_GSTCTL_GUEST_ERROR)
1390 return setErrorExternal(this, tr("Closing guest file failed"),
1391 GuestErrorInfo(GuestErrorInfo::Type_File, rcGuest, mData.mOpenInfo.mFilename.c_str()));
1392 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Closing guest file \"%s\" failed with %Rrc\n"),
1393 mData.mOpenInfo.mFilename.c_str(), vrc);
1394 }
1395
1396 LogFlowThisFunc(("Returning S_OK / vrc=%Rrc\n", vrc));
1397 return S_OK;
1398}
1399
1400HRESULT GuestFile::queryInfo(ComPtr<IFsObjInfo> &aObjInfo)
1401{
1402 AutoCaller autoCaller(this);
1403 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1404
1405 LogFlowThisFuncEnter();
1406
1407 HRESULT hr = S_OK;
1408
1409 GuestFsObjData fsObjData;
1410 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1411 int vrc = i_queryInfo(fsObjData, &rcGuest);
1412 if (RT_SUCCESS(vrc))
1413 {
1414 ComObjPtr<GuestFsObjInfo> ptrFsObjInfo;
1415 hr = ptrFsObjInfo.createObject();
1416 if (SUCCEEDED(hr))
1417 {
1418 vrc = ptrFsObjInfo->init(fsObjData);
1419 if (RT_SUCCESS(vrc))
1420 hr = ptrFsObjInfo.queryInterfaceTo(aObjInfo.asOutParam());
1421 else
1422 hr = setErrorVrc(vrc,
1423 tr("Initialization of guest file object for \"%s\" failed: %Rrc"),
1424 mData.mOpenInfo.mFilename.c_str(), vrc);
1425 }
1426 }
1427 else
1428 {
1429 if (GuestProcess::i_isGuestError(vrc))
1430 hr = setErrorExternal(this, tr("Querying guest file information failed"),
1431 GuestErrorInfo(GuestErrorInfo::Type_ToolStat, rcGuest, mData.mOpenInfo.mFilename.c_str()));
1432 else
1433 hr = setErrorVrc(vrc,
1434 tr("Querying guest file information for \"%s\" failed: %Rrc"), mData.mOpenInfo.mFilename.c_str(), vrc);
1435 }
1436
1437 LogFlowFuncLeaveRC(vrc);
1438 return hr;
1439}
1440
1441HRESULT GuestFile::querySize(LONG64 *aSize)
1442{
1443 AutoCaller autoCaller(this);
1444 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1445
1446 LogFlowThisFuncEnter();
1447
1448 HRESULT hr = S_OK;
1449
1450 GuestFsObjData fsObjData;
1451 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1452 int vrc = i_queryInfo(fsObjData, &rcGuest);
1453 if (RT_SUCCESS(vrc))
1454 {
1455 *aSize = fsObjData.mObjectSize;
1456 }
1457 else
1458 {
1459 if (GuestProcess::i_isGuestError(vrc))
1460 hr = setErrorExternal(this, tr("Querying guest file size failed"),
1461 GuestErrorInfo(GuestErrorInfo::Type_ToolStat, rcGuest, mData.mOpenInfo.mFilename.c_str()));
1462 else
1463 hr = setErrorVrc(vrc, tr("Querying guest file size for \"%s\" failed: %Rrc"), mData.mOpenInfo.mFilename.c_str(), vrc);
1464 }
1465
1466 LogFlowFuncLeaveRC(vrc);
1467 return hr;
1468}
1469
1470HRESULT GuestFile::read(ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
1471{
1472 AutoCaller autoCaller(this);
1473 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1474
1475 if (aToRead == 0)
1476 return setError(E_INVALIDARG, tr("The size to read is zero"));
1477
1478 LogFlowThisFuncEnter();
1479
1480 /* Cap the read at 1MiB because that's all the guest will return anyway. */
1481 if (aToRead > _1M)
1482 aToRead = _1M;
1483
1484 aData.resize(aToRead);
1485
1486 HRESULT hr = S_OK;
1487
1488 uint32_t cbRead;
1489 int vrc = i_readData(aToRead, aTimeoutMS,
1490 &aData.front(), aToRead, &cbRead);
1491
1492 if (RT_SUCCESS(vrc))
1493 {
1494 if (aData.size() != cbRead)
1495 aData.resize(cbRead);
1496 }
1497 else
1498 {
1499 aData.resize(0);
1500
1501 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading from file \"%s\" failed: %Rrc"),
1502 mData.mOpenInfo.mFilename.c_str(), vrc);
1503 }
1504
1505 LogFlowFuncLeaveRC(vrc);
1506 return hr;
1507}
1508
1509HRESULT GuestFile::readAt(LONG64 aOffset, ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
1510{
1511 AutoCaller autoCaller(this);
1512 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1513
1514 if (aToRead == 0)
1515 return setError(E_INVALIDARG, tr("The size to read for guest file \"%s\" is zero"), mData.mOpenInfo.mFilename.c_str());
1516
1517 LogFlowThisFuncEnter();
1518
1519 /* Cap the read at 1MiB because that's all the guest will return anyway. */
1520 if (aToRead > _1M)
1521 aToRead = _1M;
1522
1523 aData.resize(aToRead);
1524
1525 HRESULT hr = S_OK;
1526
1527 size_t cbRead;
1528 int vrc = i_readDataAt(aOffset, aToRead, aTimeoutMS,
1529 &aData.front(), aToRead, &cbRead);
1530 if (RT_SUCCESS(vrc))
1531 {
1532 if (aData.size() != cbRead)
1533 aData.resize(cbRead);
1534 }
1535 else
1536 {
1537 aData.resize(0);
1538
1539 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading from file \"%s\" (at offset %RU64) failed: %Rrc"),
1540 mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1541 }
1542
1543 LogFlowFuncLeaveRC(vrc);
1544 return hr;
1545}
1546
1547HRESULT GuestFile::seek(LONG64 aOffset, FileSeekOrigin_T aWhence, LONG64 *aNewOffset)
1548{
1549 AutoCaller autoCaller(this);
1550 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1551
1552 HRESULT hr = S_OK;
1553
1554 GUEST_FILE_SEEKTYPE eSeekType;
1555 switch (aWhence)
1556 {
1557 case FileSeekOrigin_Begin:
1558 eSeekType = GUEST_FILE_SEEKTYPE_BEGIN;
1559 break;
1560
1561 case FileSeekOrigin_Current:
1562 eSeekType = GUEST_FILE_SEEKTYPE_CURRENT;
1563 break;
1564
1565 case FileSeekOrigin_End:
1566 eSeekType = GUEST_FILE_SEEKTYPE_END;
1567 break;
1568
1569 default:
1570 return setError(E_INVALIDARG, tr("Invalid seek type for guest file \"%s\" specified"),
1571 mData.mOpenInfo.mFilename.c_str());
1572 }
1573
1574 LogFlowThisFuncEnter();
1575
1576 uint64_t uNewOffset;
1577 int vrc = i_seekAt(aOffset, eSeekType,
1578 30 * 1000 /* 30s timeout */, &uNewOffset);
1579 if (RT_SUCCESS(vrc))
1580 *aNewOffset = RT_MIN(uNewOffset, (uint64_t)INT64_MAX);
1581 else
1582 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Seeking file \"%s\" (to offset %RI64) failed: %Rrc"),
1583 mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1584
1585 LogFlowFuncLeaveRC(vrc);
1586 return hr;
1587}
1588
1589HRESULT GuestFile::setACL(const com::Utf8Str &aAcl, ULONG aMode)
1590{
1591 RT_NOREF(aAcl, aMode);
1592 ReturnComNotImplemented();
1593}
1594
1595HRESULT GuestFile::setSize(LONG64 aSize)
1596{
1597 LogFlowThisFuncEnter();
1598
1599 /*
1600 * Validate.
1601 */
1602 if (aSize < 0)
1603 return setError(E_INVALIDARG, tr("The size (%RI64) for guest file \"%s\" cannot be a negative value"),
1604 aSize, mData.mOpenInfo.mFilename.c_str());
1605
1606 /*
1607 * Register event callbacks.
1608 */
1609 int vrc;
1610 GuestWaitEvent *pWaitEvent = NULL;
1611 GuestEventTypes lstEventTypes;
1612 try
1613 {
1614 lstEventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1615 lstEventTypes.push_back(VBoxEventType_OnGuestFileSizeChanged);
1616 }
1617 catch (std::bad_alloc &)
1618 {
1619 return E_OUTOFMEMORY;
1620 }
1621
1622 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1623
1624 vrc = registerWaitEvent(lstEventTypes, &pWaitEvent);
1625 if (RT_SUCCESS(vrc))
1626 {
1627 /*
1628 * Send of the HGCM message.
1629 */
1630 VBOXHGCMSVCPARM aParms[3];
1631 HGCMSvcSetU32(&aParms[0], pWaitEvent->ContextID());
1632 HGCMSvcSetU32(&aParms[1], mObjectID /* File handle */);
1633 HGCMSvcSetU64(&aParms[2], aSize);
1634
1635 alock.release(); /* Drop write lock before sending. */
1636
1637 vrc = sendMessage(HOST_MSG_FILE_SET_SIZE, RT_ELEMENTS(aParms), aParms);
1638 if (RT_SUCCESS(vrc))
1639 {
1640 /*
1641 * Wait for the event.
1642 */
1643 VBoxEventType_T enmEvtType;
1644 ComPtr<IEvent> pIEvent;
1645 vrc = waitForEvent(pWaitEvent, RT_MS_1MIN / 2, &enmEvtType, pIEvent.asOutParam());
1646 if (RT_SUCCESS(vrc))
1647 {
1648 if (enmEvtType == VBoxEventType_OnGuestFileSizeChanged)
1649 vrc = VINF_SUCCESS;
1650 else
1651 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1652 }
1653 if (RT_FAILURE(vrc) && pWaitEvent->HasGuestError()) /* Return guest rc if available. */
1654 vrc = pWaitEvent->GetGuestError();
1655 }
1656
1657 /*
1658 * Unregister the wait event and deal with error reporting if needed.
1659 */
1660 unregisterWaitEvent(pWaitEvent);
1661 }
1662 HRESULT hrc;
1663 if (RT_SUCCESS(vrc))
1664 hrc = S_OK;
1665 else
1666 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Setting the guest file size of \"%s\" to %RU64 (%#RX64) bytes failed: %Rrc"),
1667 mData.mOpenInfo.mFilename.c_str(), aSize, aSize, vrc);
1668 LogFlowFuncLeaveRC(vrc);
1669 return hrc;
1670}
1671
1672HRESULT GuestFile::write(const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
1673{
1674 AutoCaller autoCaller(this);
1675 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1676
1677 if (aData.size() == 0)
1678 return setError(E_INVALIDARG, tr("No data to write specified"), mData.mOpenInfo.mFilename.c_str());
1679
1680 LogFlowThisFuncEnter();
1681
1682 HRESULT hr = S_OK;
1683
1684 const uint32_t cbData = (uint32_t)aData.size();
1685 const void *pvData = (void *)&aData.front();
1686 int vrc = i_writeData(aTimeoutMS, pvData, cbData, (uint32_t*)aWritten);
1687 if (RT_FAILURE(vrc))
1688 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Writing %zu bytes to guest file \"%s\" failed: %Rrc"),
1689 aData.size(), mData.mOpenInfo.mFilename.c_str(), vrc);
1690
1691 LogFlowFuncLeaveRC(vrc);
1692 return hr;
1693}
1694
1695HRESULT GuestFile::writeAt(LONG64 aOffset, const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
1696{
1697 AutoCaller autoCaller(this);
1698 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1699
1700 if (aData.size() == 0)
1701 return setError(E_INVALIDARG, tr("No data to write at for guest file \"%s\" specified"), mData.mOpenInfo.mFilename.c_str());
1702
1703 LogFlowThisFuncEnter();
1704
1705 HRESULT hr = S_OK;
1706
1707 const uint32_t cbData = (uint32_t)aData.size();
1708 const void *pvData = (void *)&aData.front();
1709 int vrc = i_writeDataAt(aOffset, aTimeoutMS, pvData, cbData, (uint32_t*)aWritten);
1710 if (RT_FAILURE(vrc))
1711 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Writing %zu bytes to file \"%s\" (at offset %RU64) failed: %Rrc"),
1712 aData.size(), mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1713
1714 LogFlowFuncLeaveRC(vrc);
1715 return hr;
1716}
1717
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