VirtualBox

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

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

6813 src-client/GuestSessionImpl.cpp

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.0 KB
Line 
1/* $Id: GuestFileImpl.cpp 50727 2014-03-07 18:21:44Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest file handling.
4 */
5
6/*
7 * Copyright (C) 2012-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "GuestFileImpl.h"
23#include "GuestSessionImpl.h"
24#include "GuestCtrlImplPrivate.h"
25#include "ConsoleImpl.h"
26#include "VirtualBoxErrorInfoImpl.h"
27
28#include "Global.h"
29#include "AutoCaller.h"
30#include "VBoxEvents.h"
31
32#include <iprt/cpp/utils.h> /* For unconst(). */
33#include <iprt/file.h>
34
35#include <VBox/com/array.h>
36#include <VBox/com/listeners.h>
37
38#ifdef LOG_GROUP
39 #undef LOG_GROUP
40#endif
41#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
42#include <VBox/log.h>
43
44
45/**
46 * Internal listener class to serve events in an
47 * active manner, e.g. without polling delays.
48 */
49class GuestFileListener
50{
51public:
52
53 GuestFileListener(void)
54 {
55 }
56
57 HRESULT init(GuestFile *pFile)
58 {
59 AssertPtrReturn(pFile, E_POINTER);
60 mFile = pFile;
61 return S_OK;
62 }
63
64 void uninit(void)
65 {
66 mFile = NULL;
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 AssertPtrReturn(mFile, E_POINTER);
79 int rc2 = mFile->signalWaitEvent(aType, aEvent);
80#ifdef DEBUG_andy
81 LogFlowFunc(("Signalling events of type=%RU32, file=%p resulted in rc=%Rrc\n",
82 aType, mFile, rc2));
83#endif
84 break;
85 }
86
87 default:
88 AssertMsgFailed(("Unhandled event %RU32\n", aType));
89 break;
90 }
91
92 return S_OK;
93 }
94
95private:
96
97 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 LogFlowThisFuncEnter();
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();
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 /* Enclose the state transition Ready->InUninit->NotReady. */
228 AutoUninitSpan autoUninitSpan(this);
229 if (autoUninitSpan.uninitDone())
230 return;
231
232 LogFlowThisFuncEnter();
233
234#ifdef VBOX_WITH_GUEST_CONTROL
235 baseUninit();
236#endif
237 LogFlowThisFuncLeave();
238}
239
240// implementation of public getters/setters for attributes
241/////////////////////////////////////////////////////////////////////////////
242
243HRESULT GuestFile::getCreationMode(ULONG *aCreationMode)
244{
245#ifndef VBOX_WITH_GUEST_CONTROL
246 ReturnComNotImplemented();
247#else
248 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
249
250 *aCreationMode = mData.mOpenInfo.mCreationMode;
251
252 return S_OK;
253#endif /* VBOX_WITH_GUEST_CONTROL */
254}
255
256HRESULT GuestFile::getDisposition(com::Utf8Str &aDisposition)
257{
258#ifndef VBOX_WITH_GUEST_CONTROL
259 ReturnComNotImplemented();
260#else
261 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
262
263 aDisposition = mData.mOpenInfo.mDisposition;
264
265 return S_OK;
266#endif /* VBOX_WITH_GUEST_CONTROL */
267}
268
269HRESULT GuestFile::getEventSource(ComPtr<IEventSource> &aEventSource)
270{
271#ifndef VBOX_WITH_GUEST_CONTROL
272 ReturnComNotImplemented();
273#else
274 /* No need to lock - lifetime constant. */
275 mEventSource.queryInterfaceTo(aEventSource.asOutParam());
276
277 return S_OK;
278#endif /* VBOX_WITH_GUEST_CONTROL */
279}
280
281HRESULT GuestFile::getFileName(com::Utf8Str &aFileName)
282{
283#ifndef VBOX_WITH_GUEST_CONTROL
284 ReturnComNotImplemented();
285#else
286 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
287
288 aFileName = mData.mOpenInfo.mFileName;
289
290 return S_OK;
291#endif /* VBOX_WITH_GUEST_CONTROL */
292}
293
294HRESULT GuestFile::getId(ULONG *aId)
295{
296#ifndef VBOX_WITH_GUEST_CONTROL
297 ReturnComNotImplemented();
298#else
299 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
300
301 *aId = mData.mID;
302
303 return S_OK;
304#endif /* VBOX_WITH_GUEST_CONTROL */
305}
306
307HRESULT GuestFile::getInitialSize(LONG64 *aInitialSize)
308{
309#ifndef VBOX_WITH_GUEST_CONTROL
310 ReturnComNotImplemented();
311#else
312 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
313
314 *aInitialSize = mData.mInitialSize;
315
316 return S_OK;
317#endif /* VBOX_WITH_GUEST_CONTROL */
318}
319
320HRESULT GuestFile::getOffset(LONG64 *aOffset)
321{
322#ifndef VBOX_WITH_GUEST_CONTROL
323 ReturnComNotImplemented();
324#else
325 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
326
327 *aOffset = mData.mOffCurrent;
328
329 return S_OK;
330#endif /* VBOX_WITH_GUEST_CONTROL */
331}
332
333HRESULT GuestFile::getOpenMode(com::Utf8Str &aOpenMode)
334{
335#ifndef VBOX_WITH_GUEST_CONTROL
336 ReturnComNotImplemented();
337#else
338 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
339
340 aOpenMode = mData.mOpenInfo.mOpenMode;
341
342 return S_OK;
343#endif /* VBOX_WITH_GUEST_CONTROL */
344}
345
346HRESULT GuestFile::getStatus(FileStatus_T *aStatus)
347{
348#ifndef VBOX_WITH_GUEST_CONTROL
349 ReturnComNotImplemented();
350#else
351 LogFlowThisFuncEnter();
352
353 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
354
355 *aStatus = mData.mStatus;
356
357 return S_OK;
358#endif /* VBOX_WITH_GUEST_CONTROL */
359}
360
361// private methods
362/////////////////////////////////////////////////////////////////////////////
363
364int GuestFile::i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
365{
366 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
367 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
368
369 LogFlowThisFunc(("strName=%s, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
370 mData.mOpenInfo.mFileName.c_str(), pCbCtx->uContextID, pCbCtx->uFunction, pSvcCb));
371
372 int vrc;
373 switch (pCbCtx->uFunction)
374 {
375 case GUEST_DISCONNECTED:
376 vrc = i_onGuestDisconnected(pCbCtx, pSvcCb);
377 break;
378
379 case GUEST_FILE_NOTIFY:
380 vrc = i_onFileNotify(pCbCtx, pSvcCb);
381 break;
382
383 default:
384 /* Silently ignore not implemented functions. */
385 vrc = VERR_NOT_SUPPORTED;
386 break;
387 }
388
389#ifdef DEBUG
390 LogFlowFuncLeaveRC(vrc);
391#endif
392 return vrc;
393}
394
395int GuestFile::i_closeFile(int *pGuestRc)
396{
397 LogFlowThisFunc(("strFile=%s\n", mData.mOpenInfo.mFileName.c_str()));
398
399 int vrc;
400
401 GuestWaitEvent *pEvent = NULL;
402 GuestEventTypes eventTypes;
403 try
404 {
405 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
406
407 vrc = registerWaitEvent(eventTypes, &pEvent);
408 }
409 catch (std::bad_alloc)
410 {
411 vrc = VERR_NO_MEMORY;
412 }
413
414 if (RT_FAILURE(vrc))
415 return vrc;
416
417 /* Prepare HGCM call. */
418 VBOXHGCMSVCPARM paParms[4];
419 int i = 0;
420 paParms[i++].setUInt32(pEvent->ContextID());
421 paParms[i++].setUInt32(mData.mID /* Guest file ID */);
422
423 vrc = sendCommand(HOST_FILE_CLOSE, i, paParms);
424 if (RT_SUCCESS(vrc))
425 vrc = i_waitForStatusChange(pEvent, 30 * 1000 /* Timeout in ms */,
426 NULL /* FileStatus */, pGuestRc);
427 unregisterWaitEvent(pEvent);
428
429 LogFlowFuncLeaveRC(vrc);
430 return vrc;
431}
432
433/* static */
434Utf8Str GuestFile::i_guestErrorToString(int guestRc)
435{
436 Utf8Str strError;
437
438 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
439 switch (guestRc)
440 {
441 case VERR_ALREADY_EXISTS:
442 strError += Utf8StrFmt(tr("File already exists"));
443 break;
444
445 case VERR_FILE_NOT_FOUND:
446 strError += Utf8StrFmt(tr("File not found"));
447 break;
448
449 case VERR_NET_HOST_NOT_FOUND:
450 strError += Utf8StrFmt(tr("Host name not found"));
451 break;
452
453 case VERR_SHARING_VIOLATION:
454 strError += Utf8StrFmt(tr("Sharing violation"));
455 break;
456
457 default:
458 strError += Utf8StrFmt("%Rrc", guestRc);
459 break;
460 }
461
462 return strError;
463}
464
465int GuestFile::i_onFileNotify(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
466{
467 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
468 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
469
470 LogFlowThisFuncEnter();
471
472 if (pSvcCbData->mParms < 3)
473 return VERR_INVALID_PARAMETER;
474
475 int vrc = VINF_SUCCESS;
476
477 int idx = 1; /* Current parameter index. */
478 CALLBACKDATA_FILE_NOTIFY dataCb;
479 /* pSvcCb->mpaParms[0] always contains the context ID. */
480 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.uType);
481 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.rc);
482
483 FileStatus_T fileStatus = FileStatus_Undefined;
484 int guestRc = (int)dataCb.rc; /* uint32_t vs. int. */
485
486 LogFlowFunc(("uType=%RU32, guestRc=%Rrc\n",
487 dataCb.uType, guestRc));
488
489 if (RT_FAILURE(guestRc))
490 {
491 int rc2 = i_setFileStatus(FileStatus_Error, guestRc);
492 AssertRC(rc2);
493
494 rc2 = signalWaitEventInternal(pCbCtx,
495 guestRc, NULL /* pPayload */);
496 AssertRC(rc2);
497
498 return VINF_SUCCESS; /* Report to the guest. */
499 }
500
501 switch (dataCb.uType)
502 {
503 case GUEST_FILE_NOTIFYTYPE_ERROR:
504 {
505 int rc2 = i_setFileStatus(FileStatus_Error, guestRc);
506 AssertRC(rc2);
507
508 break;
509 }
510
511 case GUEST_FILE_NOTIFYTYPE_OPEN:
512 {
513 if (pSvcCbData->mParms == 4)
514 {
515 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.u.open.uHandle);
516
517 {
518 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
519 AssertMsg(mData.mID == VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCbCtx->uContextID),
520 ("File ID %RU32 does not match context ID %RU32\n", mData.mID,
521 VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCbCtx->uContextID)));
522
523 /* Set the initial offset. On the guest the whole opening operation
524 * would fail if an initial seek isn't possible. */
525 mData.mOffCurrent = mData.mOpenInfo.mInitialOffset;
526 }
527
528 /* Set the process status. */
529 int rc2 = i_setFileStatus(FileStatus_Open, guestRc);
530 AssertRC(rc2);
531 }
532 else
533 vrc = VERR_NOT_SUPPORTED;
534
535 break;
536 }
537
538 case GUEST_FILE_NOTIFYTYPE_CLOSE:
539 {
540 int rc2 = i_setFileStatus(FileStatus_Closed, guestRc);
541 AssertRC(rc2);
542
543 break;
544 }
545
546 case GUEST_FILE_NOTIFYTYPE_READ:
547 {
548 if (pSvcCbData->mParms == 4)
549 {
550 pSvcCbData->mpaParms[idx++].getPointer(&dataCb.u.read.pvData,
551 &dataCb.u.read.cbData);
552 uint32_t cbRead = dataCb.u.read.cbData;
553
554 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
555
556 mData.mOffCurrent += cbRead;
557
558 alock.release();
559
560 com::SafeArray<BYTE> data((size_t)cbRead);
561 data.initFrom((BYTE*)dataCb.u.read.pvData, cbRead);
562
563 fireGuestFileReadEvent(mEventSource, mSession, this, mData.mOffCurrent,
564 cbRead, ComSafeArrayAsInParam(data));
565 }
566 else
567 vrc = VERR_NOT_SUPPORTED;
568 break;
569 }
570
571 case GUEST_FILE_NOTIFYTYPE_WRITE:
572 {
573 if (pSvcCbData->mParms == 4)
574 {
575 pSvcCbData->mpaParms[idx++].getUInt32(&dataCb.u.write.cbWritten);
576
577 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
578
579 mData.mOffCurrent += dataCb.u.write.cbWritten;
580 uint64_t uOffCurrent = mData.mOffCurrent;
581
582 alock.release();
583
584 fireGuestFileWriteEvent(mEventSource, mSession, this, uOffCurrent,
585 dataCb.u.write.cbWritten);
586 }
587 else
588 vrc = VERR_NOT_SUPPORTED;
589 break;
590 }
591
592 case GUEST_FILE_NOTIFYTYPE_SEEK:
593 {
594 if (pSvcCbData->mParms == 4)
595 {
596 pSvcCbData->mpaParms[idx++].getUInt64(&dataCb.u.seek.uOffActual);
597
598 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
599
600 mData.mOffCurrent = dataCb.u.seek.uOffActual;
601
602 alock.release();
603
604 fireGuestFileOffsetChangedEvent(mEventSource, mSession, this,
605 dataCb.u.seek.uOffActual, 0 /* Processed */);
606 }
607 else
608 vrc = VERR_NOT_SUPPORTED;
609 break;
610 }
611
612 case GUEST_FILE_NOTIFYTYPE_TELL:
613 {
614 if (pSvcCbData->mParms == 4)
615 {
616 pSvcCbData->mpaParms[idx++].getUInt64(&dataCb.u.tell.uOffActual);
617
618 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
619
620 mData.mOffCurrent = dataCb.u.tell.uOffActual;
621
622 alock.release();
623
624 fireGuestFileOffsetChangedEvent(mEventSource, mSession, this,
625 dataCb.u.tell.uOffActual, 0 /* Processed */);
626 }
627 else
628 vrc = VERR_NOT_SUPPORTED;
629 break;
630 }
631
632 default:
633 vrc = VERR_NOT_SUPPORTED;
634 break;
635 }
636
637 if (RT_SUCCESS(vrc))
638 {
639 GuestWaitEventPayload payload(dataCb.uType, &dataCb, sizeof(dataCb));
640 int rc2 = signalWaitEventInternal(pCbCtx, guestRc, &payload);
641 AssertRC(rc2);
642 }
643
644 LogFlowThisFunc(("uType=%RU32, guestRc=%Rrc\n",
645 dataCb.uType, dataCb.rc));
646
647 LogFlowFuncLeaveRC(vrc);
648 return vrc;
649}
650
651int GuestFile::i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
652{
653 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
654 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
655
656 int vrc = i_setFileStatus(FileStatus_Down, VINF_SUCCESS);
657
658 LogFlowFuncLeaveRC(vrc);
659 return vrc;
660}
661
662/**
663 * Called by IGuestSession right before this file gets removed
664 * from the public file list.
665 */
666int GuestFile::i_onRemove(void)
667{
668 LogFlowThisFuncEnter();
669
670 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
671
672 int vrc = VINF_SUCCESS;
673
674 /*
675 * Note: The event source stuff holds references to this object,
676 * so make sure that this is cleaned up *before* calling uninit().
677 */
678 if (!mEventSource.isNull())
679 {
680 mEventSource->UnregisterListener(mLocalListener);
681
682 mLocalListener.setNull();
683 unconst(mEventSource).setNull();
684 }
685
686 LogFlowFuncLeaveRC(vrc);
687 return vrc;
688}
689
690int GuestFile::i_openFile(uint32_t uTimeoutMS, int *pGuestRc)
691{
692 LogFlowThisFuncEnter();
693
694 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
695
696 LogFlowThisFunc(("strFile=%s, strOpenMode=%s, strDisposition=%s, uCreationMode=%RU32, uOffset=%RU64\n",
697 mData.mOpenInfo.mFileName.c_str(), mData.mOpenInfo.mOpenMode.c_str(),
698 mData.mOpenInfo.mDisposition.c_str(), mData.mOpenInfo.mCreationMode, mData.mOpenInfo.mInitialOffset));
699 int vrc;
700
701 GuestWaitEvent *pEvent = NULL;
702 GuestEventTypes eventTypes;
703 try
704 {
705 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
706
707 vrc = registerWaitEvent(eventTypes, &pEvent);
708 }
709 catch (std::bad_alloc)
710 {
711 vrc = VERR_NO_MEMORY;
712 }
713
714 if (RT_FAILURE(vrc))
715 return vrc;
716
717 /* Prepare HGCM call. */
718 VBOXHGCMSVCPARM paParms[8];
719 int i = 0;
720 paParms[i++].setUInt32(pEvent->ContextID());
721 paParms[i++].setPointer((void*)mData.mOpenInfo.mFileName.c_str(),
722 (ULONG)mData.mOpenInfo.mFileName.length() + 1);
723 paParms[i++].setPointer((void*)mData.mOpenInfo.mOpenMode.c_str(),
724 (ULONG)mData.mOpenInfo.mOpenMode.length() + 1);
725 paParms[i++].setPointer((void*)mData.mOpenInfo.mDisposition.c_str(),
726 (ULONG)mData.mOpenInfo.mDisposition.length() + 1);
727 paParms[i++].setPointer((void*)mData.mOpenInfo.mSharingMode.c_str(),
728 (ULONG)mData.mOpenInfo.mSharingMode.length() + 1);
729 paParms[i++].setUInt32(mData.mOpenInfo.mCreationMode);
730 paParms[i++].setUInt64(mData.mOpenInfo.mInitialOffset);
731
732 alock.release(); /* Drop write lock before sending. */
733
734 vrc = sendCommand(HOST_FILE_OPEN, i, paParms);
735 if (RT_SUCCESS(vrc))
736 vrc = i_waitForStatusChange(pEvent, uTimeoutMS,
737 NULL /* FileStatus */, pGuestRc);
738
739 unregisterWaitEvent(pEvent);
740
741 LogFlowFuncLeaveRC(vrc);
742 return vrc;
743}
744
745int GuestFile::i_readData(uint32_t uSize, uint32_t uTimeoutMS,
746 void* pvData, uint32_t cbData, uint32_t* pcbRead)
747{
748 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
749 AssertReturn(cbData, VERR_INVALID_PARAMETER);
750
751 LogFlowThisFunc(("uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
752 uSize, uTimeoutMS, pvData, cbData));
753
754 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
755
756 int vrc;
757
758 GuestWaitEvent *pEvent = NULL;
759 GuestEventTypes eventTypes;
760 try
761 {
762 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
763 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
764
765 vrc = registerWaitEvent(eventTypes, &pEvent);
766 }
767 catch (std::bad_alloc)
768 {
769 vrc = VERR_NO_MEMORY;
770 }
771
772 if (RT_FAILURE(vrc))
773 return vrc;
774
775 /* Prepare HGCM call. */
776 VBOXHGCMSVCPARM paParms[4];
777 int i = 0;
778 paParms[i++].setUInt32(pEvent->ContextID());
779 paParms[i++].setUInt32(mData.mID /* File handle */);
780 paParms[i++].setUInt32(uSize /* Size (in bytes) to read */);
781
782 alock.release(); /* Drop write lock before sending. */
783
784 uint32_t cbRead;
785 vrc = sendCommand(HOST_FILE_READ, i, paParms);
786 if (RT_SUCCESS(vrc))
787 vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
788
789 if (RT_SUCCESS(vrc))
790 {
791 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
792
793 if (pcbRead)
794 *pcbRead = cbRead;
795 }
796
797 unregisterWaitEvent(pEvent);
798
799 LogFlowFuncLeaveRC(vrc);
800 return vrc;
801}
802
803int GuestFile::i_readDataAt(uint64_t uOffset, uint32_t uSize, uint32_t uTimeoutMS,
804 void* pvData, size_t cbData, size_t* pcbRead)
805{
806 LogFlowThisFunc(("uOffset=%RU64, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
807 uOffset, uSize, uTimeoutMS, pvData, cbData));
808
809 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
810
811 int vrc;
812
813 GuestWaitEvent *pEvent = NULL;
814 GuestEventTypes 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 alock.release(); /* Drop write lock before sending. */
839
840 uint32_t cbRead;
841 vrc = sendCommand(HOST_FILE_READ_AT, i, paParms);
842 if (RT_SUCCESS(vrc))
843 vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
844
845 if (RT_SUCCESS(vrc))
846 {
847 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
848
849 if (pcbRead)
850 *pcbRead = cbRead;
851 }
852
853 unregisterWaitEvent(pEvent);
854
855 LogFlowFuncLeaveRC(vrc);
856 return vrc;
857}
858
859int GuestFile::i_seekAt(int64_t iOffset, GUEST_FILE_SEEKTYPE eSeekType,
860 uint32_t uTimeoutMS, uint64_t *puOffset)
861{
862 LogFlowThisFunc(("iOffset=%RI64, uTimeoutMS=%RU32\n",
863 iOffset, uTimeoutMS));
864
865 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
866
867 int vrc;
868
869 GuestWaitEvent *pEvent = NULL;
870 GuestEventTypes 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 /** @todo uint64_t vs. int64_t! */
893 paParms[i++].setUInt64((uint64_t)iOffset /* Offset (in bytes) to start reading */);
894
895 alock.release(); /* Drop write lock before sending. */
896
897 vrc = sendCommand(HOST_FILE_SEEK, i, paParms);
898 if (RT_SUCCESS(vrc))
899 vrc = i_waitForOffsetChange(pEvent, uTimeoutMS, puOffset);
900
901 unregisterWaitEvent(pEvent);
902
903 LogFlowFuncLeaveRC(vrc);
904 return vrc;
905}
906
907/* static */
908HRESULT GuestFile::i_setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
909{
910 AssertPtr(pInterface);
911 AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
912
913 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestFile::i_guestErrorToString(guestRc).c_str());
914}
915
916int GuestFile::i_setFileStatus(FileStatus_T fileStatus, int fileRc)
917{
918 LogFlowThisFuncEnter();
919
920 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
921
922 LogFlowThisFunc(("oldStatus=%RU32, newStatus=%RU32, fileRc=%Rrc\n",
923 mData.mStatus, fileStatus, fileRc));
924
925#ifdef VBOX_STRICT
926 if (fileStatus == FileStatus_Error)
927 {
928 AssertMsg(RT_FAILURE(fileRc), ("Guest rc must be an error (%Rrc)\n", fileRc));
929 }
930 else
931 AssertMsg(RT_SUCCESS(fileRc), ("Guest rc must not be an error (%Rrc)\n", fileRc));
932#endif
933
934 if (mData.mStatus != fileStatus)
935 {
936 mData.mStatus = fileStatus;
937 mData.mLastError = fileRc;
938
939 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
940 HRESULT hr = errorInfo.createObject();
941 ComAssertComRC(hr);
942 if (RT_FAILURE(fileRc))
943 {
944 hr = errorInfo->initEx(VBOX_E_IPRT_ERROR, fileRc,
945 COM_IIDOF(IGuestFile), getComponentName(),
946 i_guestErrorToString(fileRc));
947 ComAssertComRC(hr);
948 }
949
950 alock.release(); /* Release lock before firing off event. */
951
952 fireGuestFileStateChangedEvent(mEventSource, mSession,
953 this, fileStatus, errorInfo);
954 }
955
956 return VINF_SUCCESS;
957}
958
959int GuestFile::i_waitForOffsetChange(GuestWaitEvent *pEvent,
960 uint32_t uTimeoutMS, uint64_t *puOffset)
961{
962 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
963
964 VBoxEventType_T evtType;
965 ComPtr<IEvent> pIEvent;
966 int vrc = waitForEvent(pEvent, uTimeoutMS,
967 &evtType, pIEvent.asOutParam());
968 if (RT_SUCCESS(vrc))
969 {
970 if (evtType == VBoxEventType_OnGuestFileOffsetChanged)
971 {
972 if (puOffset)
973 {
974 ComPtr<IGuestFileOffsetChangedEvent> pFileEvent = pIEvent;
975 Assert(!pFileEvent.isNull());
976
977 HRESULT hr = pFileEvent->COMGETTER(Offset)((LONG64*)puOffset);
978 ComAssertComRC(hr);
979 }
980 }
981 else
982 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
983 }
984
985 return vrc;
986}
987
988int GuestFile::i_waitForRead(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
989 void *pvData, size_t cbData, uint32_t *pcbRead)
990{
991 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
992
993 VBoxEventType_T evtType;
994 ComPtr<IEvent> pIEvent;
995 int vrc = waitForEvent(pEvent, uTimeoutMS,
996 &evtType, pIEvent.asOutParam());
997 if (RT_SUCCESS(vrc))
998 {
999 if (evtType == VBoxEventType_OnGuestFileRead)
1000 {
1001 ComPtr<IGuestFileReadEvent> pFileEvent = pIEvent;
1002 Assert(!pFileEvent.isNull());
1003
1004 HRESULT hr;
1005 if (pvData)
1006 {
1007 com::SafeArray <BYTE> data;
1008 hr = pFileEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
1009 ComAssertComRC(hr);
1010 size_t cbRead = data.size();
1011 if ( cbRead
1012 && cbRead <= cbData)
1013 {
1014 memcpy(pvData, data.raw(), data.size());
1015 }
1016 else
1017 vrc = VERR_BUFFER_OVERFLOW;
1018 }
1019 if (pcbRead)
1020 {
1021 hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbRead);
1022 ComAssertComRC(hr);
1023 }
1024 }
1025 else
1026 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1027 }
1028
1029 return vrc;
1030}
1031
1032int GuestFile::i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1033 FileStatus_T *pFileStatus, int *pGuestRc)
1034{
1035 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1036 /* pFileStatus is optional. */
1037
1038 VBoxEventType_T evtType;
1039 ComPtr<IEvent> pIEvent;
1040 int vrc = waitForEvent(pEvent, uTimeoutMS,
1041 &evtType, pIEvent.asOutParam());
1042 if (RT_SUCCESS(vrc))
1043 {
1044 Assert(evtType == VBoxEventType_OnGuestFileStateChanged);
1045 ComPtr<IGuestFileStateChangedEvent> pFileEvent = pIEvent;
1046 Assert(!pFileEvent.isNull());
1047
1048 HRESULT hr;
1049 if (pFileStatus)
1050 {
1051 hr = pFileEvent->COMGETTER(Status)(pFileStatus);
1052 ComAssertComRC(hr);
1053 }
1054
1055 ComPtr<IVirtualBoxErrorInfo> errorInfo;
1056 hr = pFileEvent->COMGETTER(Error)(errorInfo.asOutParam());
1057 ComAssertComRC(hr);
1058
1059 LONG lGuestRc;
1060 hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
1061 ComAssertComRC(hr);
1062
1063 LogFlowThisFunc(("resultDetail=%RI32 (%Rrc)\n",
1064 lGuestRc, lGuestRc));
1065
1066 if (RT_FAILURE((int)lGuestRc))
1067 vrc = VERR_GSTCTL_GUEST_ERROR;
1068
1069 if (pGuestRc)
1070 *pGuestRc = (int)lGuestRc;
1071 }
1072
1073 return vrc;
1074}
1075
1076int GuestFile::i_waitForWrite(GuestWaitEvent *pEvent,
1077 uint32_t uTimeoutMS, uint32_t *pcbWritten)
1078{
1079 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1080
1081 VBoxEventType_T evtType;
1082 ComPtr<IEvent> pIEvent;
1083 int vrc = waitForEvent(pEvent, uTimeoutMS,
1084 &evtType, pIEvent.asOutParam());
1085 if (RT_SUCCESS(vrc))
1086 {
1087 if (evtType == VBoxEventType_OnGuestFileWrite)
1088 {
1089 if (pcbWritten)
1090 {
1091 ComPtr<IGuestFileWriteEvent> pFileEvent = pIEvent;
1092 Assert(!pFileEvent.isNull());
1093
1094 HRESULT hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbWritten);
1095 ComAssertComRC(hr);
1096 }
1097 }
1098 else
1099 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1100 }
1101
1102 return vrc;
1103}
1104
1105int GuestFile::i_writeData(uint32_t uTimeoutMS, void *pvData, uint32_t cbData,
1106 uint32_t *pcbWritten)
1107{
1108 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1109 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1110
1111 LogFlowThisFunc(("uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1112 uTimeoutMS, pvData, cbData));
1113
1114 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1115
1116 int vrc;
1117
1118 GuestWaitEvent *pEvent = NULL;
1119 GuestEventTypes eventTypes;
1120 try
1121 {
1122 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1123 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1124
1125 vrc = registerWaitEvent(eventTypes, &pEvent);
1126 }
1127 catch (std::bad_alloc)
1128 {
1129 vrc = VERR_NO_MEMORY;
1130 }
1131
1132 if (RT_FAILURE(vrc))
1133 return vrc;
1134
1135 /* Prepare HGCM call. */
1136 VBOXHGCMSVCPARM paParms[8];
1137 int i = 0;
1138 paParms[i++].setUInt32(pEvent->ContextID());
1139 paParms[i++].setUInt32(mData.mID /* File handle */);
1140 paParms[i++].setUInt32(cbData /* Size (in bytes) to write */);
1141 paParms[i++].setPointer(pvData, cbData);
1142
1143 alock.release(); /* Drop write lock before sending. */
1144
1145 uint32_t cbWritten;
1146 vrc = sendCommand(HOST_FILE_WRITE, i, paParms);
1147 if (RT_SUCCESS(vrc))
1148 vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1149
1150 if (RT_SUCCESS(vrc))
1151 {
1152 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1153
1154 if (cbWritten)
1155 *pcbWritten = cbWritten;
1156 }
1157
1158 unregisterWaitEvent(pEvent);
1159
1160 LogFlowFuncLeaveRC(vrc);
1161 return vrc;
1162}
1163
1164int GuestFile::i_writeDataAt(uint64_t uOffset, uint32_t uTimeoutMS,
1165 void *pvData, uint32_t cbData, uint32_t *pcbWritten)
1166{
1167 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1168 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1169
1170 LogFlowThisFunc(("uOffset=%RU64, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1171 uOffset, uTimeoutMS, pvData, cbData));
1172
1173 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1174
1175 int vrc;
1176
1177 GuestWaitEvent *pEvent = NULL;
1178 GuestEventTypes eventTypes;
1179 try
1180 {
1181 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1182 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1183
1184 vrc = registerWaitEvent(eventTypes, &pEvent);
1185 }
1186 catch (std::bad_alloc)
1187 {
1188 vrc = VERR_NO_MEMORY;
1189 }
1190
1191 if (RT_FAILURE(vrc))
1192 return vrc;
1193
1194 /* Prepare HGCM call. */
1195 VBOXHGCMSVCPARM paParms[8];
1196 int i = 0;
1197 paParms[i++].setUInt32(pEvent->ContextID());
1198 paParms[i++].setUInt32(mData.mID /* File handle */);
1199 paParms[i++].setUInt64(uOffset /* Offset where to starting writing */);
1200 paParms[i++].setUInt32(cbData /* Size (in bytes) to write */);
1201 paParms[i++].setPointer(pvData, cbData);
1202
1203 alock.release(); /* Drop write lock before sending. */
1204
1205 uint32_t cbWritten;
1206 vrc = sendCommand(HOST_FILE_WRITE_AT, i, paParms);
1207 if (RT_SUCCESS(vrc))
1208 vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1209
1210 if (RT_SUCCESS(vrc))
1211 {
1212 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1213
1214 if (cbWritten)
1215 *pcbWritten = cbWritten;
1216 }
1217
1218 unregisterWaitEvent(pEvent);
1219
1220 LogFlowFuncLeaveRC(vrc);
1221 return vrc;
1222}
1223
1224// Wrapped IGuestFile methods
1225/////////////////////////////////////////////////////////////////////////////
1226HRESULT GuestFile::close()
1227{
1228#ifndef VBOX_WITH_GUEST_CONTROL
1229 ReturnComNotImplemented();
1230#else
1231 LogFlowThisFuncEnter();
1232
1233 /* Close file on guest. */
1234 int guestRc;
1235 int rc = i_closeFile(&guestRc);
1236 /* On failure don't return here, instead do all the cleanup
1237 * work first and then return an error. */
1238
1239 AssertPtr(mSession);
1240 int rc2 = mSession->i_fileRemoveFromList(this);
1241 if (RT_SUCCESS(rc))
1242 rc = rc2;
1243
1244 if (RT_FAILURE(rc))
1245 {
1246 if (rc == VERR_GSTCTL_GUEST_ERROR)
1247 return GuestFile::i_setErrorExternal(this, guestRc);
1248
1249 return setError(VBOX_E_IPRT_ERROR,
1250 tr("Closing guest file failed with %Rrc\n"), rc);
1251 }
1252
1253 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
1254 return S_OK;
1255#endif /* VBOX_WITH_GUEST_CONTROL */
1256}
1257
1258HRESULT GuestFile::queryInfo(ComPtr<IFsObjInfo> &aObjInfo)
1259{
1260#ifndef VBOX_WITH_GUEST_CONTROL
1261 ReturnComNotImplemented();
1262#else
1263 ReturnComNotImplemented();
1264#endif /* VBOX_WITH_GUEST_CONTROL */
1265}
1266
1267HRESULT GuestFile::read(ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
1268{
1269#ifndef VBOX_WITH_GUEST_CONTROL
1270 ReturnComNotImplemented();
1271#else
1272 if (aToRead == 0)
1273 return setError(E_INVALIDARG, tr("The size to read is zero"));
1274
1275 com::SafeArray<BYTE> data((size_t)aToRead);
1276 Assert(data.size() >= aToRead);
1277
1278 HRESULT hr = S_OK;
1279
1280 uint32_t cbRead;
1281 int vrc = i_readData(aToRead, aTimeoutMS,
1282 data.raw(), aToRead, &cbRead);
1283
1284 if (RT_SUCCESS(vrc))
1285 {
1286 if (data.size() != cbRead)
1287 data.resize(cbRead);
1288 aData.resize(data.size());
1289 for(size_t i = 0; i < data.size(); ++i)
1290 aData[i] = data[i];
1291 }
1292 else
1293 {
1294 switch (vrc)
1295 {
1296 default:
1297 hr = setError(VBOX_E_IPRT_ERROR,
1298 tr("Reading from file \"%s\" failed: %Rrc"),
1299 mData.mOpenInfo.mFileName.c_str(), vrc);
1300 break;
1301 }
1302 }
1303
1304 LogFlowFuncLeaveRC(vrc);
1305 return hr;
1306#endif /* VBOX_WITH_GUEST_CONTROL */
1307}
1308HRESULT GuestFile::readAt(LONG64 aOffset, ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
1309
1310{
1311#ifndef VBOX_WITH_GUEST_CONTROL
1312 ReturnComNotImplemented();
1313#else
1314 if (aToRead == 0)
1315 return setError(E_INVALIDARG, tr("The size to read is zero"));
1316
1317 com::SafeArray<BYTE> data((size_t)aToRead);
1318 Assert(data.size() >= aToRead);
1319
1320 HRESULT hr = S_OK;
1321
1322 size_t cbRead;
1323 int vrc = i_readDataAt(aOffset, aToRead, aTimeoutMS,
1324 data.raw(), aToRead, &cbRead);
1325 if (RT_SUCCESS(vrc))
1326 {
1327 if (data.size() != cbRead)
1328 data.resize(cbRead);
1329 aData.resize(data.size());
1330 for(size_t i = 0; i < data.size(); ++i)
1331 aData[i] = data[i];
1332 }
1333 else
1334 {
1335 switch (vrc)
1336 {
1337 default:
1338 hr = setError(VBOX_E_IPRT_ERROR,
1339 tr("Reading from file \"%s\" (at offset %RU64) failed: %Rrc"),
1340 mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
1341 break;
1342 }
1343 }
1344
1345 LogFlowFuncLeaveRC(vrc);
1346 return hr;
1347#endif /* VBOX_WITH_GUEST_CONTROL */
1348}
1349
1350HRESULT GuestFile::seek(LONG64 aOffset, FileSeekType_T aWhence)
1351{
1352#ifndef VBOX_WITH_GUEST_CONTROL
1353 ReturnComNotImplemented();
1354#else
1355 LogFlowThisFuncEnter();
1356
1357 HRESULT hr = S_OK;
1358
1359 GUEST_FILE_SEEKTYPE eSeekType;
1360 switch (aWhence)
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; /* Never reached. */
1373 }
1374
1375 int vrc = i_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 %RI64) 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
1394HRESULT GuestFile::setACL(const com::Utf8Str &aAcl)
1395{
1396#ifndef VBOX_WITH_GUEST_CONTROL
1397 ReturnComNotImplemented();
1398#else
1399 ReturnComNotImplemented();
1400#endif /* VBOX_WITH_GUEST_CONTROL */
1401}
1402
1403HRESULT GuestFile::write(const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
1404{
1405#ifndef VBOX_WITH_GUEST_CONTROL
1406 ReturnComNotImplemented();
1407#else
1408 LogFlowThisFuncEnter();
1409
1410 HRESULT hr = S_OK;
1411
1412 com::SafeArray<BYTE> data(aData);
1413 int vrc = i_writeData(aTimeoutMS, data.raw(), (uint32_t)data.size(),
1414 (uint32_t*)aWritten);
1415 if (RT_FAILURE(vrc))
1416 {
1417 switch (vrc)
1418 {
1419 default:
1420 hr = setError(VBOX_E_IPRT_ERROR,
1421 tr("Writing %zubytes to file \"%s\" failed: %Rrc"),
1422 data.size(), mData.mOpenInfo.mFileName.c_str(), vrc);
1423 break;
1424 }
1425 }
1426
1427 LogFlowFuncLeaveRC(vrc);
1428 return hr;
1429#endif /* VBOX_WITH_GUEST_CONTROL */
1430}
1431
1432HRESULT GuestFile::writeAt(LONG64 aOffset, const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
1433
1434{
1435#ifndef VBOX_WITH_GUEST_CONTROL
1436 ReturnComNotImplemented();
1437#else
1438 LogFlowThisFuncEnter();
1439
1440 HRESULT hr = S_OK;
1441
1442 com::SafeArray<BYTE> data(aData);
1443 int vrc = i_writeData(aTimeoutMS, data.raw(), (uint32_t)data.size(),
1444 (uint32_t*)aWritten);
1445 if (RT_FAILURE(vrc))
1446 {
1447 switch (vrc)
1448 {
1449 default:
1450 hr = setError(VBOX_E_IPRT_ERROR,
1451 tr("Writing %zubytes to file \"%s\" (at offset %RU64) failed: %Rrc"),
1452 data.size(), mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
1453 break;
1454 }
1455 }
1456
1457 LogFlowFuncLeaveRC(vrc);
1458 return hr;
1459#endif /* VBOX_WITH_GUEST_CONTROL */
1460}
1461
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