VirtualBox

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

Last change on this file since 31892 was 31544, checked in by vboxsync, 14 years ago

build fix

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