VirtualBox

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

Last change on this file since 104003 was 104003, checked in by vboxsync, 7 months ago

Guest Control/Main: Added a new define GSTCTL_DEFAULT_TIMEOUT_MS and replaced most of the hardcoded timeout values with it.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.4 KB
Line 
1/* $Id: GuestFileImpl.cpp 104003 2024-03-22 16:13:12Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest file handling.
4 */
5
6/*
7 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_MAIN_GUESTFILE
33#include "LoggingNew.h"
34
35#ifndef VBOX_WITH_GUEST_CONTROL
36# error "VBOX_WITH_GUEST_CONTROL must defined in this file"
37#endif
38#include "GuestFileImpl.h"
39#include "GuestSessionImpl.h"
40#include "GuestCtrlImplPrivate.h"
41#include "ConsoleImpl.h"
42#include "VirtualBoxErrorInfoImpl.h"
43
44#include "Global.h"
45#include "AutoCaller.h"
46#include "VBoxEvents.h"
47
48#include <iprt/cpp/utils.h> /* For unconst(). */
49#include <iprt/file.h>
50
51#include <VBox/com/array.h>
52#include <VBox/com/listeners.h>
53#include <VBox/AssertGuest.h>
54
55
56/**
57 * Internal listener class to serve events in an
58 * active manner, e.g. without polling delays.
59 */
60class GuestFileListener
61{
62public:
63
64 GuestFileListener(void)
65 {
66 }
67
68 virtual ~GuestFileListener()
69 {
70 }
71
72 HRESULT init(GuestFile *pFile)
73 {
74 AssertPtrReturn(pFile, E_POINTER);
75 mFile = pFile;
76 return S_OK;
77 }
78
79 void uninit(void)
80 {
81 mFile = NULL;
82 }
83
84 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
85 {
86 switch (aType)
87 {
88 case VBoxEventType_OnGuestFileStateChanged:
89 case VBoxEventType_OnGuestFileOffsetChanged:
90 case VBoxEventType_OnGuestFileRead:
91 case VBoxEventType_OnGuestFileWrite:
92 {
93 AssertPtrReturn(mFile, E_POINTER);
94 int vrc2 = mFile->signalWaitEvent(aType, aEvent);
95 RT_NOREF(vrc2);
96#ifdef DEBUG_andy
97 LogFlowFunc(("Signalling events of type=%RU32, file=%p resulted in vrc=%Rrc\n",
98 aType, mFile, vrc2));
99#endif
100 break;
101 }
102
103 default:
104 AssertMsgFailed(("Unhandled event %RU32\n", aType));
105 break;
106 }
107
108 return S_OK;
109 }
110
111private:
112
113 /** Weak pointer to the guest file object to listen for. */
114 GuestFile *mFile;
115};
116typedef ListenerImpl<GuestFileListener, GuestFile*> GuestFileListenerImpl;
117
118VBOX_LISTENER_DECLARE(GuestFileListenerImpl)
119
120// constructor / destructor
121/////////////////////////////////////////////////////////////////////////////
122
123DEFINE_EMPTY_CTOR_DTOR(GuestFile)
124
125HRESULT GuestFile::FinalConstruct(void)
126{
127 LogFlowThisFuncEnter();
128 return BaseFinalConstruct();
129}
130
131void GuestFile::FinalRelease(void)
132{
133 LogFlowThisFuncEnter();
134 uninit();
135 BaseFinalRelease();
136 LogFlowThisFuncLeave();
137}
138
139// public initializer/uninitializer for internal purposes only
140/////////////////////////////////////////////////////////////////////////////
141
142/**
143 * Initializes a file object but does *not* open the file on the guest
144 * yet. This is done in the dedidcated openFile call.
145 *
146 * @return IPRT status code.
147 * @param pConsole Pointer to console object.
148 * @param pSession Pointer to session object.
149 * @param aObjectID The object's ID.
150 * @param openInfo File opening information.
151 */
152int GuestFile::init(Console *pConsole, GuestSession *pSession,
153 ULONG aObjectID, const GuestFileOpenInfo &openInfo)
154{
155 LogFlowThisFunc(("pConsole=%p, pSession=%p, aObjectID=%RU32, strPath=%s\n",
156 pConsole, pSession, aObjectID, openInfo.mFilename.c_str()));
157
158 AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
159 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
160
161 /* Enclose the state transition NotReady->InInit->Ready. */
162 AutoInitSpan autoInitSpan(this);
163 AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
164
165 int vrc = bindToSession(pConsole, pSession, aObjectID);
166 if (RT_SUCCESS(vrc))
167 {
168 mSession = pSession;
169
170 mData.mOpenInfo = openInfo;
171 mData.mInitialSize = 0;
172 mData.mStatus = FileStatus_Undefined;
173 mData.mLastError = VINF_SUCCESS;
174 mData.mOffCurrent = 0;
175
176 unconst(mEventSource).createObject();
177 HRESULT hr = mEventSource->init();
178 if (FAILED(hr))
179 vrc = VERR_COM_UNEXPECTED;
180 }
181
182 if (RT_SUCCESS(vrc))
183 {
184 try
185 {
186 GuestFileListener *pListener = new GuestFileListener();
187 ComObjPtr<GuestFileListenerImpl> thisListener;
188 HRESULT hr = thisListener.createObject();
189 if (SUCCEEDED(hr))
190 hr = thisListener->init(pListener, this);
191
192 if (SUCCEEDED(hr))
193 {
194 com::SafeArray <VBoxEventType_T> eventTypes;
195 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
196 eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
197 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
198 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
199 hr = mEventSource->RegisterListener(thisListener,
200 ComSafeArrayAsInParam(eventTypes),
201 TRUE /* Active listener */);
202 if (SUCCEEDED(hr))
203 {
204 vrc = baseInit();
205 if (RT_SUCCESS(vrc))
206 {
207 mLocalListener = thisListener;
208 }
209 }
210 else
211 vrc = VERR_COM_UNEXPECTED;
212 }
213 else
214 vrc = VERR_COM_UNEXPECTED;
215 }
216 catch(std::bad_alloc &)
217 {
218 vrc = VERR_NO_MEMORY;
219 }
220 }
221
222 if (RT_SUCCESS(vrc))
223 {
224 /* Confirm a successful initialization when it's the case. */
225 autoInitSpan.setSucceeded();
226 }
227 else
228 autoInitSpan.setFailed();
229
230 LogFlowFuncLeaveRC(vrc);
231 return vrc;
232}
233
234/**
235 * Uninitializes the instance.
236 * Called from FinalRelease().
237 */
238void GuestFile::uninit(void)
239{
240 /* Enclose the state transition Ready->InUninit->NotReady. */
241 AutoUninitSpan autoUninitSpan(this);
242 if (autoUninitSpan.uninitDone())
243 return;
244
245 LogFlowThisFuncEnter();
246
247 baseUninit();
248 LogFlowThisFuncLeave();
249}
250
251// implementation of public getters/setters for attributes
252/////////////////////////////////////////////////////////////////////////////
253
254HRESULT GuestFile::getCreationMode(ULONG *aCreationMode)
255{
256 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
257
258 *aCreationMode = mData.mOpenInfo.mCreationMode;
259
260 return S_OK;
261}
262
263HRESULT GuestFile::getOpenAction(FileOpenAction_T *aOpenAction)
264{
265 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
266
267 *aOpenAction = mData.mOpenInfo.mOpenAction;
268
269 return S_OK;
270}
271
272HRESULT GuestFile::getEventSource(ComPtr<IEventSource> &aEventSource)
273{
274 /* No need to lock - lifetime constant. */
275 mEventSource.queryInterfaceTo(aEventSource.asOutParam());
276
277 return S_OK;
278}
279
280HRESULT GuestFile::getFilename(com::Utf8Str &aFilename)
281{
282 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
283
284 aFilename = mData.mOpenInfo.mFilename;
285
286 return S_OK;
287}
288
289HRESULT GuestFile::getId(ULONG *aId)
290{
291 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
292
293 *aId = mObjectID;
294
295 return S_OK;
296}
297
298HRESULT GuestFile::getInitialSize(LONG64 *aInitialSize)
299{
300 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
301
302 *aInitialSize = mData.mInitialSize;
303
304 return S_OK;
305}
306
307HRESULT GuestFile::getOffset(LONG64 *aOffset)
308{
309 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
310
311 /*
312 * This is updated by GuestFile::i_onFileNotify() when read, write and seek
313 * confirmation messages are recevied.
314 *
315 * Note! This will not be accurate with older (< 5.2.32, 6.0.0 - 6.0.9)
316 * Guest Additions when using writeAt, readAt or writing to a file
317 * opened in append mode.
318 */
319 *aOffset = mData.mOffCurrent;
320
321 return S_OK;
322}
323
324HRESULT GuestFile::getAccessMode(FileAccessMode_T *aAccessMode)
325{
326 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
327
328 *aAccessMode = mData.mOpenInfo.mAccessMode;
329
330 return S_OK;
331}
332
333HRESULT GuestFile::getStatus(FileStatus_T *aStatus)
334{
335 LogFlowThisFuncEnter();
336
337 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
338
339 *aStatus = mData.mStatus;
340
341 return S_OK;
342}
343
344// private methods
345/////////////////////////////////////////////////////////////////////////////
346
347/**
348 * Entry point for guest side file callbacks.
349 *
350 * @returns VBox status code.
351 * @param pCbCtx Host callback context.
352 * @param pSvcCb Host callback data.
353 */
354int GuestFile::i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
355{
356 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
357 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
358
359 LogFlowThisFunc(("strName=%s, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
360 mData.mOpenInfo.mFilename.c_str(), pCbCtx->uContextID, pCbCtx->uMessage, pSvcCb));
361
362 int vrc;
363 switch (pCbCtx->uMessage)
364 {
365 case GUEST_MSG_DISCONNECTED:
366 vrc = i_onGuestDisconnected(pCbCtx, pSvcCb);
367 break;
368
369 case GUEST_MSG_FILE_NOTIFY:
370 vrc = i_onNotify(pCbCtx, pSvcCb);
371 break;
372
373 default:
374 /* Silently ignore not implemented functions. */
375 vrc = VERR_NOT_SUPPORTED;
376 break;
377 }
378
379#ifdef DEBUG
380 LogFlowFuncLeaveRC(vrc);
381#endif
382 return vrc;
383}
384
385/**
386 * Closes the file on the guest side and unregisters it.
387 *
388 * @returns VBox status code.
389 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
390 * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
391 * was returned.
392 */
393int GuestFile::i_close(int *prcGuest)
394{
395 LogFlowThisFunc(("strFile=%s\n", mData.mOpenInfo.mFilename.c_str()));
396
397 int vrc;
398
399 GuestWaitEvent *pEvent = NULL;
400 GuestEventTypes eventTypes;
401 try
402 {
403 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
404
405 vrc = registerWaitEvent(eventTypes, &pEvent);
406 }
407 catch (std::bad_alloc &)
408 {
409 vrc = VERR_NO_MEMORY;
410 }
411
412 if (RT_FAILURE(vrc))
413 return vrc;
414
415 /* Prepare HGCM call. */
416 VBOXHGCMSVCPARM paParms[4];
417 int i = 0;
418 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
419 HGCMSvcSetU32(&paParms[i++], mObjectID /* Guest file ID */);
420
421 vrc = sendMessage(HOST_MSG_FILE_CLOSE, i, paParms);
422 if (RT_SUCCESS(vrc))
423 vrc = i_waitForStatusChange(pEvent, GSTCTL_DEFAULT_TIMEOUT_MS, NULL /* FileStatus */, prcGuest);
424 unregisterWaitEvent(pEvent);
425
426 /* Unregister the file object from the guest session. */
427 AssertPtr(mSession);
428 int vrc2 = mSession->i_fileUnregister(this);
429 if (RT_SUCCESS(vrc))
430 vrc = vrc2;
431
432 LogFlowFuncLeaveRC(vrc);
433 return vrc;
434}
435
436/**
437 * Converts a given guest file error to a string.
438 *
439 * @returns Error string.
440 * @param rcGuest Guest file error to return string for.
441 * @param pcszWhat Hint of what was involved when the error occurred.
442 */
443/* static */
444Utf8Str GuestFile::i_guestErrorToString(int rcGuest, const char *pcszWhat)
445{
446 AssertPtrReturn(pcszWhat, "");
447
448 Utf8Str strErr;
449 switch (rcGuest)
450 {
451#define CASE_MSG(a_iRc, ...) \
452 case a_iRc: strErr.printf(__VA_ARGS__); break;
453 CASE_MSG(VERR_ACCESS_DENIED , tr("Access to guest file \"%s\" denied"), pcszWhat);
454 CASE_MSG(VERR_ALREADY_EXISTS , tr("Guest file \"%s\" already exists"), pcszWhat);
455 CASE_MSG(VERR_FILE_NOT_FOUND , tr("Guest file \"%s\" not found"), pcszWhat);
456 CASE_MSG(VERR_NET_HOST_NOT_FOUND, tr("Host name \"%s\", not found"), pcszWhat);
457 CASE_MSG(VERR_SHARING_VIOLATION , tr("Sharing violation for guest file \"%s\""), pcszWhat);
458 default:
459 strErr.printf(tr("Error %Rrc for guest file \"%s\" occurred\n"), rcGuest, pcszWhat);
460 break;
461#undef CASE_MSG
462 }
463
464 return strErr;
465}
466
467/**
468 * Called when the guest side notifies the host of a file event.
469 *
470 * @returns VBox status code.
471 * @param pCbCtx Host callback context.
472 * @param pSvcCbData Host callback data.
473 */
474int GuestFile::i_onNotify(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
475{
476 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
477 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
478
479 LogFlowThisFuncEnter();
480
481 if (pSvcCbData->mParms < 3)
482 return VERR_INVALID_PARAMETER;
483
484 int idx = 1; /* Current parameter index. */
485 CALLBACKDATA_FILE_NOTIFY dataCb;
486 RT_ZERO(dataCb);
487 /* pSvcCb->mpaParms[0] always contains the context ID. */
488 HGCMSvcGetU32(&pSvcCbData->mpaParms[idx++], &dataCb.uType);
489 HGCMSvcGetU32(&pSvcCbData->mpaParms[idx++], &dataCb.rc);
490
491 int vrcGuest = (int)dataCb.rc; /* uint32_t vs. int. */
492
493 LogFlowThisFunc(("uType=%RU32, vrcGuest=%Rrc\n", dataCb.uType, vrcGuest));
494
495 if (RT_FAILURE(vrcGuest))
496 {
497 int vrc2 = i_setFileStatus(FileStatus_Error, vrcGuest);
498 AssertRC(vrc2);
499
500 /* Ignore return code, as the event to signal might not be there (anymore). */
501 signalWaitEventInternal(pCbCtx, vrcGuest, NULL /* pPayload */);
502 return VINF_SUCCESS; /* Report to the guest. */
503 }
504
505 AssertMsg(mObjectID == VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCbCtx->uContextID),
506 ("File ID %RU32 does not match object ID %RU32\n", mObjectID,
507 VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCbCtx->uContextID)));
508
509 int vrc = VERR_NOT_SUPPORTED; /* Play safe by default. */
510
511 switch (dataCb.uType)
512 {
513 case GUEST_FILE_NOTIFYTYPE_ERROR:
514 {
515 vrc = i_setFileStatus(FileStatus_Error, vrcGuest);
516 break;
517 }
518
519 case GUEST_FILE_NOTIFYTYPE_OPEN:
520 {
521 if (pSvcCbData->mParms == 4)
522 {
523 vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[idx++], &dataCb.u.open.uHandle);
524 if (RT_FAILURE(vrc))
525 break;
526
527 /* Set the process status. */
528 vrc = i_setFileStatus(FileStatus_Open, vrcGuest);
529 }
530 break;
531 }
532
533 case GUEST_FILE_NOTIFYTYPE_CLOSE:
534 {
535 vrc = i_setFileStatus(FileStatus_Closed, vrcGuest);
536 break;
537 }
538
539 case GUEST_FILE_NOTIFYTYPE_READ:
540 {
541 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 4, ("mParms=%u\n", pSvcCbData->mParms),
542 vrc = VERR_WRONG_PARAMETER_COUNT);
543 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_PTR,
544 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
545 vrc = VERR_WRONG_PARAMETER_TYPE);
546
547 vrc = HGCMSvcGetPv(&pSvcCbData->mpaParms[idx++], &dataCb.u.read.pvData, &dataCb.u.read.cbData);
548 if (RT_FAILURE(vrc))
549 break;
550
551 const uint32_t cbRead = dataCb.u.read.cbData;
552 Log3ThisFunc(("cbRead=%RU32\n", cbRead));
553
554 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
555 mData.mOffCurrent += cbRead; /* Bogus for readAt, which is why we've got GUEST_FILE_NOTIFYTYPE_READ_OFFSET. */
556 alock.release();
557
558 try
559 {
560 com::SafeArray<BYTE> data((size_t)cbRead);
561 AssertBreakStmt(data.size() == cbRead, vrc = VERR_NO_MEMORY);
562 data.initFrom((BYTE *)dataCb.u.read.pvData, cbRead);
563 ::FireGuestFileReadEvent(mEventSource, mSession, this, mData.mOffCurrent, cbRead, ComSafeArrayAsInParam(data));
564 }
565 catch (std::bad_alloc &)
566 {
567 vrc = VERR_NO_MEMORY;
568 }
569 break;
570 }
571
572 case GUEST_FILE_NOTIFYTYPE_READ_OFFSET:
573 {
574 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 5, ("mParms=%u\n", pSvcCbData->mParms),
575 vrc = VERR_WRONG_PARAMETER_COUNT);
576 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_PTR,
577 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
578 vrc = VERR_WRONG_PARAMETER_TYPE);
579 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx + 1].type == VBOX_HGCM_SVC_PARM_64BIT,
580 ("type=%u\n", pSvcCbData->mpaParms[idx + 1].type),
581 vrc = VERR_WRONG_PARAMETER_TYPE);
582 BYTE const * const pbData = (BYTE const *)pSvcCbData->mpaParms[idx].u.pointer.addr;
583 uint32_t const cbRead = pSvcCbData->mpaParms[idx].u.pointer.size;
584 int64_t offNew = (int64_t)pSvcCbData->mpaParms[idx + 1].u.uint64;
585 Log3ThisFunc(("cbRead=%RU32 offNew=%RI64 (%#RX64)\n", cbRead, offNew, offNew));
586
587 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
588 if (offNew < 0) /* non-seekable */
589 offNew = mData.mOffCurrent + cbRead;
590 mData.mOffCurrent = offNew;
591 alock.release();
592
593 try
594 {
595 com::SafeArray<BYTE> data((size_t)cbRead);
596 AssertBreakStmt(data.size() == cbRead, vrc = VERR_NO_MEMORY);
597 data.initFrom(pbData, cbRead);
598 ::FireGuestFileReadEvent(mEventSource, mSession, this, offNew, cbRead, ComSafeArrayAsInParam(data));
599 vrc = VINF_SUCCESS;
600 }
601 catch (std::bad_alloc &)
602 {
603 vrc = VERR_NO_MEMORY;
604 }
605 break;
606 }
607
608 case GUEST_FILE_NOTIFYTYPE_WRITE:
609 {
610 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 4, ("mParms=%u\n", pSvcCbData->mParms),
611 vrc = VERR_WRONG_PARAMETER_COUNT);
612 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_32BIT,
613 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
614 vrc = VERR_WRONG_PARAMETER_TYPE);
615
616 uint32_t const cbWritten = pSvcCbData->mpaParms[idx].u.uint32;
617
618 Log3ThisFunc(("cbWritten=%RU32\n", cbWritten));
619
620 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
621 mData.mOffCurrent += cbWritten; /* Bogus for writeAt and append mode, thus GUEST_FILE_NOTIFYTYPE_WRITE_OFFSET. */
622 alock.release();
623
624 ::FireGuestFileWriteEvent(mEventSource, mSession, this, mData.mOffCurrent, cbWritten);
625 break;
626 }
627
628 case GUEST_FILE_NOTIFYTYPE_WRITE_OFFSET:
629 {
630 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 5, ("mParms=%u\n", pSvcCbData->mParms),
631 vrc = VERR_WRONG_PARAMETER_COUNT);
632 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_32BIT,
633 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
634 vrc = VERR_WRONG_PARAMETER_TYPE);
635 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx + 1].type == VBOX_HGCM_SVC_PARM_64BIT,
636 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
637 vrc = VERR_WRONG_PARAMETER_TYPE);
638 uint32_t const cbWritten = pSvcCbData->mpaParms[idx].u.uint32;
639 int64_t offNew = (int64_t)pSvcCbData->mpaParms[idx + 1].u.uint64;
640 Log3ThisFunc(("cbWritten=%RU32 offNew=%RI64 (%#RX64)\n", cbWritten, offNew, offNew));
641
642 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
643 if (offNew < 0) /* non-seekable */
644 offNew = mData.mOffCurrent + cbWritten;
645 mData.mOffCurrent = offNew;
646 alock.release();
647
648 HRESULT hrc2 = ::FireGuestFileWriteEvent(mEventSource, mSession, this, offNew, cbWritten);
649 vrc = SUCCEEDED(hrc2) ? VINF_SUCCESS : Global::vboxStatusCodeFromCOM(hrc2);
650 break;
651 }
652
653 case GUEST_FILE_NOTIFYTYPE_SEEK:
654 {
655 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 4, ("mParms=%u\n", pSvcCbData->mParms),
656 vrc = VERR_WRONG_PARAMETER_COUNT);
657
658 vrc = HGCMSvcGetU64(&pSvcCbData->mpaParms[idx++], &dataCb.u.seek.uOffActual);
659 if (RT_FAILURE(vrc))
660 break;
661
662 Log3ThisFunc(("uOffActual=%RU64\n", dataCb.u.seek.uOffActual));
663
664 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
665 mData.mOffCurrent = dataCb.u.seek.uOffActual;
666 alock.release();
667
668 ::FireGuestFileOffsetChangedEvent(mEventSource, mSession, this, dataCb.u.seek.uOffActual, 0 /* Processed */);
669 break;
670 }
671
672 case GUEST_FILE_NOTIFYTYPE_TELL:
673 /* We don't issue any HOST_MSG_FILE_TELL, so we shouldn't get these notifications! */
674 AssertFailed();
675 break;
676
677 case GUEST_FILE_NOTIFYTYPE_SET_SIZE:
678 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mParms == 4, ("mParms=%u\n", pSvcCbData->mParms),
679 vrc = VERR_WRONG_PARAMETER_COUNT);
680 ASSERT_GUEST_MSG_STMT_BREAK(pSvcCbData->mpaParms[idx].type == VBOX_HGCM_SVC_PARM_64BIT,
681 ("type=%u\n", pSvcCbData->mpaParms[idx].type),
682 vrc = VERR_WRONG_PARAMETER_TYPE);
683 dataCb.u.SetSize.cbSize = pSvcCbData->mpaParms[idx].u.uint64;
684 Log3ThisFunc(("cbSize=%RU64\n", dataCb.u.SetSize.cbSize));
685
686 ::FireGuestFileSizeChangedEvent(mEventSource, mSession, this, dataCb.u.SetSize.cbSize);
687 vrc = VINF_SUCCESS;
688 break;
689
690 default:
691 break;
692 }
693
694 try
695 {
696 if (RT_SUCCESS(vrc))
697 {
698 GuestWaitEventPayload payload(dataCb.uType, &dataCb, sizeof(dataCb));
699
700 /* Ignore return code, as the event to signal might not be there (anymore). */
701 signalWaitEventInternal(pCbCtx, vrcGuest, &payload);
702 }
703 else /* OOM situation, wrong HGCM parameters or smth. not expected. */
704 {
705 /* Ignore return code, as the event to signal might not be there (anymore). */
706 signalWaitEventInternalEx(pCbCtx, vrc, 0 /* guestRc */, NULL /* pPayload */);
707 }
708 }
709 catch (int vrcEx) /* Thrown by GuestWaitEventPayload constructor. */
710 {
711 /* Also try to signal the waiter, to let it know of the OOM situation.
712 * Ignore return code, as the event to signal might not be there (anymore). */
713 signalWaitEventInternalEx(pCbCtx, vrcEx, 0 /* guestRc */, NULL /* pPayload */);
714 vrc = vrcEx;
715 }
716
717 LogFlowThisFunc(("uType=%RU32, rcGuest=%Rrc, vrc=%Rrc\n", dataCb.uType, vrcGuest, vrc));
718 return vrc;
719}
720
721/**
722 * Called when the guest side of the file has been disconnected (closed, terminated, +++).
723 *
724 * @returns VBox status code.
725 * @param pCbCtx Host callback context.
726 * @param pSvcCbData Host callback data.
727 */
728int GuestFile::i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
729{
730 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
731 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
732
733 int vrc = i_setFileStatus(FileStatus_Down, VINF_SUCCESS);
734
735 LogFlowFuncLeaveRC(vrc);
736 return vrc;
737}
738
739/**
740 * @copydoc GuestObject::i_onUnregister
741 */
742int GuestFile::i_onUnregister(void)
743{
744 LogFlowThisFuncEnter();
745
746 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
747
748 int vrc = VINF_SUCCESS;
749
750 /*
751 * Note: The event source stuff holds references to this object,
752 * so make sure that this is cleaned up *before* calling uninit().
753 */
754 if (!mEventSource.isNull())
755 {
756 mEventSource->UnregisterListener(mLocalListener);
757
758 mLocalListener.setNull();
759 unconst(mEventSource).setNull();
760 }
761
762 LogFlowFuncLeaveRC(vrc);
763 return vrc;
764}
765
766/**
767 * @copydoc GuestObject::i_onSessionStatusChange
768 */
769int GuestFile::i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus)
770{
771 LogFlowThisFuncEnter();
772
773 int vrc = VINF_SUCCESS;
774
775 /* If the session now is in a terminated state, set the file status
776 * to "down", as there is not much else we can do now. */
777 if (GuestSession::i_isTerminated(enmSessionStatus))
778 vrc = i_setFileStatus(FileStatus_Down, 0 /* fileRc, ignored */);
779
780 LogFlowFuncLeaveRC(vrc);
781 return vrc;
782}
783
784/**
785 * Opens the file on the guest.
786 *
787 * @returns VBox status code.
788 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
789 * @param uTimeoutMS Timeout (in ms) to wait.
790 * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
791 * was returned. Optional.
792 */
793int GuestFile::i_open(uint32_t uTimeoutMS, int *prcGuest)
794{
795 AssertReturn(mData.mOpenInfo.mFilename.isNotEmpty(), VERR_INVALID_PARAMETER);
796
797 LogFlowThisFuncEnter();
798
799 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
800
801 LogFlowThisFunc(("strFile=%s, enmAccessMode=%d, enmOpenAction=%d, uCreationMode=%o, mfOpenEx=%#x\n",
802 mData.mOpenInfo.mFilename.c_str(), mData.mOpenInfo.mAccessMode, mData.mOpenInfo.mOpenAction,
803 mData.mOpenInfo.mCreationMode, mData.mOpenInfo.mfOpenEx));
804
805 /* Validate and translate open action. */
806 const char *pszOpenAction = NULL;
807 switch (mData.mOpenInfo.mOpenAction)
808 {
809 case FileOpenAction_OpenExisting: pszOpenAction = "oe"; break;
810 case FileOpenAction_OpenOrCreate: pszOpenAction = "oc"; break;
811 case FileOpenAction_CreateNew: pszOpenAction = "ce"; break;
812 case FileOpenAction_CreateOrReplace: pszOpenAction = "ca"; break;
813 case FileOpenAction_OpenExistingTruncated: pszOpenAction = "ot"; break;
814 case FileOpenAction_AppendOrCreate:
815 pszOpenAction = "oa"; /** @todo get rid of this one and implement AppendOnly/AppendRead. */
816 break;
817 default:
818 return VERR_INVALID_PARAMETER;
819 }
820
821 /* Validate and translate access mode. */
822 const char *pszAccessMode = NULL;
823 switch (mData.mOpenInfo.mAccessMode)
824 {
825 case FileAccessMode_ReadOnly: pszAccessMode = "r"; break;
826 case FileAccessMode_WriteOnly: pszAccessMode = "w"; break;
827 case FileAccessMode_ReadWrite: pszAccessMode = "r+"; break;
828 case FileAccessMode_AppendOnly: pszAccessMode = "a"; break;
829 case FileAccessMode_AppendRead: pszAccessMode = "a+"; break;
830 default: return VERR_INVALID_PARAMETER;
831 }
832
833 /* Validate and translate sharing mode. */
834 const char *pszSharingMode = NULL;
835 switch (mData.mOpenInfo.mSharingMode)
836 {
837 case FileSharingMode_All: pszSharingMode = ""; break;
838 case FileSharingMode_Read: RT_FALL_THRU();
839 case FileSharingMode_Write: RT_FALL_THRU();
840 case FileSharingMode_ReadWrite: RT_FALL_THRU();
841 case FileSharingMode_Delete: RT_FALL_THRU();
842 case FileSharingMode_ReadDelete: RT_FALL_THRU();
843 case FileSharingMode_WriteDelete: return VERR_NOT_IMPLEMENTED;
844 default: return VERR_INVALID_PARAMETER;
845 }
846
847 int vrc;
848
849 GuestWaitEvent *pEvent = NULL;
850 GuestEventTypes eventTypes;
851 try
852 {
853 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
854
855 vrc = registerWaitEvent(eventTypes, &pEvent);
856 }
857 catch (std::bad_alloc &)
858 {
859 vrc = VERR_NO_MEMORY;
860 }
861
862 if (RT_FAILURE(vrc))
863 return vrc;
864
865 /* Prepare HGCM call. */
866 VBOXHGCMSVCPARM paParms[8];
867 int i = 0;
868 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
869 HGCMSvcSetPv(&paParms[i++], (void*)mData.mOpenInfo.mFilename.c_str(),
870 (ULONG)mData.mOpenInfo.mFilename.length() + 1);
871 HGCMSvcSetStr(&paParms[i++], pszAccessMode);
872 HGCMSvcSetStr(&paParms[i++], pszOpenAction);
873 HGCMSvcSetStr(&paParms[i++], pszSharingMode);
874 HGCMSvcSetU32(&paParms[i++], mData.mOpenInfo.mCreationMode);
875 HGCMSvcSetU64(&paParms[i++], 0 /*unused offset*/);
876 /** @todo Next protocol version: add flags, replace strings, remove initial offset. */
877
878 alock.release(); /* Drop write lock before sending. */
879
880 vrc = sendMessage(HOST_MSG_FILE_OPEN, i, paParms);
881 if (RT_SUCCESS(vrc))
882 vrc = i_waitForStatusChange(pEvent, uTimeoutMS, NULL /* FileStatus */, prcGuest);
883
884 unregisterWaitEvent(pEvent);
885
886 LogFlowFuncLeaveRC(vrc);
887 return vrc;
888}
889
890/**
891 * Queries file system information from a guest file.
892 *
893 * @returns VBox status code.
894 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
895 * @param objData Where to store the file system object data on success.
896 * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
897 * was returned. Optional.
898 */
899int GuestFile::i_queryInfo(GuestFsObjData &objData, int *prcGuest)
900{
901 AssertPtrReturn(mSession, VERR_OBJECT_DESTROYED);
902 return mSession->i_fsObjQueryInfo(mData.mOpenInfo.mFilename, FALSE /* fFollowSymlinks */, objData, prcGuest);
903}
904
905/**
906 * Reads data from a guest file.
907 *
908 * @returns VBox status code.
909 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
910 * @param uSize Size (in bytes) to read.
911 * @param uTimeoutMS Timeout (in ms) to wait.
912 * @param pvData Where to store the read data on success.
913 * @param cbData Size (in bytes) of \a pvData on input.
914 * @param pcbRead Where to return to size (in bytes) read on success.
915 * Optional.
916 */
917int GuestFile::i_readData(uint32_t uSize, uint32_t uTimeoutMS,
918 void* pvData, uint32_t cbData, uint32_t* pcbRead)
919{
920 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
921 AssertReturn(cbData, VERR_INVALID_PARAMETER);
922
923 LogFlowThisFunc(("uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
924 uSize, uTimeoutMS, pvData, cbData));
925
926 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
927
928 int vrc;
929
930 GuestWaitEvent *pEvent = NULL;
931 GuestEventTypes eventTypes;
932 try
933 {
934 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
935 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
936
937 vrc = registerWaitEvent(eventTypes, &pEvent);
938 }
939 catch (std::bad_alloc &)
940 {
941 vrc = VERR_NO_MEMORY;
942 }
943
944 if (RT_FAILURE(vrc))
945 return vrc;
946
947 /* Prepare HGCM call. */
948 VBOXHGCMSVCPARM paParms[4];
949 int i = 0;
950 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
951 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
952 HGCMSvcSetU32(&paParms[i++], uSize /* Size (in bytes) to read */);
953
954 alock.release(); /* Drop write lock before sending. */
955
956 vrc = sendMessage(HOST_MSG_FILE_READ, i, paParms);
957 if (RT_SUCCESS(vrc))
958 {
959 uint32_t cbRead = 0;
960 vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
961 if (RT_SUCCESS(vrc))
962 {
963 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
964 if (pcbRead)
965 *pcbRead = cbRead;
966 }
967 else if (pEvent->HasGuestError()) /* Return guest vrc if available. */
968 vrc = pEvent->GuestResult();
969 }
970
971 unregisterWaitEvent(pEvent);
972
973 LogFlowFuncLeaveRC(vrc);
974 return vrc;
975}
976
977/**
978 * Reads data from a specific position from a guest file.
979 *
980 * @returns VBox status code.
981 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
982 * @param uOffset Offset (in bytes) to start reading from.
983 * @param uSize Size (in bytes) to read.
984 * @param uTimeoutMS Timeout (in ms) to wait.
985 * @param pvData Where to store the read data on success.
986 * @param cbData Size (in bytes) of \a pvData on input.
987 * @param pcbRead Where to return to size (in bytes) read on success.
988 * Optional.
989 */
990int GuestFile::i_readDataAt(uint64_t uOffset, uint32_t uSize, uint32_t uTimeoutMS,
991 void* pvData, size_t cbData, size_t* pcbRead)
992{
993 LogFlowThisFunc(("uOffset=%RU64, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
994 uOffset, uSize, uTimeoutMS, pvData, cbData));
995
996 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
997
998 int vrc;
999
1000 GuestWaitEvent *pEvent = NULL;
1001 GuestEventTypes eventTypes;
1002 try
1003 {
1004 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1005 eventTypes.push_back(VBoxEventType_OnGuestFileRead);
1006
1007 vrc = registerWaitEvent(eventTypes, &pEvent);
1008 }
1009 catch (std::bad_alloc &)
1010 {
1011 vrc = VERR_NO_MEMORY;
1012 }
1013
1014 if (RT_FAILURE(vrc))
1015 return vrc;
1016
1017 /* Prepare HGCM call. */
1018 VBOXHGCMSVCPARM paParms[4];
1019 int i = 0;
1020 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1021 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1022 HGCMSvcSetU64(&paParms[i++], uOffset /* Offset (in bytes) to start reading */);
1023 HGCMSvcSetU32(&paParms[i++], uSize /* Size (in bytes) to read */);
1024
1025 alock.release(); /* Drop write lock before sending. */
1026
1027 vrc = sendMessage(HOST_MSG_FILE_READ_AT, i, paParms);
1028 if (RT_SUCCESS(vrc))
1029 {
1030 uint32_t cbRead = 0;
1031 vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead);
1032 if (RT_SUCCESS(vrc))
1033 {
1034 LogFlowThisFunc(("cbRead=%RU32\n", cbRead));
1035
1036 if (pcbRead)
1037 *pcbRead = cbRead;
1038 }
1039 else if (pEvent->HasGuestError()) /* Return guest vrc if available. */
1040 vrc = pEvent->GuestResult();
1041 }
1042
1043 unregisterWaitEvent(pEvent);
1044
1045 LogFlowFuncLeaveRC(vrc);
1046 return vrc;
1047}
1048
1049/**
1050 * Seeks a guest file to a specific position.
1051 *
1052 * @returns VBox status code.
1053 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
1054 * @param iOffset Offset (in bytes) to seek.
1055 * @param eSeekType Seek type to use.
1056 * @param uTimeoutMS Timeout (in ms) to wait.
1057 * @param puOffset Where to return the new current file position (in bytes) on success.
1058 */
1059int GuestFile::i_seekAt(int64_t iOffset, GUEST_FILE_SEEKTYPE eSeekType,
1060 uint32_t uTimeoutMS, uint64_t *puOffset)
1061{
1062 LogFlowThisFunc(("iOffset=%RI64, uTimeoutMS=%RU32\n",
1063 iOffset, uTimeoutMS));
1064
1065 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1066
1067 int vrc;
1068
1069 GuestWaitEvent *pEvent = NULL;
1070 GuestEventTypes eventTypes;
1071 try
1072 {
1073 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1074 eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
1075
1076 vrc = registerWaitEvent(eventTypes, &pEvent);
1077 }
1078 catch (std::bad_alloc &)
1079 {
1080 vrc = VERR_NO_MEMORY;
1081 }
1082
1083 if (RT_FAILURE(vrc))
1084 return vrc;
1085
1086 /* Prepare HGCM call. */
1087 VBOXHGCMSVCPARM paParms[4];
1088 int i = 0;
1089 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1090 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1091 HGCMSvcSetU32(&paParms[i++], eSeekType /* Seek method */);
1092 /** @todo uint64_t vs. int64_t! */
1093 HGCMSvcSetU64(&paParms[i++], (uint64_t)iOffset /* Offset (in bytes) to start reading */);
1094
1095 alock.release(); /* Drop write lock before sending. */
1096
1097 vrc = sendMessage(HOST_MSG_FILE_SEEK, i, paParms);
1098 if (RT_SUCCESS(vrc))
1099 {
1100 uint64_t uOffset;
1101 vrc = i_waitForOffsetChange(pEvent, uTimeoutMS, &uOffset);
1102 if (RT_SUCCESS(vrc))
1103 {
1104 LogFlowThisFunc(("uOffset=%RU64\n", uOffset));
1105
1106 if (puOffset)
1107 *puOffset = uOffset;
1108 }
1109 else if (pEvent->HasGuestError()) /* Return guest vrc if available. */
1110 vrc = pEvent->GuestResult();
1111 }
1112
1113 unregisterWaitEvent(pEvent);
1114
1115 LogFlowFuncLeaveRC(vrc);
1116 return vrc;
1117}
1118
1119/**
1120 * Sets the current internal file object status.
1121 *
1122 * @returns VBox status code.
1123 * @param fileStatus New file status to set.
1124 * @param vrcFile New result code to set.
1125 *
1126 * @note Takes the write lock.
1127 */
1128int GuestFile::i_setFileStatus(FileStatus_T fileStatus, int vrcFile)
1129{
1130 LogFlowThisFuncEnter();
1131
1132 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1133
1134 LogFlowThisFunc(("oldStatus=%RU32, newStatus=%RU32, vrcFile=%Rrc\n", mData.mStatus, fileStatus, vrcFile));
1135
1136#ifdef VBOX_STRICT
1137 if (fileStatus == FileStatus_Error)
1138 AssertMsg(RT_FAILURE(vrcFile), ("Guest vrc must be an error (%Rrc)\n", vrcFile));
1139 else
1140 AssertMsg(RT_SUCCESS(vrcFile), ("Guest vrc must not be an error (%Rrc)\n", vrcFile));
1141#endif
1142
1143 if (mData.mStatus != fileStatus)
1144 {
1145 mData.mStatus = fileStatus;
1146 mData.mLastError = vrcFile;
1147
1148 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
1149 HRESULT hrc = errorInfo.createObject();
1150 ComAssertComRCRet(hrc, VERR_COM_UNEXPECTED);
1151 if (RT_FAILURE(vrcFile))
1152 {
1153 hrc = errorInfo->initEx(VBOX_E_GSTCTL_GUEST_ERROR, vrcFile,
1154 COM_IIDOF(IGuestFile), getComponentName(),
1155 i_guestErrorToString(vrcFile, mData.mOpenInfo.mFilename.c_str()));
1156 ComAssertComRCRet(hrc, VERR_COM_UNEXPECTED);
1157 }
1158 /* Note: On vrcFile success, errorInfo is set to S_OK and also sent via the event below. */
1159
1160 alock.release(); /* Release lock before firing off event. */
1161
1162 ::FireGuestFileStateChangedEvent(mEventSource, mSession, this, fileStatus, errorInfo);
1163 }
1164
1165 return VINF_SUCCESS;
1166}
1167
1168/**
1169 * Waits for a guest file offset change.
1170 *
1171 * @returns VBox status code.
1172 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
1173 * @param pEvent Guest wait event to wait for.
1174 * @param uTimeoutMS Timeout (in ms) to wait.
1175 * @param puOffset Where to return the new offset (in bytes) on success.
1176 * Optional and can be NULL.
1177 */
1178int GuestFile::i_waitForOffsetChange(GuestWaitEvent *pEvent,
1179 uint32_t uTimeoutMS, uint64_t *puOffset)
1180{
1181 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1182
1183 VBoxEventType_T evtType;
1184 ComPtr<IEvent> pIEvent;
1185 int vrc = waitForEvent(pEvent, uTimeoutMS,
1186 &evtType, pIEvent.asOutParam());
1187 if (RT_SUCCESS(vrc))
1188 {
1189 if (evtType == VBoxEventType_OnGuestFileOffsetChanged)
1190 {
1191 if (puOffset)
1192 {
1193 ComPtr<IGuestFileOffsetChangedEvent> pFileEvent = pIEvent;
1194 Assert(!pFileEvent.isNull());
1195
1196 HRESULT hr = pFileEvent->COMGETTER(Offset)((LONG64*)puOffset);
1197 ComAssertComRC(hr);
1198 }
1199 }
1200 else
1201 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1202 }
1203
1204 return vrc;
1205}
1206
1207/**
1208 * Waits for reading from a guest file.
1209 *
1210 * @returns VBox status code.
1211 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
1212 * @param pEvent Guest wait event to wait for.
1213 * @param uTimeoutMS Timeout (in ms) to wait.
1214 * @param pvData Where to store read file data on success.
1215 * @param cbData Size (in bytes) of \a pvData.
1216 * @param pcbRead Where to return the actual bytes read on success.
1217 * Optional and can be NULL.
1218 */
1219int GuestFile::i_waitForRead(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1220 void *pvData, size_t cbData, uint32_t *pcbRead)
1221{
1222 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1223
1224 VBoxEventType_T evtType;
1225 ComPtr<IEvent> pIEvent;
1226 int vrc = waitForEvent(pEvent, uTimeoutMS,
1227 &evtType, pIEvent.asOutParam());
1228 if (RT_SUCCESS(vrc))
1229 {
1230 if (evtType == VBoxEventType_OnGuestFileRead)
1231 {
1232 vrc = VINF_SUCCESS;
1233
1234 ComPtr<IGuestFileReadEvent> pFileEvent = pIEvent;
1235 Assert(!pFileEvent.isNull());
1236
1237 if (pvData)
1238 {
1239 com::SafeArray <BYTE> data;
1240 HRESULT hrc1 = pFileEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
1241 ComAssertComRC(hrc1);
1242 const size_t cbRead = data.size();
1243 if (cbRead)
1244 {
1245 if (cbRead <= cbData)
1246 memcpy(pvData, data.raw(), cbRead);
1247 else
1248 vrc = VERR_BUFFER_OVERFLOW;
1249 }
1250 /* else: used to be VERR_NO_DATA, but that messes stuff up. */
1251
1252 if (pcbRead)
1253 {
1254 *pcbRead = (uint32_t)cbRead;
1255 Assert(*pcbRead == cbRead);
1256 }
1257 }
1258 else if (pcbRead)
1259 {
1260 *pcbRead = 0;
1261 HRESULT hrc2 = pFileEvent->COMGETTER(Processed)((ULONG *)pcbRead);
1262 ComAssertComRC(hrc2); NOREF(hrc2);
1263 }
1264 }
1265 else
1266 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1267 }
1268
1269 return vrc;
1270}
1271
1272/**
1273 * Waits for a guest file status change.
1274 *
1275 * @note Similar code in GuestProcess::i_waitForStatusChange() and
1276 * GuestSession::i_waitForStatusChange().
1277 *
1278 * @returns VBox status code.
1279 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
1280 * @param pEvent Guest wait event to wait for.
1281 * @param uTimeoutMS Timeout (in ms) to wait.
1282 * @param pFileStatus Where to return the file status on success.
1283 * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
1284 * was returned.
1285 */
1286int GuestFile::i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1287 FileStatus_T *pFileStatus, int *prcGuest)
1288{
1289 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1290 /* pFileStatus is optional. */
1291
1292 VBoxEventType_T evtType;
1293 ComPtr<IEvent> pIEvent;
1294 int vrc = waitForEvent(pEvent, uTimeoutMS,
1295 &evtType, pIEvent.asOutParam());
1296 if (RT_SUCCESS(vrc))
1297 {
1298 Assert(evtType == VBoxEventType_OnGuestFileStateChanged);
1299 ComPtr<IGuestFileStateChangedEvent> pFileEvent = pIEvent;
1300 Assert(!pFileEvent.isNull());
1301
1302 HRESULT hr;
1303 if (pFileStatus)
1304 {
1305 hr = pFileEvent->COMGETTER(Status)(pFileStatus);
1306 ComAssertComRC(hr);
1307 }
1308
1309 ComPtr<IVirtualBoxErrorInfo> errorInfo;
1310 hr = pFileEvent->COMGETTER(Error)(errorInfo.asOutParam());
1311 ComAssertComRC(hr);
1312
1313 LONG lGuestRc;
1314 hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
1315 ComAssertComRC(hr);
1316
1317 LogFlowThisFunc(("resultDetail=%RI32 (%Rrc)\n",
1318 lGuestRc, lGuestRc));
1319
1320 if (RT_FAILURE((int)lGuestRc))
1321 vrc = VERR_GSTCTL_GUEST_ERROR;
1322
1323 if (prcGuest)
1324 *prcGuest = (int)lGuestRc;
1325 }
1326 /* waitForEvent may also return VERR_GSTCTL_GUEST_ERROR like we do above, so make prcGuest is set. */
1327 /** @todo r=bird: Andy, you seem to have forgotten this scenario. Showed up occasionally when
1328 * using the wrong password with a copyto command in a debug build on windows, error info
1329 * contained "Unknown Status -858993460 (0xcccccccc)". As you know windows fills the stack frames
1330 * with 0xcccccccc in debug builds to highlight use of uninitialized data, so that's what happened
1331 * here. It's actually good you didn't initialize lGuest, as it would be heck to find otherwise.
1332 *
1333 * I'm still not very impressed with the error managment or the usuefullness of the documentation
1334 * in this code, though the latter is getting better! */
1335 else if (vrc == VERR_GSTCTL_GUEST_ERROR && prcGuest)
1336 *prcGuest = pEvent->GuestResult();
1337 Assert(vrc != VERR_GSTCTL_GUEST_ERROR || !prcGuest || *prcGuest != (int)0xcccccccc);
1338
1339 return vrc;
1340}
1341
1342int GuestFile::i_waitForWrite(GuestWaitEvent *pEvent,
1343 uint32_t uTimeoutMS, uint32_t *pcbWritten)
1344{
1345 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1346
1347 VBoxEventType_T evtType;
1348 ComPtr<IEvent> pIEvent;
1349 int vrc = waitForEvent(pEvent, uTimeoutMS,
1350 &evtType, pIEvent.asOutParam());
1351 if (RT_SUCCESS(vrc))
1352 {
1353 if (evtType == VBoxEventType_OnGuestFileWrite)
1354 {
1355 if (pcbWritten)
1356 {
1357 ComPtr<IGuestFileWriteEvent> pFileEvent = pIEvent;
1358 Assert(!pFileEvent.isNull());
1359
1360 HRESULT hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbWritten);
1361 ComAssertComRC(hr);
1362 }
1363 }
1364 else
1365 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1366 }
1367
1368 return vrc;
1369}
1370
1371/**
1372 * Writes data to a guest file.
1373 *
1374 * @returns VBox status code.
1375 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
1376 * @param uTimeoutMS Timeout (in ms) to wait.
1377 * @param pvData Data to write.
1378 * @param cbData Size (in bytes) of \a pvData to write.
1379 * @param pcbWritten Where to return to size (in bytes) written on success.
1380 * Optional.
1381 */
1382int GuestFile::i_writeData(uint32_t uTimeoutMS, const void *pvData, uint32_t cbData,
1383 uint32_t *pcbWritten)
1384{
1385 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1386 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1387
1388 LogFlowThisFunc(("uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1389 uTimeoutMS, pvData, cbData));
1390
1391 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1392
1393 int vrc;
1394
1395 GuestWaitEvent *pEvent = NULL;
1396 GuestEventTypes eventTypes;
1397 try
1398 {
1399 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1400 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1401
1402 vrc = registerWaitEvent(eventTypes, &pEvent);
1403 }
1404 catch (std::bad_alloc &)
1405 {
1406 vrc = VERR_NO_MEMORY;
1407 }
1408
1409 if (RT_FAILURE(vrc))
1410 return vrc;
1411
1412 /* Prepare HGCM call. */
1413 VBOXHGCMSVCPARM paParms[8];
1414 int i = 0;
1415 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1416 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1417 HGCMSvcSetU32(&paParms[i++], cbData /* Size (in bytes) to write */);
1418 HGCMSvcSetPv (&paParms[i++], unconst(pvData), cbData);
1419
1420 alock.release(); /* Drop write lock before sending. */
1421
1422 vrc = sendMessage(HOST_MSG_FILE_WRITE, i, paParms);
1423 if (RT_SUCCESS(vrc))
1424 {
1425 uint32_t cbWritten = 0;
1426 vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1427 if (RT_SUCCESS(vrc))
1428 {
1429 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1430 if (pcbWritten)
1431 *pcbWritten = cbWritten;
1432 }
1433 else if (pEvent->HasGuestError()) /* Return guest vrc if available. */
1434 vrc = pEvent->GuestResult();
1435 }
1436
1437 unregisterWaitEvent(pEvent);
1438
1439 LogFlowFuncLeaveRC(vrc);
1440 return vrc;
1441}
1442
1443
1444/**
1445 * Writes data to a specific position to a guest file.
1446 *
1447 * @returns VBox status code.
1448 * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
1449 * @param uOffset Offset (in bytes) to start writing at.
1450 * @param uTimeoutMS Timeout (in ms) to wait.
1451 * @param pvData Data to write.
1452 * @param cbData Size (in bytes) of \a pvData to write.
1453 * @param pcbWritten Where to return to size (in bytes) written on success.
1454 * Optional.
1455 */
1456int GuestFile::i_writeDataAt(uint64_t uOffset, uint32_t uTimeoutMS,
1457 const void *pvData, uint32_t cbData, uint32_t *pcbWritten)
1458{
1459 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1460 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1461
1462 LogFlowThisFunc(("uOffset=%RU64, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
1463 uOffset, uTimeoutMS, pvData, cbData));
1464
1465 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1466
1467 int vrc;
1468
1469 GuestWaitEvent *pEvent = NULL;
1470 GuestEventTypes eventTypes;
1471 try
1472 {
1473 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1474 eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
1475
1476 vrc = registerWaitEvent(eventTypes, &pEvent);
1477 }
1478 catch (std::bad_alloc &)
1479 {
1480 vrc = VERR_NO_MEMORY;
1481 }
1482
1483 if (RT_FAILURE(vrc))
1484 return vrc;
1485
1486 /* Prepare HGCM call. */
1487 VBOXHGCMSVCPARM paParms[8];
1488 int i = 0;
1489 HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
1490 HGCMSvcSetU32(&paParms[i++], mObjectID /* File handle */);
1491 HGCMSvcSetU64(&paParms[i++], uOffset /* Offset where to starting writing */);
1492 HGCMSvcSetU32(&paParms[i++], cbData /* Size (in bytes) to write */);
1493 HGCMSvcSetPv (&paParms[i++], unconst(pvData), cbData);
1494
1495 alock.release(); /* Drop write lock before sending. */
1496
1497 vrc = sendMessage(HOST_MSG_FILE_WRITE_AT, i, paParms);
1498 if (RT_SUCCESS(vrc))
1499 {
1500 uint32_t cbWritten = 0;
1501 vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten);
1502 if (RT_SUCCESS(vrc))
1503 {
1504 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
1505 if (pcbWritten)
1506 *pcbWritten = cbWritten;
1507 }
1508 else if (pEvent->HasGuestError()) /* Return guest vrc if available. */
1509 vrc = pEvent->GuestResult();
1510 }
1511
1512 unregisterWaitEvent(pEvent);
1513
1514 LogFlowFuncLeaveRC(vrc);
1515 return vrc;
1516}
1517
1518// Wrapped IGuestFile methods
1519/////////////////////////////////////////////////////////////////////////////
1520HRESULT GuestFile::close()
1521{
1522 AutoCaller autoCaller(this);
1523 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1524
1525 LogFlowThisFuncEnter();
1526
1527 /* Close file on guest. */
1528 int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1529 int vrc = i_close(&vrcGuest);
1530 if (RT_FAILURE(vrc))
1531 {
1532 if (vrc == VERR_GSTCTL_GUEST_ERROR)
1533 {
1534 GuestErrorInfo ge(GuestErrorInfo::Type_File, vrcGuest, mData.mOpenInfo.mFilename.c_str());
1535 return setErrorBoth(VBOX_E_IPRT_ERROR, vrcGuest, tr("Closing guest file failed: %s"),
1536 GuestBase::getErrorAsString(ge).c_str());
1537 }
1538 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Closing guest file \"%s\" failed with %Rrc\n"),
1539 mData.mOpenInfo.mFilename.c_str(), vrc);
1540 }
1541
1542 LogFlowThisFunc(("Returning S_OK / vrc=%Rrc\n", vrc));
1543 return S_OK;
1544}
1545
1546HRESULT GuestFile::queryInfo(ComPtr<IFsObjInfo> &aObjInfo)
1547{
1548 AutoCaller autoCaller(this);
1549 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1550
1551 LogFlowThisFuncEnter();
1552
1553 HRESULT hrc = S_OK;
1554
1555 GuestFsObjData fsObjData;
1556 int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1557 int vrc = i_queryInfo(fsObjData, &vrcGuest);
1558 if (RT_SUCCESS(vrc))
1559 {
1560 ComObjPtr<GuestFsObjInfo> ptrFsObjInfo;
1561 hrc = ptrFsObjInfo.createObject();
1562 if (SUCCEEDED(hrc))
1563 {
1564 vrc = ptrFsObjInfo->init(fsObjData);
1565 if (RT_SUCCESS(vrc))
1566 hrc = ptrFsObjInfo.queryInterfaceTo(aObjInfo.asOutParam());
1567 else
1568 hrc = setErrorVrc(vrc,
1569 tr("Initialization of guest file object for \"%s\" failed: %Rrc"),
1570 mData.mOpenInfo.mFilename.c_str(), vrc);
1571 }
1572 }
1573 else
1574 {
1575 if (GuestProcess::i_isGuestError(vrc))
1576 {
1577 GuestErrorInfo ge(
1578#ifdef VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT
1579 GuestErrorInfo::Type_ToolStat,
1580#else
1581 GuestErrorInfo::Type_File,
1582#endif
1583 vrcGuest, mData.mOpenInfo.mFilename.c_str());
1584 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrcGuest, tr("Querying guest file information failed: %s"),
1585 GuestBase::getErrorAsString(ge).c_str());
1586 }
1587 else
1588 hrc = setErrorVrc(vrc,
1589 tr("Querying guest file information for \"%s\" failed: %Rrc"), mData.mOpenInfo.mFilename.c_str(), vrc);
1590 }
1591
1592 LogFlowFuncLeaveRC(vrc);
1593 return hrc;
1594}
1595
1596HRESULT GuestFile::querySize(LONG64 *aSize)
1597{
1598 AutoCaller autoCaller(this);
1599 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1600
1601 LogFlowThisFuncEnter();
1602
1603 HRESULT hrc = S_OK;
1604
1605 GuestFsObjData fsObjData;
1606 int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1607 int vrc = i_queryInfo(fsObjData, &vrcGuest);
1608 if (RT_SUCCESS(vrc))
1609 {
1610 *aSize = fsObjData.mObjectSize;
1611 }
1612 else
1613 {
1614 if (GuestProcess::i_isGuestError(vrc))
1615 {
1616 GuestErrorInfo ge(
1617#ifdef VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT
1618 GuestErrorInfo::Type_ToolStat,
1619#else
1620 GuestErrorInfo::Type_File,
1621#endif
1622 vrcGuest, mData.mOpenInfo.mFilename.c_str());
1623 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrcGuest, tr("Querying guest file size failed: %s"),
1624 GuestBase::getErrorAsString(ge).c_str());
1625 }
1626 else
1627 hrc = setErrorVrc(vrc, tr("Querying guest file size for \"%s\" failed: %Rrc"), mData.mOpenInfo.mFilename.c_str(), vrc);
1628 }
1629
1630 LogFlowFuncLeaveRC(vrc);
1631 return hrc;
1632}
1633
1634HRESULT GuestFile::read(ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
1635{
1636 AutoCaller autoCaller(this);
1637 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1638
1639 if (aToRead == 0)
1640 return setError(E_INVALIDARG, tr("The size to read is zero"));
1641
1642 LogFlowThisFuncEnter();
1643
1644 /* Cap the read at 1MiB because that's all the guest will return anyway. */
1645 if (aToRead > _1M)
1646 aToRead = _1M;
1647
1648 HRESULT hrc = S_OK;
1649
1650 int vrc;
1651 try
1652 {
1653 aData.resize(aToRead);
1654
1655 uint32_t cbRead;
1656 vrc = i_readData(aToRead, aTimeoutMS,
1657 &aData.front(), aToRead, &cbRead);
1658
1659 if (RT_SUCCESS(vrc))
1660 {
1661 if (aData.size() != cbRead)
1662 aData.resize(cbRead);
1663 }
1664 else
1665 {
1666 aData.resize(0);
1667 }
1668 }
1669 catch (std::bad_alloc &)
1670 {
1671 vrc = VERR_NO_MEMORY;
1672 }
1673
1674 if (RT_FAILURE(vrc))
1675 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading from file \"%s\" failed: %Rrc"),
1676 mData.mOpenInfo.mFilename.c_str(), vrc);
1677
1678 LogFlowFuncLeaveRC(vrc);
1679 return hrc;
1680}
1681
1682HRESULT GuestFile::readAt(LONG64 aOffset, ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
1683{
1684 AutoCaller autoCaller(this);
1685 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1686
1687 if (aToRead == 0)
1688 return setError(E_INVALIDARG, tr("The size to read for guest file \"%s\" is zero"), mData.mOpenInfo.mFilename.c_str());
1689
1690 LogFlowThisFuncEnter();
1691
1692 /* Cap the read at 1MiB because that's all the guest will return anyway. */
1693 if (aToRead > _1M)
1694 aToRead = _1M;
1695
1696 HRESULT hrc = S_OK;
1697
1698 int vrc;
1699 try
1700 {
1701 aData.resize(aToRead);
1702
1703 size_t cbRead;
1704 vrc = i_readDataAt(aOffset, aToRead, aTimeoutMS,
1705 &aData.front(), aToRead, &cbRead);
1706 if (RT_SUCCESS(vrc))
1707 {
1708 if (aData.size() != cbRead)
1709 aData.resize(cbRead);
1710 }
1711 else
1712 {
1713 aData.resize(0);
1714 }
1715 }
1716 catch (std::bad_alloc &)
1717 {
1718 vrc = VERR_NO_MEMORY;
1719 }
1720
1721 if (RT_FAILURE(vrc))
1722 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading from file \"%s\" (at offset %RU64) failed: %Rrc"),
1723 mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1724
1725 LogFlowFuncLeaveRC(vrc);
1726 return hrc;
1727}
1728
1729HRESULT GuestFile::seek(LONG64 aOffset, FileSeekOrigin_T aWhence, LONG64 *aNewOffset)
1730{
1731 AutoCaller autoCaller(this);
1732 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1733
1734 HRESULT hrc = S_OK;
1735
1736 GUEST_FILE_SEEKTYPE eSeekType;
1737 switch (aWhence)
1738 {
1739 case FileSeekOrigin_Begin:
1740 eSeekType = GUEST_FILE_SEEKTYPE_BEGIN;
1741 break;
1742
1743 case FileSeekOrigin_Current:
1744 eSeekType = GUEST_FILE_SEEKTYPE_CURRENT;
1745 break;
1746
1747 case FileSeekOrigin_End:
1748 eSeekType = GUEST_FILE_SEEKTYPE_END;
1749 break;
1750
1751 default:
1752 return setError(E_INVALIDARG, tr("Invalid seek type for guest file \"%s\" specified"),
1753 mData.mOpenInfo.mFilename.c_str());
1754 }
1755
1756 LogFlowThisFuncEnter();
1757
1758 uint64_t uNewOffset;
1759 int vrc = i_seekAt(aOffset, eSeekType, GSTCTL_DEFAULT_TIMEOUT_MS, &uNewOffset);
1760 if (RT_SUCCESS(vrc))
1761 *aNewOffset = RT_MIN(uNewOffset, (uint64_t)INT64_MAX);
1762 else
1763 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Seeking file \"%s\" (to offset %RI64) failed: %Rrc"),
1764 mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1765
1766 LogFlowFuncLeaveRC(vrc);
1767 return hrc;
1768}
1769
1770HRESULT GuestFile::setACL(const com::Utf8Str &aAcl, ULONG aMode)
1771{
1772 RT_NOREF(aAcl, aMode);
1773 ReturnComNotImplemented();
1774}
1775
1776HRESULT GuestFile::setSize(LONG64 aSize)
1777{
1778 LogFlowThisFuncEnter();
1779
1780 /*
1781 * Validate.
1782 */
1783 if (aSize < 0)
1784 return setError(E_INVALIDARG, tr("The size (%RI64) for guest file \"%s\" cannot be a negative value"),
1785 aSize, mData.mOpenInfo.mFilename.c_str());
1786
1787 /*
1788 * Register event callbacks.
1789 */
1790 int vrc;
1791 GuestWaitEvent *pWaitEvent = NULL;
1792 GuestEventTypes lstEventTypes;
1793 try
1794 {
1795 lstEventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
1796 lstEventTypes.push_back(VBoxEventType_OnGuestFileSizeChanged);
1797 }
1798 catch (std::bad_alloc &)
1799 {
1800 return E_OUTOFMEMORY;
1801 }
1802
1803 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1804
1805 vrc = registerWaitEvent(lstEventTypes, &pWaitEvent);
1806 if (RT_SUCCESS(vrc))
1807 {
1808 /*
1809 * Send of the HGCM message.
1810 */
1811 VBOXHGCMSVCPARM aParms[3];
1812 HGCMSvcSetU32(&aParms[0], pWaitEvent->ContextID());
1813 HGCMSvcSetU32(&aParms[1], mObjectID /* File handle */);
1814 HGCMSvcSetU64(&aParms[2], aSize);
1815
1816 alock.release(); /* Drop write lock before sending. */
1817
1818 vrc = sendMessage(HOST_MSG_FILE_SET_SIZE, RT_ELEMENTS(aParms), aParms);
1819 if (RT_SUCCESS(vrc))
1820 {
1821 /*
1822 * Wait for the event.
1823 */
1824 VBoxEventType_T enmEvtType;
1825 ComPtr<IEvent> pIEvent;
1826 vrc = waitForEvent(pWaitEvent, RT_MS_1MIN / 2, &enmEvtType, pIEvent.asOutParam());
1827 if (RT_SUCCESS(vrc))
1828 {
1829 if (enmEvtType == VBoxEventType_OnGuestFileSizeChanged)
1830 vrc = VINF_SUCCESS;
1831 else
1832 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1833 }
1834
1835 if (pWaitEvent->HasGuestError()) /* Return guest vrc if available. */
1836 vrc = pWaitEvent->GuestResult();
1837 }
1838
1839 /*
1840 * Unregister the wait event and deal with error reporting if needed.
1841 */
1842 unregisterWaitEvent(pWaitEvent);
1843 }
1844 HRESULT hrc;
1845 if (RT_SUCCESS(vrc))
1846 hrc = S_OK;
1847 else
1848 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
1849 tr("Setting the guest file size of \"%s\" to %RU64 (%#RX64) bytes failed: %Rrc", "", aSize),
1850 mData.mOpenInfo.mFilename.c_str(), aSize, aSize, vrc);
1851 LogFlowFuncLeaveRC(vrc);
1852 return hrc;
1853}
1854
1855HRESULT GuestFile::write(const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
1856{
1857 AutoCaller autoCaller(this);
1858 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1859
1860 if (aData.size() == 0)
1861 return setError(E_INVALIDARG, tr("No data to write specified"), mData.mOpenInfo.mFilename.c_str());
1862
1863 LogFlowThisFuncEnter();
1864
1865 HRESULT hrc = S_OK;
1866
1867 const uint32_t cbData = (uint32_t)aData.size();
1868 const void *pvData = (void *)&aData.front();
1869 int vrc = i_writeData(aTimeoutMS, pvData, cbData, (uint32_t*)aWritten);
1870 if (RT_FAILURE(vrc))
1871 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Writing %zu bytes to guest file \"%s\" failed: %Rrc", "", aData.size()),
1872 aData.size(), mData.mOpenInfo.mFilename.c_str(), vrc);
1873
1874 LogFlowFuncLeaveRC(vrc);
1875 return hrc;
1876}
1877
1878HRESULT GuestFile::writeAt(LONG64 aOffset, const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
1879{
1880 AutoCaller autoCaller(this);
1881 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1882
1883 if (aData.size() == 0)
1884 return setError(E_INVALIDARG, tr("No data to write at for guest file \"%s\" specified"), mData.mOpenInfo.mFilename.c_str());
1885
1886 LogFlowThisFuncEnter();
1887
1888 HRESULT hrc = S_OK;
1889
1890 const uint32_t cbData = (uint32_t)aData.size();
1891 const void *pvData = (void *)&aData.front();
1892 int vrc = i_writeDataAt(aOffset, aTimeoutMS, pvData, cbData, (uint32_t*)aWritten);
1893 if (RT_FAILURE(vrc))
1894 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
1895 tr("Writing %zu bytes to file \"%s\" (at offset %RU64) failed: %Rrc", "", aData.size()),
1896 aData.size(), mData.mOpenInfo.mFilename.c_str(), aOffset, vrc);
1897
1898 LogFlowFuncLeaveRC(vrc);
1899 return hrc;
1900}
1901
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