VirtualBox

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

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

Warnings.

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