VirtualBox

source: vbox/trunk/src/VBox/Main/MediumAttachmentImpl.cpp@ 24457

Last change on this file since 24457 was 24401, checked in by vboxsync, 15 years ago

MediumAttachment: get the controller only if mParent is valid

File size: 6.6 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#include "MediumAttachmentImpl.h"
23#include "MachineImpl.h"
24
25#include "Logging.h"
26
27// constructor / destructor
28/////////////////////////////////////////////////////////////////////////////
29
30DEFINE_EMPTY_CTOR_DTOR(MediumAttachment)
31
32HRESULT MediumAttachment::FinalConstruct()
33{
34 LogFlowThisFunc(("\n"));
35 return S_OK;
36}
37
38void MediumAttachment::FinalRelease()
39{
40 LogFlowThisFuncEnter();
41 uninit();
42 LogFlowThisFuncLeave();
43}
44
45// public initializer/uninitializer for internal purposes only
46/////////////////////////////////////////////////////////////////////////////
47
48/**
49 * Initializes the medium attachment object.
50 *
51 * @param aParent Machine object.
52 * @param aMedium Medium object.
53 * @param aController Controller the hard disk is attached to.
54 * @param aPort Port number.
55 * @param aDevice Device number on the port.
56 * @param aImplicit Wether the attachment contains an implicitly created diff.
57 */
58HRESULT MediumAttachment::init(Machine *aParent,
59 Medium *aMedium,
60 const Bstr &aControllerName,
61 LONG aPort,
62 LONG aDevice,
63 DeviceType_T aType,
64 bool aImplicit /*= false*/)
65{
66 LogFlowThisFuncEnter();
67 LogFlowThisFunc(("aParent=%p aMedium=%p aControllerName=%ls aPort=%d aDevice=%d aType=%d aImplicit=%d\n", aParent, aMedium, aControllerName.raw(), aPort, aDevice, aType, aImplicit));
68
69 if (aType == DeviceType_HardDisk)
70 AssertReturn(aMedium, E_INVALIDARG);
71
72 /* Enclose the state transition NotReady->InInit->Ready */
73 AutoInitSpan autoInitSpan(this);
74 AssertReturn(autoInitSpan.isOk(), E_FAIL);
75
76 unconst(mParent) = aParent;
77
78 m.allocate();
79 m->medium = aMedium;
80 unconst(m->controllerName) = aControllerName;
81 unconst(m->port) = aPort;
82 unconst(m->device) = aDevice;
83 unconst(m->type) = aType;
84 unconst(m->passthrough) = false;
85
86 m->implicit = aImplicit;
87
88 /* Confirm a successful initialization when it's the case */
89 autoInitSpan.setSucceeded();
90
91 LogFlowThisFuncLeave();
92 return S_OK;
93}
94
95/**
96 * Uninitializes the instance.
97 * Called from FinalRelease().
98 */
99void MediumAttachment::uninit()
100{
101 LogFlowThisFuncEnter();
102
103 /* Enclose the state transition Ready->InUninit->NotReady */
104 AutoUninitSpan autoUninitSpan(this);
105 if (autoUninitSpan.uninitDone())
106 return;
107
108 m.free();
109
110 unconst(mParent).setNull();
111
112 LogFlowThisFuncLeave();
113}
114
115/**
116 * @note Locks this object for writing.
117 */
118bool MediumAttachment::rollback()
119{
120 LogFlowThisFuncEnter();
121
122 /* sanity */
123 AutoCaller autoCaller(this);
124 AssertComRCReturn (autoCaller.rc(), false);
125
126 AutoWriteLock alock(this);
127
128 bool changed = false;
129
130 if (m.isBackedUp())
131 {
132 /* we need to check all data to see whether anything will be changed
133 * after rollback */
134 changed = m.hasActualChanges();
135 m.rollback();
136 }
137
138 LogFlowThisFuncLeave();
139 return changed;
140}
141
142/**
143 * @note Locks this object for writing.
144 */
145void MediumAttachment::commit()
146{
147 LogFlowThisFuncEnter();
148
149 /* sanity */
150 AutoCaller autoCaller(this);
151 AssertComRCReturnVoid (autoCaller.rc());
152
153 AutoWriteLock alock(this);
154
155 if (m.isBackedUp())
156 m.commit();
157
158 LogFlowThisFuncLeave();
159}
160
161
162// IHardDiskAttachment properties
163/////////////////////////////////////////////////////////////////////////////
164
165STDMETHODIMP MediumAttachment::COMGETTER(Medium)(IMedium **aHardDisk)
166{
167 LogFlowThisFuncEnter();
168
169 CheckComArgOutPointerValid(aHardDisk);
170
171 AutoCaller autoCaller(this);
172 CheckComRCReturnRC(autoCaller.rc());
173
174 AutoReadLock alock(this);
175
176 m->medium.queryInterfaceTo(aHardDisk);
177
178 LogFlowThisFuncLeave();
179 return S_OK;
180}
181
182STDMETHODIMP MediumAttachment::COMGETTER(Controller)(IStorageController **aController)
183{
184 LogFlowThisFuncEnter();
185
186 CheckComArgOutPointerValid(aController);
187
188 AutoCaller autoCaller(this);
189 CheckComRCReturnRC(autoCaller.rc());
190
191 /* m->controller is constant during life time, no need to lock */
192 /** @todo ugly hack, MediumAttachment should have a direct reference
193 * to the storage controller, but can't have that right now due to
194 * how objects are created for settings rollback support. */
195 HRESULT rc = E_FAIL;
196
197 if (mParent)
198 rc = mParent->GetStorageControllerByName(m->controllerName, aController);
199
200 LogFlowThisFuncLeave();
201 return rc;
202}
203
204STDMETHODIMP MediumAttachment::COMGETTER(Port)(LONG *aPort)
205{
206 LogFlowThisFuncEnter();
207
208 CheckComArgOutPointerValid(aPort);
209
210 AutoCaller autoCaller(this);
211 CheckComRCReturnRC(autoCaller.rc());
212
213 /* m->port is constant during life time, no need to lock */
214 *aPort = m->port;
215
216 LogFlowThisFuncLeave();
217 return S_OK;
218}
219
220STDMETHODIMP MediumAttachment::COMGETTER(Device)(LONG *aDevice)
221{
222 LogFlowThisFuncEnter();
223
224 CheckComArgOutPointerValid(aDevice);
225
226 AutoCaller autoCaller(this);
227 CheckComRCReturnRC(autoCaller.rc());
228
229 /* m->device is constant during life time, no need to lock */
230 *aDevice = m->device;
231
232 LogFlowThisFuncLeave();
233 return S_OK;
234}
235
236STDMETHODIMP MediumAttachment::COMGETTER(Type)(DeviceType_T *aType)
237{
238 LogFlowThisFuncEnter();
239
240 CheckComArgOutPointerValid(aType);
241
242 AutoCaller autoCaller(this);
243 CheckComRCReturnRC(autoCaller.rc());
244
245 /* m->type is constant during life time, no need to lock */
246 *aType = m->type;
247
248 LogFlowThisFuncLeave();
249 return S_OK;
250}
251
252STDMETHODIMP MediumAttachment::COMGETTER(Passthrough)(BOOL *aPassthrough)
253{
254 LogFlowThisFuncEnter();
255
256 CheckComArgOutPointerValid(aPassthrough);
257
258 AutoCaller autoCaller(this);
259 CheckComRCReturnRC(autoCaller.rc());
260
261 AutoReadLock lock(this);
262
263 *aPassthrough = m->passthrough;
264
265 LogFlowThisFuncLeave();
266 return S_OK;
267}
268
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