VirtualBox

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

Last change on this file since 42674 was 42674, checked in by vboxsync, 13 years ago

Build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1
2/* $Id: GuestDirectoryImpl.cpp 42674 2012-08-08 08:14:13Z 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 int rc;
168 do
169 {
170 /* Try parsing the data to see if the current block is complete. */
171 rc = mData.mStream.ParseBlock(streamBlock);
172 if (streamBlock.GetCount())
173 break;
174
175 } while (RT_SUCCESS(rc));
176
177 LogFlowFuncLeaveRC(rc);
178 return rc;
179}
180
181
182// implementation of public methods
183/////////////////////////////////////////////////////////////////////////////
184
185STDMETHODIMP GuestDirectory::Close(void)
186{
187#ifndef VBOX_WITH_GUEST_CONTROL
188 ReturnComNotImplemented();
189#else
190 LogFlowThisFuncEnter();
191
192 uninit();
193
194 return S_OK;
195#endif /* VBOX_WITH_GUEST_CONTROL */
196}
197
198STDMETHODIMP GuestDirectory::Read(IFsObjInfo **aInfo)
199{
200#ifndef VBOX_WITH_GUEST_CONTROL
201 ReturnComNotImplemented();
202#else
203 LogFlowThisFuncEnter();
204
205 AutoCaller autoCaller(this);
206 if (FAILED(autoCaller.rc())) return autoCaller.rc();
207
208 ComObjPtr<GuestProcess> pProcess = mData.mProcess;
209 Assert(!pProcess.isNull());
210
211 HRESULT hr = S_OK;
212
213 int rc;
214 GuestProcessStreamBlock streamBlock;
215
216 /** @todo Make use of exceptions! */
217
218 try
219 {
220 GuestFsObjData objData;
221
222 AutoWriteLock rlock(this COMMA_LOCKVAL_SRC_POS);
223
224 rc = parseData(streamBlock);
225 if ( RT_FAILURE(rc)
226 || streamBlock.IsEmpty()) /* More data needed. */
227 {
228 rlock.release();
229
230 rc = pProcess->waitForStart(30 * 1000 /* Timeout */);
231 if (RT_FAILURE(rc))
232 return setError(VBOX_E_IPRT_ERROR,
233 tr("Could not start reading directory \"%s\": %Rrc"),
234 mData.mName.c_str(), rc);
235 BYTE byBuf[_64K];
236 size_t cbRead = 0;
237
238 /** @todo Merge with GuestSession::queryFileInfoInternal. */
239 for (;;)
240 {
241 GuestProcessWaitResult waitRes;
242 rc = pProcess->waitFor( ProcessWaitForFlag_Terminate
243 | ProcessWaitForFlag_StdOut,
244 30 * 1000 /* Timeout */, waitRes);
245 if ( RT_FAILURE(rc)
246 || waitRes.mResult == ProcessWaitResult_Terminate
247 || waitRes.mResult == ProcessWaitResult_Error
248 || waitRes.mResult == ProcessWaitResult_Timeout)
249 {
250 break;
251 }
252
253 rc = pProcess->readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf),
254 30 * 1000 /* Timeout */, byBuf, sizeof(byBuf),
255 &cbRead);
256 if (RT_FAILURE(rc))
257 break;
258
259 if (cbRead)
260 {
261 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
262
263 rc = mData.mStream.AddData(byBuf, cbRead);
264 if (RT_FAILURE(rc))
265 break;
266
267 LogFlowThisFunc(("rc=%Rrc, cbRead=%RU64, cbStreamOut=%RU32\n",
268 rc, cbRead, mData.mStream.GetSize()));
269
270 rc = parseData(streamBlock);
271 if (RT_SUCCESS(rc))
272 {
273 /* Parsing the current stream block succeeded so
274 * we don't need more at the moment. */
275 break;
276 }
277 }
278 }
279
280 LogFlowThisFunc(("Reading done with rc=%Rrc, cbRead=%RU64, cbStream=%RU32\n",
281 rc, cbRead, mData.mStream.GetSize()));
282
283 if (RT_SUCCESS(rc))
284 {
285 rc = parseData(streamBlock);
286 if (rc == VERR_NO_DATA) /* Since this is the last parsing call, this is ok. */
287 rc = VINF_SUCCESS;
288 }
289
290 /*
291 * Note: The guest process can still be around to serve the next
292 * upcoming stream block next time.
293 */
294 if (RT_SUCCESS(rc))
295 {
296 /** @todo Move into common function. */
297 ProcessStatus_T procStatus = ProcessStatus_Undefined;
298 LONG exitCode = 0;
299
300 HRESULT hr = pProcess->COMGETTER(Status(&procStatus));
301 ComAssertComRC(hr);
302 hr = pProcess->COMGETTER(ExitCode(&exitCode));
303 ComAssertComRC(hr);
304
305 if ( ( procStatus != ProcessStatus_Started
306 && procStatus != ProcessStatus_Paused
307 && procStatus != ProcessStatus_Terminating
308 )
309 && exitCode != 0)
310 {
311 return setError(VBOX_E_IPRT_ERROR,
312 tr("Reading directory \"%s\" failed: Unable to read / access denied"),
313 mData.mName.c_str(), exitCode); /**@todo Add stringify methods! */
314 }
315 }
316 }
317
318 if (RT_SUCCESS(rc))
319 {
320 if (streamBlock.GetCount()) /* Did we get content? */
321 {
322 rc = objData.FromLs(streamBlock);
323 if (RT_FAILURE(rc))
324 return setError(VBOX_E_IPRT_ERROR,
325 tr("Reading directory \"%s\" failed: Path not found"),
326 mData.mName.c_str());
327
328 /* Create the object. */
329 ComObjPtr<GuestFsObjInfo> pFsObjInfo;
330 hr = pFsObjInfo.createObject();
331 if (FAILED(hr)) throw VERR_COM_UNEXPECTED;
332
333 rc = pFsObjInfo->init(objData);
334 if (RT_FAILURE(rc)) throw rc;
335
336 /* Return info object to the caller. */
337 hr = pFsObjInfo.queryInterfaceTo(aInfo);
338 if (FAILED(hr)) throw VERR_COM_UNEXPECTED;
339 }
340 else
341 {
342 /* Nothing to read anymore. Tell the caller. */
343 hr = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No more entries for directory \"%s\""),
344 mData.mName.c_str());
345 }
346 }
347 }
348 catch (int rc2)
349 {
350 rc = rc2;
351 }
352
353 if (RT_FAILURE(rc)) /** @todo Add more errors here. */
354 hr = setError(VBOX_E_IPRT_ERROR, tr("Error while reading directory \"%s\": %Rrc\n"),
355 mData.mName.c_str(), rc);
356
357 LogFlowFuncLeaveRC(rc);
358 return hr;
359#endif /* VBOX_WITH_GUEST_CONTROL */
360}
361
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette