VirtualBox

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

Last change on this file since 2705 was 2282, checked in by vboxsync, 18 years ago

check if the path really exists

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