1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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 | #include "SharedFolderImpl.h"
|
---|
19 | #include "VirtualBoxImpl.h"
|
---|
20 | #include "MachineImpl.h"
|
---|
21 | #include "ConsoleImpl.h"
|
---|
22 |
|
---|
23 | #include "AutoCaller.h"
|
---|
24 | #include "Logging.h"
|
---|
25 |
|
---|
26 | #include <iprt/param.h>
|
---|
27 | #include <iprt/cpp/utils.h>
|
---|
28 | #include <iprt/path.h>
|
---|
29 |
|
---|
30 | // constructor / destructor
|
---|
31 | /////////////////////////////////////////////////////////////////////////////
|
---|
32 |
|
---|
33 | SharedFolder::SharedFolder()
|
---|
34 | : mParent(NULL),
|
---|
35 | mMachine(NULL),
|
---|
36 | mConsole(NULL),
|
---|
37 | mVirtualBox(NULL)
|
---|
38 | {
|
---|
39 | }
|
---|
40 |
|
---|
41 | SharedFolder::~SharedFolder()
|
---|
42 | {
|
---|
43 | }
|
---|
44 |
|
---|
45 | HRESULT SharedFolder::FinalConstruct()
|
---|
46 | {
|
---|
47 | return BaseFinalConstruct();
|
---|
48 | }
|
---|
49 |
|
---|
50 | void SharedFolder::FinalRelease()
|
---|
51 | {
|
---|
52 | uninit();
|
---|
53 | BaseFinalRelease();
|
---|
54 | }
|
---|
55 |
|
---|
56 | // public initializer/uninitializer for internal purposes only
|
---|
57 | /////////////////////////////////////////////////////////////////////////////
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Initializes the shared folder object.
|
---|
61 | *
|
---|
62 | * @param aMachine parent Machine object
|
---|
63 | * @param aName logical name of the shared folder
|
---|
64 | * @param aHostPath full path to the shared folder on the host
|
---|
65 | * @param aWritable writable if true, readonly otherwise
|
---|
66 | * @param aAutoMount if auto mounted by guest true, false otherwise
|
---|
67 | *
|
---|
68 | * @return COM result indicator
|
---|
69 | */
|
---|
70 | HRESULT SharedFolder::init (Machine *aMachine,
|
---|
71 | CBSTR aName, CBSTR aHostPath, BOOL aWritable, BOOL aAutoMount)
|
---|
72 | {
|
---|
73 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
74 | AutoInitSpan autoInitSpan(this);
|
---|
75 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
76 |
|
---|
77 | unconst(mMachine) = aMachine;
|
---|
78 |
|
---|
79 | HRESULT rc = protectedInit(aMachine, aName, aHostPath, aWritable, aAutoMount);
|
---|
80 |
|
---|
81 | /* Confirm a successful initialization when it's the case */
|
---|
82 | if (SUCCEEDED(rc))
|
---|
83 | autoInitSpan.setSucceeded();
|
---|
84 |
|
---|
85 | return rc;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Initializes the shared folder object given another object
|
---|
90 | * (a kind of copy constructor). This object makes a private copy of data
|
---|
91 | * of the original object passed as an argument.
|
---|
92 | *
|
---|
93 | * @param aMachine parent Machine object
|
---|
94 | * @param aThat shared folder object to copy
|
---|
95 | *
|
---|
96 | * @return COM result indicator
|
---|
97 | */
|
---|
98 | HRESULT SharedFolder::initCopy (Machine *aMachine, SharedFolder *aThat)
|
---|
99 | {
|
---|
100 | ComAssertRet(aThat, E_INVALIDARG);
|
---|
101 |
|
---|
102 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
103 | AutoInitSpan autoInitSpan(this);
|
---|
104 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
105 |
|
---|
106 | unconst(mMachine) = aMachine;
|
---|
107 |
|
---|
108 | HRESULT rc = protectedInit(aMachine, aThat->m.name.raw(),
|
---|
109 | aThat->m.hostPath.raw(), aThat->m.writable,
|
---|
110 | aThat->m.autoMount);
|
---|
111 |
|
---|
112 | /* Confirm a successful initialization when it's the case */
|
---|
113 | if (SUCCEEDED(rc))
|
---|
114 | autoInitSpan.setSucceeded();
|
---|
115 |
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Initializes the shared folder object.
|
---|
121 | *
|
---|
122 | * @param aConsole Console parent object
|
---|
123 | * @param aName logical name of the shared folder
|
---|
124 | * @param aHostPath full path to the shared folder on the host
|
---|
125 | * @param aWritable writable if true, readonly otherwise
|
---|
126 | *
|
---|
127 | * @return COM result indicator
|
---|
128 | */
|
---|
129 | HRESULT SharedFolder::init(Console *aConsole,
|
---|
130 | CBSTR aName, CBSTR aHostPath, BOOL aWritable, BOOL aAutoMount)
|
---|
131 | {
|
---|
132 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
133 | AutoInitSpan autoInitSpan(this);
|
---|
134 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
135 |
|
---|
136 | unconst(mConsole) = aConsole;
|
---|
137 |
|
---|
138 | HRESULT rc = protectedInit(aConsole, aName, aHostPath, aWritable, aAutoMount);
|
---|
139 |
|
---|
140 | /* Confirm a successful initialization when it's the case */
|
---|
141 | if (SUCCEEDED(rc))
|
---|
142 | autoInitSpan.setSucceeded();
|
---|
143 |
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Initializes the shared folder object.
|
---|
149 | *
|
---|
150 | * @param aVirtualBox VirtualBox parent object
|
---|
151 | * @param aName logical name of the shared folder
|
---|
152 | * @param aHostPath full path to the shared folder on the host
|
---|
153 | * @param aWritable writable if true, readonly otherwise
|
---|
154 | *
|
---|
155 | * @return COM result indicator
|
---|
156 | */
|
---|
157 | HRESULT SharedFolder::init (VirtualBox *aVirtualBox,
|
---|
158 | CBSTR aName, CBSTR aHostPath, BOOL aWritable, BOOL aAutoMount)
|
---|
159 | {
|
---|
160 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
161 | AutoInitSpan autoInitSpan(this);
|
---|
162 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
163 |
|
---|
164 | unconst(mVirtualBox) = aVirtualBox;
|
---|
165 |
|
---|
166 | HRESULT rc = protectedInit(aVirtualBox, aName, aHostPath, aWritable, aAutoMount);
|
---|
167 |
|
---|
168 | /* Confirm a successful initialization when it's the case */
|
---|
169 | if (SUCCEEDED(rc))
|
---|
170 | autoInitSpan.setSucceeded();
|
---|
171 |
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Helper for init() methods.
|
---|
177 | *
|
---|
178 | * @note
|
---|
179 | * Must be called from under the object's lock!
|
---|
180 | */
|
---|
181 | HRESULT SharedFolder::protectedInit(VirtualBoxBase *aParent,
|
---|
182 | CBSTR aName,
|
---|
183 | CBSTR aHostPath,
|
---|
184 | BOOL aWritable,
|
---|
185 | BOOL aAutoMount)
|
---|
186 | {
|
---|
187 | LogFlowThisFunc(("aName={%ls}, aHostPath={%ls}, aWritable={%d}, aAutoMount={%d}\n",
|
---|
188 | aName, aHostPath, aWritable, aAutoMount));
|
---|
189 |
|
---|
190 | ComAssertRet(aParent && aName && aHostPath, E_INVALIDARG);
|
---|
191 |
|
---|
192 | Utf8Str hostPath = Utf8Str (aHostPath);
|
---|
193 | size_t hostPathLen = hostPath.length();
|
---|
194 |
|
---|
195 | /* Remove the trailing slash unless it's a root directory
|
---|
196 | * (otherwise the comparison with the RTPathAbs() result will fail at least
|
---|
197 | * on Linux). Note that this isn't really necessary for the shared folder
|
---|
198 | * itself, since adding a mapping eventually results into a
|
---|
199 | * RTDirOpenFiltered() call (see HostServices/SharedFolders) that seems to
|
---|
200 | * accept both the slashified paths and not. */
|
---|
201 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
202 | if (hostPathLen > 2 &&
|
---|
203 | RTPATH_IS_SEP (hostPath.c_str()[hostPathLen - 1]) &&
|
---|
204 | RTPATH_IS_VOLSEP (hostPath.c_str()[hostPathLen - 2]))
|
---|
205 | ;
|
---|
206 | #else
|
---|
207 | if (hostPathLen == 1 && RTPATH_IS_SEP(hostPath[0]))
|
---|
208 | ;
|
---|
209 | #endif
|
---|
210 | else
|
---|
211 | hostPath.stripTrailingSlash();
|
---|
212 |
|
---|
213 | /* Check whether the path is full (absolute) */
|
---|
214 | char hostPathFull[RTPATH_MAX];
|
---|
215 | int vrc = RTPathAbsEx(NULL,
|
---|
216 | hostPath.c_str(),
|
---|
217 | hostPathFull,
|
---|
218 | sizeof (hostPathFull));
|
---|
219 | if (RT_FAILURE(vrc))
|
---|
220 | return setError(E_INVALIDARG,
|
---|
221 | tr("Invalid shared folder path: '%s' (%Rrc)"),
|
---|
222 | hostPath.c_str(), vrc);
|
---|
223 |
|
---|
224 | if (RTPathCompare(hostPath.c_str(), hostPathFull) != 0)
|
---|
225 | return setError(E_INVALIDARG,
|
---|
226 | tr("Shared folder path '%s' is not absolute"),
|
---|
227 | hostPath.c_str());
|
---|
228 |
|
---|
229 | unconst(mParent) = aParent;
|
---|
230 |
|
---|
231 | unconst(m.name) = aName;
|
---|
232 | unconst(m.hostPath) = hostPath;
|
---|
233 | m.writable = aWritable;
|
---|
234 | m.autoMount = aAutoMount;
|
---|
235 |
|
---|
236 | return S_OK;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
241 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
242 | */
|
---|
243 | void SharedFolder::uninit()
|
---|
244 | {
|
---|
245 | LogFlowThisFunc(("\n"));
|
---|
246 |
|
---|
247 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
248 | AutoUninitSpan autoUninitSpan(this);
|
---|
249 | if (autoUninitSpan.uninitDone())
|
---|
250 | return;
|
---|
251 |
|
---|
252 | unconst(mParent) = NULL;
|
---|
253 |
|
---|
254 | unconst(mMachine) = NULL;
|
---|
255 | unconst(mConsole) = NULL;
|
---|
256 | unconst(mVirtualBox) = NULL;
|
---|
257 | }
|
---|
258 |
|
---|
259 | // ISharedFolder properties
|
---|
260 | /////////////////////////////////////////////////////////////////////////////
|
---|
261 |
|
---|
262 | STDMETHODIMP SharedFolder::COMGETTER(Name) (BSTR *aName)
|
---|
263 | {
|
---|
264 | CheckComArgOutPointerValid(aName);
|
---|
265 |
|
---|
266 | AutoCaller autoCaller(this);
|
---|
267 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
268 |
|
---|
269 | /* mName is constant during life time, no need to lock */
|
---|
270 | m.name.cloneTo(aName);
|
---|
271 |
|
---|
272 | return S_OK;
|
---|
273 | }
|
---|
274 |
|
---|
275 | STDMETHODIMP SharedFolder::COMGETTER(HostPath) (BSTR *aHostPath)
|
---|
276 | {
|
---|
277 | CheckComArgOutPointerValid(aHostPath);
|
---|
278 |
|
---|
279 | AutoCaller autoCaller(this);
|
---|
280 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
281 |
|
---|
282 | /* mHostPath is constant during life time, no need to lock */
|
---|
283 | m.hostPath.cloneTo(aHostPath);
|
---|
284 |
|
---|
285 | return S_OK;
|
---|
286 | }
|
---|
287 |
|
---|
288 | STDMETHODIMP SharedFolder::COMGETTER(Accessible) (BOOL *aAccessible)
|
---|
289 | {
|
---|
290 | CheckComArgOutPointerValid(aAccessible);
|
---|
291 |
|
---|
292 | AutoCaller autoCaller(this);
|
---|
293 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
294 |
|
---|
295 | /* mName and mHostPath are constant during life time, no need to lock */
|
---|
296 |
|
---|
297 | /* check whether the host path exists */
|
---|
298 | Utf8Str hostPath = Utf8Str(m.hostPath);
|
---|
299 | char hostPathFull[RTPATH_MAX];
|
---|
300 | int vrc = RTPathExists(hostPath.c_str()) ? RTPathReal(hostPath.c_str(),
|
---|
301 | hostPathFull,
|
---|
302 | sizeof(hostPathFull))
|
---|
303 | : VERR_PATH_NOT_FOUND;
|
---|
304 | if (RT_SUCCESS(vrc))
|
---|
305 | {
|
---|
306 | *aAccessible = TRUE;
|
---|
307 | return S_OK;
|
---|
308 | }
|
---|
309 |
|
---|
310 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
311 |
|
---|
312 | m.lastAccessError = BstrFmt (
|
---|
313 | tr ("'%s' is not accessible (%Rrc)"), hostPath.c_str(), vrc);
|
---|
314 |
|
---|
315 | LogWarningThisFunc(("m.lastAccessError=\"%ls\"\n", m.lastAccessError.raw()));
|
---|
316 |
|
---|
317 | *aAccessible = FALSE;
|
---|
318 | return S_OK;
|
---|
319 | }
|
---|
320 |
|
---|
321 | STDMETHODIMP SharedFolder::COMGETTER(Writable) (BOOL *aWritable)
|
---|
322 | {
|
---|
323 | CheckComArgOutPointerValid(aWritable);
|
---|
324 |
|
---|
325 | AutoCaller autoCaller(this);
|
---|
326 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
327 |
|
---|
328 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
329 |
|
---|
330 | *aWritable = m.writable;
|
---|
331 |
|
---|
332 | return S_OK;
|
---|
333 | }
|
---|
334 |
|
---|
335 | STDMETHODIMP SharedFolder::COMGETTER(AutoMount) (BOOL *aAutoMount)
|
---|
336 | {
|
---|
337 | CheckComArgOutPointerValid(aAutoMount);
|
---|
338 |
|
---|
339 | AutoCaller autoCaller(this);
|
---|
340 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
341 |
|
---|
342 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
343 |
|
---|
344 | *aAutoMount = m.autoMount;
|
---|
345 |
|
---|
346 | return S_OK;
|
---|
347 | }
|
---|
348 |
|
---|
349 | STDMETHODIMP SharedFolder::COMGETTER(LastAccessError) (BSTR *aLastAccessError)
|
---|
350 | {
|
---|
351 | CheckComArgOutPointerValid(aLastAccessError);
|
---|
352 |
|
---|
353 | AutoCaller autoCaller(this);
|
---|
354 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
355 |
|
---|
356 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
357 |
|
---|
358 | m.lastAccessError.cloneTo(aLastAccessError);
|
---|
359 |
|
---|
360 | return S_OK;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|