VirtualBox

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

Last change on this file since 25275 was 25184, checked in by vboxsync, 15 years ago

Main: bring back r55600 with fix for broken machine creation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
RevLine 
[1]1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
[8155]7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
[1]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
[5999]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.
[8155]16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
[1]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>
[21079]30#include <iprt/cpputils.h>
[21077]31#include <iprt/path.h>
[1]32
33// constructor / destructor
34/////////////////////////////////////////////////////////////////////////////
35
[4040]36SharedFolder::SharedFolder()
37 : mParent (NULL)
38{
39}
40
41SharedFolder::~SharedFolder()
42{
43}
44
[1]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
[6384]64 * @param aWritable writable if true, readonly otherwise
[1]65 *
66 * @return COM result indicator
67 */
68HRESULT SharedFolder::init (Machine *aMachine,
[15051]69 CBSTR aName, CBSTR aHostPath, BOOL aWritable)
[1]70{
[4040]71 /* Enclose the state transition NotReady->InInit->Ready */
[21878]72 AutoInitSpan autoInitSpan(this);
73 AssertReturn(autoInitSpan.isOk(), E_FAIL);
[1]74
[21878]75 unconst(mMachine) = aMachine;
[4040]76
[23257]77 HRESULT rc = protectedInit(aMachine, aName, aHostPath, aWritable);
[4040]78
79 /* Confirm a successful initialization when it's the case */
[21878]80 if (SUCCEEDED(rc))
[4040]81 autoInitSpan.setSucceeded();
82
83 return rc;
[1]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
[4040]100 /* Enclose the state transition NotReady->InInit->Ready */
[21878]101 AutoInitSpan autoInitSpan(this);
102 AssertReturn(autoInitSpan.isOk(), E_FAIL);
[1]103
[21878]104 unconst(mMachine) = aMachine;
[4040]105
[16215]106 HRESULT rc = protectedInit (aMachine, aThat->m.name,
107 aThat->m.hostPath, aThat->m.writable);
[4040]108
109 /* Confirm a successful initialization when it's the case */
[21878]110 if (SUCCEEDED(rc))
[4040]111 autoInitSpan.setSucceeded();
112
113 return rc;
[1]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
[6384]122 * @param aWritable writable if true, readonly otherwise
[1]123 *
124 * @return COM result indicator
125 */
[23257]126HRESULT SharedFolder::init(Console *aConsole,
[15051]127 CBSTR aName, CBSTR aHostPath, BOOL aWritable)
[1]128{
[4040]129 /* Enclose the state transition NotReady->InInit->Ready */
[21878]130 AutoInitSpan autoInitSpan(this);
131 AssertReturn(autoInitSpan.isOk(), E_FAIL);
[1]132
[21878]133 unconst(mConsole) = aConsole;
[4040]134
[23257]135 HRESULT rc = protectedInit(aConsole, aName, aHostPath, aWritable);
[4040]136
137 /* Confirm a successful initialization when it's the case */
[21878]138 if (SUCCEEDED(rc))
[4040]139 autoInitSpan.setSucceeded();
140
141 return rc;
[1]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
[6384]150 * @param aWritable writable if true, readonly otherwise
[1]151 *
152 * @return COM result indicator
153 */
154HRESULT SharedFolder::init (VirtualBox *aVirtualBox,
[15051]155 CBSTR aName, CBSTR aHostPath, BOOL aWritable)
[1]156{
[4040]157 /* Enclose the state transition NotReady->InInit->Ready */
[21878]158 AutoInitSpan autoInitSpan(this);
159 AssertReturn(autoInitSpan.isOk(), E_FAIL);
[1]160
[21878]161 unconst(mVirtualBox) = aVirtualBox;
[4040]162
[25184]163 HRESULT rc = protectedInit(aVirtualBox, aName, aHostPath, aWritable);
[4040]164
165 /* Confirm a successful initialization when it's the case */
[21878]166 if (SUCCEEDED(rc))
[4040]167 autoInitSpan.setSucceeded();
168
169 return rc;
[1]170}
171
172/**
173 * Helper for init() methods.
174 *
175 * @note
176 * Must be called from under the object's lock!
177 */
[25184]178HRESULT SharedFolder::protectedInit(VirtualBoxBase *aParent,
179 CBSTR aName,
180 CBSTR aHostPath,
181 BOOL aWritable)
[1]182{
[21878]183 LogFlowThisFunc(("aName={%ls}, aHostPath={%ls}, aWritable={%d}\n",
[6384]184 aName, aHostPath, aWritable));
[1]185
186 ComAssertRet (aParent && aName && aHostPath, E_INVALIDARG);
187
188 Utf8Str hostPath = Utf8Str (aHostPath);
189 size_t hostPathLen = hostPath.length();
[3007]190
[14790]191 /* Remove the trailing slash unless it's a root directory
[1]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. */
[3668]197#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
[1]198 if (hostPathLen > 2 &&
199 RTPATH_IS_SEP (hostPath.raw()[hostPathLen - 1]) &&
200 RTPATH_IS_VOLSEP (hostPath.raw()[hostPathLen - 2]))
201 ;
[977]202#else
[21079]203 if (hostPathLen == 1 && RTPATH_IS_SEP(hostPath[0]))
[977]204 ;
205#endif
[1]206 else
[21079]207 hostPath.stripTrailingSlash();
[3007]208
[1]209 /* Check whether the path is full (absolute) */
210 char hostPathFull [RTPATH_MAX];
[22173]211 int vrc = RTPathAbsEx(NULL,
212 hostPath.c_str(),
213 hostPathFull,
214 sizeof (hostPathFull));
[21878]215 if (RT_FAILURE(vrc))
[1]216 return setError (E_INVALIDARG,
[13837]217 tr ("Invalid shared folder path: '%s' (%Rrc)"), hostPath.raw(), vrc);
[1]218
[22173]219 if (RTPathCompare(hostPath.c_str(), hostPathFull) != 0)
[1]220 return setError (E_INVALIDARG,
221 tr ("Shared folder path '%s' is not absolute"), hostPath.raw());
222
[21878]223 unconst(mParent) = aParent;
[1]224
[21878]225 unconst(m.name) = aName;
226 unconst(m.hostPath) = hostPath;
[16215]227 m.writable = aWritable;
[4040]228
[1]229 return S_OK;
230}
231
232/**
233 * Uninitializes the instance and sets the ready flag to FALSE.
234 * Called either from FinalRelease() or by the parent when it gets destroyed.
235 */
236void SharedFolder::uninit()
237{
[21878]238 LogFlowThisFunc(("\n"));
[1]239
[4040]240 /* Enclose the state transition Ready->InUninit->NotReady */
[21878]241 AutoUninitSpan autoUninitSpan(this);
[4040]242 if (autoUninitSpan.uninitDone())
[1]243 return;
244
[21878]245 unconst(mParent) = NULL;
[4040]246
[21878]247 unconst(mMachine).setNull();
248 unconst(mConsole).setNull();
249 unconst(mVirtualBox).setNull();
[1]250}
251
252// ISharedFolder properties
253/////////////////////////////////////////////////////////////////////////////
254
255STDMETHODIMP SharedFolder::COMGETTER(Name) (BSTR *aName)
256{
[14972]257 CheckComArgOutPointerValid(aName);
[1]258
[21878]259 AutoCaller autoCaller(this);
[25149]260 if (FAILED(autoCaller.rc())) return autoCaller.rc();
[1]261
[4040]262 /* mName is constant during life time, no need to lock */
[21878]263 m.name.cloneTo(aName);
[4040]264
[1]265 return S_OK;
266}
267
268STDMETHODIMP SharedFolder::COMGETTER(HostPath) (BSTR *aHostPath)
269{
[14972]270 CheckComArgOutPointerValid(aHostPath);
[1]271
[21878]272 AutoCaller autoCaller(this);
[25149]273 if (FAILED(autoCaller.rc())) return autoCaller.rc();
[1]274
[4040]275 /* mHostPath is constant during life time, no need to lock */
[21878]276 m.hostPath.cloneTo(aHostPath);
[4040]277
[1]278 return S_OK;
279}
280
281STDMETHODIMP SharedFolder::COMGETTER(Accessible) (BOOL *aAccessible)
282{
[21878]283 CheckComArgOutPointerValid(aAccessible);
[1]284
[21878]285 AutoCaller autoCaller(this);
[25149]286 if (FAILED(autoCaller.rc())) return autoCaller.rc();
[1]287
[4040]288 /* mName and mHostPath are constant during life time, no need to lock */
289
290 /* check whether the host path exists */
[22173]291 Utf8Str hostPath = Utf8Str(m.hostPath);
292 char hostPathFull[RTPATH_MAX];
293 int vrc = RTPathExists(hostPath.c_str()) ? RTPathReal(hostPath.c_str(),
294 hostPathFull,
295 sizeof(hostPathFull))
[15945]296 : VERR_PATH_NOT_FOUND;
[21878]297 if (RT_SUCCESS(vrc))
[1]298 {
299 *aAccessible = TRUE;
300 return S_OK;
301 }
302
[21878]303 AutoWriteLock alock(this);
[1]304
[16215]305 m.lastAccessError = BstrFmt (
306 tr ("'%s' is not accessible (%Rrc)"), hostPath.raw(), vrc);
307
[21878]308 LogWarningThisFunc(("m.lastAccessError=\"%ls\"\n", m.lastAccessError.raw()));
[16215]309
[1]310 *aAccessible = FALSE;
311 return S_OK;
312}
313
[6384]314STDMETHODIMP SharedFolder::COMGETTER(Writable) (BOOL *aWritable)
315{
[14972]316 CheckComArgOutPointerValid(aWritable);
[6384]317
[16215]318 *aWritable = m.writable;
[6384]319
320 return S_OK;
321}
[16215]322
323STDMETHODIMP SharedFolder::COMGETTER(LastAccessError) (BSTR *aLastAccessError)
324{
[21878]325 CheckComArgOutPointerValid(aLastAccessError);
[16215]326
[21878]327 AutoCaller autoCaller(this);
[25149]328 if (FAILED(autoCaller.rc())) return autoCaller.rc();
[16215]329
[21878]330 AutoReadLock alock(this);
[16215]331
[20977]332 if (m.lastAccessError.isEmpty())
333 Bstr("").cloneTo(aLastAccessError);
334 else
[21878]335 m.lastAccessError.cloneTo(aLastAccessError);
[16215]336
337 return S_OK;
338}
339
[14772]340/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use