VirtualBox

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

Last change on this file since 662 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 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 LogFlowMember (("SharedFolder::protectedInit(): aName={%ls}, aHostPath={%ls}\n",
136 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 path unless it's a DOS-like 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 (hostPathLen > 2 &&
150 RTPATH_IS_SEP (hostPath.raw()[hostPathLen - 1]) &&
151 RTPATH_IS_VOLSEP (hostPath.raw()[hostPathLen - 2]))
152 ;
153 else
154 RTPathStripTrailingSlash (hostPath.mutableRaw());
155
156 /* Check whether the path is full (absolute) */
157 char hostPathFull [RTPATH_MAX];
158 int vrc = RTPathAbsEx (NULL, hostPath,
159 hostPathFull, sizeof (hostPathFull));
160 if (VBOX_FAILURE (vrc))
161 return setError (E_INVALIDARG,
162 tr ("Invalid shared folder path: '%s' (%Vrc)"), hostPath.raw(), vrc);
163
164 if (RTPathCompare (hostPath, hostPathFull) != 0)
165 return setError (E_INVALIDARG,
166 tr ("Shared folder path '%s' is not absolute"), hostPath.raw());
167
168 mParent = aParent;
169 unconst (mName) = aName;
170 unconst (mHostPath) = hostPath;
171
172 mParent->addDependentChild (this);
173
174 setReady (true);
175 return S_OK;
176}
177
178/**
179 * Uninitializes the instance and sets the ready flag to FALSE.
180 * Called either from FinalRelease() or by the parent when it gets destroyed.
181 */
182void SharedFolder::uninit()
183{
184 LogFlowMember (("SharedFolder::uninit()\n"));
185
186 AutoLock alock (this);
187
188 LogFlowMember (("SharedFolder::uninit(): isReady=%d\n", isReady()));
189 if (!isReady())
190 return;
191
192 setReady (false);
193
194 alock.leave();
195 mParent->removeDependentChild (this);
196}
197
198// ISharedFolder properties
199/////////////////////////////////////////////////////////////////////////////
200
201STDMETHODIMP SharedFolder::COMGETTER(Name) (BSTR *aName)
202{
203 if (!aName)
204 return E_POINTER;
205
206 AutoLock alock (this);
207 CHECK_READY();
208
209 mName.cloneTo (aName);
210 return S_OK;
211}
212
213STDMETHODIMP SharedFolder::COMGETTER(HostPath) (BSTR *aHostPath)
214{
215 if (!aHostPath)
216 return E_POINTER;
217
218 AutoLock alock (this);
219 CHECK_READY();
220
221 mHostPath.cloneTo (aHostPath);
222 return S_OK;
223}
224
225STDMETHODIMP SharedFolder::COMGETTER(Accessible) (BOOL *aAccessible)
226{
227 if (!aAccessible)
228 return E_POINTER;
229
230 AutoLock alock (this);
231 CHECK_READY();
232
233 // check whether the host path exists
234 Utf8Str hostPath = Utf8Str (mHostPath);
235 char hostPathFull [RTPATH_MAX];
236 int vrc = RTPathReal (hostPath, hostPathFull, sizeof (hostPathFull));
237 if (VBOX_SUCCESS (vrc))
238 {
239 *aAccessible = TRUE;
240 return S_OK;
241 }
242
243 HRESULT rc = S_OK;
244 if (vrc != VERR_PATH_NOT_FOUND)
245 rc = setError (E_FAIL,
246 tr ("Invalid shared folder path: '%s' (%Vrc)"), hostPath.raw(), vrc);
247
248 Log (("SharedFolder::COMGETTER(Accessible): WARNING: '%s' "
249 "is not accessible (%Vrc)\n", hostPath.raw(), vrc));
250
251 *aAccessible = FALSE;
252 return S_OK;
253}
254
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