VirtualBox

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

Last change on this file since 70772 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/* $Id: GuestDirectoryImpl.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest directory handling.
4 */
5
6/*
7 * Copyright (C) 2012-2017 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_GUEST_CONTROL //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,
61 ULONG uDirID, const GuestDirectoryOpenInfo &openInfo)
62{
63 LogFlowThisFunc(("pConsole=%p, pSession=%p, uDirID=%RU32, strPath=%s, strFilter=%s, uFlags=%x\n",
64 pConsole, pSession, uDirID, openInfo.mPath.c_str(), openInfo.mFilter.c_str(),
65 openInfo.mFlags));
66
67 AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
68 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
69
70 /* Enclose the state transition NotReady->InInit->Ready. */
71 AutoInitSpan autoInitSpan(this);
72 AssertReturn(autoInitSpan.isOk(), E_FAIL);
73
74 int vrc = bindToSession(pConsole, pSession, uDirID /* Object ID */);
75 if (RT_SUCCESS(vrc))
76 {
77 mSession = pSession;
78
79 mData.mID = uDirID;
80 mData.mOpenInfo = openInfo;
81 }
82
83 if (RT_SUCCESS(vrc))
84 {
85 /* Start the directory process on the guest. */
86 GuestProcessStartupInfo procInfo;
87 procInfo.mName = Utf8StrFmt(tr("Reading directory \"%s\""), openInfo.mPath.c_str());
88 procInfo.mTimeoutMS = 5 * 60 * 1000; /* 5 minutes timeout. */
89 procInfo.mFlags = ProcessCreateFlag_WaitForStdOut;
90 procInfo.mExecutable= Utf8Str(VBOXSERVICE_TOOL_LS);
91
92 procInfo.mArguments.push_back(procInfo.mExecutable);
93 procInfo.mArguments.push_back(Utf8Str("--machinereadable"));
94 /* We want the long output format which contains all the object details. */
95 procInfo.mArguments.push_back(Utf8Str("-l"));
96#if 0 /* Flags are not supported yet. */
97 if (uFlags & DirectoryOpenFlag_NoSymlinks)
98 procInfo.mArguments.push_back(Utf8Str("--nosymlinks")); /** @todo What does GNU here? */
99#endif
100 /** @todo Recursion support? */
101 procInfo.mArguments.push_back(openInfo.mPath); /* The directory we want to open. */
102
103 /*
104 * Start the process asynchronously and keep it around so that we can use
105 * it later in subsequent read() calls.
106 * Note: No guest rc available because operation is asynchronous.
107 */
108 vrc = mData.mProcessTool.Init(mSession, procInfo,
109 true /* Async */, NULL /* Guest rc */);
110 }
111
112 if (RT_SUCCESS(vrc))
113 {
114 /* Confirm a successful initialization when it's the case. */
115 autoInitSpan.setSucceeded();
116 return vrc;
117 }
118 else
119 autoInitSpan.setFailed();
120
121 return vrc;
122}
123
124/**
125 * Uninitializes the instance.
126 * Called from FinalRelease().
127 */
128void GuestDirectory::uninit(void)
129{
130 LogFlowThisFuncEnter();
131
132 /* Enclose the state transition Ready->InUninit->NotReady. */
133 AutoUninitSpan autoUninitSpan(this);
134 if (autoUninitSpan.uninitDone())
135 return;
136
137 LogFlowThisFuncLeave();
138}
139
140// implementation of private wrapped getters/setters for attributes
141/////////////////////////////////////////////////////////////////////////////
142
143HRESULT GuestDirectory::getDirectoryName(com::Utf8Str &aDirectoryName)
144{
145 LogFlowThisFuncEnter();
146
147 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
148
149 aDirectoryName = mData.mOpenInfo.mPath;
150
151 return S_OK;
152}
153
154HRESULT GuestDirectory::getFilter(com::Utf8Str &aFilter)
155{
156 LogFlowThisFuncEnter();
157
158 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
159
160 aFilter = mData.mOpenInfo.mFilter;
161
162 return S_OK;
163}
164
165// private methods
166/////////////////////////////////////////////////////////////////////////////
167
168int GuestDirectory::i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
169{
170 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
171 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
172
173 LogFlowThisFunc(("strPath=%s, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
174 mData.mOpenInfo.mPath.c_str(), pCbCtx->uContextID, pCbCtx->uFunction, pSvcCb));
175
176 int vrc;
177 switch (pCbCtx->uFunction)
178 {
179 case GUEST_DIR_NOTIFY:
180 {
181 int idx = 1; /* Current parameter index. */
182 CALLBACKDATA_DIR_NOTIFY dataCb;
183 /* pSvcCb->mpaParms[0] always contains the context ID. */
184 pSvcCb->mpaParms[idx++].getUInt32(&dataCb.uType);
185 pSvcCb->mpaParms[idx++].getUInt32(&dataCb.rc);
186
187 LogFlowFunc(("uType=%RU32, vrcGguest=%Rrc\n", dataCb.uType, (int)dataCb.rc));
188
189 switch (dataCb.uType)
190 {
191 /* Nothing here yet, nothing to dispatch further. */
192
193 default:
194 vrc = VERR_NOT_SUPPORTED;
195 break;
196 }
197 break;
198 }
199
200 default:
201 /* Silently ignore not implemented functions. */
202 vrc = VERR_NOT_SUPPORTED;
203 break;
204 }
205
206#ifdef DEBUG
207 LogFlowFuncLeaveRC(vrc);
208#endif
209 return vrc;
210}
211
212/* static */
213Utf8Str GuestDirectory::i_guestErrorToString(int guestRc)
214{
215 Utf8Str strError;
216
217 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
218 switch (guestRc)
219 {
220 case VERR_DIR_NOT_EMPTY:
221 strError += Utf8StrFmt("Directoy is not empty");
222 break;
223
224 default:
225 strError += Utf8StrFmt("%Rrc", guestRc);
226 break;
227 }
228
229 return strError;
230}
231
232/**
233 * Called by IGuestSession right before this directory gets
234 * removed from the public directory list.
235 */
236int GuestDirectory::i_onRemove(void)
237{
238 LogFlowThisFuncEnter();
239
240 int vrc = VINF_SUCCESS;
241
242 LogFlowFuncLeaveRC(vrc);
243 return vrc;
244}
245
246/* static */
247HRESULT GuestDirectory::i_setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
248{
249 AssertPtr(pInterface);
250 AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
251
252 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestDirectory::i_guestErrorToString(guestRc).c_str());
253}
254
255// implementation of public methods
256/////////////////////////////////////////////////////////////////////////////
257HRESULT GuestDirectory::close()
258{
259 LogFlowThisFuncEnter();
260
261 AutoCaller autoCaller(this);
262 if (FAILED(autoCaller.rc())) return autoCaller.rc();
263
264 HRESULT hr = S_OK;
265
266 int guestRc;
267 int rc = mData.mProcessTool.i_terminate(30 * 1000, &guestRc);
268 if (RT_FAILURE(rc))
269 {
270 switch (rc)
271 {
272 case VERR_GSTCTL_GUEST_ERROR:
273 hr = GuestProcess::i_setErrorExternal(this, guestRc);
274 break;
275
276 case VERR_NOT_SUPPORTED:
277 /* Silently skip old Guest Additions which do not support killing the
278 * the guest directory handling process. */
279 break;
280
281 default:
282 hr = setError(VBOX_E_IPRT_ERROR,
283 tr("Terminating open guest directory \"%s\" failed: %Rrc"),
284 mData.mOpenInfo.mPath.c_str(), rc);
285 break;
286 }
287 }
288
289 AssertPtr(mSession);
290 int rc2 = mSession->i_directoryRemoveFromList(this);
291 if (RT_SUCCESS(rc))
292 rc = rc2;
293
294 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
295 return hr;
296}
297
298HRESULT GuestDirectory::read(ComPtr<IFsObjInfo> &aObjInfo)
299{
300 LogFlowThisFuncEnter();
301
302 AutoCaller autoCaller(this);
303 if (FAILED(autoCaller.rc())) return autoCaller.rc();
304
305 GuestProcessStreamBlock curBlock;
306 int guestRc;
307
308 int rc = mData.mProcessTool.i_waitEx(GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK,
309 &curBlock, &guestRc);
310
311 /*
312 * Note: The guest process can still be around to serve the next
313 * upcoming stream block next time.
314 */
315 if ( RT_SUCCESS(rc)
316 && !mData.mProcessTool.i_isRunning())
317 {
318 rc = mData.mProcessTool.i_terminatedOk();
319 }
320
321 if (RT_SUCCESS(rc))
322 {
323 if (curBlock.GetCount()) /* Did we get content? */
324 {
325 GuestFsObjData objData;
326 rc = objData.FromLs(curBlock);
327 if (RT_FAILURE(rc))
328 rc = VERR_PATH_NOT_FOUND;
329
330 if (RT_SUCCESS(rc))
331 {
332 /* Create the object. */
333 ComObjPtr<GuestFsObjInfo> pFsObjInfo;
334 HRESULT hr2 = pFsObjInfo.createObject();
335 if (FAILED(hr2))
336 rc = VERR_COM_UNEXPECTED;
337
338 if (RT_SUCCESS(rc))
339 rc = pFsObjInfo->init(objData);
340
341 if (RT_SUCCESS(rc))
342 {
343 /* Return info object to the caller. */
344 hr2 = pFsObjInfo.queryInterfaceTo(aObjInfo.asOutParam());
345 if (FAILED(hr2))
346 rc = VERR_COM_UNEXPECTED;
347 }
348 }
349 }
350 else
351 {
352 /* Nothing to read anymore. Tell the caller. */
353 rc = VERR_NO_MORE_FILES;
354 }
355 }
356
357 HRESULT hr = S_OK;
358
359 if (RT_FAILURE(rc)) /** @todo Add more errors here. */
360 {
361 switch (rc)
362 {
363 case VERR_GSTCTL_GUEST_ERROR:
364 hr = GuestProcess::i_setErrorExternal(this, guestRc);
365 break;
366
367 case VWRN_GSTCTL_PROCESS_EXIT_CODE:
368 hr = setError(VBOX_E_IPRT_ERROR, tr("Reading directory \"%s\" failed: %Rrc"),
369 mData.mOpenInfo.mPath.c_str(), mData.mProcessTool.i_getRc());
370 break;
371
372 case VERR_PATH_NOT_FOUND:
373 hr = setError(VBOX_E_IPRT_ERROR, tr("Reading directory \"%s\" failed: Path not found"),
374 mData.mOpenInfo.mPath.c_str());
375 break;
376
377 case VERR_NO_MORE_FILES:
378 /* See SDK reference. */
379 hr = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No more entries for directory \"%s\""),
380 mData.mOpenInfo.mPath.c_str());
381 break;
382
383 default:
384 hr = setError(VBOX_E_IPRT_ERROR, tr("Error while reading directory \"%s\": %Rrc\n"),
385 mData.mOpenInfo.mPath.c_str(), rc);
386 break;
387 }
388 }
389
390 LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
391 return hr;
392}
393
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use