VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestDirectoryImpl.cpp@ 78425

Last change on this file since 78425 was 77587, checked in by vboxsync, 6 years ago

Guest Control/Main: Implemented virtual guest object methods for session status changes to allow guest objects set their internal state accordingly. The guest session's object map now also keeps a (weak) pointer to the guest objects for handling the callbacks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 KB
Line 
1/* $Id: GuestDirectoryImpl.cpp 77587 2019-03-06 16:40:18Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest directory handling.
4 */
5
6/*
7 * Copyright (C) 2012-2019 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#define LOG_GROUP LOG_GROUP_MAIN_GUESTDIRECTORY
23#include "LoggingNew.h"
24
25#ifndef VBOX_WITH_GUEST_CONTROL
26# error "VBOX_WITH_GUEST_CONTROL must defined in this file"
27#endif
28#include "GuestDirectoryImpl.h"
29#include "GuestSessionImpl.h"
30#include "GuestCtrlImplPrivate.h"
31
32#include "Global.h"
33#include "AutoCaller.h"
34
35#include <VBox/com/array.h>
36
37
38// constructor / destructor
39/////////////////////////////////////////////////////////////////////////////
40
41DEFINE_EMPTY_CTOR_DTOR(GuestDirectory)
42
43HRESULT GuestDirectory::FinalConstruct(void)
44{
45 LogFlowThisFunc(("\n"));
46 return BaseFinalConstruct();
47}
48
49void GuestDirectory::FinalRelease(void)
50{
51 LogFlowThisFuncEnter();
52 uninit();
53 BaseFinalRelease();
54 LogFlowThisFuncLeave();
55}
56
57// public initializer/uninitializer for internal purposes only
58/////////////////////////////////////////////////////////////////////////////
59
60int GuestDirectory::init(Console *pConsole, GuestSession *pSession, ULONG aObjectID, const GuestDirectoryOpenInfo &openInfo)
61{
62 LogFlowThisFunc(("pConsole=%p, pSession=%p, aObjectID=%RU32, strPath=%s, strFilter=%s, uFlags=%x\n",
63 pConsole, pSession, aObjectID, openInfo.mPath.c_str(), openInfo.mFilter.c_str(), openInfo.mFlags));
64
65 AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
66 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
67
68 /* Enclose the state transition NotReady->InInit->Ready. */
69 AutoInitSpan autoInitSpan(this);
70 AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
71
72 int vrc = bindToSession(pConsole, pSession, aObjectID);
73 if (RT_SUCCESS(vrc))
74 {
75 mSession = pSession;
76 mObjectID = aObjectID;
77
78 mData.mOpenInfo = openInfo;
79 }
80
81 if (RT_SUCCESS(vrc))
82 {
83 /* Start the directory process on the guest. */
84 GuestProcessStartupInfo procInfo;
85 procInfo.mName = Utf8StrFmt(tr("Reading directory \"%s\""), openInfo.mPath.c_str());
86 procInfo.mTimeoutMS = 5 * 60 * 1000; /* 5 minutes timeout. */
87 procInfo.mFlags = ProcessCreateFlag_WaitForStdOut;
88 procInfo.mExecutable= Utf8Str(VBOXSERVICE_TOOL_LS);
89
90 procInfo.mArguments.push_back(procInfo.mExecutable);
91 procInfo.mArguments.push_back(Utf8Str("--machinereadable"));
92 /* We want the long output format which contains all the object details. */
93 procInfo.mArguments.push_back(Utf8Str("-l"));
94#if 0 /* Flags are not supported yet. */
95 if (uFlags & DirectoryOpenFlag_NoSymlinks)
96 procInfo.mArguments.push_back(Utf8Str("--nosymlinks")); /** @todo What does GNU here? */
97#endif
98 /** @todo Recursion support? */
99 procInfo.mArguments.push_back(openInfo.mPath); /* The directory we want to open. */
100
101 /*
102 * Start the process asynchronously and keep it around so that we can use
103 * it later in subsequent read() calls.
104 * Note: No guest rc available because operation is asynchronous.
105 */
106 vrc = mData.mProcessTool.init(mSession, procInfo,
107 true /* Async */, NULL /* Guest rc */);
108 }
109
110 if (RT_SUCCESS(vrc))
111 {
112 /* Confirm a successful initialization when it's the case. */
113 autoInitSpan.setSucceeded();
114 return vrc;
115 }
116 else
117 autoInitSpan.setFailed();
118
119 return vrc;
120}
121
122/**
123 * Uninitializes the instance.
124 * Called from FinalRelease().
125 */
126void GuestDirectory::uninit(void)
127{
128 LogFlowThisFuncEnter();
129
130 /* Enclose the state transition Ready->InUninit->NotReady. */
131 AutoUninitSpan autoUninitSpan(this);
132 if (autoUninitSpan.uninitDone())
133 return;
134
135 LogFlowThisFuncLeave();
136}
137
138// implementation of private wrapped getters/setters for attributes
139/////////////////////////////////////////////////////////////////////////////
140
141HRESULT GuestDirectory::getDirectoryName(com::Utf8Str &aDirectoryName)
142{
143 LogFlowThisFuncEnter();
144
145 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
146
147 aDirectoryName = mData.mOpenInfo.mPath;
148
149 return S_OK;
150}
151
152HRESULT GuestDirectory::getFilter(com::Utf8Str &aFilter)
153{
154 LogFlowThisFuncEnter();
155
156 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
157
158 aFilter = mData.mOpenInfo.mFilter;
159
160 return S_OK;
161}
162
163// private methods
164/////////////////////////////////////////////////////////////////////////////
165
166int GuestDirectory::i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
167{
168 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
169 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
170
171 LogFlowThisFunc(("strPath=%s, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
172 mData.mOpenInfo.mPath.c_str(), pCbCtx->uContextID, pCbCtx->uMessage, pSvcCb));
173
174 int vrc;
175 switch (pCbCtx->uMessage)
176 {
177 case GUEST_MSG_DIR_NOTIFY:
178 {
179 int idx = 1; /* Current parameter index. */
180 CALLBACKDATA_DIR_NOTIFY dataCb;
181 /* pSvcCb->mpaParms[0] always contains the context ID. */
182 HGCMSvcGetU32(&pSvcCb->mpaParms[idx++], &dataCb.uType);
183 HGCMSvcGetU32(&pSvcCb->mpaParms[idx++], &dataCb.rc);
184
185 LogFlowFunc(("uType=%RU32, vrcGguest=%Rrc\n", dataCb.uType, (int)dataCb.rc));
186
187 switch (dataCb.uType)
188 {
189 /* Nothing here yet, nothing to dispatch further. */
190
191 default:
192 vrc = VERR_NOT_SUPPORTED;
193 break;
194 }
195 break;
196 }
197
198 default:
199 /* Silently ignore not implemented functions. */
200 vrc = VERR_NOT_SUPPORTED;
201 break;
202 }
203
204 LogFlowFuncLeaveRC(vrc);
205 return vrc;
206}
207
208/* static */
209Utf8Str GuestDirectory::i_guestErrorToString(int rcGuest)
210{
211 Utf8Str strError;
212
213 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
214 switch (rcGuest)
215 {
216 case VERR_CANT_CREATE:
217 strError += Utf8StrFmt("Access denied");
218 break;
219
220 case VERR_DIR_NOT_EMPTY:
221 strError += Utf8StrFmt("Not empty");
222 break;
223
224 default:
225 strError += Utf8StrFmt("%Rrc", rcGuest);
226 break;
227 }
228
229 return strError;
230}
231
232/**
233 * @copydoc GuestObject::i_onUnregister
234 */
235int GuestDirectory::i_onUnregister(void)
236{
237 LogFlowThisFuncEnter();
238
239 int vrc = VINF_SUCCESS;
240
241 LogFlowFuncLeaveRC(vrc);
242 return vrc;
243}
244
245/**
246 * @copydoc GuestObject::i_onSessionStatusChange
247 */
248int GuestDirectory::i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus)
249{
250 RT_NOREF(enmSessionStatus);
251
252 LogFlowThisFuncEnter();
253
254 int vrc = VINF_SUCCESS;
255
256 LogFlowFuncLeaveRC(vrc);
257 return vrc;
258}
259
260/**
261 * Closes this guest directory and removes it from the
262 * guest session's directory list.
263 *
264 * @return VBox status code.
265 * @param prcGuest Where to store the guest result code in case VERR_GSTCTL_GUEST_ERROR is returned.
266 */
267int GuestDirectory::i_closeInternal(int *prcGuest)
268{
269 AssertPtrReturn(prcGuest, VERR_INVALID_POINTER);
270
271 int rc = mData.mProcessTool.terminate(30 * 1000 /* 30s timeout */, prcGuest);
272 if (RT_FAILURE(rc))
273 return rc;
274
275 AssertPtr(mSession);
276 int rc2 = mSession->i_directoryUnregister(this);
277 if (RT_SUCCESS(rc))
278 rc = rc2;
279
280 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
281 return rc;
282}
283
284/**
285 * Reads the next directory entry.
286 *
287 * @return VBox status code. Will return VERR_NO_MORE_FILES if no more entries are available.
288 * @param fsObjInfo Where to store the read directory entry.
289 * @param prcGuest Where to store the guest result code in case VERR_GSTCTL_GUEST_ERROR is returned.
290 */
291int GuestDirectory::i_readInternal(ComObjPtr<GuestFsObjInfo> &fsObjInfo, int *prcGuest)
292{
293 AssertPtrReturn(prcGuest, VERR_INVALID_POINTER);
294
295 /* Create the FS info object. */
296 HRESULT hr = fsObjInfo.createObject();
297 if (FAILED(hr))
298 return VERR_COM_UNEXPECTED;
299
300 GuestProcessStreamBlock curBlock;
301 int rc = mData.mProcessTool.waitEx(GUESTPROCESSTOOL_WAIT_FLAG_STDOUT_BLOCK, &curBlock, prcGuest);
302 if (RT_SUCCESS(rc))
303 {
304 /*
305 * Note: The guest process can still be around to serve the next
306 * upcoming stream block next time.
307 */
308 if (!mData.mProcessTool.isRunning())
309 rc = mData.mProcessTool.getTerminationStatus(); /* Tool process is not running (anymore). Check termination status. */
310
311 if (RT_SUCCESS(rc))
312 {
313 if (curBlock.GetCount()) /* Did we get content? */
314 {
315 GuestFsObjData objData;
316 rc = objData.FromLs(curBlock, true /* fLong */);
317 if (RT_SUCCESS(rc))
318 {
319 rc = fsObjInfo->init(objData);
320 }
321 else
322 rc = VERR_PATH_NOT_FOUND;
323 }
324 else
325 {
326 /* Nothing to read anymore. Tell the caller. */
327 rc = VERR_NO_MORE_FILES;
328 }
329 }
330 }
331
332 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
333 return rc;
334}
335
336/* static */
337HRESULT GuestDirectory::i_setErrorExternal(VirtualBoxBase *pInterface, int rcGuest)
338{
339 AssertPtr(pInterface);
340 AssertMsg(RT_FAILURE(rcGuest), ("Guest rc does not indicate a failure when setting error\n"));
341
342 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestDirectory::i_guestErrorToString(rcGuest).c_str());
343}
344
345// implementation of public methods
346/////////////////////////////////////////////////////////////////////////////
347HRESULT GuestDirectory::close()
348{
349 AutoCaller autoCaller(this);
350 if (FAILED(autoCaller.rc())) return autoCaller.rc();
351
352 LogFlowThisFuncEnter();
353
354 HRESULT hr = S_OK;
355
356 int rcGuest;
357 int vrc = i_closeInternal(&rcGuest);
358 if (RT_FAILURE(vrc))
359 {
360 switch (vrc)
361 {
362 case VERR_GSTCTL_GUEST_ERROR:
363 hr = GuestDirectory::i_setErrorExternal(this, rcGuest);
364 break;
365
366 case VERR_NOT_SUPPORTED:
367 /* Silently skip old Guest Additions which do not support killing the
368 * the guest directory handling process. */
369 break;
370
371 default:
372 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
373 tr("Terminating open guest directory \"%s\" failed: %Rrc"), mData.mOpenInfo.mPath.c_str(), vrc);
374 break;
375 }
376 }
377
378 return hr;
379}
380
381HRESULT GuestDirectory::read(ComPtr<IFsObjInfo> &aObjInfo)
382{
383 AutoCaller autoCaller(this);
384 if (FAILED(autoCaller.rc())) return autoCaller.rc();
385
386 LogFlowThisFuncEnter();
387
388 HRESULT hr = S_OK;
389
390 ComObjPtr<GuestFsObjInfo> fsObjInfo; int rcGuest;
391 int vrc = i_readInternal(fsObjInfo, &rcGuest);
392 if (RT_SUCCESS(vrc))
393 {
394 /* Return info object to the caller. */
395 hr = fsObjInfo.queryInterfaceTo(aObjInfo.asOutParam());
396 }
397 else
398 {
399 switch (vrc)
400 {
401 case VERR_GSTCTL_GUEST_ERROR:
402 hr = GuestDirectory::i_setErrorExternal(this, rcGuest);
403 break;
404
405 case VERR_GSTCTL_PROCESS_EXIT_CODE:
406 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading directory \"%s\" failed: %Rrc"),
407 mData.mOpenInfo.mPath.c_str(), mData.mProcessTool.getRc());
408 break;
409
410 case VERR_PATH_NOT_FOUND:
411 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading directory \"%s\" failed: Path not found"),
412 mData.mOpenInfo.mPath.c_str());
413 break;
414
415 case VERR_NO_MORE_FILES:
416 /* See SDK reference. */
417 hr = setErrorBoth(VBOX_E_OBJECT_NOT_FOUND, vrc, tr("Reading directory \"%s\" failed: No more entries"),
418 mData.mOpenInfo.mPath.c_str());
419 break;
420
421 default:
422 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading directory \"%s\" returned error: %Rrc\n"),
423 mData.mOpenInfo.mPath.c_str(), vrc);
424 break;
425 }
426 }
427
428 LogFlowThisFunc(("Returning hr=%Rhrc / vrc=%Rrc\n", hr, vrc));
429 return hr;
430}
431
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