VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/SharedFolderImpl.cpp@ 78582

Last change on this file since 78582 was 78090, checked in by vboxsync, 6 years ago

*,IPRT: Use new RTPathAbsExEx function instead of RTPathAbsEx. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1/* $Id: SharedFolderImpl.cpp 78090 2019-04-10 14:19:04Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2019 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#define LOG_GROUP LOG_GROUP_MAIN_SHAREDFOLDER
19#include "SharedFolderImpl.h"
20#if !defined(VBOX_COM_INPROC)
21# include "VirtualBoxImpl.h"
22# include "MachineImpl.h"
23#endif
24#include "ConsoleImpl.h"
25
26#include "AutoCaller.h"
27
28#include <iprt/param.h>
29#include <iprt/cpp/utils.h>
30#include <iprt/path.h>
31
32/////////////////////////////////////////////////////////////////////////////
33// SharedFolder::Data structure
34/////////////////////////////////////////////////////////////////////////////
35
36struct SharedFolder::Data
37{
38 Data()
39 : fWritable(false),
40 fAutoMount(false)
41 { }
42
43 const Utf8Str strName;
44 const Utf8Str strHostPath;
45 bool fWritable;
46 bool fAutoMount;
47 const Utf8Str strAutoMountPoint;
48 Utf8Str strLastAccessError;
49};
50
51// constructor / destructor
52/////////////////////////////////////////////////////////////////////////////
53
54SharedFolder::SharedFolder()
55 : mParent(NULL),
56#if !defined(VBOX_COM_INPROC)
57 mMachine(NULL),
58 mVirtualBox(NULL)
59#else
60 mConsole(NULL)
61#endif
62{
63 m = new Data;
64}
65
66SharedFolder::~SharedFolder()
67{
68 delete m;
69 m = NULL;
70}
71
72HRESULT SharedFolder::FinalConstruct()
73{
74 return BaseFinalConstruct();
75}
76
77void SharedFolder::FinalRelease()
78{
79 uninit();
80 BaseFinalRelease();
81}
82
83// public initializer/uninitializer for internal purposes only
84/////////////////////////////////////////////////////////////////////////////
85
86#if !defined(VBOX_COM_INPROC)
87/**
88 * Initializes the shared folder object.
89 *
90 * This variant initializes a machine instance that lives in the server address space.
91 *
92 * @param aMachine parent Machine object
93 * @param aName logical name of the shared folder
94 * @param aHostPath full path to the shared folder on the host
95 * @param aWritable writable if true, readonly otherwise
96 * @param aAutoMount if auto mounted by guest true, false otherwise
97 * @param aAutoMountPoint Where the guest should try auto mount it.
98 * @param fFailOnError Whether to fail with an error if the shared folder path is bad.
99 *
100 * @return COM result indicator
101 */
102HRESULT SharedFolder::init(Machine *aMachine,
103 const Utf8Str &aName,
104 const Utf8Str &aHostPath,
105 bool aWritable,
106 bool aAutoMount,
107 const Utf8Str &aAutoMountPoint,
108 bool fFailOnError)
109{
110 /* Enclose the state transition NotReady->InInit->Ready */
111 AutoInitSpan autoInitSpan(this);
112 AssertReturn(autoInitSpan.isOk(), E_FAIL);
113
114 unconst(mMachine) = aMachine;
115
116 HRESULT rc = i_protectedInit(aMachine, aName, aHostPath, aWritable, aAutoMount, aAutoMountPoint, fFailOnError);
117
118 /* Confirm a successful initialization when it's the case */
119 if (SUCCEEDED(rc))
120 autoInitSpan.setSucceeded();
121
122 return rc;
123}
124
125/**
126 * Initializes the shared folder object given another object
127 * (a kind of copy constructor). This object makes a private copy of data
128 * of the original object passed as an argument.
129 *
130 * @param aMachine parent Machine object
131 * @param aThat shared folder object to copy
132 *
133 * @return COM result indicator
134 */
135HRESULT SharedFolder::initCopy(Machine *aMachine, SharedFolder *aThat)
136{
137 ComAssertRet(aThat, E_INVALIDARG);
138
139 /* Enclose the state transition NotReady->InInit->Ready */
140 AutoInitSpan autoInitSpan(this);
141 AssertReturn(autoInitSpan.isOk(), E_FAIL);
142
143 unconst(mMachine) = aMachine;
144
145 HRESULT rc = i_protectedInit(aMachine,
146 aThat->m->strName,
147 aThat->m->strHostPath,
148 aThat->m->fWritable,
149 aThat->m->fAutoMount,
150 aThat->m->strAutoMountPoint,
151 false /* fFailOnError */ );
152
153 /* Confirm a successful initialization when it's the case */
154 if (SUCCEEDED(rc))
155 autoInitSpan.setSucceeded();
156
157 return rc;
158}
159
160# if 0
161
162/**
163 * Initializes the shared folder object.
164 *
165 * This variant initializes a global instance that lives in the server address space. It is not presently used.
166 *
167 * @param aVirtualBox VirtualBox parent object
168 * @param aName logical name of the shared folder
169 * @param aHostPath full path to the shared folder on the host
170 * @param aWritable writable if true, readonly otherwise
171 * @param aAutoMountPoint Where the guest should try auto mount it.
172 * @param fFailOnError Whether to fail with an error if the shared folder path is bad.
173 *
174 * @return COM result indicator
175 */
176HRESULT SharedFolder::init(VirtualBox *aVirtualBox,
177 const Utf8Str &aName,
178 const Utf8Str &aHostPath,
179 bool aWritable,
180 bool aAutoMount,
181 const Utf8Str &aAutoMountPoint
182 bool fFailOnError)
183{
184 /* Enclose the state transition NotReady->InInit->Ready */
185 AutoInitSpan autoInitSpan(this);
186 AssertReturn(autoInitSpan.isOk(), E_FAIL);
187
188 unconst(mVirtualBox) = aVirtualBox;
189
190 HRESULT rc = protectedInit(aVirtualBox, aName, aHostPath, aWritable, aAutoMount, aAutoMountPoint, fFailOnError);
191
192 /* Confirm a successful initialization when it's the case */
193 if (SUCCEEDED(rc))
194 autoInitSpan.setSucceeded();
195
196 return rc;
197}
198
199# endif
200
201#else
202
203/**
204 * Initializes the shared folder object.
205 *
206 * This variant initializes an instance that lives in the console address space.
207 *
208 * @param aConsole Console parent object
209 * @param aName logical name of the shared folder
210 * @param aHostPath full path to the shared folder on the host
211 * @param aWritable writable if true, readonly otherwise
212 * @param aAutoMountPoint Where the guest should try auto mount it.
213 * @param fFailOnError Whether to fail with an error if the shared folder path is bad.
214 *
215 * @return COM result indicator
216 */
217HRESULT SharedFolder::init(Console *aConsole,
218 const Utf8Str &aName,
219 const Utf8Str &aHostPath,
220 bool aWritable,
221 bool aAutoMount,
222 const Utf8Str &aAutoMountPoint,
223 bool fFailOnError)
224{
225 /* Enclose the state transition NotReady->InInit->Ready */
226 AutoInitSpan autoInitSpan(this);
227 AssertReturn(autoInitSpan.isOk(), E_FAIL);
228
229 unconst(mConsole) = aConsole;
230
231 HRESULT rc = i_protectedInit(aConsole, aName, aHostPath, aWritable, aAutoMount, aAutoMountPoint, fFailOnError);
232
233 /* Confirm a successful initialization when it's the case */
234 if (SUCCEEDED(rc))
235 autoInitSpan.setSucceeded();
236
237 return rc;
238}
239#endif
240
241/**
242 * Shared initialization code. Called from the other constructors.
243 *
244 * @note
245 * Must be called from under the object's lock!
246 */
247HRESULT SharedFolder::i_protectedInit(VirtualBoxBase *aParent,
248 const Utf8Str &aName,
249 const Utf8Str &aHostPath,
250 bool aWritable,
251 bool aAutoMount,
252 const Utf8Str &aAutoMountPoint,
253 bool fFailOnError)
254{
255 LogFlowThisFunc(("aName={%s}, aHostPath={%s}, aWritable={%d}, aAutoMount={%d}\n",
256 aName.c_str(), aHostPath.c_str(), aWritable, aAutoMount));
257
258 ComAssertRet(aParent && aName.isNotEmpty() && aHostPath.isNotEmpty(), E_INVALIDARG);
259
260 Utf8Str hostPath = aHostPath;
261 size_t hostPathLen = hostPath.length();
262
263 /* Remove the trailing slash unless it's a root directory
264 * (otherwise the comparison with the RTPathAbs() result will fail at least
265 * on Linux). Note that this isn't really necessary for the shared folder
266 * itself, since adding a mapping eventually results into a
267 * RTDirOpenFiltered() call (see HostServices/SharedFolders) that seems to
268 * accept both the slashified paths and not. */
269#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
270 if ( hostPathLen > 2
271 && RTPATH_IS_SEP(hostPath.c_str()[hostPathLen - 1])
272 && RTPATH_IS_VOLSEP(hostPath.c_str()[hostPathLen - 2]))
273 ;
274#else
275 if (hostPathLen == 1 && RTPATH_IS_SEP(hostPath[0]))
276 ;
277#endif
278 else
279 hostPath.stripTrailingSlash();
280
281 if (fFailOnError)
282 {
283 /* Check whether the path is full (absolute) */
284 char hostPathFull[RTPATH_MAX];
285 int vrc = RTPathAbs(hostPath.c_str(),
286 hostPathFull,
287 sizeof(hostPathFull));
288 if (RT_FAILURE(vrc))
289 return setErrorBoth(E_INVALIDARG, vrc, tr("Invalid shared folder path: '%s' (%Rrc)"), hostPath.c_str(), vrc);
290
291 if (RTPathCompare(hostPath.c_str(), hostPathFull) != 0)
292 return setError(E_INVALIDARG, tr("Shared folder path '%s' is not absolute"), hostPath.c_str());
293 }
294
295 unconst(mParent) = aParent;
296
297 unconst(m->strName) = aName;
298 unconst(m->strHostPath) = hostPath;
299 m->fWritable = aWritable;
300 m->fAutoMount = aAutoMount;
301 unconst(m->strAutoMountPoint) = aAutoMountPoint;
302
303 return S_OK;
304}
305
306/**
307 * Uninitializes the instance and sets the ready flag to FALSE.
308 * Called either from FinalRelease() or by the parent when it gets destroyed.
309 */
310void SharedFolder::uninit()
311{
312 LogFlowThisFunc(("\n"));
313
314 /* Enclose the state transition Ready->InUninit->NotReady */
315 AutoUninitSpan autoUninitSpan(this);
316 if (autoUninitSpan.uninitDone())
317 return;
318
319 unconst(mParent) = NULL;
320
321#if !defined(VBOX_COM_INPROC)
322 unconst(mMachine) = NULL;
323 unconst(mVirtualBox) = NULL;
324#else
325 unconst(mConsole) = NULL;
326#endif
327}
328
329// wrapped ISharedFolder properties
330/////////////////////////////////////////////////////////////////////////////
331HRESULT SharedFolder::getName(com::Utf8Str &aName)
332{
333 /* mName is constant during life time, no need to lock */
334 aName = m->strName;
335 return S_OK;
336}
337
338HRESULT SharedFolder::getHostPath(com::Utf8Str &aHostPath)
339{
340 /* mHostPath is constant during life time, no need to lock */
341 aHostPath = m->strHostPath;
342 return S_OK;
343}
344
345HRESULT SharedFolder::getAccessible(BOOL *aAccessible)
346{
347 /* mName and mHostPath are constant during life time, no need to lock */
348
349 /* check whether the host path exists */
350 Utf8Str hostPath = m->strHostPath;
351 char hostPathFull[RTPATH_MAX];
352 int vrc = RTPathExists(hostPath.c_str()) ? RTPathReal(hostPath.c_str(),
353 hostPathFull,
354 sizeof(hostPathFull))
355 : VERR_PATH_NOT_FOUND;
356 if (RT_SUCCESS(vrc))
357 {
358 *aAccessible = TRUE;
359 return S_OK;
360 }
361
362 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
363
364 m->strLastAccessError = Utf8StrFmt(tr("'%s' is not accessible (%Rrc)"),
365 m->strHostPath.c_str(),
366 vrc);
367
368 Log1WarningThisFunc(("m.lastAccessError=\"%s\"\n", m->strLastAccessError.c_str()));
369
370 *aAccessible = FALSE;
371
372 return S_OK;
373}
374
375HRESULT SharedFolder::getWritable(BOOL *aWritable)
376{
377 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
378 *aWritable = m->fWritable;
379 return S_OK;
380}
381
382HRESULT SharedFolder::setWritable(BOOL aWritable)
383{
384 RT_NOREF(aWritable);
385 return E_NOTIMPL;
386}
387
388HRESULT SharedFolder::getAutoMount(BOOL *aAutoMount)
389{
390 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
391 *aAutoMount = m->fAutoMount;
392 return S_OK;
393}
394
395HRESULT SharedFolder::setAutoMount(BOOL aAutoMount)
396{
397 RT_NOREF(aAutoMount);
398 return E_NOTIMPL;
399}
400
401HRESULT SharedFolder::getAutoMountPoint(com::Utf8Str &aAutoMountPoint)
402{
403 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
404 aAutoMountPoint = m->strAutoMountPoint;
405 return S_OK;
406}
407
408HRESULT SharedFolder::setAutoMountPoint(com::Utf8Str const &aAutoMountPoint)
409{
410 RT_NOREF(aAutoMountPoint);
411 return E_NOTIMPL;
412}
413
414HRESULT SharedFolder::getLastAccessError(com::Utf8Str &aLastAccessError)
415{
416 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
417 aLastAccessError = m->strLastAccessError;
418 return S_OK;
419}
420
421
422const Utf8Str& SharedFolder::i_getName() const
423{
424 return m->strName;
425}
426
427const Utf8Str& SharedFolder::i_getHostPath() const
428{
429 return m->strHostPath;
430}
431
432bool SharedFolder::i_isWritable() const
433{
434 return m->fWritable;
435}
436
437bool SharedFolder::i_isAutoMounted() const
438{
439 return m->fAutoMount;
440}
441
442const Utf8Str &SharedFolder::i_getAutoMountPoint() const
443{
444 return m->strAutoMountPoint;
445}
446
447/* 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