VirtualBox

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

Last change on this file since 6388 was 6388, checked in by vboxsync, 16 years ago

BOOL seems to be more correct

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

© 2023 Oracle
ContactPrivacy policyTerms of Use