VirtualBox

source: vbox/trunk/src/VBox/Main/DVDDriveImpl.cpp@ 15708

Last change on this file since 15708 was 15661, checked in by vboxsync, 16 years ago

#3285: Improve error handling API to include unique error numbers
Document

  • IDVDDrive::mountImage
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/* $Id: DVDDriveImpl.cpp 15661 2008-12-18 14:21:08Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include "DVDDriveImpl.h"
25
26#include "MachineImpl.h"
27#include "HostImpl.h"
28#include "HostDVDDriveImpl.h"
29#include "VirtualBoxImpl.h"
30
31#include "Logging.h"
32
33#include <iprt/string.h>
34#include <iprt/cpputils.h>
35
36// constructor / destructor
37////////////////////////////////////////////////////////////////////////////////
38
39DEFINE_EMPTY_CTOR_DTOR (DVDDrive)
40
41HRESULT DVDDrive::FinalConstruct()
42{
43 return S_OK;
44}
45
46void DVDDrive::FinalRelease()
47{
48 uninit();
49}
50
51// public initializer/uninitializer for internal purposes only
52////////////////////////////////////////////////////////////////////////////////
53
54/**
55 * Initializes the DVD drive object.
56 *
57 * @param aParent Handle of the parent object.
58 */
59HRESULT DVDDrive::init (Machine *aParent)
60{
61 LogFlowThisFunc (("aParent=%p\n", aParent));
62
63 ComAssertRet (aParent, E_INVALIDARG);
64
65 /* Enclose the state transition NotReady->InInit->Ready */
66 AutoInitSpan autoInitSpan (this);
67 AssertReturn (autoInitSpan.isOk(), E_FAIL);
68
69 unconst (mParent) = aParent;
70 /* mPeer is left null */
71
72 mData.allocate();
73
74 /* Confirm a successful initialization */
75 autoInitSpan.setSucceeded();
76
77 return S_OK;
78}
79
80/**
81 * Initializes the DVD drive object given another DVD drive object
82 * (a kind of copy constructor). This object shares data with
83 * the object passed as an argument.
84 *
85 * @note This object must be destroyed before the original object
86 * it shares data with is destroyed.
87 *
88 * @note Locks @a aThat object for reading.
89 */
90HRESULT DVDDrive::init (Machine *aParent, DVDDrive *aThat)
91{
92 LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
93
94 ComAssertRet (aParent && aThat, E_INVALIDARG);
95
96 /* Enclose the state transition NotReady->InInit->Ready */
97 AutoInitSpan autoInitSpan (this);
98 AssertReturn (autoInitSpan.isOk(), E_FAIL);
99
100 unconst (mParent) = aParent;
101 unconst (mPeer) = aThat;
102
103 AutoCaller thatCaller (aThat);
104 AssertComRCReturnRC (thatCaller.rc());
105
106 AutoReadLock thatLock (aThat);
107 mData.share (aThat->mData);
108
109 /* Confirm a successful initialization */
110 autoInitSpan.setSucceeded();
111
112 return S_OK;
113}
114
115/**
116 * Initializes the DVD drive object given another DVD drive object
117 * (a kind of copy constructor). This object makes a private copy of data
118 * of the original object passed as an argument.
119 *
120 * @note Locks @a aThat object for reading.
121 */
122HRESULT DVDDrive::initCopy (Machine *aParent, DVDDrive *aThat)
123{
124 LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
125
126 ComAssertRet (aParent && aThat, E_INVALIDARG);
127
128 /* Enclose the state transition NotReady->InInit->Ready */
129 AutoInitSpan autoInitSpan (this);
130 AssertReturn (autoInitSpan.isOk(), E_FAIL);
131
132 unconst (mParent) = aParent;
133 /* mPeer is left null */
134
135 AutoCaller thatCaller (aThat);
136 AssertComRCReturnRC (thatCaller.rc());
137
138 AutoReadLock thatLock (aThat);
139 mData.attachCopy (aThat->mData);
140
141 /* at present, this must be a snapshot machine */
142 Assert (!aParent->snapshotId().isEmpty());
143
144 if (mData->mState == DriveState_ImageMounted)
145 {
146 /* associate the DVD image media with the snapshot */
147 HRESULT rc = mData->mImage->attachTo (aParent->id(),
148 aParent->snapshotId());
149 AssertComRC (rc);
150 }
151
152 /* Confirm a successful initialization */
153 autoInitSpan.setSucceeded();
154
155 return S_OK;
156}
157
158/**
159 * Uninitializes the instance and sets the ready flag to FALSE.
160 * Called either from FinalRelease() or by the parent when it gets destroyed.
161 */
162void DVDDrive::uninit()
163{
164 LogFlowThisFunc (("\n"));
165
166 /* Enclose the state transition Ready->InUninit->NotReady */
167 AutoUninitSpan autoUninitSpan (this);
168 if (autoUninitSpan.uninitDone())
169 return;
170
171 if ((mParent->type() == Machine::IsMachine ||
172 mParent->type() == Machine::IsSnapshotMachine) &&
173 mData->mState == DriveState_ImageMounted)
174 {
175 /* Deassociate the DVD image (only when mParent is a real Machine or a
176 * SnapshotMachine instance; SessionMachine instances
177 * refer to real Machine hard disks). This is necessary for a clean
178 * re-initialization of the VM after successfully re-checking the
179 * accessibility state. */
180 HRESULT rc = mData->mImage->detachFrom (mParent->id(),
181 mParent->snapshotId());
182 AssertComRC (rc);
183 }
184
185 mData.free();
186
187 unconst (mPeer).setNull();
188 unconst (mParent).setNull();
189}
190
191// IDVDDrive properties
192////////////////////////////////////////////////////////////////////////////////
193
194STDMETHODIMP DVDDrive::COMGETTER(State) (DriveState_T *aState)
195{
196 CheckComArgOutPointerValid(aState);
197
198 AutoCaller autoCaller (this);
199 CheckComRCReturnRC (autoCaller.rc());
200
201 AutoReadLock alock (this);
202
203 *aState = mData->mState;
204
205 return S_OK;
206}
207
208STDMETHODIMP DVDDrive::COMGETTER(Passthrough) (BOOL *aPassthrough)
209{
210 CheckComArgOutPointerValid(aPassthrough);
211
212 AutoCaller autoCaller (this);
213 CheckComRCReturnRC (autoCaller.rc());
214
215 AutoReadLock alock (this);
216
217 *aPassthrough = mData->mPassthrough;
218
219 return S_OK;
220}
221
222STDMETHODIMP DVDDrive::COMSETTER(Passthrough) (BOOL aPassthrough)
223{
224 AutoCaller autoCaller (this);
225 CheckComRCReturnRC (autoCaller.rc());
226
227 /* the machine needs to be mutable */
228 Machine::AutoMutableStateDependency adep (mParent);
229 CheckComRCReturnRC (adep.rc());
230
231 AutoWriteLock alock (this);
232
233 if (mData->mPassthrough != aPassthrough)
234 {
235 mData.backup();
236 mData->mPassthrough = aPassthrough;
237 }
238
239 return S_OK;
240}
241
242// IDVDDrive methods
243////////////////////////////////////////////////////////////////////////////////
244
245STDMETHODIMP DVDDrive::MountImage (IN_GUID aImageId)
246{
247 Guid imageId = aImageId;
248 CheckComArgExpr(aImageId, !imageId.isEmpty());
249
250 AutoCaller autoCaller (this);
251 CheckComRCReturnRC (autoCaller.rc());
252
253 /* the machine needs to be mutable */
254 Machine::AutoMutableStateDependency adep (mParent);
255 CheckComRCReturnRC (adep.rc());
256
257 AutoWriteLock alock (this);
258
259 HRESULT rc = E_FAIL;
260
261 /* Our lifetime is bound to mParent's lifetime, so we don't add caller.
262 * We also don't lock mParent since its mParent field is const. */
263
264 ComObjPtr <DVDImage2> image;
265 rc = mParent->virtualBox()->findDVDImage2 (&imageId, NULL,
266 true /* aSetError */, &image);
267
268 if (SUCCEEDED (rc))
269 {
270 if (mData->mState != DriveState_ImageMounted ||
271 !mData->mImage.equalsTo (image))
272 {
273 rc = image->attachTo (mParent->id(), mParent->snapshotId());
274 if (SUCCEEDED (rc))
275 {
276 /* umount() will backup data */
277 rc = unmount();
278 if (SUCCEEDED (rc))
279 {
280 mData->mImage = image;
281 mData->mState = DriveState_ImageMounted;
282
283 /* leave the lock before informing callbacks */
284 alock.unlock();
285
286 mParent->onDVDDriveChange();
287 }
288 }
289 }
290 }
291
292 return rc;
293}
294
295STDMETHODIMP DVDDrive::CaptureHostDrive (IHostDVDDrive *aHostDVDDrive)
296{
297 CheckComArgNotNull(aHostDVDDrive);
298
299 AutoCaller autoCaller (this);
300 CheckComRCReturnRC (autoCaller.rc());
301
302 /* the machine needs to be mutable */
303 Machine::AutoMutableStateDependency adep (mParent);
304 CheckComRCReturnRC (adep.rc());
305
306 AutoWriteLock alock (this);
307
308 if (mData->mState != DriveState_HostDriveCaptured ||
309 !mData->mHostDrive.equalsTo (aHostDVDDrive))
310 {
311 /* umount() will backup data */
312 HRESULT rc = unmount();
313 if (SUCCEEDED (rc))
314 {
315 mData->mHostDrive = aHostDVDDrive;
316 mData->mState = DriveState_HostDriveCaptured;
317
318 /* leave the lock before informing callbacks */
319 alock.unlock();
320
321 mParent->onDVDDriveChange();
322 }
323 }
324
325 return S_OK;
326}
327
328STDMETHODIMP DVDDrive::Unmount()
329{
330 AutoCaller autoCaller (this);
331 CheckComRCReturnRC (autoCaller.rc());
332
333 /* the machine needs to be mutable */
334 Machine::AutoMutableStateDependency adep (mParent);
335 CheckComRCReturnRC (adep.rc());
336
337 AutoWriteLock alock (this);
338
339 if (mData->mState != DriveState_NotMounted)
340 {
341 /* umount() will backup data */
342 HRESULT rc = unmount();
343 if (SUCCEEDED (rc))
344 {
345 mData->mState = DriveState_NotMounted;
346
347 /* leave the lock before informing callbacks */
348 alock.unlock();
349
350 mParent->onDVDDriveChange();
351 }
352 }
353
354 return S_OK;
355}
356
357STDMETHODIMP DVDDrive::GetImage (IDVDImage2 **aDVDImage)
358{
359 CheckComArgOutPointerValid(aDVDImage);
360
361 AutoCaller autoCaller (this);
362 CheckComRCReturnRC (autoCaller.rc());
363
364 AutoReadLock alock (this);
365
366 mData->mImage.queryInterfaceTo (aDVDImage);
367
368 return S_OK;
369}
370
371STDMETHODIMP DVDDrive::GetHostDrive(IHostDVDDrive **aHostDrive)
372{
373 CheckComArgOutPointerValid(aHostDrive);
374
375 AutoCaller autoCaller (this);
376 CheckComRCReturnRC (autoCaller.rc());
377
378 AutoReadLock alock (this);
379
380 mData->mHostDrive.queryInterfaceTo (aHostDrive);
381
382 return S_OK;
383}
384
385// public methods only for internal purposes
386////////////////////////////////////////////////////////////////////////////////
387
388/**
389 * Loads settings from the given machine node. May be called once right after
390 * this object creation.
391 *
392 * @param aMachineNode <Machine> node.
393 *
394 * @note Locks this object for writing.
395 */
396HRESULT DVDDrive::loadSettings (const settings::Key &aMachineNode)
397{
398 using namespace settings;
399
400 AssertReturn (!aMachineNode.isNull(), E_FAIL);
401
402 AutoCaller autoCaller (this);
403 AssertComRCReturnRC (autoCaller.rc());
404
405 AutoWriteLock alock (this);
406
407 /* Note: we assume that the default values for attributes of optional
408 * nodes are assigned in the Data::Data() constructor and don't do it
409 * here. It implies that this method may only be called after constructing
410 * a new BIOSSettings object while all its data fields are in the default
411 * values. Exceptions are fields whose creation time defaults don't match
412 * values that should be applied when these fields are not explicitly set
413 * in the settings file (for backwards compatibility reasons). This takes
414 * place when a setting of a newly created object must default to A while
415 * the same setting of an object loaded from the old settings file must
416 * default to B. */
417
418 HRESULT rc = S_OK;
419
420 /* DVD drive (required, contains either Image or HostDrive or nothing) */
421 Key dvdDriveNode = aMachineNode.key ("DVDDrive");
422
423 /* optional, defaults to false */
424 mData->mPassthrough = dvdDriveNode.value <bool> ("passthrough");
425
426 Key typeNode;
427
428 if (!(typeNode = dvdDriveNode.findKey ("Image")).isNull())
429 {
430 Guid uuid = typeNode.value <Guid> ("uuid");
431 rc = MountImage (uuid);
432 CheckComRCReturnRC (rc);
433 }
434 else if (!(typeNode = dvdDriveNode.findKey ("HostDrive")).isNull())
435 {
436
437 Bstr src = typeNode.stringValue ("src");
438
439 /* find the correspoding object */
440 ComObjPtr <Host> host = mParent->virtualBox()->host();
441
442 ComPtr <IHostDVDDriveCollection> coll;
443 rc = host->COMGETTER(DVDDrives) (coll.asOutParam());
444 AssertComRC (rc);
445
446 ComPtr <IHostDVDDrive> drive;
447 rc = coll->FindByName (src, drive.asOutParam());
448 if (SUCCEEDED (rc))
449 {
450 rc = CaptureHostDrive (drive);
451 CheckComRCReturnRC (rc);
452 }
453 else if (rc == E_INVALIDARG)
454 {
455 /* the host DVD drive is not currently available. we
456 * assume it will be available later and create an
457 * extra object now */
458 ComObjPtr <HostDVDDrive> hostDrive;
459 hostDrive.createObject();
460 rc = hostDrive->init (src);
461 AssertComRC (rc);
462 rc = CaptureHostDrive (hostDrive);
463 CheckComRCReturnRC (rc);
464 }
465 else
466 AssertComRC (rc);
467 }
468
469 return S_OK;
470}
471
472/**
473 * Saves settings to the given machine node.
474 *
475 * @param aMachineNode <Machine> node.
476 *
477 * @note Locks this object for reading.
478 */
479HRESULT DVDDrive::saveSettings (settings::Key &aMachineNode)
480{
481 using namespace settings;
482
483 AssertReturn (!aMachineNode.isNull(), E_FAIL);
484
485 AutoCaller autoCaller (this);
486 AssertComRCReturnRC (autoCaller.rc());
487
488 AutoReadLock alock (this);
489
490 Key node = aMachineNode.createKey ("DVDDrive");
491
492 node.setValue <bool> ("passthrough", !!mData->mPassthrough);
493
494 switch (mData->mState)
495 {
496 case DriveState_ImageMounted:
497 {
498 Assert (!mData->mImage.isNull());
499
500 Guid id;
501 HRESULT rc = mData->mImage->COMGETTER(Id) (id.asOutParam());
502 AssertComRC (rc);
503 Assert (!id.isEmpty());
504
505 Key imageNode = node.createKey ("Image");
506 imageNode.setValue <Guid> ("uuid", id);
507 break;
508 }
509 case DriveState_HostDriveCaptured:
510 {
511 Assert (!mData->mHostDrive.isNull());
512
513 Bstr name;
514 HRESULT rc = mData->mHostDrive->COMGETTER(Name) (name.asOutParam());
515 AssertComRC (rc);
516 Assert (!name.isEmpty());
517
518 Key hostDriveNode = node.createKey ("HostDrive");
519 hostDriveNode.setValue <Bstr> ("src", name);
520 break;
521 }
522 case DriveState_NotMounted:
523 /* do nothing, i.e.leave the drive node empty */
524 break;
525 default:
526 ComAssertMsgFailedRet (("Invalid drive state: %d", mData->mState),
527 E_FAIL);
528 }
529
530 return S_OK;
531}
532
533/**
534 * @note Locks this object for writing.
535 */
536bool DVDDrive::rollback()
537{
538 /* sanity */
539 AutoCaller autoCaller (this);
540 AssertComRCReturn (autoCaller.rc(), false);
541
542 AutoWriteLock alock (this);
543
544 bool changed = false;
545
546 if (mData.isBackedUp())
547 {
548 /* we need to check all data to see whether anything will be changed
549 * after rollback */
550 changed = mData.hasActualChanges();
551
552 if (changed)
553 {
554 Data *oldData = mData.backedUpData();
555
556 if (!mData->mImage.isNull() &&
557 !oldData->mImage.equalsTo (mData->mImage))
558 {
559 /* detach the current image that will go away after rollback */
560 mData->mImage->detachFrom (mParent->id(), mParent->snapshotId());
561 }
562 }
563
564 mData.rollback();
565 }
566
567 return changed;
568}
569
570/**
571 * @note Locks this object for writing, together with the peer object (also for
572 * writing) if there is one.
573 */
574void DVDDrive::commit()
575{
576 /* sanity */
577 AutoCaller autoCaller (this);
578 AssertComRCReturnVoid (autoCaller.rc());
579
580 /* sanity too */
581 AutoCaller peerCaller (mPeer);
582 AssertComRCReturnVoid (peerCaller.rc());
583
584 /* lock both for writing since we modify both (mPeer is "master" so locked
585 * first) */
586 AutoMultiWriteLock2 alock (mPeer, this);
587
588 if (mData.isBackedUp())
589 {
590 Data *oldData = mData.backedUpData();
591
592 if (!oldData->mImage.isNull() &&
593 !oldData->mImage.equalsTo (mData->mImage))
594 {
595 /* detach the old image that will go away after commit */
596 oldData->mImage->detachFrom (mParent->id(), mParent->snapshotId());
597 }
598
599 mData.commit();
600 if (mPeer)
601 {
602 /* attach new data to the peer and reshare it */
603 mPeer->mData.attach (mData);
604 }
605 }
606}
607
608/**
609 * @note Locks this object for writing, together with the peer object
610 * represented by @a aThat (locked for reading).
611 */
612void DVDDrive::copyFrom (DVDDrive *aThat)
613{
614 AssertReturnVoid (aThat != NULL);
615
616 /* sanity */
617 AutoCaller autoCaller (this);
618 AssertComRCReturnVoid (autoCaller.rc());
619
620 /* sanity too */
621 AutoCaller thatCaller (aThat);
622 AssertComRCReturnVoid (thatCaller.rc());
623
624 /* peer is not modified, lock it for reading (aThat is "master" so locked
625 * first) */
626 AutoMultiLock2 alock (aThat->rlock(), this->wlock());
627
628 /* this will back up current data */
629 mData.assignCopy (aThat->mData);
630}
631
632/**
633 * Helper to unmount a drive.
634 *
635 * @note Must be called from under this object's write lock.
636 */
637HRESULT DVDDrive::unmount()
638{
639 AssertReturn (isWriteLockOnCurrentThread(), E_FAIL);
640
641 mData.backup();
642
643 if (mData->mImage)
644 mData->mImage.setNull();
645 if (mData->mHostDrive)
646 mData->mHostDrive.setNull();
647
648 mData->mState = DriveState_NotMounted;
649
650 return S_OK;
651}
652
653// private methods
654////////////////////////////////////////////////////////////////////////////////
655
656/* 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