VirtualBox

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

Last change on this file since 3516 was 3007, checked in by vboxsync, 18 years ago

Moved the template code out of cdefs.h, partly because it didn't belong there but mostly because it was at the end of the file and would screw up any attempts made by the object cache at avoid recompiling on cdefs.h changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include "SharedFolderImpl.h"
23#include "VirtualBoxImpl.h"
24#include "MachineImpl.h"
25#include "ConsoleImpl.h"
26
27#include "Logging.h"
28
29#include <iprt/param.h>
30#include <iprt/path.h>
31#include <iprt/cpputils.h>
32
33// constructor / destructor
34/////////////////////////////////////////////////////////////////////////////
35
36HRESULT SharedFolder::FinalConstruct()
37{
38 return S_OK;
39}
40
41void SharedFolder::FinalRelease()
42{
43 uninit();
44}
45
46// public initializer/uninitializer for internal purposes only
47/////////////////////////////////////////////////////////////////////////////
48
49/**
50 * Initializes the shared folder object.
51 *
52 * @param aMachine parent Machine object
53 * @param aName logical name of the shared folder
54 * @param aHostPath full path to the shared folder on the host
55 *
56 * @return COM result indicator
57 */
58HRESULT SharedFolder::init (Machine *aMachine,
59 const BSTR aName, const BSTR aHostPath)
60{
61 AutoLock alock (this);
62 ComAssertRet (!isReady(), E_UNEXPECTED);
63
64 mMachine = aMachine;
65 return protectedInit (aMachine, aName, aHostPath);
66}
67
68/**
69 * Initializes the shared folder object given another object
70 * (a kind of copy constructor). This object makes a private copy of data
71 * of the original object passed as an argument.
72 *
73 * @param aMachine parent Machine object
74 * @param aThat shared folder object to copy
75 *
76 * @return COM result indicator
77 */
78HRESULT SharedFolder::initCopy (Machine *aMachine, SharedFolder *aThat)
79{
80 ComAssertRet (aThat, E_INVALIDARG);
81
82 AutoLock alock (this);
83 ComAssertRet (!isReady(), E_UNEXPECTED);
84
85 mMachine = aMachine;
86 return protectedInit (aMachine, aThat->mName, aThat->mHostPath);
87}
88
89/**
90 * Initializes the shared folder object.
91 *
92 * @param aConsole Console parent object
93 * @param aName logical name of the shared folder
94 * @param aHostPath full path to the shared folder on the host
95 *
96 * @return COM result indicator
97 */
98HRESULT SharedFolder::init (Console *aConsole,
99 const BSTR aName, const BSTR aHostPath)
100{
101 AutoLock alock (this);
102 ComAssertRet (!isReady(), E_UNEXPECTED);
103
104 mConsole = aConsole;
105 return protectedInit (aConsole, aName, aHostPath);
106}
107
108/**
109 * Initializes the shared folder object.
110 *
111 * @param aVirtualBox VirtualBox parent object
112 * @param aName logical name of the shared folder
113 * @param aHostPath full path to the shared folder on the host
114 *
115 * @return COM result indicator
116 */
117HRESULT SharedFolder::init (VirtualBox *aVirtualBox,
118 const BSTR aName, const BSTR aHostPath)
119{
120 AutoLock alock (this);
121 ComAssertRet (!isReady(), E_UNEXPECTED);
122
123 mVirtualBox = aVirtualBox;
124 return protectedInit (aVirtualBox, aName, aHostPath);
125}
126
127/**
128 * Helper for init() methods.
129 *
130 * @note
131 * Must be called from under the object's lock!
132 */
133HRESULT SharedFolder::protectedInit (VirtualBoxBaseWithChildren *aParent,
134 const BSTR aName, const BSTR aHostPath)
135{
136 LogFlowThisFunc (("aName={%ls}, aHostPath={%ls}\n", aName, aHostPath));
137
138 ComAssertRet (aParent && aName && aHostPath, E_INVALIDARG);
139
140 Utf8Str hostPath = Utf8Str (aHostPath);
141 size_t hostPathLen = hostPath.length();
142
143 /* Remove the trailng slash unless it's a root directory
144 * (otherwise the comparison with the RTPathAbs() result will fail at least
145 * on Linux). Note that this isn't really necessary for the shared folder
146 * itself, since adding a mapping eventually results into a
147 * RTDirOpenFiltered() call (see HostServices/SharedFolders) that seems to
148 * accept both the slashified paths and not. */
149#if defined (__OS2__) || defined (__WIN__)
150 if (hostPathLen > 2 &&
151 RTPATH_IS_SEP (hostPath.raw()[hostPathLen - 1]) &&
152 RTPATH_IS_VOLSEP (hostPath.raw()[hostPathLen - 2]))
153 ;
154#else
155 if (hostPathLen == 1 && RTPATH_IS_SEP (hostPath[0]))
156 ;
157#endif
158 else
159 RTPathStripTrailingSlash (hostPath.mutableRaw());
160
161 /* Check whether the path is full (absolute) */
162 char hostPathFull [RTPATH_MAX];
163 int vrc = RTPathAbsEx (NULL, hostPath,
164 hostPathFull, sizeof (hostPathFull));
165 if (VBOX_FAILURE (vrc))
166 return setError (E_INVALIDARG,
167 tr ("Invalid shared folder path: '%s' (%Vrc)"), hostPath.raw(), vrc);
168
169 if (RTPathCompare (hostPath, hostPathFull) != 0)
170 return setError (E_INVALIDARG,
171 tr ("Shared folder path '%s' is not absolute"), hostPath.raw());
172
173 mParent = aParent;
174 unconst (mName) = aName;
175 unconst (mHostPath) = hostPath;
176
177 mParent->addDependentChild (this);
178
179 setReady (true);
180 return S_OK;
181}
182
183/**
184 * Uninitializes the instance and sets the ready flag to FALSE.
185 * Called either from FinalRelease() or by the parent when it gets destroyed.
186 */
187void SharedFolder::uninit()
188{
189 LogFlowMember (("SharedFolder::uninit()\n"));
190
191 AutoLock alock (this);
192
193 LogFlowMember (("SharedFolder::uninit(): isReady=%d\n", isReady()));
194 if (!isReady())
195 return;
196
197 setReady (false);
198
199 alock.leave();
200 mParent->removeDependentChild (this);
201}
202
203// ISharedFolder properties
204/////////////////////////////////////////////////////////////////////////////
205
206STDMETHODIMP SharedFolder::COMGETTER(Name) (BSTR *aName)
207{
208 if (!aName)
209 return E_POINTER;
210
211 AutoLock alock (this);
212 CHECK_READY();
213
214 mName.cloneTo (aName);
215 return S_OK;
216}
217
218STDMETHODIMP SharedFolder::COMGETTER(HostPath) (BSTR *aHostPath)
219{
220 if (!aHostPath)
221 return E_POINTER;
222
223 AutoLock alock (this);
224 CHECK_READY();
225
226 mHostPath.cloneTo (aHostPath);
227 return S_OK;
228}
229
230STDMETHODIMP SharedFolder::COMGETTER(Accessible) (BOOL *aAccessible)
231{
232 if (!aAccessible)
233 return E_POINTER;
234
235 AutoLock alock (this);
236 CHECK_READY();
237
238 // check whether the host path exists
239 Utf8Str hostPath = Utf8Str (mHostPath);
240 char hostPathFull [RTPATH_MAX];
241 int vrc = RTPathExists(hostPath) ? RTPathReal (hostPath, hostPathFull, sizeof (hostPathFull))
242 : VERR_PATH_NOT_FOUND;
243 if (VBOX_SUCCESS (vrc))
244 {
245 *aAccessible = TRUE;
246 return S_OK;
247 }
248
249 HRESULT rc = S_OK;
250 if (vrc != VERR_PATH_NOT_FOUND)
251 rc = setError (E_FAIL,
252 tr ("Invalid shared folder path: '%s' (%Vrc)"), hostPath.raw(), vrc);
253
254 Log (("SharedFolder::COMGETTER(Accessible): WARNING: '%s' "
255 "is not accessible (%Vrc)\n", hostPath.raw(), vrc));
256
257 *aAccessible = FALSE;
258 return S_OK;
259}
260
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