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