VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/ConsoleSharedFolderImpl.cpp

Last change on this file was 98342, checked in by vboxsync, 16 months ago

Main: bugref:4784: addition to r155569, added missing parameter documentation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/* $Id: ConsoleSharedFolderImpl.cpp 98342 2023-01-30 06:02:24Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#define LOG_GROUP LOG_GROUP_MAIN_SHAREDFOLDER
29#include "ConsoleSharedFolderImpl.h"
30#include "ConsoleImpl.h"
31
32#include "AutoCaller.h"
33
34#include <iprt/param.h>
35#include <iprt/cpp/utils.h>
36#include <iprt/path.h>
37
38/////////////////////////////////////////////////////////////////////////////
39// ConsoleSharedFolder::Data structure
40/////////////////////////////////////////////////////////////////////////////
41
42struct ConsoleSharedFolder::Data
43{
44 Data()
45 : fWritable(false),
46 fAutoMount(false)
47 { }
48
49 const Utf8Str strName;
50 const Utf8Str strHostPath;
51 bool fWritable;
52 bool fAutoMount;
53 const Utf8Str strAutoMountPoint;
54 Utf8Str strLastAccessError;
55};
56
57// constructor / destructor
58/////////////////////////////////////////////////////////////////////////////
59
60ConsoleSharedFolder::ConsoleSharedFolder()
61 : mParent(NULL),
62 mConsole(NULL)
63{
64 m = new Data;
65}
66
67ConsoleSharedFolder::~ConsoleSharedFolder()
68{
69 delete m;
70 m = NULL;
71}
72
73HRESULT ConsoleSharedFolder::FinalConstruct()
74{
75 return BaseFinalConstruct();
76}
77
78void ConsoleSharedFolder::FinalRelease()
79{
80 uninit();
81 BaseFinalRelease();
82}
83
84// public initializer/uninitializer for internal purposes only
85/////////////////////////////////////////////////////////////////////////////
86
87/**
88 * Initializes the shared folder object.
89 *
90 * This variant initializes an instance that lives in the console address space.
91 *
92 * @param aConsole Console parent 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 ConsoleSharedFolder::init(Console *aConsole,
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(mConsole) = aConsole;
115
116 HRESULT hrc = i_protectedInit(aConsole, aName, aHostPath, aWritable, aAutoMount, aAutoMountPoint, fFailOnError);
117
118 /* Confirm a successful initialization when it's the case */
119 if (SUCCEEDED(hrc))
120 autoInitSpan.setSucceeded();
121
122 return hrc;
123}
124
125/**
126 * Shared initialization code. Called from the other constructors.
127 *
128 * @note
129 * Must be called from under the object's lock!
130 */
131HRESULT ConsoleSharedFolder::i_protectedInit(VirtualBoxBase *aParent,
132 const Utf8Str &aName,
133 const Utf8Str &aHostPath,
134 bool aWritable,
135 bool aAutoMount,
136 const Utf8Str &aAutoMountPoint,
137 bool fFailOnError)
138{
139 LogFlowThisFunc(("aName={%s}, aHostPath={%s}, aWritable={%d}, aAutoMount={%d}\n",
140 aName.c_str(), aHostPath.c_str(), aWritable, aAutoMount));
141
142 ComAssertRet(aParent && aName.isNotEmpty() && aHostPath.isNotEmpty(), E_INVALIDARG);
143
144 Utf8Str hostPath = aHostPath;
145 size_t hostPathLen = hostPath.length();
146
147 /* Remove the trailing slash unless it's a root directory
148 * (otherwise the comparison with the RTPathAbs() result will fail at least
149 * on Linux). Note that this isn't really necessary for the shared folder
150 * itself, since adding a mapping eventually results into a
151 * RTDirOpenFiltered() call (see HostServices/SharedFolders) that seems to
152 * accept both the slashified paths and not. */
153#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
154 if ( hostPathLen > 2
155 && RTPATH_IS_SEP(hostPath.c_str()[hostPathLen - 1])
156 && RTPATH_IS_VOLSEP(hostPath.c_str()[hostPathLen - 2]))
157 ;
158#else
159 if (hostPathLen == 1 && RTPATH_IS_SEP(hostPath[0]))
160 ;
161#endif
162 else
163 hostPath.stripTrailingSlash();
164
165 if (fFailOnError)
166 {
167 /* Check whether the path is full (absolute) */
168 char hostPathFull[RTPATH_MAX];
169 int vrc = RTPathAbs(hostPath.c_str(),
170 hostPathFull,
171 sizeof(hostPathFull));
172 if (RT_FAILURE(vrc))
173 return setErrorBoth(E_INVALIDARG, vrc, tr("Invalid shared folder path: '%s' (%Rrc)"), hostPath.c_str(), vrc);
174
175 if (RTPathCompare(hostPath.c_str(), hostPathFull) != 0)
176 return setError(E_INVALIDARG, tr("Shared folder path '%s' is not absolute"), hostPath.c_str());
177
178 RTFSOBJINFO ObjInfo;
179 vrc = RTPathQueryInfoEx(hostPathFull, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
180 if (RT_FAILURE(vrc))
181 return setError(E_INVALIDARG, tr("RTPathQueryInfo failed on shared folder path '%s': %Rrc"), hostPathFull, vrc);
182
183 if (!RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode))
184 return setError(E_INVALIDARG, tr("Shared folder path '%s' is not a directory"), hostPathFull);
185 }
186
187 unconst(mParent) = aParent;
188
189 unconst(m->strName) = aName;
190 unconst(m->strHostPath) = hostPath;
191 m->fWritable = aWritable;
192 m->fAutoMount = aAutoMount;
193 unconst(m->strAutoMountPoint) = aAutoMountPoint;
194
195 return S_OK;
196}
197
198/**
199 * Uninitializes the instance and sets the ready flag to FALSE.
200 * Called either from FinalRelease() or by the parent when it gets destroyed.
201 */
202void ConsoleSharedFolder::uninit()
203{
204 LogFlowThisFunc(("\n"));
205
206 /* Enclose the state transition Ready->InUninit->NotReady */
207 AutoUninitSpan autoUninitSpan(this);
208 if (autoUninitSpan.uninitDone())
209 return;
210
211 unconst(mParent) = NULL;
212 unconst(mConsole) = NULL;
213}
214
215// wrapped ISharedFolder properties
216/////////////////////////////////////////////////////////////////////////////
217HRESULT ConsoleSharedFolder::getName(com::Utf8Str &aName)
218{
219 /* mName is constant during life time, no need to lock */
220 aName = m->strName;
221 return S_OK;
222}
223
224HRESULT ConsoleSharedFolder::getHostPath(com::Utf8Str &aHostPath)
225{
226 /* mHostPath is constant during life time, no need to lock */
227 aHostPath = m->strHostPath;
228 return S_OK;
229}
230
231HRESULT ConsoleSharedFolder::getAccessible(BOOL *aAccessible)
232{
233 /* mName and mHostPath are constant during life time, no need to lock */
234
235 /* check whether the host path exists */
236 Utf8Str hostPath = m->strHostPath;
237 char hostPathFull[RTPATH_MAX];
238 int vrc = RTPathExists(hostPath.c_str()) ? RTPathReal(hostPath.c_str(),
239 hostPathFull,
240 sizeof(hostPathFull))
241 : VERR_PATH_NOT_FOUND;
242 if (RT_SUCCESS(vrc))
243 {
244 *aAccessible = TRUE;
245 return S_OK;
246 }
247
248 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
249
250 m->strLastAccessError = Utf8StrFmt(tr("'%s' is not accessible (%Rrc)"),
251 m->strHostPath.c_str(),
252 vrc);
253
254 Log1WarningThisFunc(("m.lastAccessError=\"%s\"\n", m->strLastAccessError.c_str()));
255
256 *aAccessible = FALSE;
257
258 return S_OK;
259}
260
261HRESULT ConsoleSharedFolder::getWritable(BOOL *aWritable)
262{
263 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
264 *aWritable = m->fWritable;
265 return S_OK;
266}
267
268HRESULT ConsoleSharedFolder::setWritable(BOOL aWritable)
269{
270 RT_NOREF(aWritable);
271 return E_NOTIMPL;
272}
273
274HRESULT ConsoleSharedFolder::getAutoMount(BOOL *aAutoMount)
275{
276 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
277 *aAutoMount = m->fAutoMount;
278 return S_OK;
279}
280
281HRESULT ConsoleSharedFolder::setAutoMount(BOOL aAutoMount)
282{
283 RT_NOREF(aAutoMount);
284 return E_NOTIMPL;
285}
286
287HRESULT ConsoleSharedFolder::getAutoMountPoint(com::Utf8Str &aAutoMountPoint)
288{
289 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
290 aAutoMountPoint = m->strAutoMountPoint;
291 return S_OK;
292}
293
294HRESULT ConsoleSharedFolder::setAutoMountPoint(com::Utf8Str const &aAutoMountPoint)
295{
296 RT_NOREF(aAutoMountPoint);
297 return E_NOTIMPL;
298}
299
300HRESULT ConsoleSharedFolder::getLastAccessError(com::Utf8Str &aLastAccessError)
301{
302 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
303 aLastAccessError = m->strLastAccessError;
304 return S_OK;
305}
306
307
308const Utf8Str& ConsoleSharedFolder::i_getName() const
309{
310 return m->strName;
311}
312
313const Utf8Str& ConsoleSharedFolder::i_getHostPath() const
314{
315 return m->strHostPath;
316}
317
318bool ConsoleSharedFolder::i_isWritable() const
319{
320 return m->fWritable;
321}
322
323bool ConsoleSharedFolder::i_isAutoMounted() const
324{
325 return m->fAutoMount;
326}
327
328const Utf8Str &ConsoleSharedFolder::i_getAutoMountPoint() const
329{
330 return m->strAutoMountPoint;
331}
332
333/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use