VirtualBox

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

Last change on this file since 85091 was 84865, checked in by vboxsync, 5 years ago

Guest Control/Main: Try to fix crashes. bugref:9320

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.0 KB
Line 
1/* $Id: GuestFileImpl.cpp 84865 2020-06-17 19:49:37Z 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,
532 cbRead, ComSafeArrayAsInParam(data));
533 }
534 break;
535 }
536
537 case GUEST_FILE_NOTIFYTYPE_READ_OFFSET:
538 {
539 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 5, ("mParms=%u\n", pSvcCbData->mParms),
540 rc = VERR_WRONG_PARAMETER_COUNT);
541 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_PTR,
542 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
543 rc = VERR_WRONG_PARAMETER_TYPE);
544 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx + 1].type == VBOX_HGCM_SVC_PARM_64BIT,
545 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
546 rc = VERR_WRONG_PARAMETER_TYPE);
547 BYTE const * const pbData = (BYTE const *)pSvcCbData->mpaParms[idx].u.pointer.addr;
548 uint32_t const cbRead = pSvcCbData->mpaParms[idx].u.pointer.size;
549 int64_t offNew = (int64_t)pSvcCbData->mpaParms[idx + 1].u.uint64;
550 Log3ThisFunc(("cbRead=%RU32 offNew=%RI64 (%#RX64)\n", cbRead, offNew, offNew));
551
552 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
553 if (offNew < 0) /* non-seekable */
554 offNew = mData.mOffCurrent + cbRead;
555 mData.mOffCurrent = offNew;
556 alock.release();
557
558 try
559 {
560 com::SafeArray<BYTE> data((size_t)cbRead);
561 data.initFrom(pbData, cbRead);
562 fireGuestFileReadEvent(mEventSource, mSession, this, offNew, cbRead, ComSafeArrayAsInParam(data));
563 rc = VINF_SUCCESS;
564 }
565 catch (std::bad_alloc &)
566 {
567 rc = VERR_NO_MEMORY;
568 }
569 break;
570 }
571
572 case GUEST_FILE_NOTIFYTYPE_WRITE:
573 {
574 if (pSvcCbData->mParms == 4)
575 {
576 rc = HGCMSvcGetU32(&pSvcCbData->mpaParms[idx++], &dataCb.u.write.cbWritten);
577 if (RT_FAILURE(rc))
578 break;
579
580 const uint32_t cbWritten = dataCb.u.write.cbWritten;
581
582 Log3ThisFunc(("cbWritten=%RU32\n", cbWritten));
583
584 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
585
586 mData.mOffCurrent += cbWritten; /* Bogus for writeAt and append mode, thus GUEST_FILE_NOTIFYTYPE_WRITE_OFFSET. */
587
588 alock.release();
589
590 fireGuestFileWriteEvent(mEventSource, mSession, this, mData.mOffCurrent, cbWritten);
591 }
592 break;
593 }
594
595 case GUEST_FILE_NOTIFYTYPE_WRITE_OFFSET:
596 {
597 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 5, ("mParms=%u\n", pSvcCbData->mParms),
598 rc = VERR_WRONG_PARAMETER_COUNT);
599 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_32BIT,
600 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
601 rc = VERR_WRONG_PARAMETER_TYPE);
602 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx + 1].type == VBOX_HGCM_SVC_PARM_64BIT,
603 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
604 rc = VERR_WRONG_PARAMETER_TYPE);
605 uint32_t const cbWritten = pSvcCbData->mpaParms[idx].u.uint32;
606 int64_t offNew = (int64_t)pSvcCbData->mpaParms[idx + 1].u.uint64;
607 Log3ThisFunc(("cbWritten=%RU32 offNew=%RI64 (%#RX64)\n", cbWritten, offNew, offNew));
608
609 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
610 if (offNew < 0) /* non-seekable */
611 offNew = mData.mOffCurrent + cbWritten;
612 mData.mOffCurrent = offNew;
613 alock.release();
614
615 try
616 {
617 fireGuestFileWriteEvent(mEventSource, mSession, this, offNew, cbWritten);
618 rc = VINF_SUCCESS;
619 }
620 catch (std::bad_alloc &)
621 {
622 rc = VERR_NO_MEMORY;
623 }
624 break;
625 }
626
627 case GUEST_FILE_NOTIFYTYPE_SEEK:
628 {
629 if (pSvcCbData->mParms == 4)
630 {
631 rc = HGCMSvcGetU64(&pSvcCbData->mpaParms[idx++], &dataCb.u.seek.uOffActual);
632 if (RT_FAILURE(rc))
633 break;
634
635 Log3ThisFunc(("uOffActual=%RU64\n", dataCb.u.seek.uOffActual));
636
637 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
638
639 mData.mOffCurrent = dataCb.u.seek.uOffActual;
640
641 alock.release();
642
643 fireGuestFileOffsetChangedEvent(mEventSource, mSession, this, dataCb.u.seek.uOffActual, 0 /* Processed */);
644 }
645 break;
646 }
647
648 case GUEST_FILE_NOTIFYTYPE_TELL:
649 /* We don't issue any HOST_MSG_FILE_TELL, so we shouldn't get these notifications! */
650 AssertFailed();
651 break;
652
653 case GUEST_FILE_NOTIFYTYPE_SET_SIZE:
654 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 4, ("mParms=%u\n", pSvcCbData->mParms),
655 rc = VERR_WRONG_PARAMETER_COUNT);
656 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_64BIT,
657 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
658 rc = VERR_WRONG_PARAMETER_TYPE);
659 dataCb.u.SetSize.cbSize = pSvcCbData->mpaParms[idx].u.uint64;
660 Log3ThisFunc(("cbSize=%RU64\n", dataCb.u.SetSize.cbSize));
661
662 fireGuestFileSizeChangedEvent(mEventSource, mSession, this, dataCb.u.SetSize.cbSize);
663 rc = VINF_SUCCESS;
664 break;
665
666 default:
667 break;
668 }
669
670 if (RT_SUCCESS(rc))
671 {
672 try
673 {
674 GuestWaitEventPayload payload(dataCb.uType, &dataCb, sizeof(dataCb));
675
676 /* Ignore rc, as the event to signal might not be there (anymore). */
677 signalWaitEventInternal(pCbCtx, rcGuest, &payload);
678 }
679 catch (int rcEx) /* Thrown by GuestWaitEventPayload constructor. */
680 {
681 rc = rcEx;
682 }
683 }
684
685 LogFlowThisFunc(("uType=%RU32, rcGuest=%Rrc, rc=%Rrc\n", dataCb.uType, rcGuest, rc));
686 return rc;
687}
688
689int GuestFile::i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
690{
691 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
692 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
693
694 int vrc = i_setFileStatus(FileStatus_Down, VINF_SUCCESS);
695
696 LogFlowFuncLeaveRC(vrc);
697 return vrc;
698}
699
700/**
701 * @copydoc GuestObject::i_onUnregister
702 */
703int GuestFile::i_onUnregister(void)
704{
705 LogFlowThisFuncEnter();
706
707 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
708
709 int vrc = VINF_SUCCESS;
710
711 /*
712 * Note: The event source stuff holds references to this object,
713 * so make sure that this is cleaned up *before* calling uninit().
714 */
715 if (!mEventSource.isNull())
716 {
717 mEventSource->UnregisterListener(mLocalListener);
718
719 mLocalListener.setNull();
720 unconst(mEventSource).setNull();
721 }
722
723 LogFlowFuncLeaveRC(vrc);
724 return vrc;
725}
726
727/**
728 * @copydoc GuestObject::i_onSessionStatusChange
729 */
730int GuestFile::i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus)
731{
732 LogFlowThisFuncEnter();
733
734 int vrc = VINF_SUCCESS;
735
736 /* If the session now is in a terminated state, set the file status
737 * to "down", as there is not much else we can do now. */
738 if (GuestSession::i_isTerminated(enmSessionStatus))
739 vrc = i_setFileStatus(FileStatus_Down, 0 /* fileRc, ignored */);
740
741 LogFlowFuncLeaveRC(vrc);
742 return vrc;
743}
744
745int GuestFile::i_openFile(uint32_t uTimeoutMS, int *prcGuest)
746{
747 AssertReturn(mData.mOpenInfo.mFilename.isNotEmpty(), VERR_INVALID_PARAMETER);
748
749 LogFlowThisFuncEnter();
750
751 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
752
753 LogFlowThisFunc(("strFile=%s, enmAccessMode=%d, enmOpenAction=%d, uCreationMode=%o, mfOpenEx=%#x\n",
754 mData.mOpenInfo.mFilename.c_str(), mData.mOpenInfo.mAccessMode, mData.mOpenInfo.mOpenAction,
755 mData.mOpenInfo.mCreationMode, mData.mOpenInfo.mfOpenEx));
756
757 /* Validate and translate open action. */
758 const char *pszOpenAction = NULL;
759 switch (mData.mOpenInfo.mOpenAction)
760 {
761 case FileOpenAction_OpenExisting: pszOpenAction = "oe"; break;
762 case FileOpenAction_OpenOrCreate: pszOpenAction = "oc"; break;
763 case FileOpenAction_CreateNew: pszOpenAction = "ce"; break;
764 case FileOpenAction_CreateOrReplace: pszOpenAction = "ca"; break;
765 case FileOpenAction_OpenExistingTruncated: pszOpenAction = "ot"; break;
766 case FileOpenAction_AppendOrCreate:
767 pszOpenAction = "oa"; /** @todo get rid of this one and implement AppendOnly/AppendRead. */
768 break;
769 default:
770 return VERR_INVALID_PARAMETER;
771 }
772
773 /* Validate and translate access mode. */
774 const char *pszAccessMode = NULL;
775 switch (mData.mOpenInfo.mAccessMode)
776 {
777 case FileAccessMode_ReadOnly: pszAccessMode = "r"; break;
778 case FileAccessMode_WriteOnly: pszAccessMode = "w"; break;
779 case FileAccessMode_ReadWrite: pszAccessMode = "r+"; break;
780 case FileAccessMode_AppendOnly: pszAccessMode = "a"; break;
781 case FileAccessMode_AppendRead: pszAccessMode = "a+"; break;
782 default: return VERR_INVALID_PARAMETER;
783 }
784
785 /* Validate and translate sharing mode. */
786 const char *pszSharingMode = NULL;
787 switch (mData.mOpenInfo.mSharingMode)
788 {
789 case FileSharingMode_All: pszSharingMode = ""; break;
790 case FileSharingMode_Read: RT_FALL_THRU();
791 case FileSharingMode_Write: RT_FALL_THRU();
792 case FileSharingMode_ReadWrite: RT_FALL_THRU();
793 case FileSharingMode_Delete: RT_FALL_THRU();
794 case FileSharingMode_ReadDelete: RT_FALL_THRU();
795 case FileSharingMode_WriteDelete: return VERR_NOT_IMPLEMENTED;
796 default: return VERR_INVALID_PARAMETER;
797 }
798
799 int vrc;
800
801 GuestWaitEvent *pEvent = NULL;
802 GuestEventTypes eventTypes;
803 try
804 {
805 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
806
807 vrc = registerWaitEvent(eventTypes, &pEvent);
808 }
809 catch (std::bad_alloc &)
810 {
811 vrc = VERR_NO_MEMORY;
812 }
813
814 if (RT_FAILURE(vrc))
815 return vrc;
816
817 /* Prepare HGCM call. */
818 VBOXHGCMSVCPARM paParms[8];
819 int i = 0;
820 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
821 HGCMSvcSetPv(&paParms[i++], (void*)mData.mOpenInfo.mFilename.c_str(),
822 (ULONG)mData.mOpenInfo.mFilename.length() + 1);
823 HGCMSvcSetStr(&paParms[i++], pszAccessMode);
824 HGCMSvcSetStr(&paParms[i++], pszOpenAction);
825 HGCMSvcSetStr(&paParms[i++], pszSharingMode);
826 HGCMSvcSetU32(&paParms[i++], mData.mOpenInfo.mCreationMode);
827 HGCMSvcSetU64(&paParms[i++], 0 /*unused offset*/);
828 /** @todo Next protocol version: add flags, replace strings, remove initial offset. */
829
830 alock.release(); /* Drop write lock before sending. */
831
832 vrc = sendMessage(HOST_MSG_FILE_OPEN, i, paParms);
833 if (RT_SUCCESS(vrc))
834 vrc = i_waitForStatusChange(pEvent, uTimeoutMS, NULL /* FileStatus */, prcGuest);
835
836 unregisterWaitEvent(pEvent);
837
838 LogFlowFuncLeaveRC(vrc);
839 return vrc;
840}
841
842int GuestFile::i_queryInfo(GuestFsObjData &objData, int *prcGuest)
843{
844 AssertPtr(mSession);
845 return mSession->i_fsQueryInfo(mData.mOpenInfo.mFilename, FALSE /* fFollowSymlinks */, objData, prcGuest);
846}
847
848int GuestFile::i_readData(uint32_t uSize, uint32_t uTimeoutMS,
849 void* pvData, uint32_t cbData, uint32_t* pcbRead)
850{
851 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
852 AssertReturn(cbData, VERR_INVALID_PARAMETER);
853
854 LogFlowThisFunc(("uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
855 uSize, uTimeoutMS, pvData, cbData));
856
857 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
858
859 int vrc;
860
861 GuestWaitEvent *pEvent = NULL;
862 GuestEventTypes eventTypes;
863 try
864 {
865 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
866 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
867
868 vrc = registerWaitEvent(eventTypes, &pEvent);
869 }
870 catch (std::bad_alloc &)
871 {
872 vrc = VERR_NO_MEMORY;
873 }
874
875 if (RT_FAILURE(vrc))
876 return vrc;
877
878 /* Prepare HGCM call. */
879 VBOXHGCMSVCPARM paParms[4];
880 int i = 0;
881 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
882 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
883 HGCMSvcSetU32(&paParms[i++], uSize /* Size (in bytes) to read */);
884
885 alock.release(); /* Drop write lock before sending. */
886
887 vrc = sendMessage(HOST_MSG_FILE_READ, i, paParms);
888 if (RT_SUCCESS(vrc))
889 {
890 uint32_t cbRead = 0;
891 vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
892 if (RT_SUCCESS(vrc))
893 {
894 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
895 if (pcbRead)
896 *pcbRead = cbRead;
897 }
898 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
899 {
900 vrc = pEvent->GetGuestError();
901 }
902 }
903
904 unregisterWaitEvent(pEvent);
905
906 LogFlowFuncLeaveRC(vrc);
907 return vrc;
908}
909
910int GuestFile::i_readDataAt(uint64_t uOffset, uint32_t uSize, uint32_t uTimeoutMS,
911 void* pvData, size_t cbData, size_t* pcbRead)
912{
913 LogFlowThisFunc(("uOffset=%RU64, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
914 uOffset, uSize, uTimeoutMS, pvData, cbData));
915
916 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
917
918 int vrc;
919
920 GuestWaitEvent *pEvent = NULL;
921 GuestEventTypes eventTypes;
922 try
923 {
924 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
925 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
926
927 vrc = registerWaitEvent(eventTypes, &pEvent);
928 }
929 catch (std::bad_alloc &)
930 {
931 vrc = VERR_NO_MEMORY;
932 }
933
934 if (RT_FAILURE(vrc))
935 return vrc;
936
937 /* Prepare HGCM call. */
938 VBOXHGCMSVCPARM paParms[4];
939 int i = 0;
940 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
941 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
942 HGCMSvcSetU64(&paParms[i++], uOffset /* Offset (in bytes) to start reading */);
943 HGCMSvcSetU32(&paParms[i++], uSize /* Size (in bytes) to read */);
944
945 alock.release(); /* Drop write lock before sending. */
946
947 vrc = sendMessage(HOST_MSG_FILE_READ_AT, i, paParms);
948 if (RT_SUCCESS(vrc))
949 {
950 uint32_t cbRead = 0;
951 vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
952 if (RT_SUCCESS(vrc))
953 {
954 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
955
956 if (pcbRead)
957 *pcbRead = cbRead;
958 }
959 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
960 {
961 vrc = pEvent->GetGuestError();
962 }
963 }
964
965 unregisterWaitEvent(pEvent);
966
967 LogFlowFuncLeaveRC(vrc);
968 return vrc;
969}
970
971int GuestFile::i_seekAt(int64_t iOffset, GUEST_FILE_SEEKTYPE eSeekType,
972 uint32_t uTimeoutMS, uint64_t *puOffset)
973{
974 LogFlowThisFunc(("iOffset=%RI64, uTimeoutMS=%RU32\n",
975 iOffset, uTimeoutMS));
976
977 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
978
979 int vrc;
980
981 GuestWaitEvent *pEvent = NULL;
982 GuestEventTypes eventTypes;
983 try
984 {
985 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
986 eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
987
988 vrc = registerWaitEvent(eventTypes, &pEvent);
989 }
990 catch (std::bad_alloc &)
991 {
992 vrc = VERR_NO_MEMORY;
993 }
994
995 if (RT_FAILURE(vrc))
996 return vrc;
997
998 /* Prepare HGCM call. */
999 VBOXHGCMSVCPARM paParms[4];
1000 int i = 0;
1001 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1002 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1003 HGCMSvcSetU32(&paParms[i++], eSeekType /* Seek method */);
1004 /** @todo uint64_t vs. int64_t! */
1005 HGCMSvcSetU64(&paParms[i++], (uint64_t)iOffset /* Offset (in bytes) to start reading */);
1006
1007 alock.release(); /* Drop write lock before sending. */
1008
1009 vrc = sendMessage(HOST_MSG_FILE_SEEK, i, paParms);
1010 if (RT_SUCCESS(vrc))
1011 {
1012 uint64_t uOffset;
1013 vrc = i_waitForOffsetChange(pEvent, uTimeoutMS, &uOffset);
1014 if (RT_SUCCESS(vrc))
1015 {
1016 LogFlowThisFunc(("uOffset=%RU64\n", uOffset));
1017
1018 if (puOffset)
1019 *puOffset = uOffset;
1020 }
1021 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
1022 {
1023 vrc = pEvent->GetGuestError();
1024 }
1025 }
1026
1027 unregisterWaitEvent(pEvent);
1028
1029 LogFlowFuncLeaveRC(vrc);
1030 return vrc;
1031}
1032
1033int GuestFile::i_setFileStatus(FileStatus_T fileStatus, int fileRc)
1034{
1035 LogFlowThisFuncEnter();
1036
1037 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1038
1039 LogFlowThisFunc(("oldStatus=%RU32, newStatus=%RU32, fileRc=%Rrc\n",
1040 mData.mStatus, fileStatus, fileRc));
1041
1042#ifdef VBOX_STRICT
1043 if (fileStatus == FileStatus_Error)
1044 {
1045 AssertMsg(RT_FAILURE(fileRc), ("Guest rc must be an error (%Rrc)\n", fileRc));
1046 }
1047 else
1048 AssertMsg(RT_SUCCESS(fileRc), ("Guest rc must not be an error (%Rrc)\n", fileRc));
1049#endif
1050
1051 if (mData.mStatus != fileStatus)
1052 {
1053 mData.mStatus = fileStatus;
1054 mData.mLastError = fileRc;
1055
1056 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
1057 HRESULT hr = errorInfo.createObject();
1058 ComAssertComRC(hr);
1059 if (RT_FAILURE(fileRc))
1060 {
1061 hr = errorInfo->initEx(VBOX_E_IPRT_ERROR, fileRc,
1062 COM_IIDOF(IGuestFile), getComponentName(),
1063 i_guestErrorToString(fileRc, mData.mOpenInfo.mFilename.c_str()));
1064 ComAssertComRC(hr);
1065 }
1066
1067 alock.release(); /* Release lock before firing off event. */
1068
1069 fireGuestFileStateChangedEvent(mEventSource, mSession,
1070 this, fileStatus, errorInfo);
1071 }
1072
1073 return VINF_SUCCESS;
1074}
1075
1076int GuestFile::i_waitForOffsetChange(GuestWaitEvent *pEvent,
1077 uint32_t uTimeoutMS, uint64_t *puOffset)
1078{
1079 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1080
1081 VBoxEventType_T evtType;
1082 ComPtr<IEvent> pIEvent;
1083 int vrc = waitForEvent(pEvent, uTimeoutMS,
1084 &evtType, pIEvent.asOutParam());
1085 if (RT_SUCCESS(vrc))
1086 {
1087 if (evtType == VBoxEventType_OnGuestFileOffsetChanged)
1088 {
1089 if (puOffset)
1090 {
1091 ComPtr<IGuestFileOffsetChangedEvent> pFileEvent = pIEvent;
1092 Assert(!pFileEvent.isNull());
1093
1094 HRESULT hr = pFileEvent->COMGETTER(Offset)((LONG64*)puOffset);
1095 ComAssertComRC(hr);
1096 }
1097 }
1098 else
1099 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1100 }
1101
1102 return vrc;
1103}
1104
1105int GuestFile::i_waitForRead(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1106 void *pvData, size_t cbData, uint32_t *pcbRead)
1107{
1108 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1109
1110 VBoxEventType_T evtType;
1111 ComPtr<IEvent> pIEvent;
1112 int vrc = waitForEvent(pEvent, uTimeoutMS,
1113 &evtType, pIEvent.asOutParam());
1114 if (RT_SUCCESS(vrc))
1115 {
1116 if (evtType == VBoxEventType_OnGuestFileRead)
1117 {
1118 vrc = VINF_SUCCESS;
1119
1120 ComPtr<IGuestFileReadEvent> pFileEvent = pIEvent;
1121 Assert(!pFileEvent.isNull());
1122
1123 if (pvData)
1124 {
1125 com::SafeArray <BYTE> data;
1126 HRESULT hrc1 = pFileEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
1127 ComAssertComRC(hrc1);
1128 const size_t cbRead = data.size();
1129 if (cbRead)
1130 {
1131 if (cbRead <= cbData)
1132 memcpy(pvData, data.raw(), cbRead);
1133 else
1134 vrc = VERR_BUFFER_OVERFLOW;
1135 }
1136 /* else: used to be VERR_NO_DATA, but that messes stuff up. */
1137
1138 if (pcbRead)
1139 {
1140 *pcbRead = (uint32_t)cbRead;
1141 Assert(*pcbRead == cbRead);
1142 }
1143 }
1144 else if (pcbRead)
1145 {
1146 *pcbRead = 0;
1147 HRESULT hrc2 = pFileEvent->COMGETTER(Processed)((ULONG *)pcbRead);
1148 ComAssertComRC(hrc2); NOREF(hrc2);
1149 }
1150 }
1151 else
1152 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1153 }
1154
1155 return vrc;
1156}
1157
1158/**
1159 * Undocumented, use with great care.
1160 *
1161 * @note Similar code in GuestProcess::i_waitForStatusChange() and
1162 * GuestSession::i_waitForStatusChange().
1163 */
1164int GuestFile::i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1165 FileStatus_T *pFileStatus, int *prcGuest)
1166{
1167 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1168 /* pFileStatus is optional. */
1169
1170 VBoxEventType_T evtType;
1171 ComPtr<IEvent> pIEvent;
1172 int vrc = waitForEvent(pEvent, uTimeoutMS,
1173 &evtType, pIEvent.asOutParam());
1174 if (RT_SUCCESS(vrc))
1175 {
1176 Assert(evtType == VBoxEventType_OnGuestFileStateChanged);
1177 ComPtr<IGuestFileStateChangedEvent> pFileEvent = pIEvent;
1178 Assert(!pFileEvent.isNull());
1179
1180 HRESULT hr;
1181 if (pFileStatus)
1182 {
1183 hr = pFileEvent->COMGETTER(Status)(pFileStatus);
1184 ComAssertComRC(hr);
1185 }
1186
1187 ComPtr<IVirtualBoxErrorInfo> errorInfo;
1188 hr = pFileEvent->COMGETTER(Error)(errorInfo.asOutParam());
1189 ComAssertComRC(hr);
1190
1191 LONG lGuestRc;
1192 hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
1193 ComAssertComRC(hr);
1194
1195 LogFlowThisFunc(("resultDetail=%RI32 (%Rrc)\n",
1196 lGuestRc, lGuestRc));
1197
1198 if (RT_FAILURE((int)lGuestRc))
1199 vrc = VERR_GSTCTL_GUEST_ERROR;
1200
1201 if (prcGuest)
1202 *prcGuest = (int)lGuestRc;
1203 }
1204 /* waitForEvent may also return VERR_GSTCTL_GUEST_ERROR like we do above, so make prcGuest is set. */
1205 /** @todo r=bird: Andy, you seem to have forgotten this scenario. Showed up occasionally when
1206 * using the wrong password with a copyto command in a debug build on windows, error info
1207 * contained "Unknown Status -858993460 (0xcccccccc)". As you know windows fills the stack frames
1208 * with 0xcccccccc in debug builds to highlight use of uninitialized data, so that's what happened
1209 * here. It's actually good you didn't initialize lGuest, as it would be heck to find otherwise.
1210 *
1211 * I'm still not very impressed with the error managment or the usuefullness of the documentation
1212 * in this code, though the latter is getting better! */
1213 else if (vrc == VERR_GSTCTL_GUEST_ERROR && prcGuest)
1214 *prcGuest = pEvent->GuestResult();
1215 Assert(vrc != VERR_GSTCTL_GUEST_ERROR || !prcGuest || *prcGuest != (int)0xcccccccc);
1216
1217 return vrc;
1218}
1219
1220int GuestFile::i_waitForWrite(GuestWaitEvent *pEvent,
1221 uint32_t uTimeoutMS, uint32_t *pcbWritten)
1222{
1223 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1224
1225 VBoxEventType_T evtType;
1226 ComPtr<IEvent> pIEvent;
1227 int vrc = waitForEvent(pEvent, uTimeoutMS,
1228 &evtType, pIEvent.asOutParam());
1229 if (RT_SUCCESS(vrc))
1230 {
1231 if (evtType == VBoxEventType_OnGuestFileWrite)
1232 {
1233 if (pcbWritten)
1234 {
1235 ComPtr<IGuestFileWriteEvent> pFileEvent = pIEvent;
1236 Assert(!pFileEvent.isNull());
1237
1238 HRESULT hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbWritten);
1239 ComAssertComRC(hr);
1240 }
1241 }
1242 else
1243 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1244 }
1245
1246 return vrc;
1247}
1248
1249int GuestFile::i_writeData(uint32_t uTimeoutMS, const void *pvData, uint32_t cbData,
1250 uint32_t *pcbWritten)
1251{
1252 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1253 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1254
1255 LogFlowThisFunc(("uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1256 uTimeoutMS, pvData, cbData));
1257
1258 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1259
1260 int vrc;
1261
1262 GuestWaitEvent *pEvent = NULL;
1263 GuestEventTypes eventTypes;
1264 try
1265 {
1266 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1267 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1268
1269 vrc = registerWaitEvent(eventTypes, &pEvent);
1270 }
1271 catch (std::bad_alloc &)
1272 {
1273 vrc = VERR_NO_MEMORY;
1274 }
1275
1276 if (RT_FAILURE(vrc))
1277 return vrc;
1278
1279 /* Prepare HGCM call. */
1280 VBOXHGCMSVCPARM paParms[8];
1281 int i = 0;
1282 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1283 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1284 HGCMSvcSetU32(&paParms[i++], cbData /* Size (in bytes) to write */);
1285 HGCMSvcSetPv (&paParms[i++], unconst(pvData), cbData);
1286
1287 alock.release(); /* Drop write lock before sending. */
1288
1289 vrc = sendMessage(HOST_MSG_FILE_WRITE, i, paParms);
1290 if (RT_SUCCESS(vrc))
1291 {
1292 uint32_t cbWritten = 0;
1293 vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1294 if (RT_SUCCESS(vrc))
1295 {
1296 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1297 if (pcbWritten)
1298 *pcbWritten = cbWritten;
1299 }
1300 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
1301 {
1302 vrc = pEvent->GetGuestError();
1303 }
1304 }
1305
1306 unregisterWaitEvent(pEvent);
1307
1308 LogFlowFuncLeaveRC(vrc);
1309 return vrc;
1310}
1311
1312int GuestFile::i_writeDataAt(uint64_t uOffset, uint32_t uTimeoutMS,
1313 const void *pvData, uint32_t cbData, uint32_t *pcbWritten)
1314{
1315 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1316 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1317
1318 LogFlowThisFunc(("uOffset=%RU64, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1319 uOffset, uTimeoutMS, pvData, cbData));
1320
1321 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1322
1323 int vrc;
1324
1325 GuestWaitEvent *pEvent = NULL;
1326 GuestEventTypes eventTypes;
1327 try
1328 {
1329 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1330 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1331
1332 vrc = registerWaitEvent(eventTypes, &pEvent);
1333 }
1334 catch (std::bad_alloc &)
1335 {
1336 vrc = VERR_NO_MEMORY;
1337 }
1338
1339 if (RT_FAILURE(vrc))
1340 return vrc;
1341
1342 /* Prepare HGCM call. */
1343 VBOXHGCMSVCPARM paParms[8];
1344 int i = 0;
1345 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1346 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1347 HGCMSvcSetU64(&paParms[i++], uOffset /* Offset where to starting writing */);
1348 HGCMSvcSetU32(&paParms[i++], cbData /* Size (in bytes) to write */);
1349 HGCMSvcSetPv (&paParms[i++], unconst(pvData), cbData);
1350
1351 alock.release(); /* Drop write lock before sending. */
1352
1353 vrc = sendMessage(HOST_MSG_FILE_WRITE_AT, i, paParms);
1354 if (RT_SUCCESS(vrc))
1355 {
1356 uint32_t cbWritten = 0;
1357 vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1358 if (RT_SUCCESS(vrc))
1359 {
1360 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1361 if (pcbWritten)
1362 *pcbWritten = cbWritten;
1363 }
1364 else if (pEvent->HasGuestError()) /* Return guest rc if available. */
1365 {
1366 vrc = pEvent->GetGuestError();
1367 }
1368 }
1369
1370 unregisterWaitEvent(pEvent);
1371
1372 LogFlowFuncLeaveRC(vrc);
1373 return vrc;
1374}
1375
1376// Wrapped IGuestFile methods
1377/////////////////////////////////////////////////////////////////////////////
1378HRESULT GuestFile::close()
1379{
1380 AutoCaller autoCaller(this);
1381 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1382
1383 LogFlowThisFuncEnter();
1384
1385 /* Close file on guest. */
1386 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1387 int vrc = i_closeFile(&rcGuest);
1388 /* On failure don't return here, instead do all the cleanup
1389 * work first and then return an error. */
1390
1391 AssertPtr(mSession);
1392 int vrc2 = mSession->i_fileUnregister(this);
1393 if (RT_SUCCESS(vrc))
1394 vrc = vrc2;
1395
1396 if (RT_FAILURE(vrc))
1397 {
1398 if (vrc == VERR_GSTCTL_GUEST_ERROR)
1399 return setErrorExternal(this, tr("Closing guest file failed"),
1400 GuestErrorInfo(GuestErrorInfo::Type_File, rcGuest, mData.mOpenInfo.mFilename.c_str()));
1401 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Closing guest file \"%s\" failed with %Rrc\n"),
1402 mData.mOpenInfo.mFilename.c_str(), vrc);
1403 }
1404
1405 LogFlowThisFunc(("Returning S_OK / vrc=%Rrc\n", vrc));
1406 return S_OK;
1407}
1408
1409HRESULT GuestFile::queryInfo(ComPtr<IFsObjInfo> &aObjInfo)
1410{
1411 AutoCaller autoCaller(this);
1412 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1413
1414 LogFlowThisFuncEnter();
1415
1416 HRESULT hr = S_OK;
1417
1418 GuestFsObjData fsObjData;
1419 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1420 int vrc = i_queryInfo(fsObjData, &rcGuest);
1421 if (RT_SUCCESS(vrc))
1422 {
1423 ComObjPtr<GuestFsObjInfo> ptrFsObjInfo;
1424 hr = ptrFsObjInfo.createObject();
1425 if (SUCCEEDED(hr))
1426 {
1427 vrc = ptrFsObjInfo->init(fsObjData);
1428 if (RT_SUCCESS(vrc))
1429 hr = ptrFsObjInfo.queryInterfaceTo(aObjInfo.asOutParam());
1430 else
1431 hr = setErrorVrc(vrc,
1432 tr("Initialization of guest file object for \"%s\" failed: %Rrc"),
1433 mData.mOpenInfo.mFilename.c_str(), vrc);
1434 }
1435 }
1436 else
1437 {
1438 if (GuestProcess::i_isGuestError(vrc))
1439 hr = setErrorExternal(this, tr("Querying guest file information failed"),
1440 GuestErrorInfo(GuestErrorInfo::Type_ToolStat, rcGuest, mData.mOpenInfo.mFilename.c_str()));
1441 else
1442 hr = setErrorVrc(vrc,
1443 tr("Querying guest file information for \"%s\" failed: %Rrc"), mData.mOpenInfo.mFilename.c_str(), vrc);
1444 }
1445
1446 LogFlowFuncLeaveRC(vrc);
1447 return hr;
1448}
1449
1450HRESULT GuestFile::querySize(LONG64 *aSize)
1451{
1452 AutoCaller autoCaller(this);
1453 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1454
1455 LogFlowThisFuncEnter();
1456
1457 HRESULT hr = S_OK;
1458
1459 GuestFsObjData fsObjData;
1460 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1461 int vrc = i_queryInfo(fsObjData, &rcGuest);
1462 if (RT_SUCCESS(vrc))
1463 {
1464 *aSize = fsObjData.mObjectSize;
1465 }
1466 else
1467 {
1468 if (GuestProcess::i_isGuestError(vrc))
1469 hr = setErrorExternal(this, tr("Querying guest file size failed"),
1470 GuestErrorInfo(GuestErrorInfo::Type_ToolStat, rcGuest, mData.mOpenInfo.mFilename.c_str()));
1471 else
1472 hr = setErrorVrc(vrc, tr("Querying guest file size for \"%s\" failed: %Rrc"), mData.mOpenInfo.mFilename.c_str(), vrc);
1473 }
1474
1475 LogFlowFuncLeaveRC(vrc);
1476 return hr;
1477}
1478
1479HRESULT GuestFile::read(ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
1480{
1481 AutoCaller autoCaller(this);
1482 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1483
1484 if (aToRead == 0)
1485 return setError(E_INVALIDARG, tr("The size to read is zero"));
1486
1487 LogFlowThisFuncEnter();
1488
1489 /* Cap the read at 1MiB because that's all the guest will return anyway. */
1490 if (aToRead > _1M)
1491 aToRead = _1M;
1492
1493 aData.resize(aToRead);
1494
1495 HRESULT hr = S_OK;
1496
1497 uint32_t cbRead;
1498 int vrc = i_readData(aToRead, aTimeoutMS,
1499 &aData.front(), aToRead, &cbRead);
1500
1501 if (RT_SUCCESS(vrc))
1502 {
1503 if (aData.size() != cbRead)
1504 aData.resize(cbRead);
1505 }
1506 else
1507 {
1508 aData.resize(0);
1509
1510 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading from file \"%s\" failed: %Rrc"),
1511 mData.mOpenInfo.mFilename.c_str(), vrc);
1512 }
1513
1514 LogFlowFuncLeaveRC(vrc);
1515 return hr;
1516}
1517
1518HRESULT GuestFile::readAt(LONG64 aOffset, ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
1519{
1520 AutoCaller autoCaller(this);
1521 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1522
1523 if (aToRead == 0)
1524 return setError(E_INVALIDARG, tr("The size to read for guest file \"%s\" is zero"), mData.mOpenInfo.mFilename.c_str());
1525
1526 LogFlowThisFuncEnter();
1527
1528 /* Cap the read at 1MiB because that's all the guest will return anyway. */
1529 if (aToRead > _1M)
1530 aToRead = _1M;
1531
1532 aData.resize(aToRead);
1533
1534 HRESULT hr = S_OK;
1535
1536 size_t cbRead;
1537 int vrc = i_readDataAt(aOffset, aToRead, aTimeoutMS,
1538 &aData.front(), aToRead, &cbRead);
1539 if (RT_SUCCESS(vrc))
1540 {
1541 if (aData.size() != cbRead)
1542 aData.resize(cbRead);
1543 }
1544 else
1545 {
1546 aData.resize(0);
1547
1548 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading from file \"%s\" (at offset %RU64) failed: %Rrc"),
1549 mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1550 }
1551
1552 LogFlowFuncLeaveRC(vrc);
1553 return hr;
1554}
1555
1556HRESULT GuestFile::seek(LONG64 aOffset, FileSeekOrigin_T aWhence, LONG64 *aNewOffset)
1557{
1558 AutoCaller autoCaller(this);
1559 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1560
1561 HRESULT hr = S_OK;
1562
1563 GUEST_FILE_SEEKTYPE eSeekType;
1564 switch (aWhence)
1565 {
1566 case FileSeekOrigin_Begin:
1567 eSeekType = GUEST_FILE_SEEKTYPE_BEGIN;
1568 break;
1569
1570 case FileSeekOrigin_Current:
1571 eSeekType = GUEST_FILE_SEEKTYPE_CURRENT;
1572 break;
1573
1574 case FileSeekOrigin_End:
1575 eSeekType = GUEST_FILE_SEEKTYPE_END;
1576 break;
1577
1578 default:
1579 return setError(E_INVALIDARG, tr("Invalid seek type for guest file \"%s\" specified"),
1580 mData.mOpenInfo.mFilename.c_str());
1581 }
1582
1583 LogFlowThisFuncEnter();
1584
1585 uint64_t uNewOffset;
1586 int vrc = i_seekAt(aOffset, eSeekType,
1587 30 * 1000 /* 30s timeout */, &uNewOffset);
1588 if (RT_SUCCESS(vrc))
1589 *aNewOffset = RT_MIN(uNewOffset, (uint64_t)INT64_MAX);
1590 else
1591 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Seeking file \"%s\" (to offset %RI64) failed: %Rrc"),
1592 mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1593
1594 LogFlowFuncLeaveRC(vrc);
1595 return hr;
1596}
1597
1598HRESULT GuestFile::setACL(const com::Utf8Str &aAcl, ULONG aMode)
1599{
1600 RT_NOREF(aAcl, aMode);
1601 ReturnComNotImplemented();
1602}
1603
1604HRESULT GuestFile::setSize(LONG64 aSize)
1605{
1606 LogFlowThisFuncEnter();
1607
1608 /*
1609 * Validate.
1610 */
1611 if (aSize < 0)
1612 return setError(E_INVALIDARG, tr("The size (%RI64) for guest file \"%s\" cannot be a negative value"),
1613 aSize, mData.mOpenInfo.mFilename.c_str());
1614
1615 /*
1616 * Register event callbacks.
1617 */
1618 int vrc;
1619 GuestWaitEvent *pWaitEvent = NULL;
1620 GuestEventTypes lstEventTypes;
1621 try
1622 {
1623 lstEventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1624 lstEventTypes.push_back(VBoxEventType_OnGuestFileSizeChanged);
1625 }
1626 catch (std::bad_alloc &)
1627 {
1628 return E_OUTOFMEMORY;
1629 }
1630
1631 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1632
1633 vrc = registerWaitEvent(lstEventTypes, &pWaitEvent);
1634 if (RT_SUCCESS(vrc))
1635 {
1636 /*
1637 * Send of the HGCM message.
1638 */
1639 VBOXHGCMSVCPARM aParms[3];
1640 HGCMSvcSetU32(&aParms[0], pWaitEvent->ContextID());
1641 HGCMSvcSetU32(&aParms[1], mObjectID /* File handle */);
1642 HGCMSvcSetU64(&aParms[2], aSize);
1643
1644 alock.release(); /* Drop write lock before sending. */
1645
1646 vrc = sendMessage(HOST_MSG_FILE_SET_SIZE, RT_ELEMENTS(aParms), aParms);
1647 if (RT_SUCCESS(vrc))
1648 {
1649 /*
1650 * Wait for the event.
1651 */
1652 VBoxEventType_T enmEvtType;
1653 ComPtr<IEvent> pIEvent;
1654 vrc = waitForEvent(pWaitEvent, RT_MS_1MIN / 2, &enmEvtType, pIEvent.asOutParam());
1655 if (RT_SUCCESS(vrc))
1656 {
1657 if (enmEvtType == VBoxEventType_OnGuestFileSizeChanged)
1658 vrc = VINF_SUCCESS;
1659 else
1660 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1661 }
1662 if (RT_FAILURE(vrc) && pWaitEvent->HasGuestError()) /* Return guest rc if available. */
1663 vrc = pWaitEvent->GetGuestError();
1664 }
1665
1666 /*
1667 * Unregister the wait event and deal with error reporting if needed.
1668 */
1669 unregisterWaitEvent(pWaitEvent);
1670 }
1671 HRESULT hrc;
1672 if (RT_SUCCESS(vrc))
1673 hrc = S_OK;
1674 else
1675 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Setting the guest file size of \"%s\" to %RU64 (%#RX64) bytes failed: %Rrc"),
1676 mData.mOpenInfo.mFilename.c_str(), aSize, aSize, vrc);
1677 LogFlowFuncLeaveRC(vrc);
1678 return hrc;
1679}
1680
1681HRESULT GuestFile::write(const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
1682{
1683 AutoCaller autoCaller(this);
1684 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1685
1686 if (aData.size() == 0)
1687 return setError(E_INVALIDARG, tr("No data to write specified"), mData.mOpenInfo.mFilename.c_str());
1688
1689 LogFlowThisFuncEnter();
1690
1691 HRESULT hr = S_OK;
1692
1693 const uint32_t cbData = (uint32_t)aData.size();
1694 const void *pvData = (void *)&aData.front();
1695 int vrc = i_writeData(aTimeoutMS, pvData, cbData, (uint32_t*)aWritten);
1696 if (RT_FAILURE(vrc))
1697 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Writing %zu bytes to guest file \"%s\" failed: %Rrc"),
1698 aData.size(), mData.mOpenInfo.mFilename.c_str(), vrc);
1699
1700 LogFlowFuncLeaveRC(vrc);
1701 return hr;
1702}
1703
1704HRESULT GuestFile::writeAt(LONG64 aOffset, const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
1705{
1706 AutoCaller autoCaller(this);
1707 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1708
1709 if (aData.size() == 0)
1710 return setError(E_INVALIDARG, tr("No data to write at for guest file \"%s\" specified"), mData.mOpenInfo.mFilename.c_str());
1711
1712 LogFlowThisFuncEnter();
1713
1714 HRESULT hr = S_OK;
1715
1716 const uint32_t cbData = (uint32_t)aData.size();
1717 const void *pvData = (void *)&aData.front();
1718 int vrc = i_writeDataAt(aOffset, aTimeoutMS, pvData, cbData, (uint32_t*)aWritten);
1719 if (RT_FAILURE(vrc))
1720 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Writing %zu bytes to file \"%s\" (at offset %RU64) failed: %Rrc"),
1721 aData.size(), mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1722
1723 LogFlowFuncLeaveRC(vrc);
1724 return hr;
1725}
1726
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