VirtualBox

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

Last change on this file since 2511 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include "FloppyDriveImpl.h"
23#include "MachineImpl.h"
24#include "Logging.h"
25
26#include <iprt/string.h>
27
28// defines
29/////////////////////////////////////////////////////////////////////////////
30
31// constructor / destructor
32/////////////////////////////////////////////////////////////////////////////
33
34HRESULT FloppyDrive::FinalConstruct()
35{
36 return S_OK;
37}
38
39void FloppyDrive::FinalRelease()
40{
41 if (isReady())
42 uninit ();
43}
44
45// public initializer/uninitializer for internal purposes only
46/////////////////////////////////////////////////////////////////////////////
47
48/**
49 * Initializes the Floppy drive object.
50 *
51 * @returns COM result indicator
52 * @param parent handle of our parent object
53 */
54HRESULT FloppyDrive::init (Machine *parent)
55{
56 LogFlowMember (("FloppyDrive::init (%p)\n", parent));
57
58 ComAssertRet (parent, E_INVALIDARG);
59
60 AutoLock alock (this);
61 ComAssertRet (!isReady(), E_UNEXPECTED);
62
63 mParent = parent;
64 // mPeer is left null
65
66 mData.allocate();
67
68 setReady (true);
69 return S_OK;
70}
71
72/**
73 * Initializes the Floppy drive object given another Floppy drive object
74 * (a kind of copy constructor). This object shares data with
75 * the object passed as an argument.
76 *
77 * @note This object must be destroyed before the original object
78 * it shares data with is destroyed.
79 */
80HRESULT FloppyDrive::init (Machine *parent, FloppyDrive *that)
81{
82 LogFlowMember (("FloppyDrive::init (%p, %p)\n", parent, that));
83
84 ComAssertRet (parent && that, E_INVALIDARG);
85
86 AutoLock alock (this);
87 ComAssertRet (!isReady(), E_UNEXPECTED);
88
89 mParent = parent;
90 mPeer = that;
91
92 AutoLock thatlock (that);
93 mData.share (that->mData);
94
95 setReady (true);
96 return S_OK;
97}
98
99/**
100 * Initializes the guest object given another guest object
101 * (a kind of copy constructor). This object makes a private copy of data
102 * of the original object passed as an argument.
103 */
104HRESULT FloppyDrive::initCopy (Machine *parent, FloppyDrive *that)
105{
106 LogFlowMember (("FloppyDrive::initCopy (%p, %p)\n", parent, that));
107
108 ComAssertRet (parent && that, E_INVALIDARG);
109
110 AutoLock alock (this);
111 ComAssertRet (!isReady(), E_UNEXPECTED);
112
113 mParent = parent;
114 // mPeer is left null
115
116 AutoLock thatlock (that);
117 mData.attachCopy (that->mData);
118
119 setReady (true);
120 return S_OK;
121}
122
123/**
124 * Uninitializes the instance and sets the ready flag to FALSE.
125 * Called either from FinalRelease() or by the parent when it gets destroyed.
126 */
127void FloppyDrive::uninit()
128{
129 LogFlowMember (("FloppyDrive::uninit()\n"));
130
131 AutoLock alock (this);
132 AssertReturn (isReady(), (void) 0);
133
134 mData.free();
135
136 mPeer.setNull();
137 mParent.setNull();
138
139 setReady (false);
140}
141
142// IFloppyDrive properties
143/////////////////////////////////////////////////////////////////////////////
144
145STDMETHODIMP FloppyDrive::COMGETTER(Enabled) (BOOL *enabled)
146{
147 if (!enabled)
148 return E_POINTER;
149
150 AutoLock alock(this);
151 CHECK_READY();
152
153 *enabled = mData->mEnabled;
154 return S_OK;
155}
156
157STDMETHODIMP FloppyDrive::COMSETTER(Enabled) (BOOL enabled)
158{
159 LogFlowMember(("FloppyDrive::SetEnabled: %s\n", enabled ? "enable" : "disable"));
160
161 AutoLock alock(this);
162 CHECK_READY();
163
164 CHECK_MACHINE_MUTABILITY (mParent);
165
166 if (mData->mEnabled != enabled)
167 {
168 mData.backup();
169 mData->mEnabled = enabled;
170
171 alock.unlock();
172 mParent->onFloppyDriveChange();
173 }
174
175 return S_OK;
176}
177
178STDMETHODIMP FloppyDrive::COMGETTER(State) (DriveState_T *driveState)
179{
180 if (!driveState)
181 return E_POINTER;
182
183 AutoLock alock (this);
184 CHECK_READY();
185
186 *driveState = mData->mDriveState;
187 return S_OK;
188}
189
190// IFloppyDrive methods
191/////////////////////////////////////////////////////////////////////////////
192
193STDMETHODIMP FloppyDrive::MountImage(INPTR GUIDPARAM imageId)
194{
195 if (Guid::isEmpty(imageId))
196 return E_INVALIDARG;
197
198 AutoLock alock (this);
199 CHECK_READY();
200
201 CHECK_MACHINE_MUTABILITY (mParent);
202
203 HRESULT rc = E_FAIL;
204
205 // find the image in the collection
206 ComPtr <IVirtualBox> vbox;
207 rc = mParent->COMGETTER(Parent) (vbox.asOutParam());
208 AssertComRCReturn (rc, rc);
209
210 ComPtr <IFloppyImage> image;
211 rc = vbox->GetFloppyImage (imageId, image.asOutParam());
212 if (SUCCEEDED (rc))
213 {
214 if (mData->mDriveState != DriveState_ImageMounted ||
215 !mData->mFloppyImage.equalsTo (image))
216 {
217 mData.backup();
218 unmount();
219 mData->mFloppyImage = image;
220 mData->mDriveState = DriveState_ImageMounted;
221
222 alock.unlock();
223 mParent->onFloppyDriveChange();
224 }
225 }
226
227 return rc;
228}
229
230STDMETHODIMP FloppyDrive::CaptureHostDrive(IHostFloppyDrive *hostFloppyDrive)
231{
232 if (!hostFloppyDrive)
233 return E_INVALIDARG;
234
235 AutoLock alock (this);
236 CHECK_READY();
237
238 CHECK_MACHINE_MUTABILITY (mParent);
239
240 if (mData->mDriveState != DriveState_HostDriveCaptured ||
241 !mData->mHostDrive.equalsTo (hostFloppyDrive))
242 {
243 mData.backup();
244 unmount();
245 mData->mHostDrive = hostFloppyDrive;
246 mData->mDriveState = DriveState_HostDriveCaptured;
247
248 alock.unlock();
249 mParent->onFloppyDriveChange();
250 }
251
252 return S_OK;
253}
254
255STDMETHODIMP FloppyDrive::Unmount()
256{
257 AutoLock alock (this);
258 CHECK_READY();
259
260 CHECK_MACHINE_MUTABILITY (mParent);
261
262 if (mData->mDriveState != DriveState_NotMounted)
263 {
264 mData.backup();
265 unmount();
266 mData->mDriveState = DriveState_NotMounted;
267
268 alock.unlock();
269 mParent->onFloppyDriveChange();
270 }
271
272 return S_OK;
273}
274
275STDMETHODIMP FloppyDrive::GetImage(IFloppyImage **floppyImage)
276{
277 if (!floppyImage)
278 return E_POINTER;
279
280 AutoLock alock (this);
281 CHECK_READY();
282
283 mData->mFloppyImage.queryInterfaceTo (floppyImage);
284 return S_OK;
285}
286
287STDMETHODIMP FloppyDrive::GetHostDrive(IHostFloppyDrive **hostDrive)
288{
289 if (!hostDrive)
290 return E_POINTER;
291
292 AutoLock alock (this);
293 CHECK_READY();
294
295 mData->mHostDrive.queryInterfaceTo (hostDrive);
296 return S_OK;
297}
298
299// public methods only for internal purposes
300/////////////////////////////////////////////////////////////////////////////
301
302bool FloppyDrive::rollback()
303{
304 AutoLock alock (this);
305
306 bool changed = false;
307
308 if (mData.isBackedUp())
309 {
310 // we need to check all data to see whether anything will be changed
311 // after rollback
312 changed = mData.hasActualChanges();
313 mData.rollback();
314 }
315
316 return changed;
317}
318
319void FloppyDrive::commit()
320{
321 AutoLock alock (this);
322 if (mData.isBackedUp())
323 {
324 mData.commit();
325 if (mPeer)
326 {
327 // attach new data to the peer and reshare it
328 AutoLock peerlock (mPeer);
329 mPeer->mData.attach (mData);
330 }
331 }
332}
333
334void FloppyDrive::copyFrom (FloppyDrive *aThat)
335{
336 AutoLock alock (this);
337
338 // this will back up current data
339 mData.assignCopy (aThat->mData);
340}
341
342// private methods
343/////////////////////////////////////////////////////////////////////////////
344
345/**
346 * Helper to unmount a drive
347 *
348 * @returns COM status code
349 *
350 */
351HRESULT FloppyDrive::unmount()
352{
353 if (mData->mFloppyImage)
354 mData->mFloppyImage.setNull();
355 if (mData->mHostDrive)
356 mData->mHostDrive.setNull();
357 return S_OK;
358}
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