VirtualBox

source: vbox/trunk/src/VBox/Main/FloppyDriveImpl.cpp@ 15834

Last change on this file since 15834 was 15714, checked in by vboxsync, 16 years ago

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

  • IFloppyDrive::mountImage
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 KB
Line 
1/* $Id: FloppyDriveImpl.cpp 15714 2008-12-22 12:46:17Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2007 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 "FloppyDriveImpl.h"
25
26#include "MachineImpl.h"
27#include "HostImpl.h"
28#include "HostFloppyDriveImpl.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 (FloppyDrive)
40
41HRESULT FloppyDrive::FinalConstruct()
42{
43 return S_OK;
44}
45
46void FloppyDrive::FinalRelease()
47{
48 uninit();
49}
50
51// public initializer/uninitializer for internal purposes only
52/////////////////////////////////////////////////////////////////////////////
53
54/**
55 * Initializes the Floppy drive object.
56 *
57 * @param aParent Handle of the parent object.
58 */
59HRESULT FloppyDrive::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 Floppy drive object given another Floppy 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 FloppyDrive::init (Machine *aParent, FloppyDrive *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 guest object given another guest 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 FloppyDrive::initCopy (Machine *aParent, FloppyDrive *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 FloppyDrive::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// IFloppyDrive properties
192/////////////////////////////////////////////////////////////////////////////
193
194STDMETHODIMP FloppyDrive::COMGETTER(Enabled) (BOOL *aEnabled)
195{
196 CheckComArgOutPointerValid(aEnabled);
197
198 AutoCaller autoCaller (this);
199 CheckComRCReturnRC (autoCaller.rc());
200
201 AutoReadLock alock (this);
202
203 *aEnabled = mData->mEnabled;
204
205 return S_OK;
206}
207
208STDMETHODIMP FloppyDrive::COMSETTER(Enabled) (BOOL aEnabled)
209{
210 LogFlowThisFunc (("aEnabled=%RTbool\n", aEnabled));
211
212 AutoCaller autoCaller (this);
213 CheckComRCReturnRC (autoCaller.rc());
214
215 /* the machine needs to be mutable */
216 Machine::AutoMutableStateDependency adep (mParent);
217 CheckComRCReturnRC (adep.rc());
218
219 AutoWriteLock alock (this);
220
221 if (mData->mEnabled != aEnabled)
222 {
223 mData.backup();
224 mData->mEnabled = aEnabled;
225
226 /* leave the lock before informing callbacks */
227 alock.unlock();
228
229 mParent->onFloppyDriveChange();
230 }
231
232 return S_OK;
233}
234
235STDMETHODIMP FloppyDrive::COMGETTER(State) (DriveState_T *aState)
236{
237 CheckComArgOutPointerValid(aState);
238
239 AutoCaller autoCaller (this);
240 CheckComRCReturnRC (autoCaller.rc());
241
242 AutoReadLock alock (this);
243
244 *aState = mData->mState;
245
246 return S_OK;
247}
248
249// IFloppyDrive methods
250/////////////////////////////////////////////////////////////////////////////
251
252STDMETHODIMP FloppyDrive::MountImage (IN_GUID aImageId)
253{
254 Guid imageId = aImageId;
255 CheckComArgExpr(aImageId, !imageId.isEmpty());
256
257 AutoCaller autoCaller (this);
258 CheckComRCReturnRC (autoCaller.rc());
259
260 /* the machine needs to be mutable */
261 Machine::AutoMutableStateDependency adep (mParent);
262 CheckComRCReturnRC (adep.rc());
263
264 AutoWriteLock alock (this);
265
266 HRESULT rc = E_FAIL;
267
268 /* Our lifetime is bound to mParent's lifetime, so we don't add caller.
269 * We also don't lock mParent since its mParent field is const. */
270
271 ComObjPtr <FloppyImage2> image;
272 rc = mParent->virtualBox()->findFloppyImage2 (&imageId, NULL,
273 true /* aSetError */, &image);
274
275 if (SUCCEEDED (rc))
276 {
277 if (mData->mState != DriveState_ImageMounted ||
278 !mData->mImage.equalsTo (image))
279 {
280 rc = image->attachTo (mParent->id(), mParent->snapshotId());
281 if (SUCCEEDED (rc))
282 {
283 /* umount() will backup data */
284 rc = unmount();
285 if (SUCCEEDED (rc))
286 {
287 mData->mImage = image;
288 mData->mState = DriveState_ImageMounted;
289
290 /* leave the lock before informing callbacks */
291 alock.unlock();
292
293 mParent->onFloppyDriveChange();
294 }
295 }
296 }
297 }
298
299 return rc;
300}
301
302STDMETHODIMP FloppyDrive::CaptureHostDrive (IHostFloppyDrive *aHostFloppyDrive)
303{
304 CheckComArgNotNull(aHostFloppyDrive);
305
306 AutoCaller autoCaller (this);
307 CheckComRCReturnRC (autoCaller.rc());
308
309 /* the machine needs to be mutable */
310 Machine::AutoMutableStateDependency adep (mParent);
311 CheckComRCReturnRC (adep.rc());
312
313 AutoWriteLock alock (this);
314
315 if (mData->mState != DriveState_HostDriveCaptured ||
316 !mData->mHostDrive.equalsTo (aHostFloppyDrive))
317 {
318 /* umount() will backup data */
319 HRESULT rc = unmount();
320 if (SUCCEEDED (rc))
321 {
322 mData->mHostDrive = aHostFloppyDrive;
323 mData->mState = DriveState_HostDriveCaptured;
324
325 /* leave the lock before informing callbacks */
326 alock.unlock();
327
328 mParent->onFloppyDriveChange();
329 }
330 }
331
332 return S_OK;
333}
334
335STDMETHODIMP FloppyDrive::Unmount()
336{
337 AutoCaller autoCaller (this);
338 CheckComRCReturnRC (autoCaller.rc());
339
340 /* the machine needs to be mutable */
341 Machine::AutoMutableStateDependency adep (mParent);
342 CheckComRCReturnRC (adep.rc());
343
344 AutoWriteLock alock (this);
345
346 if (mData->mState != DriveState_NotMounted)
347 {
348 /* umount() will backup data */
349 HRESULT rc = unmount();
350 if (SUCCEEDED (rc))
351 {
352 mData->mState = DriveState_NotMounted;
353
354 /* leave the lock before informing callbacks */
355 alock.unlock();
356
357 mParent->onFloppyDriveChange();
358 }
359 }
360
361 return S_OK;
362}
363
364STDMETHODIMP FloppyDrive::GetImage (IFloppyImage2 **aFloppyImage)
365{
366 CheckComArgOutPointerValid(aFloppyImage);
367
368 AutoCaller autoCaller (this);
369 CheckComRCReturnRC (autoCaller.rc());
370
371 AutoReadLock alock (this);
372
373 mData->mImage.queryInterfaceTo (aFloppyImage);
374
375 return S_OK;
376}
377
378STDMETHODIMP FloppyDrive::GetHostDrive (IHostFloppyDrive **aHostDrive)
379{
380 CheckComArgOutPointerValid(aHostDrive);
381
382 AutoCaller autoCaller (this);
383 CheckComRCReturnRC (autoCaller.rc());
384
385 AutoReadLock alock (this);
386
387 mData->mHostDrive.queryInterfaceTo (aHostDrive);
388
389 return S_OK;
390}
391
392// public methods only for internal purposes
393/////////////////////////////////////////////////////////////////////////////
394
395/**
396 * Loads settings from the given machine node. May be called once right after
397 * this object creation.
398 *
399 * @param aMachineNode <Machine> node.
400 *
401 * @note Locks this object for writing.
402 */
403HRESULT FloppyDrive::loadSettings (const settings::Key &aMachineNode)
404{
405 using namespace settings;
406
407 AssertReturn (!aMachineNode.isNull(), E_FAIL);
408
409 AutoCaller autoCaller (this);
410 AssertComRCReturnRC (autoCaller.rc());
411
412 AutoWriteLock alock (this);
413
414 /* Note: we assume that the default values for attributes of optional
415 * nodes are assigned in the Data::Data() constructor and don't do it
416 * here. It implies that this method may only be called after constructing
417 * a new BIOSSettings object while all its data fields are in the default
418 * values. Exceptions are fields whose creation time defaults don't match
419 * values that should be applied when these fields are not explicitly set
420 * in the settings file (for backwards compatibility reasons). This takes
421 * place when a setting of a newly created object must default to A while
422 * the same setting of an object loaded from the old settings file must
423 * default to B. */
424
425 HRESULT rc = S_OK;
426
427 /* Floppy drive (required, contains either Image or HostDrive or nothing) */
428 Key floppyDriveNode = aMachineNode.key ("FloppyDrive");
429
430 /* optional, defaults to true */
431 mData->mEnabled = floppyDriveNode.value <bool> ("enabled");
432
433 Key typeNode;
434
435 if (!(typeNode = floppyDriveNode.findKey ("Image")).isNull())
436 {
437 Guid uuid = typeNode.value <Guid> ("uuid");
438 rc = MountImage (uuid);
439 CheckComRCReturnRC (rc);
440 }
441 else if (!(typeNode = floppyDriveNode.findKey ("HostDrive")).isNull())
442 {
443
444 Bstr src = typeNode.stringValue ("src");
445
446 /* find the correspoding object */
447 ComObjPtr <Host> host = mParent->virtualBox()->host();
448
449 ComPtr <IHostFloppyDriveCollection> coll;
450 rc = host->COMGETTER(FloppyDrives) (coll.asOutParam());
451 AssertComRC (rc);
452
453 ComPtr <IHostFloppyDrive> drive;
454 rc = coll->FindByName (src, drive.asOutParam());
455 if (SUCCEEDED (rc))
456 {
457 rc = CaptureHostDrive (drive);
458 CheckComRCReturnRC (rc);
459 }
460 else if (rc == E_INVALIDARG)
461 {
462 /* the host DVD drive is not currently available. we
463 * assume it will be available later and create an
464 * extra object now */
465 ComObjPtr <HostFloppyDrive> hostDrive;
466 hostDrive.createObject();
467 rc = hostDrive->init (src);
468 AssertComRC (rc);
469 rc = CaptureHostDrive (hostDrive);
470 CheckComRCReturnRC (rc);
471 }
472 else
473 AssertComRC (rc);
474 }
475
476 return S_OK;
477}
478
479/**
480 * Saves settings to the given machine node.
481 *
482 * @param aMachineNode <Machine> node.
483 *
484 * @note Locks this object for reading.
485 */
486HRESULT FloppyDrive::saveSettings (settings::Key &aMachineNode)
487{
488 using namespace settings;
489
490 AssertReturn (!aMachineNode.isNull(), E_FAIL);
491
492 AutoCaller autoCaller (this);
493 AssertComRCReturnRC (autoCaller.rc());
494
495 AutoReadLock alock (this);
496
497 Key node = aMachineNode.createKey ("FloppyDrive");
498
499 node.setValue <bool> ("enabled", !!mData->mEnabled);
500
501 switch (mData->mState)
502 {
503 case DriveState_ImageMounted:
504 {
505 Assert (!mData->mImage.isNull());
506
507 Guid id;
508 HRESULT rc = mData->mImage->COMGETTER(Id) (id.asOutParam());
509 AssertComRC (rc);
510 Assert (!id.isEmpty());
511
512 Key imageNode = node.createKey ("Image");
513 imageNode.setValue <Guid> ("uuid", id);
514 break;
515 }
516 case DriveState_HostDriveCaptured:
517 {
518 Assert (!mData->mHostDrive.isNull());
519
520 Bstr name;
521 HRESULT rc = mData->mHostDrive->COMGETTER(Name) (name.asOutParam());
522 AssertComRC (rc);
523 Assert (!name.isEmpty());
524
525 Key hostDriveNode = node.createKey ("HostDrive");
526 hostDriveNode.setValue <Bstr> ("src", name);
527 break;
528 }
529 case DriveState_NotMounted:
530 /* do nothing, i.e.leave the drive node empty */
531 break;
532 default:
533 ComAssertMsgFailedRet (("Invalid drive state: %d", mData->mState),
534 E_FAIL);
535 }
536
537 return S_OK;
538}
539
540/**
541 * @note Locks this object for writing.
542 */
543bool FloppyDrive::rollback()
544{
545 /* sanity */
546 AutoCaller autoCaller (this);
547 AssertComRCReturn (autoCaller.rc(), false);
548
549 AutoWriteLock alock (this);
550
551 bool changed = false;
552
553 if (mData.isBackedUp())
554 {
555 /* we need to check all data to see whether anything will be changed
556 * after rollback */
557 changed = mData.hasActualChanges();
558
559 if (changed)
560 {
561 Data *oldData = mData.backedUpData();
562
563 if (!mData->mImage.isNull() &&
564 !oldData->mImage.equalsTo (mData->mImage))
565 {
566 /* detach the current image that will go away after rollback */
567 mData->mImage->detachFrom (mParent->id(), mParent->snapshotId());
568 }
569 }
570
571 mData.rollback();
572 }
573
574 return changed;
575}
576
577/**
578 * @note Locks this object for writing, together with the peer object (also for
579 * writing) if there is one.
580 */
581void FloppyDrive::commit()
582{
583 /* sanity */
584 AutoCaller autoCaller (this);
585 AssertComRCReturnVoid (autoCaller.rc());
586
587 /* sanity too */
588 AutoCaller peerCaller (mPeer);
589 AssertComRCReturnVoid (peerCaller.rc());
590
591 /* lock both for writing since we modify both (mPeer is "master" so locked
592 * first) */
593 AutoMultiWriteLock2 alock (mPeer, this);
594
595 if (mData.isBackedUp())
596 {
597 Data *oldData = mData.backedUpData();
598
599 if (!oldData->mImage.isNull() &&
600 !oldData->mImage.equalsTo (mData->mImage))
601 {
602 /* detach the old image that will go away after commit */
603 oldData->mImage->detachFrom (mParent->id(), mParent->snapshotId());
604 }
605
606 mData.commit();
607 if (mPeer)
608 {
609 /* attach new data to the peer and reshare it */
610 mPeer->mData.attach (mData);
611 }
612 }
613}
614
615/**
616 * @note Locks this object for writing, together with the peer object (locked
617 * for reading) if there is one.
618 */
619void FloppyDrive::copyFrom (FloppyDrive *aThat)
620{
621 /* sanity */
622 AutoCaller autoCaller (this);
623 AssertComRCReturnVoid (autoCaller.rc());
624
625 /* sanity too */
626 AutoCaller thatCaller (aThat);
627 AssertComRCReturnVoid (thatCaller.rc());
628
629 /* peer is not modified, lock it for reading (aThat is "master" so locked
630 * first) */
631 AutoMultiLock2 alock (aThat->rlock(), this->wlock());
632
633 /* this will back up current data */
634 mData.assignCopy (aThat->mData);
635}
636
637/**
638 * Helper to unmount a drive.
639 *
640 * @note Must be called from under this object's write lock.
641 */
642HRESULT FloppyDrive::unmount()
643{
644 AssertReturn (isWriteLockOnCurrentThread(), E_FAIL);
645
646 mData.backup();
647
648 if (mData->mImage)
649 mData->mImage.setNull();
650 if (mData->mHostDrive)
651 mData->mHostDrive.setNull();
652
653 mData->mState = DriveState_NotMounted;
654
655 return S_OK;
656}
657
658// private methods
659/////////////////////////////////////////////////////////////////////////////
660
661/* 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