VirtualBox

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

Last change on this file since 48915 was 48818, checked in by vboxsync, 11 years ago

Main/GuestCtrl: Fire IGuestFile events in every case; there might we waiters depending on it. seekAt() is now accepting relative offsets.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.7 KB
Line 
1
2/* $Id: GuestFileImpl.cpp 48818 2013-10-02 13:50:27Z vboxsync $ */
3/** @file
4 * VirtualBox Main - Guest file handling.
5 */
6
7/*
8 * Copyright (C) 2012-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include "GuestFileImpl.h"
24#include "GuestSessionImpl.h"
25#include "GuestCtrlImplPrivate.h"
26#include "ConsoleImpl.h"
27#include "VirtualBoxErrorInfoImpl.h"
28
29#include "Global.h"
30#include "AutoCaller.h"
31#include "VBoxEvents.h"
32
33#include <iprt/cpp/utils.h> /* For unconst(). */
34#include <iprt/file.h>
35
36#include <VBox/com/array.h>
37#include <VBox/com/listeners.h>
38
39#ifdef LOG_GROUP
40 #undef LOG_GROUP
41#endif
42#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
43#include <VBox/log.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 HRESULT init(GuestFile *pFile)
59 {
60 mFile = pFile;
61 return S_OK;
62 }
63
64 void uninit(void)
65 {
66 mFile.setNull();
67 }
68
69 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
70 {
71 switch (aType)
72 {
73 case VBoxEventType_OnGuestFileStateChanged:
74 case VBoxEventType_OnGuestFileOffsetChanged:
75 case VBoxEventType_OnGuestFileRead:
76 case VBoxEventType_OnGuestFileWrite:
77 {
78 Assert(!mFile.isNull());
79 int rc2 = mFile->signalWaitEvents(aType, aEvent);
80#ifdef DEBUG_andy
81 LogFlowFunc(("Signalling events of type=%ld, file=%p resulted in rc=%Rrc\n",
82 aType, mFile, rc2));
83#endif
84 break;
85 }
86
87 default:
88 AssertMsgFailed(("Unhandled event %ld\n", aType));
89 break;
90 }
91
92 return S_OK;
93 }
94
95private:
96
97 ComObjPtr<GuestFile> mFile;
98};
99typedef ListenerImpl<GuestFileListener, GuestFile*> GuestFileListenerImpl;
100
101VBOX_LISTENER_DECLARE(GuestFileListenerImpl)
102
103// constructor / destructor
104/////////////////////////////////////////////////////////////////////////////
105
106DEFINE_EMPTY_CTOR_DTOR(GuestFile)
107
108HRESULT GuestFile::FinalConstruct(void)
109{
110 LogFlowThisFunc(("\n"));
111 return BaseFinalConstruct();
112}
113
114void GuestFile::FinalRelease(void)
115{
116 LogFlowThisFuncEnter();
117 uninit();
118 BaseFinalRelease();
119 LogFlowThisFuncLeave();
120}
121
122// public initializer/uninitializer for internal purposes only
123/////////////////////////////////////////////////////////////////////////////
124
125/**
126 * Initializes a file object but does *not* open the file on the guest
127 * yet. This is done in the dedidcated openFile call.
128 *
129 * @return IPRT status code.
130 * @param pConsole Pointer to console object.
131 * @param pSession Pointer to session object.
132 * @param uFileID Host-based file ID (part of the context ID).
133 * @param openInfo File opening information.
134 */
135int GuestFile::init(Console *pConsole, GuestSession *pSession,
136 ULONG uFileID, const GuestFileOpenInfo &openInfo)
137{
138 LogFlowThisFunc(("pConsole=%p, pSession=%p, uFileID=%RU32, strPath=%s\n",
139 pConsole, pSession, uFileID, openInfo.mFileName.c_str()));
140
141 AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
142 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
143
144 /* Enclose the state transition NotReady->InInit->Ready. */
145 AutoInitSpan autoInitSpan(this);
146 AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
147
148#ifndef VBOX_WITH_GUEST_CONTROL
149 autoInitSpan.setSucceeded();
150 return VINF_SUCCESS;
151#else
152 int vrc = bindToSession(pConsole, pSession, uFileID /* Object ID */);
153 if (RT_SUCCESS(vrc))
154 {
155 mSession = pSession;
156
157 mData.mID = uFileID;
158 mData.mInitialSize = 0;
159 mData.mStatus = FileStatus_Undefined;
160 mData.mOpenInfo = openInfo;
161
162 unconst(mEventSource).createObject();
163 HRESULT hr = mEventSource->init(static_cast<IGuestFile*>(this));
164 if (FAILED(hr))
165 vrc = VERR_COM_UNEXPECTED;
166 }
167
168 if (RT_SUCCESS(vrc))
169 {
170 try
171 {
172 GuestFileListener *pListener = new GuestFileListener();
173 ComObjPtr<GuestFileListenerImpl> thisListener;
174 HRESULT hr = thisListener.createObject();
175 if (SUCCEEDED(hr))
176 hr = thisListener->init(pListener, this);
177
178 if (SUCCEEDED(hr))
179 {
180 com::SafeArray <VBoxEventType_T> eventTypes;
181 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
182 eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
183 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
184 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
185 hr = mEventSource->RegisterListener(thisListener,
186 ComSafeArrayAsInParam(eventTypes),
187 TRUE /* Active listener */);
188 if (SUCCEEDED(hr))
189 {
190 vrc = baseInit();
191 if (RT_SUCCESS(vrc))
192 {
193 mLocalListener = thisListener;
194 }
195 }
196 else
197 vrc = VERR_COM_UNEXPECTED;
198 }
199 else
200 vrc = VERR_COM_UNEXPECTED;
201 }
202 catch(std::bad_alloc &)
203 {
204 vrc = VERR_NO_MEMORY;
205 }
206 }
207
208 if (RT_SUCCESS(vrc))
209 {
210 /* Confirm a successful initialization when it's the case. */
211 autoInitSpan.setSucceeded();
212 }
213 else
214 autoInitSpan.setFailed();
215
216 LogFlowFuncLeaveRC(vrc);
217 return vrc;
218#endif /* VBOX_WITH_GUEST_CONTROL */
219}
220
221/**
222 * Uninitializes the instance.
223 * Called from FinalRelease().
224 */
225void GuestFile::uninit(void)
226{
227 LogFlowThisFunc(("\n"));
228
229 /* Enclose the state transition Ready->InUninit->NotReady. */
230 AutoUninitSpan autoUninitSpan(this);
231 if (autoUninitSpan.uninitDone())
232 return;
233
234#ifdef VBOX_WITH_GUEST_CONTROL
235 baseUninit();
236
237 mEventSource->UnregisterListener(mLocalListener);
238 unconst(mEventSource).setNull();
239#endif
240
241 LogFlowThisFuncLeave();
242}
243
244// implementation of public getters/setters for attributes
245/////////////////////////////////////////////////////////////////////////////
246
247STDMETHODIMP GuestFile::COMGETTER(CreationMode)(ULONG *aCreationMode)
248{
249#ifndef VBOX_WITH_GUEST_CONTROL
250 ReturnComNotImplemented();
251#else
252 AutoCaller autoCaller(this);
253 if (FAILED(autoCaller.rc())) return autoCaller.rc();
254
255 CheckComArgOutPointerValid(aCreationMode);
256
257 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
258
259 *aCreationMode = mData.mOpenInfo.mCreationMode;
260
261 return S_OK;
262#endif /* VBOX_WITH_GUEST_CONTROL */
263}
264
265STDMETHODIMP GuestFile::COMGETTER(Disposition)(BSTR *aDisposition)
266{
267#ifndef VBOX_WITH_GUEST_CONTROL
268 ReturnComNotImplemented();
269#else
270 AutoCaller autoCaller(this);
271 if (FAILED(autoCaller.rc())) return autoCaller.rc();
272
273 CheckComArgOutPointerValid(aDisposition);
274
275 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
276
277 mData.mOpenInfo.mDisposition.cloneTo(aDisposition);
278
279 return S_OK;
280#endif /* VBOX_WITH_GUEST_CONTROL */
281}
282
283STDMETHODIMP GuestFile::COMGETTER(EventSource)(IEventSource ** aEventSource)
284{
285#ifndef VBOX_WITH_GUEST_CONTROL
286 ReturnComNotImplemented();
287#else
288 CheckComArgOutPointerValid(aEventSource);
289
290 AutoCaller autoCaller(this);
291 if (FAILED(autoCaller.rc())) return autoCaller.rc();
292
293 /* No need to lock - lifetime constant. */
294 mEventSource.queryInterfaceTo(aEventSource);
295
296 return S_OK;
297#endif /* VBOX_WITH_GUEST_CONTROL */
298}
299
300STDMETHODIMP GuestFile::COMGETTER(FileName)(BSTR *aFileName)
301{
302#ifndef VBOX_WITH_GUEST_CONTROL
303 ReturnComNotImplemented();
304#else
305 AutoCaller autoCaller(this);
306 if (FAILED(autoCaller.rc())) return autoCaller.rc();
307
308 CheckComArgOutPointerValid(aFileName);
309
310 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
311
312 mData.mOpenInfo.mFileName.cloneTo(aFileName);
313
314 return S_OK;
315#endif /* VBOX_WITH_GUEST_CONTROL */
316}
317
318STDMETHODIMP GuestFile::COMGETTER(Id)(ULONG *aID)
319{
320#ifndef VBOX_WITH_GUEST_CONTROL
321 ReturnComNotImplemented();
322#else
323 AutoCaller autoCaller(this);
324 if (FAILED(autoCaller.rc())) return autoCaller.rc();
325
326 CheckComArgOutPointerValid(aID);
327
328 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
329
330 *aID = mData.mID;
331
332 return S_OK;
333#endif /* VBOX_WITH_GUEST_CONTROL */
334}
335
336STDMETHODIMP GuestFile::COMGETTER(InitialSize)(LONG64 *aInitialSize)
337{
338#ifndef VBOX_WITH_GUEST_CONTROL
339 ReturnComNotImplemented();
340#else
341 AutoCaller autoCaller(this);
342 if (FAILED(autoCaller.rc())) return autoCaller.rc();
343
344 CheckComArgOutPointerValid(aInitialSize);
345
346 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
347
348 *aInitialSize = mData.mInitialSize;
349
350 return S_OK;
351#endif /* VBOX_WITH_GUEST_CONTROL */
352}
353
354STDMETHODIMP GuestFile::COMGETTER(Offset)(LONG64 *aOffset)
355{
356#ifndef VBOX_WITH_GUEST_CONTROL
357 ReturnComNotImplemented();
358#else
359 AutoCaller autoCaller(this);
360 if (FAILED(autoCaller.rc())) return autoCaller.rc();
361
362 CheckComArgOutPointerValid(aOffset);
363
364 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
365
366 *aOffset = mData.mOffCurrent;
367
368 return S_OK;
369#endif /* VBOX_WITH_GUEST_CONTROL */
370}
371
372STDMETHODIMP GuestFile::COMGETTER(OpenMode)(BSTR *aOpenMode)
373{
374#ifndef VBOX_WITH_GUEST_CONTROL
375 ReturnComNotImplemented();
376#else
377 AutoCaller autoCaller(this);
378 if (FAILED(autoCaller.rc())) return autoCaller.rc();
379
380 CheckComArgOutPointerValid(aOpenMode);
381
382 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
383
384 mData.mOpenInfo.mOpenMode.cloneTo(aOpenMode);
385
386 return S_OK;
387#endif /* VBOX_WITH_GUEST_CONTROL */
388}
389
390STDMETHODIMP GuestFile::COMGETTER(Status)(FileStatus_T *aStatus)
391{
392#ifndef VBOX_WITH_GUEST_CONTROL
393 ReturnComNotImplemented();
394#else
395 LogFlowThisFuncEnter();
396
397 AutoCaller autoCaller(this);
398 if (FAILED(autoCaller.rc())) return autoCaller.rc();
399
400 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
401
402 *aStatus = mData.mStatus;
403
404 return S_OK;
405#endif /* VBOX_WITH_GUEST_CONTROL */
406}
407
408// private methods
409/////////////////////////////////////////////////////////////////////////////
410
411int GuestFile::callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
412{
413#ifdef DEBUG
414 LogFlowThisFunc(("strName=%s, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
415 mData.mOpenInfo.mFileName.c_str(), pCbCtx->uContextID, pCbCtx->uFunction, pSvcCb));
416#endif
417 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
418
419 int vrc;
420 switch (pCbCtx->uFunction)
421 {
422 case GUEST_DISCONNECTED:
423 vrc = onGuestDisconnected(pCbCtx, pSvcCb);
424 break;
425
426 case GUEST_FILE_NOTIFY:
427 vrc = onFileNotify(pCbCtx, pSvcCb);
428 break;
429
430 default:
431 /* Silently ignore not implemented functions. */
432 vrc = VERR_NOT_SUPPORTED;
433 break;
434 }
435
436#ifdef DEBUG
437 LogFlowFuncLeaveRC(vrc);
438#endif
439 return vrc;
440}
441
442int GuestFile::closeFile(int *pGuestRc)
443{
444 LogFlowThisFunc(("strFile=%s\n", mData.mOpenInfo.mFileName.c_str()));
445
446 int vrc;
447
448 GuestWaitEvent *pEvent = NULL;
449 std::list < VBoxEventType_T > eventTypes;
450 try
451 {
452 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
453
454 vrc = registerWaitEvent(eventTypes, &pEvent);
455 }
456 catch (std::bad_alloc)
457 {
458 vrc = VERR_NO_MEMORY;
459 }
460
461 if (RT_FAILURE(vrc))
462 return vrc;
463
464 /* Prepare HGCM call. */
465 VBOXHGCMSVCPARM paParms[4];
466 int i = 0;
467 paParms[i++].setUInt32(pEvent->ContextID());
468 paParms[i++].setUInt32(mData.mID /* Guest file ID */);
469
470 vrc = sendCommand(HOST_FILE_CLOSE, i, paParms);
471 if (RT_SUCCESS(vrc))
472 vrc = waitForStatusChange(pEvent, 30 * 1000 /* Timeout in ms */,
473 NULL /* FileStatus */, pGuestRc);
474 unregisterWaitEvent(pEvent);
475
476 LogFlowFuncLeaveRC(vrc);
477 return vrc;
478}
479
480/* static */
481Utf8Str GuestFile::guestErrorToString(int guestRc)
482{
483 Utf8Str strError;
484
485 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
486 switch (guestRc)
487 {
488 case VERR_ALREADY_EXISTS:
489 strError += Utf8StrFmt(tr("File already exists"));
490 break;
491
492 case VERR_FILE_NOT_FOUND:
493 strError += Utf8StrFmt(tr("File not found"));
494 break;
495
496 case VERR_NET_HOST_NOT_FOUND:
497 strError += Utf8StrFmt(tr("Host name not found"));
498 break;
499
500 case VERR_SHARING_VIOLATION:
501 strError += Utf8StrFmt(tr("Sharing violation"));
502 break;
503
504 default:
505 strError += Utf8StrFmt("%Rrc", guestRc);
506 break;
507 }
508
509 return strError;
510}
511
512int GuestFile::onFileNotify(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
513{
514 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
515 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
516
517 LogFlowThisFuncEnter();
518
519 if (pSvcCbData->mParms < 3)
520 return VERR_INVALID_PARAMETER;
521
522 int vrc = VINF_SUCCESS;
523
524 int idx = 1; /* Current parameter index. */
525 CALLBACKDATA_FILE_NOTIFY dataCb;
526 /* pSvcCb->mpaParms[0] always contains the context ID. */
527 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.uType);
528 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.rc);
529
530 FileStatus_T fileStatus = FileStatus_Undefined;
531 int guestRc = (int)dataCb.rc; /* uint32_t vs. int. */
532
533 LogFlowFunc(("uType=%RU32, guestRc=%Rrc\n",
534 dataCb.uType, guestRc));
535
536 if (RT_FAILURE(guestRc))
537 {
538 int rc2 = setFileStatus(FileStatus_Error, guestRc);
539 AssertRC(rc2);
540
541 return VINF_SUCCESS; /* Report to the guest. */
542 }
543
544 switch (dataCb.uType)
545 {
546 case GUEST_FILE_NOTIFYTYPE_ERROR:
547 {
548 int rc2 = setFileStatus(FileStatus_Error, guestRc);
549 AssertRC(rc2);
550 break;
551 }
552
553 case GUEST_FILE_NOTIFYTYPE_OPEN:
554 {
555 if (pSvcCbData->mParms == 4)
556 {
557 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.u.open.uHandle);
558
559 {
560 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
561 AssertMsg(mData.mID == VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCbCtx->uContextID),
562 ("File ID %RU32 does not match context ID %RU32\n", mData.mID,
563 VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCbCtx->uContextID)));
564
565 /* Set the initial offset. On the guest the whole opening operation
566 * would fail if an initial seek isn't possible. */
567 mData.mOffCurrent = mData.mOpenInfo.mInitialOffset;
568 }
569
570 /* Set the process status. */
571 int rc2 = setFileStatus(FileStatus_Open, guestRc);
572 if (RT_SUCCESS(vrc))
573 vrc = rc2;
574 }
575 else
576 vrc = VERR_NOT_SUPPORTED;
577
578 break;
579 }
580
581 case GUEST_FILE_NOTIFYTYPE_CLOSE:
582 {
583 int rc2 = setFileStatus(FileStatus_Closed, guestRc);
584 AssertRC(rc2);
585
586 break;
587 }
588
589 case GUEST_FILE_NOTIFYTYPE_READ:
590 {
591 if (pSvcCbData->mParms == 4)
592 {
593 pSvcCbData->mpaParms[idx++].getPointer(&dataCb.u.read.pvData,
594 &dataCb.u.read.cbData);
595 uint32_t cbRead = dataCb.u.read.cbData;
596
597 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
598
599 mData.mOffCurrent += cbRead;
600
601 alock.release();
602
603 com::SafeArray<BYTE> data((size_t)cbRead);
604 data.initFrom((BYTE*)dataCb.u.read.pvData, cbRead);
605
606 fireGuestFileReadEvent(mEventSource, mSession, this, mData.mOffCurrent,
607 cbRead, ComSafeArrayAsInParam(data));
608 }
609 else
610 vrc = VERR_NOT_SUPPORTED;
611 break;
612 }
613
614 case GUEST_FILE_NOTIFYTYPE_WRITE:
615 {
616 if (pSvcCbData->mParms == 4)
617 {
618 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.u.write.cbWritten);
619
620 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
621
622 mData.mOffCurrent += dataCb.u.write.cbWritten;
623 uint64_t uOffCurrent = mData.mOffCurrent;
624
625 alock.release();
626
627 fireGuestFileWriteEvent(mEventSource, mSession, this, uOffCurrent,
628 dataCb.u.write.cbWritten);
629 }
630 else
631 vrc = VERR_NOT_SUPPORTED;
632 break;
633 }
634
635 case GUEST_FILE_NOTIFYTYPE_SEEK:
636 {
637 if (pSvcCbData->mParms == 4)
638 {
639 pSvcCbData->mpaParms[idx++].getUInt64(&dataCb.u.seek.uOffActual);
640
641 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
642
643 mData.mOffCurrent = dataCb.u.seek.uOffActual;
644
645 alock.release();
646
647 fireGuestFileOffsetChangedEvent(mEventSource, mSession, this,
648 dataCb.u.seek.uOffActual, 0 /* Processed */);
649 }
650 else
651 vrc = VERR_NOT_SUPPORTED;
652 break;
653 }
654
655 case GUEST_FILE_NOTIFYTYPE_TELL:
656 {
657 if (pSvcCbData->mParms == 4)
658 {
659 pSvcCbData->mpaParms[idx++].getUInt64(&dataCb.u.tell.uOffActual);
660
661 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
662
663 mData.mOffCurrent = dataCb.u.tell.uOffActual;
664
665 alock.release();
666
667 fireGuestFileOffsetChangedEvent(mEventSource, mSession, this,
668 dataCb.u.tell.uOffActual, 0 /* Processed */);
669 }
670 else
671 vrc = VERR_NOT_SUPPORTED;
672 break;
673 }
674
675 default:
676 vrc = VERR_NOT_SUPPORTED;
677 break;
678 }
679
680 LogFlowThisFunc(("uType=%RU32, guestRc=%Rrc\n",
681 dataCb.uType, dataCb.rc));
682
683 LogFlowFuncLeaveRC(vrc);
684 return vrc;
685}
686
687int GuestFile::onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
688{
689 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
690 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
691
692 int vrc = setFileStatus(FileStatus_Down, VINF_SUCCESS);
693
694 LogFlowFuncLeaveRC(vrc);
695 return vrc;
696}
697
698int GuestFile::openFile(uint32_t uTimeoutMS, int *pGuestRc)
699{
700 LogFlowThisFuncEnter();
701
702 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
703
704 LogFlowThisFunc(("strFile=%s, strOpenMode=%s, strDisposition=%s, uCreationMode=%RU32, uOffset=%RU64\n",
705 mData.mOpenInfo.mFileName.c_str(), mData.mOpenInfo.mOpenMode.c_str(),
706 mData.mOpenInfo.mDisposition.c_str(), mData.mOpenInfo.mCreationMode, mData.mOpenInfo.mInitialOffset));
707 int vrc;
708
709 GuestWaitEvent *pEvent = NULL;
710 std::list < VBoxEventType_T > eventTypes;
711 try
712 {
713 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
714
715 vrc = registerWaitEvent(eventTypes, &pEvent);
716 }
717 catch (std::bad_alloc)
718 {
719 vrc = VERR_NO_MEMORY;
720 }
721
722 if (RT_FAILURE(vrc))
723 return vrc;
724
725 /* Prepare HGCM call. */
726 VBOXHGCMSVCPARM paParms[8];
727 int i = 0;
728 paParms[i++].setUInt32(pEvent->ContextID());
729 paParms[i++].setPointer((void*)mData.mOpenInfo.mFileName.c_str(),
730 (ULONG)mData.mOpenInfo.mFileName.length() + 1);
731 paParms[i++].setPointer((void*)mData.mOpenInfo.mOpenMode.c_str(),
732 (ULONG)mData.mOpenInfo.mOpenMode.length() + 1);
733 paParms[i++].setPointer((void*)mData.mOpenInfo.mDisposition.c_str(),
734 (ULONG)mData.mOpenInfo.mDisposition.length() + 1);
735 paParms[i++].setPointer((void*)mData.mOpenInfo.mSharingMode.c_str(),
736 (ULONG)mData.mOpenInfo.mSharingMode.length() + 1);
737 paParms[i++].setUInt32(mData.mOpenInfo.mCreationMode);
738 paParms[i++].setUInt64(mData.mOpenInfo.mInitialOffset);
739
740 alock.release(); /* Drop read lock before sending. */
741
742 vrc = sendCommand(HOST_FILE_OPEN, i, paParms);
743 if (RT_SUCCESS(vrc))
744 vrc = waitForStatusChange(pEvent, uTimeoutMS,
745 NULL /* FileStatus */, pGuestRc);
746
747 unregisterWaitEvent(pEvent);
748
749 LogFlowFuncLeaveRC(vrc);
750 return vrc;
751}
752
753int GuestFile::readData(uint32_t uSize, uint32_t uTimeoutMS,
754 void* pvData, uint32_t cbData, uint32_t* pcbRead)
755{
756 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
757 AssertReturn(cbData, VERR_INVALID_PARAMETER);
758
759 LogFlowThisFunc(("uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
760 uSize, uTimeoutMS, pvData, cbData));
761 int vrc;
762
763 GuestWaitEvent *pEvent = NULL;
764 std::list < VBoxEventType_T > eventTypes;
765 try
766 {
767 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
768 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
769
770 vrc = registerWaitEvent(eventTypes, &pEvent);
771 }
772 catch (std::bad_alloc)
773 {
774 vrc = VERR_NO_MEMORY;
775 }
776
777 if (RT_FAILURE(vrc))
778 return vrc;
779
780 /* Prepare HGCM call. */
781 VBOXHGCMSVCPARM paParms[4];
782 int i = 0;
783 paParms[i++].setUInt32(pEvent->ContextID());
784 paParms[i++].setUInt32(mData.mID /* File handle */);
785 paParms[i++].setUInt32(uSize /* Size (in bytes) to read */);
786
787 uint32_t cbRead;
788 vrc = sendCommand(HOST_FILE_READ, i, paParms);
789 if (RT_SUCCESS(vrc))
790 vrc = waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
791
792 if (RT_SUCCESS(vrc))
793 {
794 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
795
796 if (pcbRead)
797 *pcbRead = cbRead;
798 }
799
800 unregisterWaitEvent(pEvent);
801
802 LogFlowFuncLeaveRC(vrc);
803 return vrc;
804}
805
806int GuestFile::readDataAt(uint64_t uOffset, uint32_t uSize, uint32_t uTimeoutMS,
807 void* pvData, size_t cbData, size_t* pcbRead)
808{
809 LogFlowThisFunc(("uOffset=%RU64, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
810 uOffset, uSize, uTimeoutMS, pvData, cbData));
811 int vrc;
812
813 GuestWaitEvent *pEvent = NULL;
814 std::list < VBoxEventType_T > eventTypes;
815 try
816 {
817 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
818 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
819
820 vrc = registerWaitEvent(eventTypes, &pEvent);
821 }
822 catch (std::bad_alloc)
823 {
824 vrc = VERR_NO_MEMORY;
825 }
826
827 if (RT_FAILURE(vrc))
828 return vrc;
829
830 /* Prepare HGCM call. */
831 VBOXHGCMSVCPARM paParms[4];
832 int i = 0;
833 paParms[i++].setUInt32(pEvent->ContextID());
834 paParms[i++].setUInt32(mData.mID /* File handle */);
835 paParms[i++].setUInt64(uOffset /* Offset (in bytes) to start reading */);
836 paParms[i++].setUInt32(uSize /* Size (in bytes) to read */);
837
838 uint32_t cbRead;
839 vrc = sendCommand(HOST_FILE_READ_AT, i, paParms);
840 if (RT_SUCCESS(vrc))
841 vrc = waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
842
843 if (RT_SUCCESS(vrc))
844 {
845 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
846
847 if (pcbRead)
848 *pcbRead = cbRead;
849 }
850
851 unregisterWaitEvent(pEvent);
852
853 LogFlowFuncLeaveRC(vrc);
854 return vrc;
855}
856
857int GuestFile::seekAt(int64_t iOffset, GUEST_FILE_SEEKTYPE eSeekType,
858 uint32_t uTimeoutMS, uint64_t *puOffset)
859{
860 LogFlowThisFunc(("iOffset=%RI64, uTimeoutMS=%RU32\n",
861 iOffset, uTimeoutMS));
862 int vrc;
863
864 GuestWaitEvent *pEvent = NULL;
865 std::list < VBoxEventType_T > eventTypes;
866 try
867 {
868 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
869 eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
870
871 vrc = registerWaitEvent(eventTypes, &pEvent);
872 }
873 catch (std::bad_alloc)
874 {
875 vrc = VERR_NO_MEMORY;
876 }
877
878 if (RT_FAILURE(vrc))
879 return vrc;
880
881 /* Prepare HGCM call. */
882 VBOXHGCMSVCPARM paParms[4];
883 int i = 0;
884 paParms[i++].setUInt32(pEvent->ContextID());
885 paParms[i++].setUInt32(mData.mID /* File handle */);
886 paParms[i++].setUInt32(eSeekType /* Seek method */);
887 /** @todo uint64_t vs. int64_t! */
888 paParms[i++].setUInt64((uint64_t)iOffset /* Offset (in bytes) to start reading */);
889
890 vrc = sendCommand(HOST_FILE_SEEK, i, paParms);
891 if (RT_SUCCESS(vrc))
892 vrc = waitForOffsetChange(pEvent, uTimeoutMS, puOffset);
893
894 unregisterWaitEvent(pEvent);
895
896 LogFlowFuncLeaveRC(vrc);
897 return vrc;
898}
899
900/* static */
901HRESULT GuestFile::setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
902{
903 AssertPtr(pInterface);
904 AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
905
906 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestFile::guestErrorToString(guestRc).c_str());
907}
908
909int GuestFile::setFileStatus(FileStatus_T fileStatus, int fileRc)
910{
911 LogFlowThisFuncEnter();
912
913 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
914
915 LogFlowThisFunc(("oldStatus=%ld, newStatus=%ld, fileRc=%Rrc\n",
916 mData.mStatus, fileStatus, fileRc));
917
918#ifdef VBOX_STRICT
919 if (fileStatus == FileStatus_Error)
920 {
921 AssertMsg(RT_FAILURE(fileRc), ("Guest rc must be an error (%Rrc)\n", fileRc));
922 }
923 else
924 AssertMsg(RT_SUCCESS(fileRc), ("Guest rc must not be an error (%Rrc)\n", fileRc));
925#endif
926
927 if (mData.mStatus != fileStatus)
928 {
929 mData.mStatus = fileStatus;
930 mData.mLastError = fileRc;
931
932 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
933 HRESULT hr = errorInfo.createObject();
934 ComAssertComRC(hr);
935 if (RT_FAILURE(fileRc))
936 {
937 hr = errorInfo->initEx(VBOX_E_IPRT_ERROR, fileRc,
938 COM_IIDOF(IGuestFile), getComponentName(),
939 guestErrorToString(fileRc));
940 ComAssertComRC(hr);
941 }
942
943 alock.release(); /* Release lock before firing off event. */
944
945 fireGuestFileStateChangedEvent(mEventSource, mSession,
946 this, fileStatus, errorInfo);
947 }
948
949 return VINF_SUCCESS;
950}
951
952int GuestFile::waitForOffsetChange(GuestWaitEvent *pEvent,
953 uint32_t uTimeoutMS, uint64_t *puOffset)
954{
955 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
956
957 VBoxEventType_T evtType;
958 ComPtr<IEvent> pIEvent;
959 int vrc = waitForEvent(pEvent, uTimeoutMS,
960 &evtType, pIEvent.asOutParam());
961 if (RT_SUCCESS(vrc))
962 {
963 if (evtType == VBoxEventType_OnGuestFileOffsetChanged)
964 {
965 if (puOffset)
966 {
967 ComPtr<IGuestFileOffsetChangedEvent> pFileEvent = pIEvent;
968 Assert(!pFileEvent.isNull());
969
970 HRESULT hr = pFileEvent->COMGETTER(Offset)((LONG64*)puOffset);
971 ComAssertComRC(hr);
972 }
973 }
974 else
975 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
976 }
977
978 return vrc;
979}
980
981int GuestFile::waitForRead(GuestWaitEvent *pEvent,
982 uint32_t uTimeoutMS, void *pvData, size_t cbData, uint32_t *pcbRead)
983{
984 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
985
986 VBoxEventType_T evtType;
987 ComPtr<IEvent> pIEvent;
988 int vrc = waitForEvent(pEvent, uTimeoutMS,
989 &evtType, pIEvent.asOutParam());
990 if (RT_SUCCESS(vrc))
991 {
992 if (evtType == VBoxEventType_OnGuestFileRead)
993 {
994 ComPtr<IGuestFileReadEvent> pFileEvent = pIEvent;
995 Assert(!pFileEvent.isNull());
996
997 HRESULT hr;
998 if (pvData)
999 {
1000 com::SafeArray <BYTE> data;
1001 hr = pFileEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
1002 ComAssertComRC(hr);
1003 size_t cbRead = data.size();
1004 if ( cbRead
1005 && cbRead <= cbData)
1006 {
1007 memcpy(pvData, data.raw(), data.size());
1008 }
1009 else
1010 vrc = VERR_BUFFER_OVERFLOW;
1011 }
1012 if (pcbRead)
1013 {
1014 hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbRead);
1015 ComAssertComRC(hr);
1016 }
1017 }
1018 else
1019 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1020 }
1021
1022 return vrc;
1023}
1024
1025int GuestFile::waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1026 FileStatus_T *pFileStatus, int *pGuestRc)
1027{
1028 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1029 /* pFileStatus is optional. */
1030
1031 VBoxEventType_T evtType;
1032 ComPtr<IEvent> pIEvent;
1033 int vrc = waitForEvent(pEvent, uTimeoutMS,
1034 &evtType, pIEvent.asOutParam());
1035 if (RT_SUCCESS(vrc))
1036 {
1037 Assert(evtType == VBoxEventType_OnGuestFileStateChanged);
1038 ComPtr<IGuestFileStateChangedEvent> pFileEvent = pIEvent;
1039 Assert(!pFileEvent.isNull());
1040
1041 HRESULT hr;
1042 if (pFileStatus)
1043 {
1044 hr = pFileEvent->COMGETTER(Status)(pFileStatus);
1045 ComAssertComRC(hr);
1046 }
1047
1048 ComPtr<IVirtualBoxErrorInfo> errorInfo;
1049 hr = pFileEvent->COMGETTER(Error)(errorInfo.asOutParam());
1050 ComAssertComRC(hr);
1051
1052 LONG lGuestRc;
1053 hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
1054 ComAssertComRC(hr);
1055
1056 LogFlowThisFunc(("resultDetail=%RI32 (rc=%Rrc)\n",
1057 lGuestRc, lGuestRc));
1058
1059 if (RT_FAILURE((int)lGuestRc))
1060 vrc = VERR_GSTCTL_GUEST_ERROR;
1061
1062 if (pGuestRc)
1063 *pGuestRc = (int)lGuestRc;
1064 }
1065
1066 return vrc;
1067}
1068
1069int GuestFile::waitForWrite(GuestWaitEvent *pEvent,
1070 uint32_t uTimeoutMS, uint32_t *pcbWritten)
1071{
1072 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1073
1074 VBoxEventType_T evtType;
1075 ComPtr<IEvent> pIEvent;
1076 int vrc = waitForEvent(pEvent, uTimeoutMS,
1077 &evtType, pIEvent.asOutParam());
1078 if (RT_SUCCESS(vrc))
1079 {
1080 if (evtType == VBoxEventType_OnGuestFileWrite)
1081 {
1082 if (pcbWritten)
1083 {
1084 ComPtr<IGuestFileWriteEvent> pFileEvent = pIEvent;
1085 Assert(!pFileEvent.isNull());
1086
1087 HRESULT hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbWritten);
1088 ComAssertComRC(hr);
1089 }
1090 }
1091 else
1092 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1093 }
1094
1095 return vrc;
1096}
1097
1098int GuestFile::writeData(uint32_t uTimeoutMS, void *pvData, uint32_t cbData,
1099 uint32_t *pcbWritten)
1100{
1101 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1102 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1103
1104 LogFlowThisFunc(("uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1105 uTimeoutMS, pvData, cbData));
1106 int vrc;
1107
1108 GuestWaitEvent *pEvent = NULL;
1109 std::list < VBoxEventType_T > eventTypes;
1110 try
1111 {
1112 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1113 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1114
1115 vrc = registerWaitEvent(eventTypes, &pEvent);
1116 }
1117 catch (std::bad_alloc)
1118 {
1119 vrc = VERR_NO_MEMORY;
1120 }
1121
1122 if (RT_FAILURE(vrc))
1123 return vrc;
1124
1125 /* Prepare HGCM call. */
1126 VBOXHGCMSVCPARM paParms[8];
1127 int i = 0;
1128 paParms[i++].setUInt32(pEvent->ContextID());
1129 paParms[i++].setUInt32(mData.mID /* File handle */);
1130 paParms[i++].setUInt32(cbData /* Size (in bytes) to write */);
1131 paParms[i++].setPointer(pvData, cbData);
1132
1133 uint32_t cbWritten;
1134 vrc = sendCommand(HOST_FILE_WRITE, i, paParms);
1135 if (RT_SUCCESS(vrc))
1136 vrc = waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1137
1138 if (RT_SUCCESS(vrc))
1139 {
1140 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1141
1142 if (cbWritten)
1143 *pcbWritten = cbWritten;
1144 }
1145
1146 unregisterWaitEvent(pEvent);
1147
1148 LogFlowFuncLeaveRC(vrc);
1149 return vrc;
1150}
1151
1152int GuestFile::writeDataAt(uint64_t uOffset, uint32_t uTimeoutMS,
1153 void *pvData, uint32_t cbData, uint32_t *pcbWritten)
1154{
1155 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1156 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1157
1158 LogFlowThisFunc(("uOffset=%RU64, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1159 uOffset, uTimeoutMS, pvData, cbData));
1160 int vrc;
1161
1162 GuestWaitEvent *pEvent = NULL;
1163 std::list < VBoxEventType_T > eventTypes;
1164 try
1165 {
1166 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1167 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1168
1169 vrc = registerWaitEvent(eventTypes, &pEvent);
1170 }
1171 catch (std::bad_alloc)
1172 {
1173 vrc = VERR_NO_MEMORY;
1174 }
1175
1176 if (RT_FAILURE(vrc))
1177 return vrc;
1178
1179 /* Prepare HGCM call. */
1180 VBOXHGCMSVCPARM paParms[8];
1181 int i = 0;
1182 paParms[i++].setUInt32(pEvent->ContextID());
1183 paParms[i++].setUInt32(mData.mID /* File handle */);
1184 paParms[i++].setUInt64(uOffset /* Offset where to starting writing */);
1185 paParms[i++].setUInt32(cbData /* Size (in bytes) to write */);
1186 paParms[i++].setPointer(pvData, cbData);
1187
1188 uint32_t cbWritten;
1189 vrc = sendCommand(HOST_FILE_WRITE_AT, i, paParms);
1190 if (RT_SUCCESS(vrc))
1191 vrc = waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1192
1193 if (RT_SUCCESS(vrc))
1194 {
1195 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1196
1197 if (cbWritten)
1198 *pcbWritten = cbWritten;
1199 }
1200
1201 unregisterWaitEvent(pEvent);
1202
1203 LogFlowFuncLeaveRC(vrc);
1204 return vrc;
1205}
1206
1207// implementation of public methods
1208/////////////////////////////////////////////////////////////////////////////
1209
1210STDMETHODIMP GuestFile::Close(void)
1211{
1212#ifndef VBOX_WITH_GUEST_CONTROL
1213 ReturnComNotImplemented();
1214#else
1215 LogFlowThisFuncEnter();
1216
1217 AutoCaller autoCaller(this);
1218 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1219
1220 /* Close file on guest. */
1221 int guestRc;
1222 int rc = closeFile(&guestRc);
1223 /* On failure don't return here, instead do all the cleanup
1224 * work first and then return an error. */
1225
1226 AssertPtr(mSession);
1227 int rc2 = mSession->fileRemoveFromList(this);
1228 if (RT_SUCCESS(rc))
1229 rc = rc2;
1230
1231 if (RT_FAILURE(rc))
1232 {
1233 if (rc == VERR_GSTCTL_GUEST_ERROR)
1234 return GuestFile::setErrorExternal(this, guestRc);
1235
1236 return setError(VBOX_E_IPRT_ERROR,
1237 tr("Closing guest file failed with %Rrc\n"), rc);
1238 }
1239
1240 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
1241 return S_OK;
1242#endif /* VBOX_WITH_GUEST_CONTROL */
1243}
1244
1245STDMETHODIMP GuestFile::QueryInfo(IFsObjInfo **aInfo)
1246{
1247#ifndef VBOX_WITH_GUEST_CONTROL
1248 ReturnComNotImplemented();
1249#else
1250 AutoCaller autoCaller(this);
1251 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1252
1253 ReturnComNotImplemented();
1254#endif /* VBOX_WITH_GUEST_CONTROL */
1255}
1256
1257STDMETHODIMP GuestFile::Read(ULONG aToRead, ULONG aTimeoutMS, ComSafeArrayOut(BYTE, aData))
1258{
1259#ifndef VBOX_WITH_GUEST_CONTROL
1260 ReturnComNotImplemented();
1261#else
1262 if (aToRead == 0)
1263 return setError(E_INVALIDARG, tr("The size to read is zero"));
1264 CheckComArgOutSafeArrayPointerValid(aData);
1265
1266 AutoCaller autoCaller(this);
1267 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1268
1269 com::SafeArray<BYTE> data((size_t)aToRead);
1270 Assert(data.size() >= aToRead);
1271
1272 HRESULT hr = S_OK;
1273
1274 uint32_t cbRead;
1275 int vrc = readData(aToRead, aTimeoutMS,
1276 data.raw(), aToRead, &cbRead);
1277 if (RT_SUCCESS(vrc))
1278 {
1279 if (data.size() != cbRead)
1280 data.resize(cbRead);
1281 data.detachTo(ComSafeArrayOutArg(aData));
1282 }
1283 else
1284 {
1285 switch (vrc)
1286 {
1287 default:
1288 hr = setError(VBOX_E_IPRT_ERROR,
1289 tr("Reading from file \"%s\" failed: %Rrc"),
1290 mData.mOpenInfo.mFileName.c_str(), vrc);
1291 break;
1292 }
1293 }
1294
1295 LogFlowFuncLeaveRC(vrc);
1296 return hr;
1297#endif /* VBOX_WITH_GUEST_CONTROL */
1298}
1299
1300STDMETHODIMP GuestFile::ReadAt(LONG64 aOffset, ULONG aToRead, ULONG aTimeoutMS, ComSafeArrayOut(BYTE, aData))
1301{
1302#ifndef VBOX_WITH_GUEST_CONTROL
1303 ReturnComNotImplemented();
1304#else
1305 if (aToRead == 0)
1306 return setError(E_INVALIDARG, tr("The size to read is zero"));
1307 CheckComArgOutSafeArrayPointerValid(aData);
1308
1309 AutoCaller autoCaller(this);
1310 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1311
1312 com::SafeArray<BYTE> data((size_t)aToRead);
1313 Assert(data.size() >= aToRead);
1314
1315 HRESULT hr = S_OK;
1316
1317 size_t cbRead;
1318 int vrc = readDataAt(aOffset, aToRead, aTimeoutMS,
1319 data.raw(), aToRead, &cbRead);
1320 if (RT_SUCCESS(vrc))
1321 {
1322 if (data.size() != cbRead)
1323 data.resize(cbRead);
1324 data.detachTo(ComSafeArrayOutArg(aData));
1325 }
1326 else
1327 {
1328 switch (vrc)
1329 {
1330 default:
1331 hr = setError(VBOX_E_IPRT_ERROR,
1332 tr("Reading from file \"%s\" (at offset %RU64) failed: %Rrc"),
1333 mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
1334 break;
1335 }
1336 }
1337
1338 LogFlowFuncLeaveRC(vrc);
1339 return hr;
1340#endif /* VBOX_WITH_GUEST_CONTROL */
1341}
1342
1343STDMETHODIMP GuestFile::Seek(LONG64 aOffset, FileSeekType_T aType)
1344{
1345#ifndef VBOX_WITH_GUEST_CONTROL
1346 ReturnComNotImplemented();
1347#else
1348 LogFlowThisFuncEnter();
1349
1350 AutoCaller autoCaller(this);
1351 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1352
1353 HRESULT hr = S_OK;
1354
1355 GUEST_FILE_SEEKTYPE eSeekType;
1356 switch (aType)
1357 {
1358 case FileSeekType_Set:
1359 eSeekType = GUEST_FILE_SEEKTYPE_BEGIN;
1360 break;
1361
1362 case FileSeekType_Current:
1363 eSeekType = GUEST_FILE_SEEKTYPE_CURRENT;
1364 break;
1365
1366 default:
1367 return setError(E_INVALIDARG, tr("Invalid seek type specified"));
1368 break; /* Never reached. */
1369 }
1370
1371 int vrc = seekAt(aOffset, eSeekType,
1372 30 * 1000 /* 30s timeout */, NULL /* puOffset */);
1373 if (RT_FAILURE(vrc))
1374 {
1375 switch (vrc)
1376 {
1377 default:
1378 hr = setError(VBOX_E_IPRT_ERROR,
1379 tr("Seeking file \"%s\" (to offset %RI64) failed: %Rrc"),
1380 mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
1381 break;
1382 }
1383 }
1384
1385 LogFlowFuncLeaveRC(vrc);
1386 return hr;
1387#endif /* VBOX_WITH_GUEST_CONTROL */
1388}
1389
1390STDMETHODIMP GuestFile::SetACL(IN_BSTR aACL)
1391{
1392#ifndef VBOX_WITH_GUEST_CONTROL
1393 ReturnComNotImplemented();
1394#else
1395 AutoCaller autoCaller(this);
1396 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1397
1398 ReturnComNotImplemented();
1399#endif /* VBOX_WITH_GUEST_CONTROL */
1400}
1401
1402STDMETHODIMP GuestFile::Write(ComSafeArrayIn(BYTE, aData), ULONG aTimeoutMS, ULONG *aWritten)
1403{
1404#ifndef VBOX_WITH_GUEST_CONTROL
1405 ReturnComNotImplemented();
1406#else
1407 LogFlowThisFuncEnter();
1408
1409 CheckComArgSafeArrayNotNull(aData);
1410 CheckComArgOutPointerValid(aWritten);
1411
1412 AutoCaller autoCaller(this);
1413 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1414
1415 HRESULT hr = S_OK;
1416
1417 com::SafeArray<BYTE> data(ComSafeArrayInArg(aData));
1418 int vrc = writeData(aTimeoutMS, data.raw(), (uint32_t)data.size(),
1419 (uint32_t*)aWritten);
1420 if (RT_FAILURE(vrc))
1421 {
1422 switch (vrc)
1423 {
1424 default:
1425 hr = setError(VBOX_E_IPRT_ERROR,
1426 tr("Writing %zubytes to file \"%s\" failed: %Rrc"),
1427 data.size(), mData.mOpenInfo.mFileName.c_str(), vrc);
1428 break;
1429 }
1430 }
1431
1432 LogFlowFuncLeaveRC(vrc);
1433 return hr;
1434#endif /* VBOX_WITH_GUEST_CONTROL */
1435}
1436
1437STDMETHODIMP GuestFile::WriteAt(LONG64 aOffset, ComSafeArrayIn(BYTE, aData), ULONG aTimeoutMS, ULONG *aWritten)
1438{
1439#ifndef VBOX_WITH_GUEST_CONTROL
1440 ReturnComNotImplemented();
1441#else
1442 LogFlowThisFuncEnter();
1443
1444 CheckComArgSafeArrayNotNull(aData);
1445 CheckComArgOutPointerValid(aWritten);
1446
1447 AutoCaller autoCaller(this);
1448 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1449
1450 HRESULT hr = S_OK;
1451
1452 com::SafeArray<BYTE> data(ComSafeArrayInArg(aData));
1453 int vrc = writeData(aTimeoutMS, data.raw(), (uint32_t)data.size(),
1454 (uint32_t*)aWritten);
1455 if (RT_FAILURE(vrc))
1456 {
1457 switch (vrc)
1458 {
1459 default:
1460 hr = setError(VBOX_E_IPRT_ERROR,
1461 tr("Writing %zubytes to file \"%s\" (at offset %RU64) failed: %Rrc"),
1462 data.size(), mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
1463 break;
1464 }
1465 }
1466
1467 LogFlowFuncLeaveRC(vrc);
1468 return hr;
1469#endif /* VBOX_WITH_GUEST_CONTROL */
1470}
1471
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