VirtualBox

source: vbox/trunk/src/VBox/Main/SharedFolderImpl.cpp@ 28456

Last change on this file since 28456 was 27607, checked in by vboxsync, 15 years ago

Main: remove templates for 'weak' com pointers which do nothing anyway

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#include "SharedFolderImpl.h"
23#include "VirtualBoxImpl.h"
24#include "MachineImpl.h"
25#include "ConsoleImpl.h"
26
27#include "AutoCaller.h"
28#include "Logging.h"
29
30#include <iprt/param.h>
31#include <iprt/cpp/utils.h>
32#include <iprt/path.h>
33
34// constructor / destructor
35/////////////////////////////////////////////////////////////////////////////
36
37SharedFolder::SharedFolder()
38 : mParent(NULL),
39 mMachine(NULL),
40 mConsole(NULL),
41 mVirtualBox(NULL)
42{
43}
44
45SharedFolder::~SharedFolder()
46{
47}
48
49HRESULT SharedFolder::FinalConstruct()
50{
51 return S_OK;
52}
53
54void SharedFolder::FinalRelease()
55{
56 uninit();
57}
58
59// public initializer/uninitializer for internal purposes only
60/////////////////////////////////////////////////////////////////////////////
61
62/**
63 * Initializes the shared folder object.
64 *
65 * @param aMachine parent Machine object
66 * @param aName logical name of the shared folder
67 * @param aHostPath full path to the shared folder on the host
68 * @param aWritable writable if true, readonly otherwise
69 *
70 * @return COM result indicator
71 */
72HRESULT SharedFolder::init (Machine *aMachine,
73 CBSTR aName, CBSTR aHostPath, BOOL aWritable)
74{
75 /* Enclose the state transition NotReady->InInit->Ready */
76 AutoInitSpan autoInitSpan(this);
77 AssertReturn(autoInitSpan.isOk(), E_FAIL);
78
79 unconst(mMachine) = aMachine;
80
81 HRESULT rc = protectedInit(aMachine, aName, aHostPath, aWritable);
82
83 /* Confirm a successful initialization when it's the case */
84 if (SUCCEEDED(rc))
85 autoInitSpan.setSucceeded();
86
87 return rc;
88}
89
90/**
91 * Initializes the shared folder object given another object
92 * (a kind of copy constructor). This object makes a private copy of data
93 * of the original object passed as an argument.
94 *
95 * @param aMachine parent Machine object
96 * @param aThat shared folder object to copy
97 *
98 * @return COM result indicator
99 */
100HRESULT SharedFolder::initCopy (Machine *aMachine, SharedFolder *aThat)
101{
102 ComAssertRet(aThat, E_INVALIDARG);
103
104 /* Enclose the state transition NotReady->InInit->Ready */
105 AutoInitSpan autoInitSpan(this);
106 AssertReturn(autoInitSpan.isOk(), E_FAIL);
107
108 unconst(mMachine) = aMachine;
109
110 HRESULT rc = protectedInit (aMachine, aThat->m.name,
111 aThat->m.hostPath, aThat->m.writable);
112
113 /* Confirm a successful initialization when it's the case */
114 if (SUCCEEDED(rc))
115 autoInitSpan.setSucceeded();
116
117 return rc;
118}
119
120/**
121 * Initializes the shared folder object.
122 *
123 * @param aConsole Console parent object
124 * @param aName logical name of the shared folder
125 * @param aHostPath full path to the shared folder on the host
126 * @param aWritable writable if true, readonly otherwise
127 *
128 * @return COM result indicator
129 */
130HRESULT SharedFolder::init(Console *aConsole,
131 CBSTR aName, CBSTR aHostPath, BOOL aWritable)
132{
133 /* Enclose the state transition NotReady->InInit->Ready */
134 AutoInitSpan autoInitSpan(this);
135 AssertReturn(autoInitSpan.isOk(), E_FAIL);
136
137 unconst(mConsole) = aConsole;
138
139 HRESULT rc = protectedInit(aConsole, aName, aHostPath, aWritable);
140
141 /* Confirm a successful initialization when it's the case */
142 if (SUCCEEDED(rc))
143 autoInitSpan.setSucceeded();
144
145 return rc;
146}
147
148/**
149 * Initializes the shared folder object.
150 *
151 * @param aVirtualBox VirtualBox parent object
152 * @param aName logical name of the shared folder
153 * @param aHostPath full path to the shared folder on the host
154 * @param aWritable writable if true, readonly otherwise
155 *
156 * @return COM result indicator
157 */
158HRESULT SharedFolder::init (VirtualBox *aVirtualBox,
159 CBSTR aName, CBSTR aHostPath, BOOL aWritable)
160{
161 /* Enclose the state transition NotReady->InInit->Ready */
162 AutoInitSpan autoInitSpan(this);
163 AssertReturn(autoInitSpan.isOk(), E_FAIL);
164
165 unconst(mVirtualBox) = aVirtualBox;
166
167 HRESULT rc = protectedInit(aVirtualBox, aName, aHostPath, aWritable);
168
169 /* Confirm a successful initialization when it's the case */
170 if (SUCCEEDED(rc))
171 autoInitSpan.setSucceeded();
172
173 return rc;
174}
175
176/**
177 * Helper for init() methods.
178 *
179 * @note
180 * Must be called from under the object's lock!
181 */
182HRESULT SharedFolder::protectedInit(VirtualBoxBase *aParent,
183 CBSTR aName,
184 CBSTR aHostPath,
185 BOOL aWritable)
186{
187 LogFlowThisFunc(("aName={%ls}, aHostPath={%ls}, aWritable={%d}\n",
188 aName, aHostPath, aWritable));
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.raw()[hostPathLen - 1]) &&
204 RTPATH_IS_VOLSEP (hostPath.raw()[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.raw(), 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.raw());
228
229 unconst(mParent) = aParent;
230
231 unconst(m.name) = aName;
232 unconst(m.hostPath) = hostPath;
233 m.writable = aWritable;
234
235 return S_OK;
236}
237
238/**
239 * Uninitializes the instance and sets the ready flag to FALSE.
240 * Called either from FinalRelease() or by the parent when it gets destroyed.
241 */
242void SharedFolder::uninit()
243{
244 LogFlowThisFunc(("\n"));
245
246 /* Enclose the state transition Ready->InUninit->NotReady */
247 AutoUninitSpan autoUninitSpan(this);
248 if (autoUninitSpan.uninitDone())
249 return;
250
251 unconst(mParent) = NULL;
252
253 unconst(mMachine) = NULL;
254 unconst(mConsole) = NULL;
255 unconst(mVirtualBox) = NULL;
256}
257
258// ISharedFolder properties
259/////////////////////////////////////////////////////////////////////////////
260
261STDMETHODIMP SharedFolder::COMGETTER(Name) (BSTR *aName)
262{
263 CheckComArgOutPointerValid(aName);
264
265 AutoCaller autoCaller(this);
266 if (FAILED(autoCaller.rc())) return autoCaller.rc();
267
268 /* mName is constant during life time, no need to lock */
269 m.name.cloneTo(aName);
270
271 return S_OK;
272}
273
274STDMETHODIMP SharedFolder::COMGETTER(HostPath) (BSTR *aHostPath)
275{
276 CheckComArgOutPointerValid(aHostPath);
277
278 AutoCaller autoCaller(this);
279 if (FAILED(autoCaller.rc())) return autoCaller.rc();
280
281 /* mHostPath is constant during life time, no need to lock */
282 m.hostPath.cloneTo(aHostPath);
283
284 return S_OK;
285}
286
287STDMETHODIMP SharedFolder::COMGETTER(Accessible) (BOOL *aAccessible)
288{
289 CheckComArgOutPointerValid(aAccessible);
290
291 AutoCaller autoCaller(this);
292 if (FAILED(autoCaller.rc())) return autoCaller.rc();
293
294 /* mName and mHostPath are constant during life time, no need to lock */
295
296 /* check whether the host path exists */
297 Utf8Str hostPath = Utf8Str(m.hostPath);
298 char hostPathFull[RTPATH_MAX];
299 int vrc = RTPathExists(hostPath.c_str()) ? RTPathReal(hostPath.c_str(),
300 hostPathFull,
301 sizeof(hostPathFull))
302 : VERR_PATH_NOT_FOUND;
303 if (RT_SUCCESS(vrc))
304 {
305 *aAccessible = TRUE;
306 return S_OK;
307 }
308
309 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
310
311 m.lastAccessError = BstrFmt (
312 tr ("'%s' is not accessible (%Rrc)"), hostPath.raw(), vrc);
313
314 LogWarningThisFunc(("m.lastAccessError=\"%ls\"\n", m.lastAccessError.raw()));
315
316 *aAccessible = FALSE;
317 return S_OK;
318}
319
320STDMETHODIMP SharedFolder::COMGETTER(Writable) (BOOL *aWritable)
321{
322 CheckComArgOutPointerValid(aWritable);
323
324 *aWritable = m.writable;
325
326 return S_OK;
327}
328
329STDMETHODIMP SharedFolder::COMGETTER(LastAccessError) (BSTR *aLastAccessError)
330{
331 CheckComArgOutPointerValid(aLastAccessError);
332
333 AutoCaller autoCaller(this);
334 if (FAILED(autoCaller.rc())) return autoCaller.rc();
335
336 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
337
338 m.lastAccessError.cloneTo(aLastAccessError);
339
340 return S_OK;
341}
342
343/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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