[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>
|
---|
| 30 | #include <iprt/path.h>
|
---|
[3007] | 31 | #include <iprt/cpputils.h>
|
---|
[1] | 32 |
|
---|
| 33 | // constructor / destructor
|
---|
| 34 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 35 |
|
---|
[4040] | 36 | SharedFolder::SharedFolder()
|
---|
| 37 | : mParent (NULL)
|
---|
| 38 | {
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | SharedFolder::~SharedFolder()
|
---|
| 42 | {
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[1] | 45 | HRESULT SharedFolder::FinalConstruct()
|
---|
| 46 | {
|
---|
| 47 | return S_OK;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | void 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 | */
|
---|
| 68 | HRESULT SharedFolder::init (Machine *aMachine,
|
---|
[6388] | 69 | const BSTR aName, const BSTR aHostPath, BOOL aWritable)
|
---|
[1] | 70 | {
|
---|
[4040] | 71 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
| 72 | AutoInitSpan autoInitSpan (this);
|
---|
| 73 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
[1] | 74 |
|
---|
[4040] | 75 | unconst (mMachine) = aMachine;
|
---|
| 76 |
|
---|
[6384] | 77 | HRESULT rc = protectedInit (aMachine, aName, aHostPath, aWritable);
|
---|
[4040] | 78 |
|
---|
| 79 | /* Confirm a successful initialization when it's the case */
|
---|
| 80 | if (SUCCEEDED (rc))
|
---|
| 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 | */
|
---|
| 96 | HRESULT SharedFolder::initCopy (Machine *aMachine, SharedFolder *aThat)
|
---|
| 97 | {
|
---|
| 98 | ComAssertRet (aThat, E_INVALIDARG);
|
---|
| 99 |
|
---|
[4040] | 100 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
| 101 | AutoInitSpan autoInitSpan (this);
|
---|
| 102 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
[1] | 103 |
|
---|
[4040] | 104 | unconst (mMachine) = aMachine;
|
---|
| 105 |
|
---|
| 106 | HRESULT rc = protectedInit (aMachine, aThat->mData.mName,
|
---|
[6384] | 107 | aThat->mData.mHostPath, aThat->mData.mWritable);
|
---|
[4040] | 108 |
|
---|
| 109 | /* Confirm a successful initialization when it's the case */
|
---|
| 110 | if (SUCCEEDED (rc))
|
---|
| 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 | */
|
---|
| 126 | HRESULT SharedFolder::init (Console *aConsole,
|
---|
[6388] | 127 | const BSTR aName, const BSTR aHostPath, BOOL aWritable)
|
---|
[1] | 128 | {
|
---|
[4040] | 129 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
| 130 | AutoInitSpan autoInitSpan (this);
|
---|
| 131 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
[1] | 132 |
|
---|
[4040] | 133 | unconst (mConsole) = aConsole;
|
---|
| 134 |
|
---|
[6384] | 135 | HRESULT rc = protectedInit (aConsole, aName, aHostPath, aWritable);
|
---|
[4040] | 136 |
|
---|
| 137 | /* Confirm a successful initialization when it's the case */
|
---|
| 138 | if (SUCCEEDED (rc))
|
---|
| 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 | */
|
---|
| 154 | HRESULT SharedFolder::init (VirtualBox *aVirtualBox,
|
---|
[6388] | 155 | const BSTR aName, const BSTR aHostPath, BOOL aWritable)
|
---|
[1] | 156 | {
|
---|
[4040] | 157 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
| 158 | AutoInitSpan autoInitSpan (this);
|
---|
| 159 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
[1] | 160 |
|
---|
[4040] | 161 | unconst (mVirtualBox) = aVirtualBox;
|
---|
| 162 |
|
---|
[6384] | 163 | HRESULT rc = protectedInit (aVirtualBox, aName, aHostPath, aWritable);
|
---|
[4040] | 164 |
|
---|
| 165 | /* Confirm a successful initialization when it's the case */
|
---|
| 166 | if (SUCCEEDED (rc))
|
---|
| 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 | */
|
---|
[4040] | 178 | HRESULT SharedFolder::protectedInit (VirtualBoxBaseWithChildrenNEXT *aParent,
|
---|
[6388] | 179 | const BSTR aName, const BSTR aHostPath, BOOL aWritable)
|
---|
[1] | 180 | {
|
---|
[6384] | 181 | LogFlowThisFunc (("aName={%ls}, aHostPath={%ls}, aWritable={%d}\n",
|
---|
| 182 | aName, aHostPath, aWritable));
|
---|
[1] | 183 |
|
---|
| 184 | ComAssertRet (aParent && aName && aHostPath, E_INVALIDARG);
|
---|
| 185 |
|
---|
| 186 | Utf8Str hostPath = Utf8Str (aHostPath);
|
---|
| 187 | size_t hostPathLen = hostPath.length();
|
---|
[3007] | 188 |
|
---|
[977] | 189 | /* Remove the trailng slash unless it's a root directory
|
---|
[1] | 190 | * (otherwise the comparison with the RTPathAbs() result will fail at least
|
---|
| 191 | * on Linux). Note that this isn't really necessary for the shared folder
|
---|
| 192 | * itself, since adding a mapping eventually results into a
|
---|
| 193 | * RTDirOpenFiltered() call (see HostServices/SharedFolders) that seems to
|
---|
| 194 | * accept both the slashified paths and not. */
|
---|
[3668] | 195 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
[1] | 196 | if (hostPathLen > 2 &&
|
---|
| 197 | RTPATH_IS_SEP (hostPath.raw()[hostPathLen - 1]) &&
|
---|
| 198 | RTPATH_IS_VOLSEP (hostPath.raw()[hostPathLen - 2]))
|
---|
| 199 | ;
|
---|
[977] | 200 | #else
|
---|
| 201 | if (hostPathLen == 1 && RTPATH_IS_SEP (hostPath[0]))
|
---|
| 202 | ;
|
---|
| 203 | #endif
|
---|
[1] | 204 | else
|
---|
| 205 | RTPathStripTrailingSlash (hostPath.mutableRaw());
|
---|
[3007] | 206 |
|
---|
[1] | 207 | /* Check whether the path is full (absolute) */
|
---|
| 208 | char hostPathFull [RTPATH_MAX];
|
---|
| 209 | int vrc = RTPathAbsEx (NULL, hostPath,
|
---|
| 210 | hostPathFull, sizeof (hostPathFull));
|
---|
| 211 | if (VBOX_FAILURE (vrc))
|
---|
| 212 | return setError (E_INVALIDARG,
|
---|
| 213 | tr ("Invalid shared folder path: '%s' (%Vrc)"), hostPath.raw(), vrc);
|
---|
| 214 |
|
---|
| 215 | if (RTPathCompare (hostPath, hostPathFull) != 0)
|
---|
| 216 | return setError (E_INVALIDARG,
|
---|
| 217 | tr ("Shared folder path '%s' is not absolute"), hostPath.raw());
|
---|
| 218 |
|
---|
[4040] | 219 | unconst (mParent) = aParent;
|
---|
[1] | 220 |
|
---|
[4040] | 221 | /* register with parent */
|
---|
[1] | 222 | mParent->addDependentChild (this);
|
---|
| 223 |
|
---|
[4040] | 224 | unconst (mData.mName) = aName;
|
---|
| 225 | unconst (mData.mHostPath) = hostPath;
|
---|
[6384] | 226 | mData.mWritable = aWritable;
|
---|
[4040] | 227 |
|
---|
[1] | 228 | return S_OK;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
| 233 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
| 234 | */
|
---|
| 235 | void SharedFolder::uninit()
|
---|
| 236 | {
|
---|
[4040] | 237 | LogFlowThisFunc (("\n"));
|
---|
[1] | 238 |
|
---|
[4040] | 239 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
| 240 | AutoUninitSpan autoUninitSpan (this);
|
---|
| 241 | if (autoUninitSpan.uninitDone())
|
---|
[1] | 242 | return;
|
---|
| 243 |
|
---|
[4040] | 244 | if (mParent)
|
---|
| 245 | mParent->removeDependentChild (this);
|
---|
[1] | 246 |
|
---|
[4040] | 247 | unconst (mParent) = NULL;
|
---|
| 248 |
|
---|
| 249 | unconst (mMachine).setNull();
|
---|
| 250 | unconst (mConsole).setNull();
|
---|
| 251 | unconst (mVirtualBox).setNull();
|
---|
[1] | 252 | }
|
---|
| 253 |
|
---|
| 254 | // ISharedFolder properties
|
---|
| 255 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 256 |
|
---|
| 257 | STDMETHODIMP SharedFolder::COMGETTER(Name) (BSTR *aName)
|
---|
| 258 | {
|
---|
| 259 | if (!aName)
|
---|
| 260 | return E_POINTER;
|
---|
| 261 |
|
---|
[4040] | 262 | AutoCaller autoCaller (this);
|
---|
| 263 | CheckComRCReturnRC (autoCaller.rc());
|
---|
[1] | 264 |
|
---|
[4040] | 265 | /* mName is constant during life time, no need to lock */
|
---|
| 266 | mData.mName.cloneTo (aName);
|
---|
| 267 |
|
---|
[1] | 268 | return S_OK;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | STDMETHODIMP SharedFolder::COMGETTER(HostPath) (BSTR *aHostPath)
|
---|
| 272 | {
|
---|
| 273 | if (!aHostPath)
|
---|
| 274 | return E_POINTER;
|
---|
| 275 |
|
---|
[4040] | 276 | AutoCaller autoCaller (this);
|
---|
| 277 | CheckComRCReturnRC (autoCaller.rc());
|
---|
[1] | 278 |
|
---|
[4040] | 279 | /* mHostPath is constant during life time, no need to lock */
|
---|
| 280 | mData.mHostPath.cloneTo (aHostPath);
|
---|
| 281 |
|
---|
[1] | 282 | return S_OK;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | STDMETHODIMP SharedFolder::COMGETTER(Accessible) (BOOL *aAccessible)
|
---|
| 286 | {
|
---|
| 287 | if (!aAccessible)
|
---|
| 288 | return E_POINTER;
|
---|
| 289 |
|
---|
[4040] | 290 | AutoCaller autoCaller (this);
|
---|
| 291 | CheckComRCReturnRC (autoCaller.rc());
|
---|
[1] | 292 |
|
---|
[4040] | 293 | /* mName and mHostPath are constant during life time, no need to lock */
|
---|
| 294 |
|
---|
| 295 | /* check whether the host path exists */
|
---|
| 296 | Utf8Str hostPath = Utf8Str (mData.mHostPath);
|
---|
[1] | 297 | char hostPathFull [RTPATH_MAX];
|
---|
[4040] | 298 | int vrc = RTPathExists(hostPath) ? RTPathReal (hostPath, hostPathFull,
|
---|
| 299 | sizeof (hostPathFull))
|
---|
[2282] | 300 | : VERR_PATH_NOT_FOUND;
|
---|
[1] | 301 | if (VBOX_SUCCESS (vrc))
|
---|
| 302 | {
|
---|
| 303 | *aAccessible = TRUE;
|
---|
| 304 | return S_OK;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | HRESULT rc = S_OK;
|
---|
| 308 | if (vrc != VERR_PATH_NOT_FOUND)
|
---|
| 309 | rc = setError (E_FAIL,
|
---|
| 310 | tr ("Invalid shared folder path: '%s' (%Vrc)"), hostPath.raw(), vrc);
|
---|
| 311 |
|
---|
[4040] | 312 | LogWarningThisFunc (("'%s' is not accessible (%Vrc)\n", hostPath.raw(), vrc));
|
---|
[1] | 313 |
|
---|
| 314 | *aAccessible = FALSE;
|
---|
| 315 | return S_OK;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[6384] | 318 | STDMETHODIMP SharedFolder::COMGETTER(Writable) (BOOL *aWritable)
|
---|
| 319 | {
|
---|
| 320 | if (!aWritable)
|
---|
| 321 | return E_POINTER;
|
---|
| 322 |
|
---|
| 323 | *aWritable = mData.mWritable;
|
---|
| 324 |
|
---|
| 325 | return S_OK;
|
---|
| 326 | }
|
---|