VirtualBox

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

Last change on this file since 42783 was 42759, checked in by vboxsync, 12 years ago

Guest Control 2.0: Bugfixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1
2/* $Id: GuestDirectoryImpl.cpp 42759 2012-08-10 15:53:25Z vboxsync $ */
3/** @file
4 * VirtualBox Main - XXX.
5 */
6
7/*
8 * Copyright (C) 2012 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include "GuestDirectoryImpl.h"
24#include "GuestSessionImpl.h"
25#include "GuestCtrlImplPrivate.h"
26
27#include "Global.h"
28#include "AutoCaller.h"
29
30#include <VBox/com/array.h>
31
32
33// constructor / destructor
34/////////////////////////////////////////////////////////////////////////////
35
36DEFINE_EMPTY_CTOR_DTOR(GuestDirectory)
37
38HRESULT GuestDirectory::FinalConstruct(void)
39{
40 LogFlowThisFunc(("\n"));
41 return BaseFinalConstruct();
42}
43
44void GuestDirectory::FinalRelease(void)
45{
46 LogFlowThisFuncEnter();
47 uninit();
48 BaseFinalRelease();
49 LogFlowThisFuncLeave();
50}
51
52// public initializer/uninitializer for internal purposes only
53/////////////////////////////////////////////////////////////////////////////
54
55int GuestDirectory::init(GuestSession *aSession,
56 const Utf8Str &strPath, const Utf8Str &strFilter /*= ""*/, uint32_t uFlags /*= 0*/)
57{
58 LogFlowThisFunc(("strPath=%s, strFilter=%s, uFlags=%x\n",
59 strPath.c_str(), strFilter.c_str(), uFlags));
60
61 /* Enclose the state transition NotReady->InInit->Ready. */
62 AutoInitSpan autoInitSpan(this);
63 AssertReturn(autoInitSpan.isOk(), E_FAIL);
64
65 mData.mParent = aSession;
66 mData.mName = strPath;
67 mData.mFilter = strFilter;
68 mData.mFlags = uFlags;
69
70 /* Start the directory process on the guest. */
71 GuestProcessStartupInfo procInfo;
72 procInfo.mName = Utf8StrFmt(tr("Reading directory \"%s\"", strPath.c_str()));
73 procInfo.mCommand = Utf8Str(VBOXSERVICE_TOOL_LS);
74 procInfo.mTimeoutMS = 0; /* No timeout. */
75 procInfo.mFlags = ProcessCreateFlag_Hidden | ProcessCreateFlag_WaitForStdOut;
76
77 procInfo.mArguments.push_back(Utf8Str("--machinereadable"));
78 /* We want the long output format which contains all the object details. */
79 procInfo.mArguments.push_back(Utf8Str("-l"));
80#if 0 /* Flags are not supported yet. */
81 if (uFlags & DirectoryOpenFlag_NoSymlinks)
82 procInfo.mArguments.push_back(Utf8Str("--nosymlinks")); /** @todo What does GNU here? */
83#endif
84 /** @todo Recursion support? */
85 procInfo.mArguments.push_back(strPath); /* The directory we want to open. */
86
87 /*
88 * Start the process asynchronously and keep it around so that we can use
89 * it later in subsequent read() calls.
90 */
91 int rc = mData.mParent->processCreateExInteral(procInfo, mData.mProcess);
92 if (RT_SUCCESS(rc))
93 rc = mData.mProcess->startProcessAsync();
94
95 LogFlowThisFunc(("rc=%Rrc\n", rc));
96
97 if (RT_SUCCESS(rc))
98 {
99 /* Confirm a successful initialization when it's the case. */
100 autoInitSpan.setSucceeded();
101 return rc;
102 }
103
104 autoInitSpan.setFailed();
105 return rc;
106}
107
108/**
109 * Uninitializes the instance.
110 * Called from FinalRelease().
111 */
112void GuestDirectory::uninit(void)
113{
114 LogFlowThisFunc(("\n"));
115
116 /* Enclose the state transition Ready->InUninit->NotReady. */
117 AutoUninitSpan autoUninitSpan(this);
118 if (autoUninitSpan.uninitDone())
119 return;
120
121 if (!mData.mProcess.isNull())
122 mData.mProcess->uninit();
123}
124
125// implementation of public getters/setters for attributes
126/////////////////////////////////////////////////////////////////////////////
127
128STDMETHODIMP GuestDirectory::COMGETTER(DirectoryName)(BSTR *aName)
129{
130 LogFlowThisFuncEnter();
131
132 CheckComArgOutPointerValid(aName);
133
134 AutoCaller autoCaller(this);
135 if (FAILED(autoCaller.rc())) return autoCaller.rc();
136
137 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
138
139 mData.mName.cloneTo(aName);
140
141 return S_OK;
142}
143
144STDMETHODIMP GuestDirectory::COMGETTER(Filter)(BSTR *aFilter)
145{
146 LogFlowThisFuncEnter();
147
148 CheckComArgOutPointerValid(aFilter);
149
150 AutoCaller autoCaller(this);
151 if (FAILED(autoCaller.rc())) return autoCaller.rc();
152
153 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
154
155 mData.mFilter.cloneTo(aFilter);
156
157 return S_OK;
158}
159
160// private methods
161/////////////////////////////////////////////////////////////////////////////
162
163int GuestDirectory::parseData(GuestProcessStreamBlock &streamBlock)
164{
165 LogFlowThisFunc(("cbStream=%RU32\n", mData.mStream.GetSize()));
166
167 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
168
169 int rc;
170 do
171 {
172 /* Try parsing the data to see if the current block is complete. */
173 rc = mData.mStream.ParseBlock(streamBlock);
174 if (streamBlock.GetCount())
175 break;
176
177 } while (RT_SUCCESS(rc));
178
179 LogFlowFuncLeaveRC(rc);
180 return rc;
181}
182
183
184// implementation of public methods
185/////////////////////////////////////////////////////////////////////////////
186
187STDMETHODIMP GuestDirectory::Close(void)
188{
189#ifndef VBOX_WITH_GUEST_CONTROL
190 ReturnComNotImplemented();
191#else
192 LogFlowThisFuncEnter();
193
194 uninit();
195
196 return S_OK;
197#endif /* VBOX_WITH_GUEST_CONTROL */
198}
199
200STDMETHODIMP GuestDirectory::Read(IFsObjInfo **aInfo)
201{
202#ifndef VBOX_WITH_GUEST_CONTROL
203 ReturnComNotImplemented();
204#else
205 LogFlowThisFuncEnter();
206
207 AutoCaller autoCaller(this);
208 if (FAILED(autoCaller.rc())) return autoCaller.rc();
209
210 ComObjPtr<GuestProcess> pProcess = mData.mProcess;
211 Assert(!pProcess.isNull());
212
213 GuestProcessStreamBlock streamBlock;
214 GuestFsObjData objData;
215
216 int rc = parseData(streamBlock);
217 if ( RT_FAILURE(rc)
218 || streamBlock.IsEmpty()) /* More data needed. */
219 {
220 rc = pProcess->waitForStart(30 * 1000 /* 30s timeout */);
221 }
222
223 if (RT_SUCCESS(rc))
224 {
225 BYTE byBuf[_64K];
226 size_t cbRead = 0;
227
228 /** @todo Merge with GuestSession::queryFileInfoInternal. */
229 for (;RT_SUCCESS(rc);)
230 {
231 GuestProcessWaitResult waitRes;
232 rc = pProcess->waitFor( ProcessWaitForFlag_Terminate
233 | ProcessWaitForFlag_StdOut,
234 30 * 1000 /* Timeout */, waitRes);
235 if ( RT_FAILURE(rc)
236 || waitRes.mResult == ProcessWaitResult_Terminate
237 || waitRes.mResult == ProcessWaitResult_Error
238 || waitRes.mResult == ProcessWaitResult_Timeout)
239 {
240 if (RT_FAILURE(waitRes.mRC))
241 rc = waitRes.mRC;
242 break;
243 }
244
245 rc = pProcess->readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf),
246 30 * 1000 /* Timeout */, byBuf, sizeof(byBuf),
247 &cbRead);
248 if (RT_FAILURE(rc))
249 break;
250
251 if (cbRead)
252 {
253 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
254
255 rc = mData.mStream.AddData(byBuf, cbRead);
256 if (RT_FAILURE(rc))
257 break;
258
259 LogFlowThisFunc(("rc=%Rrc, cbRead=%RU64, cbStreamOut=%RU32\n",
260 rc, cbRead, mData.mStream.GetSize()));
261
262 rc = parseData(streamBlock);
263 if (RT_SUCCESS(rc))
264 {
265 /* Parsing the current stream block succeeded so
266 * we don't need more at the moment. */
267 break;
268 }
269 }
270 }
271
272 LogFlowThisFunc(("Reading done with rc=%Rrc, cbRead=%RU64, cbStream=%RU32\n",
273 rc, cbRead, mData.mStream.GetSize()));
274
275 if (RT_SUCCESS(rc))
276 {
277 rc = parseData(streamBlock);
278 if (rc == VERR_NO_DATA) /* Since this is the last parsing call, this is ok. */
279 rc = VINF_SUCCESS;
280 }
281
282 /*
283 * Note: The guest process can still be around to serve the next
284 * upcoming stream block next time.
285 */
286 if (RT_SUCCESS(rc))
287 {
288 /** @todo Move into common function. */
289 ProcessStatus_T procStatus = ProcessStatus_Undefined;
290 LONG exitCode = 0;
291
292 HRESULT hr2 = pProcess->COMGETTER(Status(&procStatus));
293 ComAssertComRC(hr2);
294 hr2 = pProcess->COMGETTER(ExitCode(&exitCode));
295 ComAssertComRC(hr2);
296
297 if ( ( procStatus != ProcessStatus_Started
298 && procStatus != ProcessStatus_Paused
299 && procStatus != ProcessStatus_Terminating
300 )
301 && exitCode != 0)
302 {
303 rc = VERR_ACCESS_DENIED;
304 }
305 }
306 }
307
308 if (RT_SUCCESS(rc))
309 {
310 if (streamBlock.GetCount()) /* Did we get content? */
311 {
312 rc = objData.FromLs(streamBlock);
313 if (RT_FAILURE(rc))
314 rc = VERR_PATH_NOT_FOUND;
315
316 if (RT_SUCCESS(rc))
317 {
318 /* Create the object. */
319 ComObjPtr<GuestFsObjInfo> pFsObjInfo;
320 HRESULT hr2 = pFsObjInfo.createObject();
321 if (FAILED(hr2))
322 rc = VERR_COM_UNEXPECTED;
323
324 if (RT_SUCCESS(rc))
325 rc = pFsObjInfo->init(objData);
326
327 if (RT_SUCCESS(rc))
328 {
329 /* Return info object to the caller. */
330 hr2 = pFsObjInfo.queryInterfaceTo(aInfo);
331 if (FAILED(hr2))
332 rc = VERR_COM_UNEXPECTED;
333 }
334 }
335 }
336 else
337 {
338 /* Nothing to read anymore. Tell the caller. */
339 rc = VERR_NO_MORE_FILES;
340 }
341 }
342
343 HRESULT hr = S_OK;
344
345 if (RT_FAILURE(rc)) /** @todo Add more errors here. */
346 {
347 switch (rc)
348 {
349 case VERR_ACCESS_DENIED:
350 hr = setError(VBOX_E_IPRT_ERROR, tr("Reading directory \"%s\" failed: Unable to read / access denied"),
351 mData.mName.c_str());
352 break;
353
354 case VERR_PATH_NOT_FOUND:
355 hr = setError(VBOX_E_IPRT_ERROR, tr("Reading directory \"%s\" failed: Path not found"),
356 mData.mName.c_str());
357 break;
358
359 case VERR_NO_MORE_FILES:
360 hr = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No more entries for directory \"%s\""),
361 mData.mName.c_str());
362 break;
363
364 default:
365 hr = setError(VBOX_E_IPRT_ERROR, tr("Error while reading directory \"%s\": %Rrc\n"),
366 mData.mName.c_str(), rc);
367 break;
368 }
369 }
370
371 LogFlowFuncLeaveRC(rc);
372 return hr;
373#endif /* VBOX_WITH_GUEST_CONTROL */
374}
375
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