VirtualBox

source: vbox/trunk/src/VBox/Main/include/MediumImpl.h@ 24210

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

Main/Medium: substantial fixing of Medium::queryInfo(), and eliminate some dead code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 11.9 KB
Line 
1/* $Id: MediumImpl.h 24210 2009-10-30 16:30:19Z vboxsync $ */
2/** @file
3 *
4 * VirtualBox COM class implementation
5 */
6
7/*
8 * Copyright (C) 2008-2009 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#ifndef ____H_MEDIUMIMPL
24#define ____H_MEDIUMIMPL
25
26#include "VirtualBoxBase.h"
27
28#include <VBox/com/SupportErrorInfo.h>
29
30class VirtualBox;
31class Progress;
32struct VM;
33
34namespace settings
35{
36 struct Medium;
37}
38
39////////////////////////////////////////////////////////////////////////////////
40
41/**
42 * Medium component class for all media types.
43 */
44class ATL_NO_VTABLE Medium :
45 public VirtualBoxBaseWithTypedChildren<Medium>,
46 public com::SupportErrorInfoImpl<Medium, IMedium>,
47 public VirtualBoxSupportTranslation<Medium>,
48 VBOX_SCRIPTABLE_IMPL(IMedium)
49{
50public:
51
52 typedef VirtualBoxBaseWithTypedChildren<Medium>::DependentChildren List;
53
54 class MergeChain;
55 class ImageChain;
56
57 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(Medium)
58
59 DECLARE_NOT_AGGREGATABLE(Medium)
60
61 DECLARE_PROTECT_FINAL_CONSTRUCT()
62
63 BEGIN_COM_MAP(Medium)
64 COM_INTERFACE_ENTRY(ISupportErrorInfo)
65 COM_INTERFACE_ENTRY(IMedium)
66 END_COM_MAP()
67
68 DECLARE_EMPTY_CTOR_DTOR(Medium)
69
70 enum HDDOpenMode { OpenReadWrite, OpenReadOnly };
71 // have to use a special enum for the overloaded init() below;
72 // can't use AccessMode_T from XIDL because that's mapped to an int
73 // and would be ambiguous
74
75 // public initializer/uninitializer for internal purposes only
76 HRESULT init(VirtualBox *aVirtualBox,
77 CBSTR aFormat,
78 CBSTR aLocation);
79 HRESULT init(VirtualBox *aVirtualBox,
80 CBSTR aLocation,
81 HDDOpenMode enOpenMode,
82 DeviceType_T aDeviceType,
83 BOOL aSetImageId,
84 const Guid &aImageId,
85 BOOL aSetParentId,
86 const Guid &aParentId);
87 // initializer used when loading settings
88 HRESULT init(VirtualBox *aVirtualBox,
89 Medium *aParent,
90 DeviceType_T aDeviceType,
91 const settings::Medium &data);
92 // initializer for host floppy/DVD
93 HRESULT init(VirtualBox *aVirtualBox,
94 DeviceType_T aDeviceType,
95 CBSTR aLocation,
96 CBSTR aDescription = NULL);
97 void uninit();
98
99 // IMedium properties
100 STDMETHOD(COMGETTER(Id))(BSTR *aId);
101 STDMETHOD(COMGETTER(Description))(BSTR *aDescription);
102 STDMETHOD(COMSETTER(Description))(IN_BSTR aDescription);
103 STDMETHOD(COMGETTER(State))(MediumState_T *aState);
104 STDMETHOD(COMGETTER(Location))(BSTR *aLocation);
105 STDMETHOD(COMSETTER(Location))(IN_BSTR aLocation);
106 STDMETHOD(COMGETTER(Name))(BSTR *aName);
107 STDMETHOD(COMGETTER(DeviceType))(DeviceType_T *aDeviceType);
108 STDMETHOD(COMGETTER(HostDrive))(BOOL *aHostDrive);
109 STDMETHOD(COMGETTER(Size))(ULONG64 *aSize);
110 STDMETHOD(COMGETTER(Format))(BSTR *aFormat);
111 STDMETHOD(COMGETTER(Type))(MediumType_T *aType);
112 STDMETHOD(COMSETTER(Type))(MediumType_T aType);
113 STDMETHOD(COMGETTER(Parent))(IMedium **aParent);
114 STDMETHOD(COMGETTER(Children))(ComSafeArrayOut(IMedium *, aChildren));
115 STDMETHOD(COMGETTER(Base))(IMedium **aBase);
116 STDMETHOD(COMGETTER(ReadOnly))(BOOL *aReadOnly);
117 STDMETHOD(COMGETTER(LogicalSize))(ULONG64 *aLogicalSize);
118 STDMETHOD(COMGETTER(AutoReset))(BOOL *aAutoReset);
119 STDMETHOD(COMSETTER(AutoReset))(BOOL aAutoReset);
120 STDMETHOD(COMGETTER(LastAccessError))(BSTR *aLastAccessError);
121 STDMETHOD(COMGETTER(MachineIds))(ComSafeArrayOut(BSTR, aMachineIds));
122
123 // IMedium methods
124 STDMETHOD(GetSnapshotIds)(IN_BSTR aMachineId,
125 ComSafeArrayOut(BSTR, aSnapshotIds));
126 STDMETHOD(LockRead)(MediumState_T *aState);
127 STDMETHOD(UnlockRead)(MediumState_T *aState);
128 STDMETHOD(LockWrite)(MediumState_T *aState);
129 STDMETHOD(UnlockWrite)(MediumState_T *aState);
130 STDMETHOD(Close)();
131 STDMETHOD(GetProperty)(IN_BSTR aName, BSTR *aValue);
132 STDMETHOD(SetProperty)(IN_BSTR aName, IN_BSTR aValue);
133 STDMETHOD(GetProperties)(IN_BSTR aNames,
134 ComSafeArrayOut(BSTR, aReturnNames),
135 ComSafeArrayOut(BSTR, aReturnValues));
136 STDMETHOD(SetProperties)(ComSafeArrayIn(IN_BSTR, aNames),
137 ComSafeArrayIn(IN_BSTR, aValues));
138 STDMETHOD(CreateBaseStorage)(ULONG64 aLogicalSize,
139 MediumVariant_T aVariant,
140 IProgress **aProgress);
141 STDMETHOD(DeleteStorage)(IProgress **aProgress);
142 STDMETHOD(CreateDiffStorage)(IMedium *aTarget,
143 MediumVariant_T aVariant,
144 IProgress **aProgress);
145 STDMETHOD(MergeTo)(IN_BSTR aTargetId, IProgress **aProgress);
146 STDMETHOD(CloneTo)(IMedium *aTarget, MediumVariant_T aVariant,
147 IMedium *aParent, IProgress **aProgress);
148 STDMETHOD(Compact)(IProgress **aProgress);
149 STDMETHOD(Resize)(ULONG64 aLogicalSize, IProgress **aProgress);
150 STDMETHOD(Reset)(IProgress **aProgress);
151
152 // public methods for internal purposes only
153
154 HRESULT FinalConstruct();
155 void FinalRelease();
156
157 HRESULT updatePath(const char *aOldPath, const char *aNewPath);
158
159 HRESULT attachTo(const Guid &aMachineId,
160 const Guid &aSnapshotId = Guid::Empty);
161 HRESULT detachFrom(const Guid &aMachineId,
162 const Guid &aSnapshotId = Guid::Empty);
163
164 const Guid& id() const;
165 MediumState_T state() const;
166 const Bstr& location() const;
167 const Bstr& locationFull() const;
168
169 const Guid* getFirstMachineBackrefId() const;
170 const Guid* getFirstMachineBackrefSnapshotId() const;
171
172 bool isAttachedTo(const Guid &aMachineId);
173
174 /**
175 * Shortcut to VirtualBoxBaseWithTypedChildrenNEXT::dependentChildren().
176 */
177 const List &children() const { return dependentChildren(); }
178
179 void updatePaths(const char *aOldPath, const char *aNewPath);
180
181 ComObjPtr<Medium> base(uint32_t *aLevel = NULL);
182
183 bool isReadOnly();
184
185 HRESULT saveSettings(settings::Medium &data);
186
187 HRESULT compareLocationTo(const char *aLocation, int &aResult);
188
189 /**
190 * Shortcut to #deleteStorage() that doesn't wait for operation completion
191 * and implies the progress object will be used for waiting.
192 */
193 HRESULT deleteStorageNoWait(ComObjPtr<Progress> &aProgress)
194 { return deleteStorage(&aProgress, false /* aWait */); }
195
196 /**
197 * Shortcut to #deleteStorage() that wait for operation completion by
198 * blocking the current thread.
199 */
200 HRESULT deleteStorageAndWait(ComObjPtr<Progress> *aProgress = NULL)
201 { return deleteStorage(aProgress, true /* aWait */); }
202
203 /**
204 * Shortcut to #createDiffStorage() that doesn't wait for operation
205 * completion and implies the progress object will be used for waiting.
206 */
207 HRESULT createDiffStorageNoWait(ComObjPtr<Medium> &aTarget,
208 MediumVariant_T aVariant,
209 ComObjPtr<Progress> &aProgress)
210 { return createDiffStorage(aTarget, aVariant, &aProgress, false /* aWait */); }
211
212 /**
213 * Shortcut to #createDiffStorage() that wait for operation completion by
214 * blocking the current thread.
215 */
216 HRESULT createDiffStorageAndWait(ComObjPtr<Medium> &aTarget,
217 MediumVariant_T aVariant,
218 ComObjPtr<Progress> *aProgress = NULL)
219 { return createDiffStorage(aTarget, aVariant, aProgress, true /* aWait */); }
220
221 HRESULT prepareMergeTo(Medium *aTarget, MergeChain * &aChain,
222 bool aIgnoreAttachments = false);
223
224 /**
225 * Shortcut to #mergeTo() that doesn't wait for operation completion and
226 * implies the progress object will be used for waiting.
227 */
228 HRESULT mergeToNoWait(MergeChain *aChain,
229 ComObjPtr<Progress> &aProgress)
230 { return mergeTo(aChain, &aProgress, false /* aWait */); }
231
232 /**
233 * Shortcut to #mergeTo() that wait for operation completion by
234 * blocking the current thread.
235 */
236 HRESULT mergeToAndWait(MergeChain *aChain,
237 ComObjPtr<Progress> *aProgress = NULL)
238 { return mergeTo(aChain, aProgress, true /* aWait */); }
239
240 void cancelMergeTo(MergeChain *aChain);
241
242 Utf8Str name();
243
244 HRESULT prepareDiscard(MergeChain * &aChain);
245 HRESULT discard(ComObjPtr<Progress> &aProgress, MergeChain *aChain);
246 void cancelDiscard(MergeChain *aChain);
247
248 /** Returns a preferred format for a differencing hard disk. */
249 Bstr preferredDiffFormat();
250
251 // unsafe inline public methods for internal purposes only (ensure there is
252 // a caller and a read lock before calling them!)
253
254 ComObjPtr<Medium> parent() const { return static_cast<Medium *>(mParent); }
255 MediumType_T type() const;
256
257 /** For com::SupportErrorInfoImpl. */
258 static const char *ComponentName() { return "Medium"; }
259
260protected:
261
262 RWLockHandle* treeLock();
263
264 /** Reimplements VirtualBoxWithTypedChildren::childrenLock() to return
265 * treeLock(). */
266 RWLockHandle *childrenLock() { return treeLock(); }
267
268private:
269
270 HRESULT queryInfo();
271
272 /**
273 * Performs extra checks if the medium can be closed and returns S_OK in
274 * this case. Otherwise, returns a respective error message. Called by
275 * Close() from within this object's AutoMayUninitSpan and from under
276 * mVirtualBox write lock.
277 */
278 HRESULT canClose();
279
280 /**
281 * Unregisters this medium with mVirtualBox. Called by Close() from within
282 * this object's AutoMayUninitSpan and from under mVirtualBox write lock.
283 */
284 HRESULT unregisterWithVirtualBox();
285
286 HRESULT setStateError();
287
288 /** weak VirtualBox parent */
289 const ComObjPtr<VirtualBox, ComWeakRef> mVirtualBox;
290
291 HRESULT deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait);
292
293 HRESULT createDiffStorage(ComObjPtr<Medium> &aTarget,
294 MediumVariant_T aVariant,
295 ComObjPtr<Progress> *aProgress,
296 bool aWait);
297
298 HRESULT mergeTo(MergeChain *aChain,
299 ComObjPtr<Progress> *aProgress,
300 bool aWait);
301
302 HRESULT setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str());
303 HRESULT setFormat(CBSTR aFormat);
304
305 Utf8Str vdError(int aVRC);
306
307 static DECLCALLBACK(void) vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
308 const char *pszFormat, va_list va);
309
310 static DECLCALLBACK(int) vdProgressCall(VM* /* pVM */, unsigned uPercent,
311 void *pvUser);
312
313 static DECLCALLBACK(bool) vdConfigAreKeysValid(void *pvUser,
314 const char *pszzValid);
315 static DECLCALLBACK(int) vdConfigQuerySize(void *pvUser, const char *pszName,
316 size_t *pcbValue);
317 static DECLCALLBACK(int) vdConfigQuery(void *pvUser, const char *pszName,
318 char *pszValue, size_t cchValue);
319
320 static DECLCALLBACK(int) taskThread(RTTHREAD thread, void *pvUser);
321
322 /** weak parent */
323 ComObjPtr<Medium, ComWeakRef> mParent;
324
325 struct Task;
326 friend struct Task;
327
328 struct Data; // opaque data struct, defined in MediumImpl.cpp
329 Data *m;
330};
331
332#endif /* ____H_MEDIUMIMPL */
333
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