VirtualBox

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

Last change on this file since 28851 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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