1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * 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 "Logging.h"
|
---|
24 |
|
---|
25 | #include <iprt/param.h>
|
---|
26 | #include <iprt/path.h>
|
---|
27 | #include <iprt/cpputils.h>
|
---|
28 |
|
---|
29 | // constructor / destructor
|
---|
30 | /////////////////////////////////////////////////////////////////////////////
|
---|
31 |
|
---|
32 | SharedFolder::SharedFolder()
|
---|
33 | : mParent (NULL)
|
---|
34 | {
|
---|
35 | }
|
---|
36 |
|
---|
37 | SharedFolder::~SharedFolder()
|
---|
38 | {
|
---|
39 | }
|
---|
40 |
|
---|
41 | HRESULT SharedFolder::FinalConstruct()
|
---|
42 | {
|
---|
43 | return S_OK;
|
---|
44 | }
|
---|
45 |
|
---|
46 | void SharedFolder::FinalRelease()
|
---|
47 | {
|
---|
48 | uninit();
|
---|
49 | }
|
---|
50 |
|
---|
51 | // public initializer/uninitializer for internal purposes only
|
---|
52 | /////////////////////////////////////////////////////////////////////////////
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Initializes the shared folder object.
|
---|
56 | *
|
---|
57 | * @param aMachine parent Machine object
|
---|
58 | * @param aName logical name of the shared folder
|
---|
59 | * @param aHostPath full path to the shared folder on the host
|
---|
60 | *
|
---|
61 | * @return COM result indicator
|
---|
62 | */
|
---|
63 | HRESULT SharedFolder::init (Machine *aMachine,
|
---|
64 | const BSTR aName, const BSTR aHostPath)
|
---|
65 | {
|
---|
66 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
67 | AutoInitSpan autoInitSpan (this);
|
---|
68 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
69 |
|
---|
70 | unconst (mMachine) = aMachine;
|
---|
71 |
|
---|
72 | HRESULT rc = protectedInit (aMachine, aName, aHostPath);
|
---|
73 |
|
---|
74 | /* Confirm a successful initialization when it's the case */
|
---|
75 | if (SUCCEEDED (rc))
|
---|
76 | autoInitSpan.setSucceeded();
|
---|
77 |
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Initializes the shared folder object given another object
|
---|
83 | * (a kind of copy constructor). This object makes a private copy of data
|
---|
84 | * of the original object passed as an argument.
|
---|
85 | *
|
---|
86 | * @param aMachine parent Machine object
|
---|
87 | * @param aThat shared folder object to copy
|
---|
88 | *
|
---|
89 | * @return COM result indicator
|
---|
90 | */
|
---|
91 | HRESULT SharedFolder::initCopy (Machine *aMachine, SharedFolder *aThat)
|
---|
92 | {
|
---|
93 | ComAssertRet (aThat, E_INVALIDARG);
|
---|
94 |
|
---|
95 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
96 | AutoInitSpan autoInitSpan (this);
|
---|
97 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
98 |
|
---|
99 | unconst (mMachine) = aMachine;
|
---|
100 |
|
---|
101 | HRESULT rc = protectedInit (aMachine, aThat->mData.mName,
|
---|
102 | aThat->mData.mHostPath);
|
---|
103 |
|
---|
104 | /* Confirm a successful initialization when it's the case */
|
---|
105 | if (SUCCEEDED (rc))
|
---|
106 | autoInitSpan.setSucceeded();
|
---|
107 |
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Initializes the shared folder object.
|
---|
113 | *
|
---|
114 | * @param aConsole Console parent object
|
---|
115 | * @param aName logical name of the shared folder
|
---|
116 | * @param aHostPath full path to the shared folder on the host
|
---|
117 | *
|
---|
118 | * @return COM result indicator
|
---|
119 | */
|
---|
120 | HRESULT SharedFolder::init (Console *aConsole,
|
---|
121 | const BSTR aName, const BSTR aHostPath)
|
---|
122 | {
|
---|
123 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
124 | AutoInitSpan autoInitSpan (this);
|
---|
125 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
126 |
|
---|
127 | unconst (mConsole) = aConsole;
|
---|
128 |
|
---|
129 | HRESULT rc = protectedInit (aConsole, aName, aHostPath);
|
---|
130 |
|
---|
131 | /* Confirm a successful initialization when it's the case */
|
---|
132 | if (SUCCEEDED (rc))
|
---|
133 | autoInitSpan.setSucceeded();
|
---|
134 |
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Initializes the shared folder object.
|
---|
140 | *
|
---|
141 | * @param aVirtualBox VirtualBox parent object
|
---|
142 | * @param aName logical name of the shared folder
|
---|
143 | * @param aHostPath full path to the shared folder on the host
|
---|
144 | *
|
---|
145 | * @return COM result indicator
|
---|
146 | */
|
---|
147 | HRESULT SharedFolder::init (VirtualBox *aVirtualBox,
|
---|
148 | const BSTR aName, const BSTR aHostPath)
|
---|
149 | {
|
---|
150 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
151 | AutoInitSpan autoInitSpan (this);
|
---|
152 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
153 |
|
---|
154 | unconst (mVirtualBox) = aVirtualBox;
|
---|
155 |
|
---|
156 | HRESULT rc = protectedInit (aVirtualBox, aName, aHostPath);
|
---|
157 |
|
---|
158 | /* Confirm a successful initialization when it's the case */
|
---|
159 | if (SUCCEEDED (rc))
|
---|
160 | autoInitSpan.setSucceeded();
|
---|
161 |
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Helper for init() methods.
|
---|
167 | *
|
---|
168 | * @note
|
---|
169 | * Must be called from under the object's lock!
|
---|
170 | */
|
---|
171 | HRESULT SharedFolder::protectedInit (VirtualBoxBaseWithChildrenNEXT *aParent,
|
---|
172 | const BSTR aName, const BSTR aHostPath)
|
---|
173 | {
|
---|
174 | LogFlowThisFunc (("aName={%ls}, aHostPath={%ls}\n", aName, aHostPath));
|
---|
175 |
|
---|
176 | ComAssertRet (aParent && aName && aHostPath, E_INVALIDARG);
|
---|
177 |
|
---|
178 | Utf8Str hostPath = Utf8Str (aHostPath);
|
---|
179 | size_t hostPathLen = hostPath.length();
|
---|
180 |
|
---|
181 | /* Remove the trailng slash unless it's a root directory
|
---|
182 | * (otherwise the comparison with the RTPathAbs() result will fail at least
|
---|
183 | * on Linux). Note that this isn't really necessary for the shared folder
|
---|
184 | * itself, since adding a mapping eventually results into a
|
---|
185 | * RTDirOpenFiltered() call (see HostServices/SharedFolders) that seems to
|
---|
186 | * accept both the slashified paths and not. */
|
---|
187 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
188 | if (hostPathLen > 2 &&
|
---|
189 | RTPATH_IS_SEP (hostPath.raw()[hostPathLen - 1]) &&
|
---|
190 | RTPATH_IS_VOLSEP (hostPath.raw()[hostPathLen - 2]))
|
---|
191 | ;
|
---|
192 | #else
|
---|
193 | if (hostPathLen == 1 && RTPATH_IS_SEP (hostPath[0]))
|
---|
194 | ;
|
---|
195 | #endif
|
---|
196 | else
|
---|
197 | RTPathStripTrailingSlash (hostPath.mutableRaw());
|
---|
198 |
|
---|
199 | /* Check whether the path is full (absolute) */
|
---|
200 | char hostPathFull [RTPATH_MAX];
|
---|
201 | int vrc = RTPathAbsEx (NULL, hostPath,
|
---|
202 | hostPathFull, sizeof (hostPathFull));
|
---|
203 | if (VBOX_FAILURE (vrc))
|
---|
204 | return setError (E_INVALIDARG,
|
---|
205 | tr ("Invalid shared folder path: '%s' (%Vrc)"), hostPath.raw(), vrc);
|
---|
206 |
|
---|
207 | if (RTPathCompare (hostPath, hostPathFull) != 0)
|
---|
208 | return setError (E_INVALIDARG,
|
---|
209 | tr ("Shared folder path '%s' is not absolute"), hostPath.raw());
|
---|
210 |
|
---|
211 | unconst (mParent) = aParent;
|
---|
212 |
|
---|
213 | /* register with parent */
|
---|
214 | mParent->addDependentChild (this);
|
---|
215 |
|
---|
216 | unconst (mData.mName) = aName;
|
---|
217 | unconst (mData.mHostPath) = hostPath;
|
---|
218 |
|
---|
219 | return S_OK;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
224 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
225 | */
|
---|
226 | void SharedFolder::uninit()
|
---|
227 | {
|
---|
228 | LogFlowThisFunc (("\n"));
|
---|
229 |
|
---|
230 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
231 | AutoUninitSpan autoUninitSpan (this);
|
---|
232 | if (autoUninitSpan.uninitDone())
|
---|
233 | return;
|
---|
234 |
|
---|
235 | if (mParent)
|
---|
236 | mParent->removeDependentChild (this);
|
---|
237 |
|
---|
238 | unconst (mParent) = NULL;
|
---|
239 |
|
---|
240 | unconst (mMachine).setNull();
|
---|
241 | unconst (mConsole).setNull();
|
---|
242 | unconst (mVirtualBox).setNull();
|
---|
243 | }
|
---|
244 |
|
---|
245 | // ISharedFolder properties
|
---|
246 | /////////////////////////////////////////////////////////////////////////////
|
---|
247 |
|
---|
248 | STDMETHODIMP SharedFolder::COMGETTER(Name) (BSTR *aName)
|
---|
249 | {
|
---|
250 | if (!aName)
|
---|
251 | return E_POINTER;
|
---|
252 |
|
---|
253 | AutoCaller autoCaller (this);
|
---|
254 | CheckComRCReturnRC (autoCaller.rc());
|
---|
255 |
|
---|
256 | /* mName is constant during life time, no need to lock */
|
---|
257 | mData.mName.cloneTo (aName);
|
---|
258 |
|
---|
259 | return S_OK;
|
---|
260 | }
|
---|
261 |
|
---|
262 | STDMETHODIMP SharedFolder::COMGETTER(HostPath) (BSTR *aHostPath)
|
---|
263 | {
|
---|
264 | if (!aHostPath)
|
---|
265 | return E_POINTER;
|
---|
266 |
|
---|
267 | AutoCaller autoCaller (this);
|
---|
268 | CheckComRCReturnRC (autoCaller.rc());
|
---|
269 |
|
---|
270 | /* mHostPath is constant during life time, no need to lock */
|
---|
271 | mData.mHostPath.cloneTo (aHostPath);
|
---|
272 |
|
---|
273 | return S_OK;
|
---|
274 | }
|
---|
275 |
|
---|
276 | STDMETHODIMP SharedFolder::COMGETTER(Accessible) (BOOL *aAccessible)
|
---|
277 | {
|
---|
278 | if (!aAccessible)
|
---|
279 | return E_POINTER;
|
---|
280 |
|
---|
281 | AutoCaller autoCaller (this);
|
---|
282 | CheckComRCReturnRC (autoCaller.rc());
|
---|
283 |
|
---|
284 | /* mName and mHostPath are constant during life time, no need to lock */
|
---|
285 |
|
---|
286 | /* check whether the host path exists */
|
---|
287 | Utf8Str hostPath = Utf8Str (mData.mHostPath);
|
---|
288 | char hostPathFull [RTPATH_MAX];
|
---|
289 | int vrc = RTPathExists(hostPath) ? RTPathReal (hostPath, hostPathFull,
|
---|
290 | sizeof (hostPathFull))
|
---|
291 | : VERR_PATH_NOT_FOUND;
|
---|
292 | if (VBOX_SUCCESS (vrc))
|
---|
293 | {
|
---|
294 | *aAccessible = TRUE;
|
---|
295 | return S_OK;
|
---|
296 | }
|
---|
297 |
|
---|
298 | HRESULT rc = S_OK;
|
---|
299 | if (vrc != VERR_PATH_NOT_FOUND)
|
---|
300 | rc = setError (E_FAIL,
|
---|
301 | tr ("Invalid shared folder path: '%s' (%Vrc)"), hostPath.raw(), vrc);
|
---|
302 |
|
---|
303 | LogWarningThisFunc (("'%s' is not accessible (%Vrc)\n", hostPath.raw(), vrc));
|
---|
304 |
|
---|
305 | *aAccessible = FALSE;
|
---|
306 | return S_OK;
|
---|
307 | }
|
---|
308 |
|
---|