1 |
|
---|
2 | /* $Id: GuestDirectoryImpl.cpp 42693 2012-08-08 22:37:51Z 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 |
|
---|
36 | DEFINE_EMPTY_CTOR_DTOR(GuestDirectory)
|
---|
37 |
|
---|
38 | HRESULT GuestDirectory::FinalConstruct(void)
|
---|
39 | {
|
---|
40 | LogFlowThisFunc(("\n"));
|
---|
41 | return BaseFinalConstruct();
|
---|
42 | }
|
---|
43 |
|
---|
44 | void GuestDirectory::FinalRelease(void)
|
---|
45 | {
|
---|
46 | LogFlowThisFuncEnter();
|
---|
47 | uninit();
|
---|
48 | BaseFinalRelease();
|
---|
49 | LogFlowThisFuncLeave();
|
---|
50 | }
|
---|
51 |
|
---|
52 | // public initializer/uninitializer for internal purposes only
|
---|
53 | /////////////////////////////////////////////////////////////////////////////
|
---|
54 |
|
---|
55 | int 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 | */
|
---|
112 | void 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 |
|
---|
128 | STDMETHODIMP 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 |
|
---|
144 | STDMETHODIMP 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 |
|
---|
163 | int 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 |
|
---|
187 | STDMETHODIMP 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 |
|
---|
200 | STDMETHODIMP 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 | rc = waitRes.mRC;
|
---|
241 | break;
|
---|
242 | }
|
---|
243 |
|
---|
244 | rc = pProcess->readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf),
|
---|
245 | 30 * 1000 /* Timeout */, byBuf, sizeof(byBuf),
|
---|
246 | &cbRead);
|
---|
247 | if (RT_FAILURE(rc))
|
---|
248 | break;
|
---|
249 |
|
---|
250 | if (cbRead)
|
---|
251 | {
|
---|
252 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
253 |
|
---|
254 | rc = mData.mStream.AddData(byBuf, cbRead);
|
---|
255 | if (RT_FAILURE(rc))
|
---|
256 | break;
|
---|
257 |
|
---|
258 | LogFlowThisFunc(("rc=%Rrc, cbRead=%RU64, cbStreamOut=%RU32\n",
|
---|
259 | rc, cbRead, mData.mStream.GetSize()));
|
---|
260 |
|
---|
261 | rc = parseData(streamBlock);
|
---|
262 | if (RT_SUCCESS(rc))
|
---|
263 | {
|
---|
264 | /* Parsing the current stream block succeeded so
|
---|
265 | * we don't need more at the moment. */
|
---|
266 | break;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | LogFlowThisFunc(("Reading done with rc=%Rrc, cbRead=%RU64, cbStream=%RU32\n",
|
---|
272 | rc, cbRead, mData.mStream.GetSize()));
|
---|
273 |
|
---|
274 | if (RT_SUCCESS(rc))
|
---|
275 | {
|
---|
276 | rc = parseData(streamBlock);
|
---|
277 | if (rc == VERR_NO_DATA) /* Since this is the last parsing call, this is ok. */
|
---|
278 | rc = VINF_SUCCESS;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * Note: The guest process can still be around to serve the next
|
---|
283 | * upcoming stream block next time.
|
---|
284 | */
|
---|
285 | if (RT_SUCCESS(rc))
|
---|
286 | {
|
---|
287 | /** @todo Move into common function. */
|
---|
288 | ProcessStatus_T procStatus = ProcessStatus_Undefined;
|
---|
289 | LONG exitCode = 0;
|
---|
290 |
|
---|
291 | HRESULT hr2 = pProcess->COMGETTER(Status(&procStatus));
|
---|
292 | ComAssertComRC(hr2);
|
---|
293 | hr2 = pProcess->COMGETTER(ExitCode(&exitCode));
|
---|
294 | ComAssertComRC(hr2);
|
---|
295 |
|
---|
296 | if ( ( procStatus != ProcessStatus_Started
|
---|
297 | && procStatus != ProcessStatus_Paused
|
---|
298 | && procStatus != ProcessStatus_Terminating
|
---|
299 | )
|
---|
300 | && exitCode != 0)
|
---|
301 | {
|
---|
302 | rc = VERR_ACCESS_DENIED;
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | if (RT_SUCCESS(rc))
|
---|
308 | {
|
---|
309 | if (streamBlock.GetCount()) /* Did we get content? */
|
---|
310 | {
|
---|
311 | rc = objData.FromLs(streamBlock);
|
---|
312 | if (RT_FAILURE(rc))
|
---|
313 | rc = VERR_PATH_NOT_FOUND;
|
---|
314 |
|
---|
315 | if (RT_SUCCESS(rc))
|
---|
316 | {
|
---|
317 | /* Create the object. */
|
---|
318 | ComObjPtr<GuestFsObjInfo> pFsObjInfo;
|
---|
319 | HRESULT hr2 = pFsObjInfo.createObject();
|
---|
320 | if (FAILED(hr2))
|
---|
321 | rc = VERR_COM_UNEXPECTED;
|
---|
322 |
|
---|
323 | if (RT_SUCCESS(rc))
|
---|
324 | rc = pFsObjInfo->init(objData);
|
---|
325 |
|
---|
326 | if (RT_SUCCESS(rc))
|
---|
327 | {
|
---|
328 | /* Return info object to the caller. */
|
---|
329 | hr2 = pFsObjInfo.queryInterfaceTo(aInfo);
|
---|
330 | if (FAILED(hr2))
|
---|
331 | rc = VERR_COM_UNEXPECTED;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 | else
|
---|
336 | {
|
---|
337 | /* Nothing to read anymore. Tell the caller. */
|
---|
338 | rc = VERR_NO_MORE_FILES;
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | HRESULT hr = S_OK;
|
---|
343 |
|
---|
344 | if (RT_FAILURE(rc)) /** @todo Add more errors here. */
|
---|
345 | {
|
---|
346 | switch (rc)
|
---|
347 | {
|
---|
348 | case VERR_ACCESS_DENIED:
|
---|
349 | hr = setError(VBOX_E_IPRT_ERROR, tr("Reading directory \"%s\" failed: Unable to read / access denied"),
|
---|
350 | mData.mName.c_str());
|
---|
351 | break;
|
---|
352 |
|
---|
353 | case VERR_PATH_NOT_FOUND:
|
---|
354 | hr = setError(VBOX_E_IPRT_ERROR, tr("Reading directory \"%s\" failed: Path not found"),
|
---|
355 | mData.mName.c_str());
|
---|
356 | break;
|
---|
357 |
|
---|
358 | case VERR_NO_MORE_FILES:
|
---|
359 | hr = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No more entries for directory \"%s\""),
|
---|
360 | mData.mName.c_str());
|
---|
361 | break;
|
---|
362 |
|
---|
363 | default:
|
---|
364 | hr = setError(VBOX_E_IPRT_ERROR, tr("Error while reading directory \"%s\": %Rrc\n"),
|
---|
365 | mData.mName.c_str(), rc);
|
---|
366 | break;
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | LogFlowFuncLeaveRC(rc);
|
---|
371 | return hr;
|
---|
372 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
373 | }
|
---|
374 |
|
---|