VirtualBox

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

Last change on this file since 33868 was 32718, checked in by vboxsync, 14 years ago

com/string: Remove bool conversion operator and other convenience error operators. They are hiding programming errors (like incorrect empty string checks, and in one case a free of the wrong pointer).

  • 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.raw(),
108 aThat->m.hostPath.raw(), aThat->m.writable,
109 aThat->m.autoMount);
110
111 /* Confirm a successful initialization when it's the case */
112 if (SUCCEEDED(rc))
113 autoInitSpan.setSucceeded();
114
115 return rc;
116}
117
118/**
119 * Initializes the shared folder object.
120 *
121 * @param aConsole Console parent object
122 * @param aName logical name of the shared folder
123 * @param aHostPath full path to the shared folder on the host
124 * @param aWritable writable if true, readonly otherwise
125 *
126 * @return COM result indicator
127 */
128HRESULT SharedFolder::init(Console *aConsole,
129 CBSTR aName, CBSTR aHostPath, BOOL aWritable, BOOL aAutoMount)
130{
131 /* Enclose the state transition NotReady->InInit->Ready */
132 AutoInitSpan autoInitSpan(this);
133 AssertReturn(autoInitSpan.isOk(), E_FAIL);
134
135 unconst(mConsole) = aConsole;
136
137 HRESULT rc = protectedInit(aConsole, aName, aHostPath, aWritable, aAutoMount);
138
139 /* Confirm a successful initialization when it's the case */
140 if (SUCCEEDED(rc))
141 autoInitSpan.setSucceeded();
142
143 return rc;
144}
145
146/**
147 * Initializes the shared folder object.
148 *
149 * @param aVirtualBox VirtualBox parent object
150 * @param aName logical name of the shared folder
151 * @param aHostPath full path to the shared folder on the host
152 * @param aWritable writable if true, readonly otherwise
153 *
154 * @return COM result indicator
155 */
156HRESULT SharedFolder::init (VirtualBox *aVirtualBox,
157 CBSTR aName, CBSTR aHostPath, BOOL aWritable, BOOL aAutoMount)
158{
159 /* Enclose the state transition NotReady->InInit->Ready */
160 AutoInitSpan autoInitSpan(this);
161 AssertReturn(autoInitSpan.isOk(), E_FAIL);
162
163 unconst(mVirtualBox) = aVirtualBox;
164
165 HRESULT rc = protectedInit(aVirtualBox, aName, aHostPath, aWritable, aAutoMount);
166
167 /* Confirm a successful initialization when it's the case */
168 if (SUCCEEDED(rc))
169 autoInitSpan.setSucceeded();
170
171 return rc;
172}
173
174/**
175 * Helper for init() methods.
176 *
177 * @note
178 * Must be called from under the object's lock!
179 */
180HRESULT SharedFolder::protectedInit(VirtualBoxBase *aParent,
181 CBSTR aName,
182 CBSTR aHostPath,
183 BOOL aWritable,
184 BOOL aAutoMount)
185{
186 LogFlowThisFunc(("aName={%ls}, aHostPath={%ls}, aWritable={%d}, aAutoMount={%d}\n",
187 aName, aHostPath, aWritable, aAutoMount));
188
189 ComAssertRet(aParent && aName && aHostPath, E_INVALIDARG);
190
191 Utf8Str hostPath = Utf8Str (aHostPath);
192 size_t hostPathLen = hostPath.length();
193
194 /* Remove the trailing slash unless it's a root directory
195 * (otherwise the comparison with the RTPathAbs() result will fail at least
196 * on Linux). Note that this isn't really necessary for the shared folder
197 * itself, since adding a mapping eventually results into a
198 * RTDirOpenFiltered() call (see HostServices/SharedFolders) that seems to
199 * accept both the slashified paths and not. */
200#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
201 if (hostPathLen > 2 &&
202 RTPATH_IS_SEP (hostPath.c_str()[hostPathLen - 1]) &&
203 RTPATH_IS_VOLSEP (hostPath.c_str()[hostPathLen - 2]))
204 ;
205#else
206 if (hostPathLen == 1 && RTPATH_IS_SEP(hostPath[0]))
207 ;
208#endif
209 else
210 hostPath.stripTrailingSlash();
211
212 /* Check whether the path is full (absolute) */
213 char hostPathFull[RTPATH_MAX];
214 int vrc = RTPathAbsEx(NULL,
215 hostPath.c_str(),
216 hostPathFull,
217 sizeof (hostPathFull));
218 if (RT_FAILURE(vrc))
219 return setError(E_INVALIDARG,
220 tr("Invalid shared folder path: '%s' (%Rrc)"),
221 hostPath.c_str(), vrc);
222
223 if (RTPathCompare(hostPath.c_str(), hostPathFull) != 0)
224 return setError(E_INVALIDARG,
225 tr("Shared folder path '%s' is not absolute"),
226 hostPath.c_str());
227
228 unconst(mParent) = aParent;
229
230 unconst(m.name) = aName;
231 unconst(m.hostPath) = hostPath;
232 m.writable = aWritable;
233 m.autoMount = aAutoMount;
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.c_str(), 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 AutoCaller autoCaller(this);
325 if (FAILED(autoCaller.rc())) return autoCaller.rc();
326
327 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
328
329 *aWritable = m.writable;
330
331 return S_OK;
332}
333
334STDMETHODIMP SharedFolder::COMGETTER(AutoMount) (BOOL *aAutoMount)
335{
336 CheckComArgOutPointerValid(aAutoMount);
337
338 AutoCaller autoCaller(this);
339 if (FAILED(autoCaller.rc())) return autoCaller.rc();
340
341 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
342
343 *aAutoMount = m.autoMount;
344
345 return S_OK;
346}
347
348STDMETHODIMP SharedFolder::COMGETTER(LastAccessError) (BSTR *aLastAccessError)
349{
350 CheckComArgOutPointerValid(aLastAccessError);
351
352 AutoCaller autoCaller(this);
353 if (FAILED(autoCaller.rc())) return autoCaller.rc();
354
355 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
356
357 m.lastAccessError.cloneTo(aLastAccessError);
358
359 return S_OK;
360}
361
362/* 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