VirtualBox

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

Last change on this file since 91516 was 91516, checked in by vboxsync, 3 years ago

Main: Fixed continuation indent for a bunch of places now using GuestErrorInfo as a variable. Fixed two no-else-after-return cases too. bugref:1909

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.6 KB
Line 
1/* $Id: GuestDirectoryImpl.cpp 91516 2021-10-01 14:20:07Z 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/**
226 * Converts a given guest directory error to a string.
227 *
228 * @returns Error string.
229 * @param rcGuest Guest file error to return string for.
230 * @param pcszWhat Hint of what was involved when the error occurred.
231 */
232/* static */
233Utf8Str GuestDirectory::i_guestErrorToString(int rcGuest, const char *pcszWhat)
234{
235 AssertPtrReturn(pcszWhat, "");
236
237 Utf8Str strErr;
238
239#define CASE_MSG(a_iRc, ...) \
240 case a_iRc: strErr = Utf8StrFmt(__VA_ARGS__); break;
241
242 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
243 switch (rcGuest)
244 {
245 CASE_MSG(VERR_CANT_CREATE , tr("Access to guest directory \"%s\" is denied"), pcszWhat);
246 CASE_MSG(VERR_DIR_NOT_EMPTY, tr("Guest directory \"%s\" is not empty"), pcszWhat);
247 default:
248 {
249 char szDefine[80];
250 RTErrQueryDefine(rcGuest, szDefine, sizeof(szDefine), false /*fFailIfUnknown*/);
251 strErr = Utf8StrFmt(tr("Error %s for guest directory \"%s\" occurred\n"), szDefine, pcszWhat);
252 break;
253 }
254 }
255
256#undef CASE_MSG
257
258 return strErr;
259}
260
261/**
262 * @copydoc GuestObject::i_onUnregister
263 */
264int GuestDirectory::i_onUnregister(void)
265{
266 LogFlowThisFuncEnter();
267
268 int vrc = VINF_SUCCESS;
269
270 LogFlowFuncLeaveRC(vrc);
271 return vrc;
272}
273
274/**
275 * @copydoc GuestObject::i_onSessionStatusChange
276 */
277int GuestDirectory::i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus)
278{
279 RT_NOREF(enmSessionStatus);
280
281 LogFlowThisFuncEnter();
282
283 int vrc = VINF_SUCCESS;
284
285 LogFlowFuncLeaveRC(vrc);
286 return vrc;
287}
288
289/**
290 * Closes this guest directory and removes it from the
291 * guest session's directory list.
292 *
293 * @return VBox status code.
294 * @param prcGuest Where to store the guest result code in case VERR_GSTCTL_GUEST_ERROR is returned.
295 */
296int GuestDirectory::i_closeInternal(int *prcGuest)
297{
298 AssertPtrReturn(prcGuest, VERR_INVALID_POINTER);
299
300 int rc = mData.mProcessTool.terminate(30 * 1000 /* 30s timeout */, prcGuest);
301 if (RT_FAILURE(rc))
302 return rc;
303
304 AssertPtr(mSession);
305 int rc2 = mSession->i_directoryUnregister(this);
306 if (RT_SUCCESS(rc))
307 rc = rc2;
308
309 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
310 return rc;
311}
312
313/**
314 * Reads the next directory entry, internal version.
315 *
316 * @return VBox status code. Will return VERR_NO_MORE_FILES if no more entries are available.
317 * @param objData Where to store the read directory entry as internal object data.
318 * @param prcGuest Where to store the guest result code in case VERR_GSTCTL_GUEST_ERROR is returned.
319 */
320int GuestDirectory::i_readInternal(GuestFsObjData &objData, int *prcGuest)
321{
322 AssertPtrReturn(prcGuest, VERR_INVALID_POINTER);
323
324 GuestProcessStreamBlock curBlock;
325 int rc = mData.mProcessTool.waitEx(GUESTPROCESSTOOL_WAIT_FLAG_STDOUT_BLOCK, &curBlock, prcGuest);
326 if (RT_SUCCESS(rc))
327 {
328 /*
329 * Note: The guest process can still be around to serve the next
330 * upcoming stream block next time.
331 */
332 if (!mData.mProcessTool.isRunning())
333 rc = mData.mProcessTool.getTerminationStatus(); /* Tool process is not running (anymore). Check termination status. */
334
335 if (RT_SUCCESS(rc))
336 {
337 if (curBlock.GetCount()) /* Did we get content? */
338 {
339 if (curBlock.GetString("name"))
340 {
341 rc = objData.FromLs(curBlock, true /* fLong */);
342 }
343 else
344 rc = VERR_PATH_NOT_FOUND;
345 }
346 else
347 {
348 /* Nothing to read anymore. Tell the caller. */
349 rc = VERR_NO_MORE_FILES;
350 }
351 }
352 }
353
354 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
355 return rc;
356}
357
358/**
359 * Reads the next directory entry.
360 *
361 * @return VBox status code. Will return VERR_NO_MORE_FILES if no more entries are available.
362 * @param fsObjInfo Where to store the read directory entry.
363 * @param prcGuest Where to store the guest result code in case VERR_GSTCTL_GUEST_ERROR is returned.
364 */
365int GuestDirectory::i_read(ComObjPtr<GuestFsObjInfo> &fsObjInfo, int *prcGuest)
366{
367 AssertPtrReturn(prcGuest, VERR_INVALID_POINTER);
368
369 /* Create the FS info object. */
370 HRESULT hr = fsObjInfo.createObject();
371 if (FAILED(hr))
372 return VERR_COM_UNEXPECTED;
373
374 int rc;
375
376 /* If we have a valid object data cache, read from it. */
377 if (mData.mObjData.mName.isNotEmpty())
378 {
379 rc = fsObjInfo->init(mData.mObjData);
380 if (RT_SUCCESS(rc))
381 {
382 mData.mObjData.mName = ""; /* Mark the object data as being empty (beacon). */
383 }
384 }
385 else /* Otherwise ask the guest for the next object data (block). */
386 {
387
388 GuestFsObjData objData;
389 rc = i_readInternal(objData, prcGuest);
390 if (RT_SUCCESS(rc))
391 rc = fsObjInfo->init(objData);
392 }
393
394 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
395 return rc;
396}
397
398// implementation of public methods
399/////////////////////////////////////////////////////////////////////////////
400HRESULT GuestDirectory::close()
401{
402 AutoCaller autoCaller(this);
403 if (FAILED(autoCaller.rc())) return autoCaller.rc();
404
405 LogFlowThisFuncEnter();
406
407 HRESULT hr = S_OK;
408
409 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
410 int vrc = i_closeInternal(&rcGuest);
411 if (RT_FAILURE(vrc))
412 {
413 switch (vrc)
414 {
415 case VERR_GSTCTL_GUEST_ERROR:
416 {
417 GuestErrorInfo ge(GuestErrorInfo::Type_Directory, rcGuest, mData.mOpenInfo.mPath.c_str());
418 hr = setErrorBoth(VBOX_E_IPRT_ERROR, rcGuest, tr("Closing guest directory failed: %s"),
419 GuestBase::getErrorAsString(ge).c_str());
420 break;
421 }
422 case VERR_NOT_SUPPORTED:
423 /* Silently skip old Guest Additions which do not support killing the
424 * the guest directory handling process. */
425 break;
426
427 default:
428 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
429 tr("Closing guest directory \"%s\" failed: %Rrc"), mData.mOpenInfo.mPath.c_str(), vrc);
430 break;
431 }
432 }
433
434 return hr;
435}
436
437HRESULT GuestDirectory::read(ComPtr<IFsObjInfo> &aObjInfo)
438{
439 AutoCaller autoCaller(this);
440 if (FAILED(autoCaller.rc())) return autoCaller.rc();
441
442 LogFlowThisFuncEnter();
443
444 HRESULT hr = S_OK;
445
446 ComObjPtr<GuestFsObjInfo> fsObjInfo;
447 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
448 int vrc = i_read(fsObjInfo, &rcGuest);
449 if (RT_SUCCESS(vrc))
450 {
451 /* Return info object to the caller. */
452 hr = fsObjInfo.queryInterfaceTo(aObjInfo.asOutParam());
453 }
454 else
455 {
456 switch (vrc)
457 {
458 case VERR_GSTCTL_GUEST_ERROR:
459 {
460 GuestErrorInfo ge(GuestErrorInfo::Type_ToolLs, rcGuest, mData.mOpenInfo.mPath.c_str());
461 hr = setErrorBoth(VBOX_E_IPRT_ERROR, rcGuest, tr("Reading guest directory failed: %s"),
462 GuestBase::getErrorAsString(ge).c_str());
463 break;
464 }
465 case VERR_GSTCTL_PROCESS_EXIT_CODE:
466 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading guest directory \"%s\" failed: %Rrc"),
467 mData.mOpenInfo.mPath.c_str(), mData.mProcessTool.getRc());
468 break;
469
470 case VERR_PATH_NOT_FOUND:
471 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading guest directory \"%s\" failed: Path not found"),
472 mData.mOpenInfo.mPath.c_str());
473 break;
474
475 case VERR_NO_MORE_FILES:
476 /* See SDK reference. */
477 hr = setErrorBoth(VBOX_E_OBJECT_NOT_FOUND, vrc, tr("Reading guest directory \"%s\" failed: No more entries"),
478 mData.mOpenInfo.mPath.c_str());
479 break;
480
481 default:
482 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading guest directory \"%s\" returned error: %Rrc\n"),
483 mData.mOpenInfo.mPath.c_str(), vrc);
484 break;
485 }
486 }
487
488 LogFlowThisFunc(("Returning hr=%Rhrc / vrc=%Rrc\n", hr, vrc));
489 return hr;
490}
491
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