VirtualBox

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

Last change on this file since 83967 was 83489, checked in by vboxsync, 5 years ago

Guest Control/Main: Fixed IGuestSession::directoryOpen() API to fail if a guest directory does not exist . bugref:9320

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/* $Id: GuestDirectoryImpl.cpp 83489 2020-03-30 16:38:23Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest directory handling.
4 */
5
6/*
7 * Copyright (C) 2012-2020 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("Opening 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 synchronously and keep it around so that we can use
103 * it later in subsequent read() calls.
104 */
105 vrc = mData.mProcessTool.init(mSession, procInfo, false /* Async */, NULL /* Guest rc */);
106 if (RT_SUCCESS(vrc))
107 {
108 /* As we need to know if the directory we were about to open exists and and is accessible,
109 * do the first read here in order to return a meaningful status here. */
110 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
111 vrc = i_readInternal(mData.mObjData, &rcGuest);
112 if (RT_FAILURE(vrc))
113 {
114 /*
115 * We need to actively terminate our process tool in case of an error here,
116 * as this otherwise would be done on (directory) object destruction implicitly.
117 * This in turn then will run into a timeout, as the directory object won't be
118 * around anymore at that time. Ugly, but that's how it is for the moment.
119 */
120 int vrcTerm = mData.mProcessTool.terminate(30 * RT_MS_1SEC, NULL /* prcGuest */);
121 AssertRC(vrcTerm);
122
123 if (vrc == VERR_GSTCTL_GUEST_ERROR)
124 vrc = rcGuest;
125 }
126 }
127 }
128
129 /* Confirm a successful initialization when it's the case. */
130 if (RT_SUCCESS(vrc))
131 autoInitSpan.setSucceeded();
132 else
133 autoInitSpan.setFailed();
134
135 LogFlowFuncLeaveRC(vrc);
136 return vrc;
137}
138
139/**
140 * Uninitializes the instance.
141 * Called from FinalRelease().
142 */
143void GuestDirectory::uninit(void)
144{
145 LogFlowThisFuncEnter();
146
147 /* Enclose the state transition Ready->InUninit->NotReady. */
148 AutoUninitSpan autoUninitSpan(this);
149 if (autoUninitSpan.uninitDone())
150 return;
151
152 LogFlowThisFuncLeave();
153}
154
155// implementation of private wrapped getters/setters for attributes
156/////////////////////////////////////////////////////////////////////////////
157
158HRESULT GuestDirectory::getDirectoryName(com::Utf8Str &aDirectoryName)
159{
160 LogFlowThisFuncEnter();
161
162 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
163
164 aDirectoryName = mData.mOpenInfo.mPath;
165
166 return S_OK;
167}
168
169HRESULT GuestDirectory::getFilter(com::Utf8Str &aFilter)
170{
171 LogFlowThisFuncEnter();
172
173 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
174
175 aFilter = mData.mOpenInfo.mFilter;
176
177 return S_OK;
178}
179
180// private methods
181/////////////////////////////////////////////////////////////////////////////
182
183int GuestDirectory::i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
184{
185 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
186 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
187
188 LogFlowThisFunc(("strPath=%s, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
189 mData.mOpenInfo.mPath.c_str(), pCbCtx->uContextID, pCbCtx->uMessage, pSvcCb));
190
191 int vrc;
192 switch (pCbCtx->uMessage)
193 {
194 case GUEST_MSG_DIR_NOTIFY:
195 {
196 int idx = 1; /* Current parameter index. */
197 CALLBACKDATA_DIR_NOTIFY dataCb;
198 /* pSvcCb->mpaParms[0] always contains the context ID. */
199 HGCMSvcGetU32(&pSvcCb->mpaParms[idx++], &dataCb.uType);
200 HGCMSvcGetU32(&pSvcCb->mpaParms[idx++], &dataCb.rc);
201
202 LogFlowFunc(("uType=%RU32, vrcGguest=%Rrc\n", dataCb.uType, (int)dataCb.rc));
203
204 switch (dataCb.uType)
205 {
206 /* Nothing here yet, nothing to dispatch further. */
207
208 default:
209 vrc = VERR_NOT_SUPPORTED;
210 break;
211 }
212 break;
213 }
214
215 default:
216 /* Silently ignore not implemented functions. */
217 vrc = VERR_NOT_SUPPORTED;
218 break;
219 }
220
221 LogFlowFuncLeaveRC(vrc);
222 return vrc;
223}
224
225/* static */
226Utf8Str GuestDirectory::i_guestErrorToString(int rcGuest)
227{
228 Utf8Str strError;
229
230 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
231 switch (rcGuest)
232 {
233 case VERR_CANT_CREATE:
234 strError += Utf8StrFmt("Access denied");
235 break;
236
237 case VERR_DIR_NOT_EMPTY:
238 strError += Utf8StrFmt("Not empty");
239 break;
240
241 default:
242 strError += Utf8StrFmt("%Rrc", rcGuest);
243 break;
244 }
245
246 return strError;
247}
248
249/**
250 * @copydoc GuestObject::i_onUnregister
251 */
252int GuestDirectory::i_onUnregister(void)
253{
254 LogFlowThisFuncEnter();
255
256 int vrc = VINF_SUCCESS;
257
258 LogFlowFuncLeaveRC(vrc);
259 return vrc;
260}
261
262/**
263 * @copydoc GuestObject::i_onSessionStatusChange
264 */
265int GuestDirectory::i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus)
266{
267 RT_NOREF(enmSessionStatus);
268
269 LogFlowThisFuncEnter();
270
271 int vrc = VINF_SUCCESS;
272
273 LogFlowFuncLeaveRC(vrc);
274 return vrc;
275}
276
277/**
278 * Closes this guest directory and removes it from the
279 * guest session's directory list.
280 *
281 * @return VBox status code.
282 * @param prcGuest Where to store the guest result code in case VERR_GSTCTL_GUEST_ERROR is returned.
283 */
284int GuestDirectory::i_closeInternal(int *prcGuest)
285{
286 AssertPtrReturn(prcGuest, VERR_INVALID_POINTER);
287
288 int rc = mData.mProcessTool.terminate(30 * 1000 /* 30s timeout */, prcGuest);
289 if (RT_FAILURE(rc))
290 return rc;
291
292 AssertPtr(mSession);
293 int rc2 = mSession->i_directoryUnregister(this);
294 if (RT_SUCCESS(rc))
295 rc = rc2;
296
297 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
298 return rc;
299}
300
301/**
302 * Reads the next directory entry, internal version.
303 *
304 * @return VBox status code. Will return VERR_NO_MORE_FILES if no more entries are available.
305 * @param objData Where to store the read directory entry as internal object data.
306 * @param prcGuest Where to store the guest result code in case VERR_GSTCTL_GUEST_ERROR is returned.
307 */
308int GuestDirectory::i_readInternal(GuestFsObjData &objData, int *prcGuest)
309{
310 AssertPtrReturn(prcGuest, VERR_INVALID_POINTER);
311
312 GuestProcessStreamBlock curBlock;
313 int rc = mData.mProcessTool.waitEx(GUESTPROCESSTOOL_WAIT_FLAG_STDOUT_BLOCK, &curBlock, prcGuest);
314 if (RT_SUCCESS(rc))
315 {
316 /*
317 * Note: The guest process can still be around to serve the next
318 * upcoming stream block next time.
319 */
320 if (!mData.mProcessTool.isRunning())
321 rc = mData.mProcessTool.getTerminationStatus(); /* Tool process is not running (anymore). Check termination status. */
322
323 if (RT_SUCCESS(rc))
324 {
325 if (curBlock.GetCount()) /* Did we get content? */
326 {
327 if (curBlock.GetString("name"))
328 {
329 rc = objData.FromLs(curBlock, true /* fLong */);
330 }
331 else
332 rc = VERR_PATH_NOT_FOUND;
333 }
334 else
335 {
336 /* Nothing to read anymore. Tell the caller. */
337 rc = VERR_NO_MORE_FILES;
338 }
339 }
340 }
341
342 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
343 return rc;
344}
345
346/**
347 * Reads the next directory entry.
348 *
349 * @return VBox status code. Will return VERR_NO_MORE_FILES if no more entries are available.
350 * @param fsObjInfo Where to store the read directory entry.
351 * @param prcGuest Where to store the guest result code in case VERR_GSTCTL_GUEST_ERROR is returned.
352 */
353int GuestDirectory::i_read(ComObjPtr<GuestFsObjInfo> &fsObjInfo, int *prcGuest)
354{
355 AssertPtrReturn(prcGuest, VERR_INVALID_POINTER);
356
357 /* Create the FS info object. */
358 HRESULT hr = fsObjInfo.createObject();
359 if (FAILED(hr))
360 return VERR_COM_UNEXPECTED;
361
362 int rc;
363
364 /* If we have a valid object data cache, read from it. */
365 if (mData.mObjData.mName.isNotEmpty())
366 {
367 rc = fsObjInfo->init(mData.mObjData);
368 if (RT_SUCCESS(rc))
369 {
370 mData.mObjData.mName = ""; /* Mark the object data as being empty (beacon). */
371 }
372 }
373 else /* Otherwise ask the guest for the next object data (block). */
374 {
375
376 GuestFsObjData objData;
377 rc = i_readInternal(objData, prcGuest);
378 if (RT_SUCCESS(rc))
379 rc = fsObjInfo->init(objData);
380 }
381
382 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
383 return rc;
384}
385
386/* static */
387HRESULT GuestDirectory::i_setErrorExternal(VirtualBoxBase *pInterface, int rcGuest)
388{
389 AssertPtr(pInterface);
390 AssertMsg(RT_FAILURE(rcGuest), ("Guest rc does not indicate a failure when setting error\n"));
391
392 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestDirectory::i_guestErrorToString(rcGuest).c_str());
393}
394
395// implementation of public methods
396/////////////////////////////////////////////////////////////////////////////
397HRESULT GuestDirectory::close()
398{
399 AutoCaller autoCaller(this);
400 if (FAILED(autoCaller.rc())) return autoCaller.rc();
401
402 LogFlowThisFuncEnter();
403
404 HRESULT hr = S_OK;
405
406 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
407 int vrc = i_closeInternal(&rcGuest);
408 if (RT_FAILURE(vrc))
409 {
410 switch (vrc)
411 {
412 case VERR_GSTCTL_GUEST_ERROR:
413 hr = GuestDirectory::i_setErrorExternal(this, rcGuest);
414 break;
415
416 case VERR_NOT_SUPPORTED:
417 /* Silently skip old Guest Additions which do not support killing the
418 * the guest directory handling process. */
419 break;
420
421 default:
422 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
423 tr("Terminating open guest directory \"%s\" failed: %Rrc"), mData.mOpenInfo.mPath.c_str(), vrc);
424 break;
425 }
426 }
427
428 return hr;
429}
430
431HRESULT GuestDirectory::read(ComPtr<IFsObjInfo> &aObjInfo)
432{
433 AutoCaller autoCaller(this);
434 if (FAILED(autoCaller.rc())) return autoCaller.rc();
435
436 LogFlowThisFuncEnter();
437
438 HRESULT hr = S_OK;
439
440 ComObjPtr<GuestFsObjInfo> fsObjInfo;
441 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
442 int vrc = i_read(fsObjInfo, &rcGuest);
443 if (RT_SUCCESS(vrc))
444 {
445 /* Return info object to the caller. */
446 hr = fsObjInfo.queryInterfaceTo(aObjInfo.asOutParam());
447 }
448 else
449 {
450 switch (vrc)
451 {
452 case VERR_GSTCTL_GUEST_ERROR:
453 hr = GuestDirectory::i_setErrorExternal(this, rcGuest);
454 break;
455
456 case VERR_GSTCTL_PROCESS_EXIT_CODE:
457 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading directory \"%s\" failed: %Rrc"),
458 mData.mOpenInfo.mPath.c_str(), mData.mProcessTool.getRc());
459 break;
460
461 case VERR_PATH_NOT_FOUND:
462 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading directory \"%s\" failed: Path not found"),
463 mData.mOpenInfo.mPath.c_str());
464 break;
465
466 case VERR_NO_MORE_FILES:
467 /* See SDK reference. */
468 hr = setErrorBoth(VBOX_E_OBJECT_NOT_FOUND, vrc, tr("Reading directory \"%s\" failed: No more entries"),
469 mData.mOpenInfo.mPath.c_str());
470 break;
471
472 default:
473 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading directory \"%s\" returned error: %Rrc\n"),
474 mData.mOpenInfo.mPath.c_str(), vrc);
475 break;
476 }
477 }
478
479 LogFlowThisFunc(("Returning hr=%Rhrc / vrc=%Rrc\n", hr, vrc));
480 return hr;
481}
482
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