VirtualBox

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

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

GuestCtrl/IGuestFile: Fixed initial offset reporting when using IGuestSession::openFileEx().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.0 KB
Line 
1
2/* $Id: GuestFileImpl.cpp 48720 2013-09-26 15:56:16Z 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 if (cbRead)
597 {
598 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
599
600 mData.mOffCurrent += cbRead;
601
602 alock.release();
603
604 com::SafeArray<BYTE> data((size_t)cbRead);
605 data.initFrom((BYTE*)dataCb.u.read.pvData, cbRead);
606
607 fireGuestFileReadEvent(mEventSource, mSession, this, mData.mOffCurrent,
608 cbRead, ComSafeArrayAsInParam(data));
609 }
610 }
611 else
612 vrc = VERR_NOT_SUPPORTED;
613 break;
614 }
615
616 case GUEST_FILE_NOTIFYTYPE_WRITE:
617 {
618 if (pSvcCbData->mParms == 4)
619 {
620 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.u.write.cbWritten);
621
622 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
623
624 mData.mOffCurrent += dataCb.u.write.cbWritten;
625 uint64_t uOffCurrent = mData.mOffCurrent;
626
627 alock.release();
628
629 if (dataCb.u.write.cbWritten)
630 fireGuestFileWriteEvent(mEventSource, mSession, this, uOffCurrent,
631 dataCb.u.write.cbWritten);
632 }
633 else
634 vrc = VERR_NOT_SUPPORTED;
635 break;
636 }
637
638 case GUEST_FILE_NOTIFYTYPE_SEEK:
639 {
640 if (pSvcCbData->mParms == 4)
641 {
642 pSvcCbData->mpaParms[idx++].getUInt64(&dataCb.u.seek.uOffActual);
643
644 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
645
646 mData.mOffCurrent = dataCb.u.seek.uOffActual;
647 uint64_t uOffCurrent = mData.mOffCurrent;
648
649 alock.release();
650
651 if (dataCb.u.seek.uOffActual)
652 fireGuestFileOffsetChangedEvent(mEventSource, mSession, this,
653 uOffCurrent, 0 /* Processed */);
654 }
655 else
656 vrc = VERR_NOT_SUPPORTED;
657 break;
658 }
659
660 case GUEST_FILE_NOTIFYTYPE_TELL:
661 {
662 if (pSvcCbData->mParms == 4)
663 {
664 pSvcCbData->mpaParms[idx++].getUInt64(&dataCb.u.tell.uOffActual);
665
666 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
667
668 if (mData.mOffCurrent != dataCb.u.tell.uOffActual)
669 {
670 mData.mOffCurrent = dataCb.u.tell.uOffActual;
671 uint64_t uOffCurrent = mData.mOffCurrent;
672
673 alock.release();
674
675 fireGuestFileOffsetChangedEvent(mEventSource, mSession, this,
676 uOffCurrent, 0 /* Processed */);
677 }
678 }
679 else
680 vrc = VERR_NOT_SUPPORTED;
681 break;
682 }
683
684 default:
685 vrc = VERR_NOT_SUPPORTED;
686 break;
687 }
688
689 LogFlowThisFunc(("uType=%RU32, guestRc=%Rrc\n",
690 dataCb.uType, dataCb.rc));
691
692 LogFlowFuncLeaveRC(vrc);
693 return vrc;
694}
695
696int GuestFile::onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
697{
698 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
699 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
700
701 int vrc = setFileStatus(FileStatus_Down, VINF_SUCCESS);
702
703 LogFlowFuncLeaveRC(vrc);
704 return vrc;
705}
706
707int GuestFile::openFile(uint32_t uTimeoutMS, int *pGuestRc)
708{
709 LogFlowThisFuncEnter();
710
711 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
712
713 LogFlowThisFunc(("strFile=%s, strOpenMode=%s, strDisposition=%s, uCreationMode=%RU32, uOffset=%RU64\n",
714 mData.mOpenInfo.mFileName.c_str(), mData.mOpenInfo.mOpenMode.c_str(),
715 mData.mOpenInfo.mDisposition.c_str(), mData.mOpenInfo.mCreationMode, mData.mOpenInfo.mInitialOffset));
716 int vrc;
717
718 GuestWaitEvent *pEvent = NULL;
719 std::list < VBoxEventType_T > eventTypes;
720 try
721 {
722 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
723
724 vrc = registerWaitEvent(eventTypes, &pEvent);
725 }
726 catch (std::bad_alloc)
727 {
728 vrc = VERR_NO_MEMORY;
729 }
730
731 if (RT_FAILURE(vrc))
732 return vrc;
733
734 /* Prepare HGCM call. */
735 VBOXHGCMSVCPARM paParms[8];
736 int i = 0;
737 paParms[i++].setUInt32(pEvent->ContextID());
738 paParms[i++].setPointer((void*)mData.mOpenInfo.mFileName.c_str(),
739 (ULONG)mData.mOpenInfo.mFileName.length() + 1);
740 paParms[i++].setPointer((void*)mData.mOpenInfo.mOpenMode.c_str(),
741 (ULONG)mData.mOpenInfo.mOpenMode.length() + 1);
742 paParms[i++].setPointer((void*)mData.mOpenInfo.mDisposition.c_str(),
743 (ULONG)mData.mOpenInfo.mDisposition.length() + 1);
744 paParms[i++].setPointer((void*)mData.mOpenInfo.mSharingMode.c_str(),
745 (ULONG)mData.mOpenInfo.mSharingMode.length() + 1);
746 paParms[i++].setUInt32(mData.mOpenInfo.mCreationMode);
747 paParms[i++].setUInt64(mData.mOpenInfo.mInitialOffset);
748
749 alock.release(); /* Drop read lock before sending. */
750
751 vrc = sendCommand(HOST_FILE_OPEN, i, paParms);
752 if (RT_SUCCESS(vrc))
753 vrc = waitForStatusChange(pEvent, uTimeoutMS,
754 NULL /* FileStatus */, pGuestRc);
755
756 unregisterWaitEvent(pEvent);
757
758 LogFlowFuncLeaveRC(vrc);
759 return vrc;
760}
761
762int GuestFile::readData(uint32_t uSize, uint32_t uTimeoutMS,
763 void* pvData, uint32_t cbData, uint32_t* pcbRead)
764{
765 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
766 AssertReturn(cbData, VERR_INVALID_PARAMETER);
767
768 LogFlowThisFunc(("uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
769 uSize, uTimeoutMS, pvData, cbData));
770 int vrc;
771
772 GuestWaitEvent *pEvent = NULL;
773 std::list < VBoxEventType_T > eventTypes;
774 try
775 {
776 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
777 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
778
779 vrc = registerWaitEvent(eventTypes, &pEvent);
780 }
781 catch (std::bad_alloc)
782 {
783 vrc = VERR_NO_MEMORY;
784 }
785
786 if (RT_FAILURE(vrc))
787 return vrc;
788
789 /* Prepare HGCM call. */
790 VBOXHGCMSVCPARM paParms[4];
791 int i = 0;
792 paParms[i++].setUInt32(pEvent->ContextID());
793 paParms[i++].setUInt32(mData.mID /* File handle */);
794 paParms[i++].setUInt32(uSize /* Size (in bytes) to read */);
795
796 uint32_t cbRead;
797 vrc = sendCommand(HOST_FILE_READ, i, paParms);
798 if (RT_SUCCESS(vrc))
799 vrc = waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
800
801 if (RT_SUCCESS(vrc))
802 {
803 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
804
805 if (pcbRead)
806 *pcbRead = cbRead;
807 }
808
809 unregisterWaitEvent(pEvent);
810
811 LogFlowFuncLeaveRC(vrc);
812 return vrc;
813}
814
815int GuestFile::readDataAt(uint64_t uOffset, uint32_t uSize, uint32_t uTimeoutMS,
816 void* pvData, size_t cbData, size_t* pcbRead)
817{
818 LogFlowThisFunc(("uOffset=%RU64, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
819 uOffset, uSize, uTimeoutMS, pvData, cbData));
820 int vrc;
821
822 GuestWaitEvent *pEvent = NULL;
823 std::list < VBoxEventType_T > eventTypes;
824 try
825 {
826 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
827 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
828
829 vrc = registerWaitEvent(eventTypes, &pEvent);
830 }
831 catch (std::bad_alloc)
832 {
833 vrc = VERR_NO_MEMORY;
834 }
835
836 if (RT_FAILURE(vrc))
837 return vrc;
838
839 /* Prepare HGCM call. */
840 VBOXHGCMSVCPARM paParms[4];
841 int i = 0;
842 paParms[i++].setUInt32(pEvent->ContextID());
843 paParms[i++].setUInt32(mData.mID /* File handle */);
844 paParms[i++].setUInt64(uOffset /* Offset (in bytes) to start reading */);
845 paParms[i++].setUInt32(uSize /* Size (in bytes) to read */);
846
847 uint32_t cbRead;
848 vrc = sendCommand(HOST_FILE_READ_AT, i, paParms);
849 if (RT_SUCCESS(vrc))
850 vrc = waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
851
852 if (RT_SUCCESS(vrc))
853 {
854 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
855
856 if (pcbRead)
857 *pcbRead = cbRead;
858 }
859
860 unregisterWaitEvent(pEvent);
861
862 LogFlowFuncLeaveRC(vrc);
863 return vrc;
864}
865
866int GuestFile::seekAt(uint64_t uOffset, GUEST_FILE_SEEKTYPE eSeekType,
867 uint32_t uTimeoutMS, uint64_t *puOffset)
868{
869 LogFlowThisFunc(("uOffset=%RU64, uTimeoutMS=%RU32\n",
870 uOffset, uTimeoutMS));
871 int vrc;
872
873 GuestWaitEvent *pEvent = NULL;
874 std::list < VBoxEventType_T > eventTypes;
875 try
876 {
877 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
878 eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
879
880 vrc = registerWaitEvent(eventTypes, &pEvent);
881 }
882 catch (std::bad_alloc)
883 {
884 vrc = VERR_NO_MEMORY;
885 }
886
887 if (RT_FAILURE(vrc))
888 return vrc;
889
890 /* Prepare HGCM call. */
891 VBOXHGCMSVCPARM paParms[4];
892 int i = 0;
893 paParms[i++].setUInt32(pEvent->ContextID());
894 paParms[i++].setUInt32(mData.mID /* File handle */);
895 paParms[i++].setUInt32(eSeekType /* Seek method */);
896 paParms[i++].setUInt64(uOffset /* Offset (in bytes) to start reading */);
897
898 vrc = sendCommand(HOST_FILE_SEEK, i, paParms);
899 if (RT_SUCCESS(vrc))
900 vrc = waitForOffsetChange(pEvent, uTimeoutMS, puOffset);
901
902 unregisterWaitEvent(pEvent);
903
904 LogFlowFuncLeaveRC(vrc);
905 return vrc;
906}
907
908/* static */
909HRESULT GuestFile::setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
910{
911 AssertPtr(pInterface);
912 AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
913
914 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestFile::guestErrorToString(guestRc).c_str());
915}
916
917int GuestFile::setFileStatus(FileStatus_T fileStatus, int fileRc)
918{
919 LogFlowThisFuncEnter();
920
921 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
922
923 LogFlowThisFunc(("oldStatus=%ld, newStatus=%ld, fileRc=%Rrc\n",
924 mData.mStatus, fileStatus, fileRc));
925
926#ifdef VBOX_STRICT
927 if (fileStatus == FileStatus_Error)
928 {
929 AssertMsg(RT_FAILURE(fileRc), ("Guest rc must be an error (%Rrc)\n", fileRc));
930 }
931 else
932 AssertMsg(RT_SUCCESS(fileRc), ("Guest rc must not be an error (%Rrc)\n", fileRc));
933#endif
934
935 if (mData.mStatus != fileStatus)
936 {
937 mData.mStatus = fileStatus;
938 mData.mLastError = fileRc;
939
940 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
941 HRESULT hr = errorInfo.createObject();
942 ComAssertComRC(hr);
943 if (RT_FAILURE(fileRc))
944 {
945 hr = errorInfo->initEx(VBOX_E_IPRT_ERROR, fileRc,
946 COM_IIDOF(IGuestFile), getComponentName(),
947 guestErrorToString(fileRc));
948 ComAssertComRC(hr);
949 }
950
951 alock.release(); /* Release lock before firing off event. */
952
953 fireGuestFileStateChangedEvent(mEventSource, mSession,
954 this, fileStatus, errorInfo);
955 }
956
957 return VINF_SUCCESS;
958}
959
960int GuestFile::waitForOffsetChange(GuestWaitEvent *pEvent,
961 uint32_t uTimeoutMS, uint64_t *puOffset)
962{
963 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
964
965 VBoxEventType_T evtType;
966 ComPtr<IEvent> pIEvent;
967 int vrc = waitForEvent(pEvent, uTimeoutMS,
968 &evtType, pIEvent.asOutParam());
969 if (RT_SUCCESS(vrc))
970 {
971 if (evtType == VBoxEventType_OnGuestFileOffsetChanged)
972 {
973 if (puOffset)
974 {
975 ComPtr<IGuestFileOffsetChangedEvent> pFileEvent = pIEvent;
976 Assert(!pFileEvent.isNull());
977
978 HRESULT hr = pFileEvent->COMGETTER(Offset)((LONG64*)puOffset);
979 ComAssertComRC(hr);
980 }
981 }
982 else
983 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
984 }
985
986 return vrc;
987}
988
989int GuestFile::waitForRead(GuestWaitEvent *pEvent,
990 uint32_t uTimeoutMS, void *pvData, size_t cbData, uint32_t *pcbRead)
991{
992 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
993
994 VBoxEventType_T evtType;
995 ComPtr<IEvent> pIEvent;
996 int vrc = waitForEvent(pEvent, uTimeoutMS,
997 &evtType, pIEvent.asOutParam());
998 if (RT_SUCCESS(vrc))
999 {
1000 if (evtType == VBoxEventType_OnGuestFileRead)
1001 {
1002 ComPtr<IGuestFileReadEvent> pFileEvent = pIEvent;
1003 Assert(!pFileEvent.isNull());
1004
1005 HRESULT hr;
1006 if (pvData)
1007 {
1008 com::SafeArray <BYTE> data;
1009 hr = pFileEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
1010 ComAssertComRC(hr);
1011 size_t cbRead = data.size();
1012 if ( cbRead
1013 && cbRead <= cbData)
1014 {
1015 memcpy(pvData, data.raw(), data.size());
1016 }
1017 else
1018 vrc = VERR_BUFFER_OVERFLOW;
1019 }
1020 if (pcbRead)
1021 {
1022 hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbRead);
1023 ComAssertComRC(hr);
1024 }
1025 }
1026 else
1027 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1028 }
1029
1030 return vrc;
1031}
1032
1033int GuestFile::waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1034 FileStatus_T *pFileStatus, int *pGuestRc)
1035{
1036 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1037 /* pFileStatus is optional. */
1038
1039 VBoxEventType_T evtType;
1040 ComPtr<IEvent> pIEvent;
1041 int vrc = waitForEvent(pEvent, uTimeoutMS,
1042 &evtType, pIEvent.asOutParam());
1043 if (RT_SUCCESS(vrc))
1044 {
1045 Assert(evtType == VBoxEventType_OnGuestFileStateChanged);
1046 ComPtr<IGuestFileStateChangedEvent> pFileEvent = pIEvent;
1047 Assert(!pFileEvent.isNull());
1048
1049 HRESULT hr;
1050 if (pFileStatus)
1051 {
1052 hr = pFileEvent->COMGETTER(Status)(pFileStatus);
1053 ComAssertComRC(hr);
1054 }
1055
1056 ComPtr<IVirtualBoxErrorInfo> errorInfo;
1057 hr = pFileEvent->COMGETTER(Error)(errorInfo.asOutParam());
1058 ComAssertComRC(hr);
1059
1060 LONG lGuestRc;
1061 hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
1062 ComAssertComRC(hr);
1063
1064 LogFlowThisFunc(("resultDetail=%RI32 (rc=%Rrc)\n",
1065 lGuestRc, lGuestRc));
1066
1067 if (RT_FAILURE((int)lGuestRc))
1068 vrc = VERR_GSTCTL_GUEST_ERROR;
1069
1070 if (pGuestRc)
1071 *pGuestRc = (int)lGuestRc;
1072 }
1073
1074 return vrc;
1075}
1076
1077int GuestFile::waitForWrite(GuestWaitEvent *pEvent,
1078 uint32_t uTimeoutMS, uint32_t *pcbWritten)
1079{
1080 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1081
1082 VBoxEventType_T evtType;
1083 ComPtr<IEvent> pIEvent;
1084 int vrc = waitForEvent(pEvent, uTimeoutMS,
1085 &evtType, pIEvent.asOutParam());
1086 if (RT_SUCCESS(vrc))
1087 {
1088 if (evtType == VBoxEventType_OnGuestFileWrite)
1089 {
1090 if (pcbWritten)
1091 {
1092 ComPtr<IGuestFileWriteEvent> pFileEvent = pIEvent;
1093 Assert(!pFileEvent.isNull());
1094
1095 HRESULT hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbWritten);
1096 ComAssertComRC(hr);
1097 }
1098 }
1099 else
1100 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1101 }
1102
1103 return vrc;
1104}
1105
1106int GuestFile::writeData(uint32_t uTimeoutMS, void *pvData, uint32_t cbData,
1107 uint32_t *pcbWritten)
1108{
1109 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1110 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1111
1112 LogFlowThisFunc(("uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1113 uTimeoutMS, pvData, cbData));
1114 int vrc;
1115
1116 GuestWaitEvent *pEvent = NULL;
1117 std::list < VBoxEventType_T > eventTypes;
1118 try
1119 {
1120 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1121 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1122
1123 vrc = registerWaitEvent(eventTypes, &pEvent);
1124 }
1125 catch (std::bad_alloc)
1126 {
1127 vrc = VERR_NO_MEMORY;
1128 }
1129
1130 if (RT_FAILURE(vrc))
1131 return vrc;
1132
1133 /* Prepare HGCM call. */
1134 VBOXHGCMSVCPARM paParms[8];
1135 int i = 0;
1136 paParms[i++].setUInt32(pEvent->ContextID());
1137 paParms[i++].setUInt32(mData.mID /* File handle */);
1138 paParms[i++].setUInt32(cbData /* Size (in bytes) to write */);
1139 paParms[i++].setPointer(pvData, cbData);
1140
1141 uint32_t cbWritten;
1142 vrc = sendCommand(HOST_FILE_WRITE, i, paParms);
1143 if (RT_SUCCESS(vrc))
1144 vrc = waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1145
1146 if (RT_SUCCESS(vrc))
1147 {
1148 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1149
1150 if (cbWritten)
1151 *pcbWritten = cbWritten;
1152 }
1153
1154 unregisterWaitEvent(pEvent);
1155
1156 LogFlowFuncLeaveRC(vrc);
1157 return vrc;
1158}
1159
1160int GuestFile::writeDataAt(uint64_t uOffset, uint32_t uTimeoutMS,
1161 void *pvData, uint32_t cbData, uint32_t *pcbWritten)
1162{
1163 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1164 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1165
1166 LogFlowThisFunc(("uOffset=%RU64, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1167 uOffset, uTimeoutMS, pvData, cbData));
1168 int vrc;
1169
1170 GuestWaitEvent *pEvent = NULL;
1171 std::list < VBoxEventType_T > eventTypes;
1172 try
1173 {
1174 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1175 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1176
1177 vrc = registerWaitEvent(eventTypes, &pEvent);
1178 }
1179 catch (std::bad_alloc)
1180 {
1181 vrc = VERR_NO_MEMORY;
1182 }
1183
1184 if (RT_FAILURE(vrc))
1185 return vrc;
1186
1187 /* Prepare HGCM call. */
1188 VBOXHGCMSVCPARM paParms[8];
1189 int i = 0;
1190 paParms[i++].setUInt32(pEvent->ContextID());
1191 paParms[i++].setUInt32(mData.mID /* File handle */);
1192 paParms[i++].setUInt64(uOffset /* Offset where to starting writing */);
1193 paParms[i++].setUInt32(cbData /* Size (in bytes) to write */);
1194 paParms[i++].setPointer(pvData, cbData);
1195
1196 uint32_t cbWritten;
1197 vrc = sendCommand(HOST_FILE_WRITE_AT, i, paParms);
1198 if (RT_SUCCESS(vrc))
1199 vrc = waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1200
1201 if (RT_SUCCESS(vrc))
1202 {
1203 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1204
1205 if (cbWritten)
1206 *pcbWritten = cbWritten;
1207 }
1208
1209 unregisterWaitEvent(pEvent);
1210
1211 LogFlowFuncLeaveRC(vrc);
1212 return vrc;
1213}
1214
1215// implementation of public methods
1216/////////////////////////////////////////////////////////////////////////////
1217
1218STDMETHODIMP GuestFile::Close(void)
1219{
1220#ifndef VBOX_WITH_GUEST_CONTROL
1221 ReturnComNotImplemented();
1222#else
1223 LogFlowThisFuncEnter();
1224
1225 AutoCaller autoCaller(this);
1226 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1227
1228 /* Close file on guest. */
1229 int guestRc;
1230 int rc = closeFile(&guestRc);
1231 /* On failure don't return here, instead do all the cleanup
1232 * work first and then return an error. */
1233
1234 AssertPtr(mSession);
1235 int rc2 = mSession->fileRemoveFromList(this);
1236 if (RT_SUCCESS(rc))
1237 rc = rc2;
1238
1239 if (RT_FAILURE(rc))
1240 {
1241 if (rc == VERR_GSTCTL_GUEST_ERROR)
1242 return GuestFile::setErrorExternal(this, guestRc);
1243
1244 return setError(VBOX_E_IPRT_ERROR,
1245 tr("Closing guest file failed with %Rrc\n"), rc);
1246 }
1247
1248 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
1249 return S_OK;
1250#endif /* VBOX_WITH_GUEST_CONTROL */
1251}
1252
1253STDMETHODIMP GuestFile::QueryInfo(IFsObjInfo **aInfo)
1254{
1255#ifndef VBOX_WITH_GUEST_CONTROL
1256 ReturnComNotImplemented();
1257#else
1258 AutoCaller autoCaller(this);
1259 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1260
1261 ReturnComNotImplemented();
1262#endif /* VBOX_WITH_GUEST_CONTROL */
1263}
1264
1265STDMETHODIMP GuestFile::Read(ULONG aToRead, ULONG aTimeoutMS, ComSafeArrayOut(BYTE, aData))
1266{
1267#ifndef VBOX_WITH_GUEST_CONTROL
1268 ReturnComNotImplemented();
1269#else
1270 if (aToRead == 0)
1271 return setError(E_INVALIDARG, tr("The size to read is zero"));
1272 CheckComArgOutSafeArrayPointerValid(aData);
1273
1274 AutoCaller autoCaller(this);
1275 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1276
1277 com::SafeArray<BYTE> data((size_t)aToRead);
1278 Assert(data.size() >= aToRead);
1279
1280 HRESULT hr = S_OK;
1281
1282 uint32_t cbRead;
1283 int vrc = readData(aToRead, aTimeoutMS,
1284 data.raw(), aToRead, &cbRead);
1285 if (RT_SUCCESS(vrc))
1286 {
1287 if (data.size() != cbRead)
1288 data.resize(cbRead);
1289 data.detachTo(ComSafeArrayOutArg(aData));
1290 }
1291 else
1292 {
1293 switch (vrc)
1294 {
1295 default:
1296 hr = setError(VBOX_E_IPRT_ERROR,
1297 tr("Reading from file \"%s\" failed: %Rrc"),
1298 mData.mOpenInfo.mFileName.c_str(), vrc);
1299 break;
1300 }
1301 }
1302
1303 LogFlowFuncLeaveRC(vrc);
1304 return hr;
1305#endif /* VBOX_WITH_GUEST_CONTROL */
1306}
1307
1308STDMETHODIMP GuestFile::ReadAt(LONG64 aOffset, ULONG aToRead, ULONG aTimeoutMS, ComSafeArrayOut(BYTE, aData))
1309{
1310#ifndef VBOX_WITH_GUEST_CONTROL
1311 ReturnComNotImplemented();
1312#else
1313 if (aToRead == 0)
1314 return setError(E_INVALIDARG, tr("The size to read is zero"));
1315 CheckComArgOutSafeArrayPointerValid(aData);
1316
1317 AutoCaller autoCaller(this);
1318 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1319
1320 com::SafeArray<BYTE> data((size_t)aToRead);
1321 Assert(data.size() >= aToRead);
1322
1323 HRESULT hr = S_OK;
1324
1325 size_t cbRead;
1326 int vrc = readDataAt(aOffset, aToRead, aTimeoutMS,
1327 data.raw(), aToRead, &cbRead);
1328 if (RT_SUCCESS(vrc))
1329 {
1330 if (data.size() != cbRead)
1331 data.resize(cbRead);
1332 data.detachTo(ComSafeArrayOutArg(aData));
1333 }
1334 else
1335 {
1336 switch (vrc)
1337 {
1338 default:
1339 hr = setError(VBOX_E_IPRT_ERROR,
1340 tr("Reading from file \"%s\" (at offset %RU64) failed: %Rrc"),
1341 mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
1342 break;
1343 }
1344 }
1345
1346 LogFlowFuncLeaveRC(vrc);
1347 return hr;
1348#endif /* VBOX_WITH_GUEST_CONTROL */
1349}
1350
1351STDMETHODIMP GuestFile::Seek(LONG64 aOffset, FileSeekType_T aType)
1352{
1353#ifndef VBOX_WITH_GUEST_CONTROL
1354 ReturnComNotImplemented();
1355#else
1356 LogFlowThisFuncEnter();
1357
1358 AutoCaller autoCaller(this);
1359 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1360
1361 HRESULT hr = S_OK;
1362
1363 GUEST_FILE_SEEKTYPE eSeekType;
1364 switch (aType)
1365 {
1366 case FileSeekType_Set:
1367 eSeekType = GUEST_FILE_SEEKTYPE_BEGIN;
1368 break;
1369
1370 case FileSeekType_Current:
1371 eSeekType = GUEST_FILE_SEEKTYPE_CURRENT;
1372 break;
1373
1374 default:
1375 return setError(E_INVALIDARG, tr("Invalid seek type specified"));
1376 break;
1377 }
1378
1379 int vrc = seekAt(aOffset, eSeekType,
1380 30 * 1000 /* 30s timeout */, NULL /* puOffset */);
1381 if (RT_FAILURE(vrc))
1382 {
1383 switch (vrc)
1384 {
1385 default:
1386 hr = setError(VBOX_E_IPRT_ERROR,
1387 tr("Seeking file \"%s\" (to offset %RU64) failed: %Rrc"),
1388 mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
1389 break;
1390 }
1391 }
1392
1393 LogFlowFuncLeaveRC(vrc);
1394 return hr;
1395#endif /* VBOX_WITH_GUEST_CONTROL */
1396}
1397
1398STDMETHODIMP GuestFile::SetACL(IN_BSTR aACL)
1399{
1400#ifndef VBOX_WITH_GUEST_CONTROL
1401 ReturnComNotImplemented();
1402#else
1403 AutoCaller autoCaller(this);
1404 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1405
1406 ReturnComNotImplemented();
1407#endif /* VBOX_WITH_GUEST_CONTROL */
1408}
1409
1410STDMETHODIMP GuestFile::Write(ComSafeArrayIn(BYTE, aData), ULONG aTimeoutMS, ULONG *aWritten)
1411{
1412#ifndef VBOX_WITH_GUEST_CONTROL
1413 ReturnComNotImplemented();
1414#else
1415 LogFlowThisFuncEnter();
1416
1417 CheckComArgOutPointerValid(aWritten);
1418
1419 AutoCaller autoCaller(this);
1420 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1421
1422 HRESULT hr = S_OK;
1423
1424 com::SafeArray<BYTE> data(ComSafeArrayInArg(aData));
1425 int vrc = writeData(aTimeoutMS, data.raw(), (uint32_t)data.size(),
1426 (uint32_t*)aWritten);
1427 if (RT_FAILURE(vrc))
1428 {
1429 switch (vrc)
1430 {
1431 default:
1432 hr = setError(VBOX_E_IPRT_ERROR,
1433 tr("Writing %zubytes to file \"%s\" failed: %Rrc"),
1434 data.size(), mData.mOpenInfo.mFileName.c_str(), vrc);
1435 break;
1436 }
1437 }
1438
1439 LogFlowFuncLeaveRC(vrc);
1440 return hr;
1441#endif /* VBOX_WITH_GUEST_CONTROL */
1442}
1443
1444STDMETHODIMP GuestFile::WriteAt(LONG64 aOffset, ComSafeArrayIn(BYTE, aData), ULONG aTimeoutMS, ULONG *aWritten)
1445{
1446#ifndef VBOX_WITH_GUEST_CONTROL
1447 ReturnComNotImplemented();
1448#else
1449 LogFlowThisFuncEnter();
1450
1451 CheckComArgOutPointerValid(aWritten);
1452
1453 AutoCaller autoCaller(this);
1454 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1455
1456 HRESULT hr = S_OK;
1457
1458 com::SafeArray<BYTE> data(ComSafeArrayInArg(aData));
1459 int vrc = writeData(aTimeoutMS, data.raw(), (uint32_t)data.size(),
1460 (uint32_t*)aWritten);
1461 if (RT_FAILURE(vrc))
1462 {
1463 switch (vrc)
1464 {
1465 default:
1466 hr = setError(VBOX_E_IPRT_ERROR,
1467 tr("Writing %zubytes to file \"%s\" (at offset %RU64) failed: %Rrc"),
1468 data.size(), mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
1469 break;
1470 }
1471 }
1472
1473 LogFlowFuncLeaveRC(vrc);
1474 return hr;
1475#endif /* VBOX_WITH_GUEST_CONTROL */
1476}
1477
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