VirtualBox

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

Last change on this file since 15157 was 15051, checked in by vboxsync, 16 years ago

Main: Cleaned up the long standing const BSTR = const (OLECHAR *) on WIn32 vs (const PRunichar) * on XPCOM clash. Cleaned up BSTR/GUID macros (IN_BSTR replaces INPTR BSTR, IN_GUID replaces INPTR GUIDPARAM, OUT_GUID replaces GUIDPARAMOUT).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.1 KB
Line 
1/* $Id: FloppyDriveImpl.cpp 15051 2008-12-05 17:20:00Z 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 if (imageId.isEmpty())
256 return E_INVALIDARG;
257
258 AutoCaller autoCaller (this);
259 CheckComRCReturnRC (autoCaller.rc());
260
261 /* the machine needs to be mutable */
262 Machine::AutoMutableStateDependency adep (mParent);
263 CheckComRCReturnRC (adep.rc());
264
265 AutoWriteLock alock (this);
266
267 HRESULT rc = E_FAIL;
268
269 /* Our lifetime is bound to mParent's lifetime, so we don't add caller.
270 * We also don't lock mParent since its mParent field is const. */
271
272 ComObjPtr <FloppyImage2> image;
273 rc = mParent->virtualBox()->findFloppyImage2 (&imageId, NULL,
274 true /* aSetError */, &image);
275
276 if (SUCCEEDED (rc))
277 {
278 if (mData->mState != DriveState_ImageMounted ||
279 !mData->mImage.equalsTo (image))
280 {
281 rc = image->attachTo (mParent->id(), mParent->snapshotId());
282 if (SUCCEEDED (rc))
283 {
284 mData.backup();
285
286 unmount();
287
288 mData->mImage = image;
289 mData->mState = DriveState_ImageMounted;
290
291 /* leave the lock before informing callbacks */
292 alock.unlock();
293
294 mParent->onFloppyDriveChange();
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 mData.backup();
319
320 unmount();
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 return S_OK;
332}
333
334STDMETHODIMP FloppyDrive::Unmount()
335{
336 AutoCaller autoCaller (this);
337 CheckComRCReturnRC (autoCaller.rc());
338
339 /* the machine needs to be mutable */
340 Machine::AutoMutableStateDependency adep (mParent);
341 CheckComRCReturnRC (adep.rc());
342
343 AutoWriteLock alock (this);
344
345 if (mData->mState != DriveState_NotMounted)
346 {
347 mData.backup();
348
349 unmount();
350
351 mData->mState = DriveState_NotMounted;
352
353 /* leave the lock before informing callbacks */
354 alock.unlock();
355
356 mParent->onFloppyDriveChange();
357 }
358
359 return S_OK;
360}
361
362STDMETHODIMP FloppyDrive::GetImage (IFloppyImage2 **aFloppyImage)
363{
364 CheckComArgOutPointerValid(aFloppyImage);
365
366 AutoCaller autoCaller (this);
367 CheckComRCReturnRC (autoCaller.rc());
368
369 AutoReadLock alock (this);
370
371 mData->mImage.queryInterfaceTo (aFloppyImage);
372
373 return S_OK;
374}
375
376STDMETHODIMP FloppyDrive::GetHostDrive (IHostFloppyDrive **aHostDrive)
377{
378 CheckComArgOutPointerValid(aHostDrive);
379
380 AutoCaller autoCaller (this);
381 CheckComRCReturnRC (autoCaller.rc());
382
383 AutoReadLock alock (this);
384
385 mData->mHostDrive.queryInterfaceTo (aHostDrive);
386
387 return S_OK;
388}
389
390// public methods only for internal purposes
391/////////////////////////////////////////////////////////////////////////////
392
393/**
394 * Loads settings from the given machine node.
395 * May be called once right after this object creation.
396 *
397 * @param aMachineNode <Machine> node.
398 *
399 * @note Locks this object for writing.
400 */
401HRESULT FloppyDrive::loadSettings (const settings::Key &aMachineNode)
402{
403 using namespace settings;
404
405 AssertReturn (!aMachineNode.isNull(), E_FAIL);
406
407 AutoCaller autoCaller (this);
408 AssertComRCReturnRC (autoCaller.rc());
409
410 AutoWriteLock alock (this);
411
412 /* Note: we assume that the default values for attributes of optional
413 * nodes are assigned in the Data::Data() constructor and don't do it
414 * here. It implies that this method may only be called after constructing
415 * a new BIOSSettings object while all its data fields are in the default
416 * values. Exceptions are fields whose creation time defaults don't match
417 * values that should be applied when these fields are not explicitly set
418 * in the settings file (for backwards compatibility reasons). This takes
419 * place when a setting of a newly created object must default to A while
420 * the same setting of an object loaded from the old settings file must
421 * default to B. */
422
423 HRESULT rc = S_OK;
424
425 /* Floppy drive (required, contains either Image or HostDrive or nothing) */
426 Key floppyDriveNode = aMachineNode.key ("FloppyDrive");
427
428 /* optional, defaults to true */
429 mData->mEnabled = floppyDriveNode.value <bool> ("enabled");
430
431 Key typeNode;
432
433 if (!(typeNode = floppyDriveNode.findKey ("Image")).isNull())
434 {
435 Guid uuid = typeNode.value <Guid> ("uuid");
436 rc = MountImage (uuid);
437 CheckComRCReturnRC (rc);
438 }
439 else if (!(typeNode = floppyDriveNode.findKey ("HostDrive")).isNull())
440 {
441
442 Bstr src = typeNode.stringValue ("src");
443
444 /* find the correspoding object */
445 ComObjPtr <Host> host = mParent->virtualBox()->host();
446
447 ComPtr <IHostFloppyDriveCollection> coll;
448 rc = host->COMGETTER(FloppyDrives) (coll.asOutParam());
449 AssertComRC (rc);
450
451 ComPtr <IHostFloppyDrive> drive;
452 rc = coll->FindByName (src, drive.asOutParam());
453 if (SUCCEEDED (rc))
454 {
455 rc = CaptureHostDrive (drive);
456 CheckComRCReturnRC (rc);
457 }
458 else if (rc == E_INVALIDARG)
459 {
460 /* the host DVD drive is not currently available. we
461 * assume it will be available later and create an
462 * extra object now */
463 ComObjPtr <HostFloppyDrive> hostDrive;
464 hostDrive.createObject();
465 rc = hostDrive->init (src);
466 AssertComRC (rc);
467 rc = CaptureHostDrive (hostDrive);
468 CheckComRCReturnRC (rc);
469 }
470 else
471 AssertComRC (rc);
472 }
473
474 return S_OK;
475}
476
477/**
478 * Saves settings to the given machine node.
479 *
480 * @param aMachineNode <Machine> node.
481 *
482 * @note Locks this object for reading.
483 */
484HRESULT FloppyDrive::saveSettings (settings::Key &aMachineNode)
485{
486 using namespace settings;
487
488 AssertReturn (!aMachineNode.isNull(), E_FAIL);
489
490 AutoCaller autoCaller (this);
491 AssertComRCReturnRC (autoCaller.rc());
492
493 AutoReadLock alock (this);
494
495 Key node = aMachineNode.createKey ("FloppyDrive");
496
497 node.setValue <bool> ("enabled", !!mData->mEnabled);
498
499 switch (mData->mState)
500 {
501 case DriveState_ImageMounted:
502 {
503 Assert (!mData->mImage.isNull());
504
505 Guid id;
506 HRESULT rc = mData->mImage->COMGETTER(Id) (id.asOutParam());
507 AssertComRC (rc);
508 Assert (!id.isEmpty());
509
510 Key imageNode = node.createKey ("Image");
511 imageNode.setValue <Guid> ("uuid", id);
512 break;
513 }
514 case DriveState_HostDriveCaptured:
515 {
516 Assert (!mData->mHostDrive.isNull());
517
518 Bstr name;
519 HRESULT rc = mData->mHostDrive->COMGETTER(Name) (name.asOutParam());
520 AssertComRC (rc);
521 Assert (!name.isEmpty());
522
523 Key hostDriveNode = node.createKey ("HostDrive");
524 hostDriveNode.setValue <Bstr> ("src", name);
525 break;
526 }
527 case DriveState_NotMounted:
528 /* do nothing, i.e.leave the drive node empty */
529 break;
530 default:
531 ComAssertMsgFailedRet (("Invalid drive state: %d", mData->mState),
532 E_FAIL);
533 }
534
535 return S_OK;
536}
537
538/**
539 * @note Locks this object for writing.
540 */
541bool FloppyDrive::rollback()
542{
543 /* sanity */
544 AutoCaller autoCaller (this);
545 AssertComRCReturn (autoCaller.rc(), false);
546
547 AutoWriteLock alock (this);
548
549 bool changed = false;
550
551 if (mData.isBackedUp())
552 {
553 /* we need to check all data to see whether anything will be changed
554 * after rollback */
555 changed = mData.hasActualChanges();
556
557 if (changed)
558 {
559 Data *oldData = mData.backedUpData();
560
561 if (!mData->mImage.isNull() &&
562 !oldData->mImage.equalsTo (mData->mImage))
563 {
564 /* detach the current image that will go away after rollback */
565 mData->mImage->detachFrom (mParent->id(), mParent->snapshotId());
566 }
567 }
568
569 mData.rollback();
570 }
571
572 return changed;
573}
574
575/**
576 * @note Locks this object for writing, together with the peer object (also
577 * for writing) if there is one.
578 */
579void FloppyDrive::commit()
580{
581 /* sanity */
582 AutoCaller autoCaller (this);
583 AssertComRCReturnVoid (autoCaller.rc());
584
585 /* sanity too */
586 AutoCaller peerCaller (mPeer);
587 AssertComRCReturnVoid (peerCaller.rc());
588
589 /* lock both for writing since we modify both (mPeer is "master" so locked
590 * first) */
591 AutoMultiWriteLock2 alock (mPeer, this);
592
593 if (mData.isBackedUp())
594 {
595 Data *oldData = mData.backedUpData();
596
597 if (!oldData->mImage.isNull() &&
598 !oldData->mImage.equalsTo (mData->mImage))
599 {
600 /* detach the old image that will go away after commit */
601 oldData->mImage->detachFrom (mParent->id(), mParent->snapshotId());
602 }
603
604 mData.commit();
605 if (mPeer)
606 {
607 /* attach new data to the peer and reshare it */
608 mPeer->mData.attach (mData);
609 }
610 }
611}
612
613/**
614 * @note Locks this object for writing, together with the peer object (locked
615 * for reading) if there is one.
616 */
617void FloppyDrive::copyFrom (FloppyDrive *aThat)
618{
619 /* sanity */
620 AutoCaller autoCaller (this);
621 AssertComRCReturnVoid (autoCaller.rc());
622
623 /* sanity too */
624 AutoCaller thatCaller (aThat);
625 AssertComRCReturnVoid (thatCaller.rc());
626
627 /* peer is not modified, lock it for reading (aThat is "master" so locked
628 * first) */
629 AutoMultiLock2 alock (aThat->rlock(), this->wlock());
630
631 /* this will back up current data */
632 mData.assignCopy (aThat->mData);
633}
634
635// private methods
636/////////////////////////////////////////////////////////////////////////////
637
638/**
639 * Helper to unmount a drive.
640 *
641 * @return COM status code
642 */
643HRESULT FloppyDrive::unmount()
644{
645 AssertReturn (isWriteLockOnCurrentThread(), E_FAIL);
646
647 if (mData->mImage)
648 mData->mImage.setNull();
649 if (mData->mHostDrive)
650 mData->mHostDrive.setNull();
651
652 return S_OK;
653}
654/* 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