VirtualBox

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

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

Main: Don't use Logging.h.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1/* $Id: SharedFolderImpl.cpp 76592 2019-01-01 20:13:07Z 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 = RTPathAbsEx(NULL,
286 hostPath.c_str(),
287 hostPathFull,
288 sizeof (hostPathFull));
289 if (RT_FAILURE(vrc))
290 return setErrorBoth(E_INVALIDARG, vrc, tr("Invalid shared folder path: '%s' (%Rrc)"), hostPath.c_str(), vrc);
291
292 if (RTPathCompare(hostPath.c_str(), hostPathFull) != 0)
293 return setError(E_INVALIDARG, tr("Shared folder path '%s' is not absolute"), hostPath.c_str());
294 }
295
296 unconst(mParent) = aParent;
297
298 unconst(m->strName) = aName;
299 unconst(m->strHostPath) = hostPath;
300 m->fWritable = aWritable;
301 m->fAutoMount = aAutoMount;
302 unconst(m->strAutoMountPoint) = aAutoMountPoint;
303
304 return S_OK;
305}
306
307/**
308 * Uninitializes the instance and sets the ready flag to FALSE.
309 * Called either from FinalRelease() or by the parent when it gets destroyed.
310 */
311void SharedFolder::uninit()
312{
313 LogFlowThisFunc(("\n"));
314
315 /* Enclose the state transition Ready->InUninit->NotReady */
316 AutoUninitSpan autoUninitSpan(this);
317 if (autoUninitSpan.uninitDone())
318 return;
319
320 unconst(mParent) = NULL;
321
322#if !defined(VBOX_COM_INPROC)
323 unconst(mMachine) = NULL;
324 unconst(mVirtualBox) = NULL;
325#else
326 unconst(mConsole) = NULL;
327#endif
328}
329
330// wrapped ISharedFolder properties
331/////////////////////////////////////////////////////////////////////////////
332HRESULT SharedFolder::getName(com::Utf8Str &aName)
333{
334 /* mName is constant during life time, no need to lock */
335 aName = m->strName;
336 return S_OK;
337}
338
339HRESULT SharedFolder::getHostPath(com::Utf8Str &aHostPath)
340{
341 /* mHostPath is constant during life time, no need to lock */
342 aHostPath = m->strHostPath;
343 return S_OK;
344}
345
346HRESULT SharedFolder::getAccessible(BOOL *aAccessible)
347{
348 /* mName and mHostPath are constant during life time, no need to lock */
349
350 /* check whether the host path exists */
351 Utf8Str hostPath = m->strHostPath;
352 char hostPathFull[RTPATH_MAX];
353 int vrc = RTPathExists(hostPath.c_str()) ? RTPathReal(hostPath.c_str(),
354 hostPathFull,
355 sizeof(hostPathFull))
356 : VERR_PATH_NOT_FOUND;
357 if (RT_SUCCESS(vrc))
358 {
359 *aAccessible = TRUE;
360 return S_OK;
361 }
362
363 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
364
365 m->strLastAccessError = Utf8StrFmt(tr("'%s' is not accessible (%Rrc)"),
366 m->strHostPath.c_str(),
367 vrc);
368
369 Log1WarningThisFunc(("m.lastAccessError=\"%s\"\n", m->strLastAccessError.c_str()));
370
371 *aAccessible = FALSE;
372
373 return S_OK;
374}
375
376HRESULT SharedFolder::getWritable(BOOL *aWritable)
377{
378 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
379 *aWritable = m->fWritable;
380 return S_OK;
381}
382
383HRESULT SharedFolder::setWritable(BOOL aWritable)
384{
385 RT_NOREF(aWritable);
386 return E_NOTIMPL;
387}
388
389HRESULT SharedFolder::getAutoMount(BOOL *aAutoMount)
390{
391 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
392 *aAutoMount = m->fAutoMount;
393 return S_OK;
394}
395
396HRESULT SharedFolder::setAutoMount(BOOL aAutoMount)
397{
398 RT_NOREF(aAutoMount);
399 return E_NOTIMPL;
400}
401
402HRESULT SharedFolder::getAutoMountPoint(com::Utf8Str &aAutoMountPoint)
403{
404 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
405 aAutoMountPoint = m->strAutoMountPoint;
406 return S_OK;
407}
408
409HRESULT SharedFolder::setAutoMountPoint(com::Utf8Str const &aAutoMountPoint)
410{
411 RT_NOREF(aAutoMountPoint);
412 return E_NOTIMPL;
413}
414
415HRESULT SharedFolder::getLastAccessError(com::Utf8Str &aLastAccessError)
416{
417 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
418 aLastAccessError = m->strLastAccessError;
419 return S_OK;
420}
421
422
423const Utf8Str& SharedFolder::i_getName() const
424{
425 return m->strName;
426}
427
428const Utf8Str& SharedFolder::i_getHostPath() const
429{
430 return m->strHostPath;
431}
432
433bool SharedFolder::i_isWritable() const
434{
435 return m->fWritable;
436}
437
438bool SharedFolder::i_isAutoMounted() const
439{
440 return m->fAutoMount;
441}
442
443const Utf8Str &SharedFolder::i_getAutoMountPoint() const
444{
445 return m->strAutoMountPoint;
446}
447
448/* 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