VirtualBox

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

Last change on this file since 22302 was 22173, checked in by vboxsync, 15 years ago

Main: the big XML settings rework. Move XML reading/writing out of interface implementation code into separate layer so it can handle individual settings versions in the future.

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