1 | /* $Id: MediumImpl.h 24563 2009-11-10 19:07:00Z 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 |
|
---|
30 | class VirtualBox;
|
---|
31 | class Progress;
|
---|
32 | struct VM;
|
---|
33 |
|
---|
34 | namespace settings
|
---|
35 | {
|
---|
36 | struct Medium;
|
---|
37 | }
|
---|
38 |
|
---|
39 | ////////////////////////////////////////////////////////////////////////////////
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Medium component class for all media types.
|
---|
43 | */
|
---|
44 | class ATL_NO_VTABLE Medium :
|
---|
45 | public VirtualBoxBaseWithTypedChildren<Medium>,
|
---|
46 | public com::SupportErrorInfoImpl<Medium, IMedium>,
|
---|
47 | public VirtualBoxSupportTranslation<Medium>,
|
---|
48 | VBOX_SCRIPTABLE_IMPL(IMedium)
|
---|
49 | {
|
---|
50 | public:
|
---|
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(RefreshState)(MediumState_T *aState);
|
---|
125 | STDMETHOD(GetSnapshotIds)(IN_BSTR aMachineId,
|
---|
126 | ComSafeArrayOut(BSTR, aSnapshotIds));
|
---|
127 | STDMETHOD(LockRead)(MediumState_T *aState);
|
---|
128 | STDMETHOD(UnlockRead)(MediumState_T *aState);
|
---|
129 | STDMETHOD(LockWrite)(MediumState_T *aState);
|
---|
130 | STDMETHOD(UnlockWrite)(MediumState_T *aState);
|
---|
131 | STDMETHOD(Close)();
|
---|
132 | STDMETHOD(GetProperty)(IN_BSTR aName, BSTR *aValue);
|
---|
133 | STDMETHOD(SetProperty)(IN_BSTR aName, IN_BSTR aValue);
|
---|
134 | STDMETHOD(GetProperties)(IN_BSTR aNames,
|
---|
135 | ComSafeArrayOut(BSTR, aReturnNames),
|
---|
136 | ComSafeArrayOut(BSTR, aReturnValues));
|
---|
137 | STDMETHOD(SetProperties)(ComSafeArrayIn(IN_BSTR, aNames),
|
---|
138 | ComSafeArrayIn(IN_BSTR, aValues));
|
---|
139 | STDMETHOD(CreateBaseStorage)(ULONG64 aLogicalSize,
|
---|
140 | MediumVariant_T aVariant,
|
---|
141 | IProgress **aProgress);
|
---|
142 | STDMETHOD(DeleteStorage)(IProgress **aProgress);
|
---|
143 | STDMETHOD(CreateDiffStorage)(IMedium *aTarget,
|
---|
144 | MediumVariant_T aVariant,
|
---|
145 | IProgress **aProgress);
|
---|
146 | STDMETHOD(MergeTo)(IN_BSTR aTargetId, IProgress **aProgress);
|
---|
147 | STDMETHOD(CloneTo)(IMedium *aTarget, MediumVariant_T aVariant,
|
---|
148 | IMedium *aParent, IProgress **aProgress);
|
---|
149 | STDMETHOD(Compact)(IProgress **aProgress);
|
---|
150 | STDMETHOD(Resize)(ULONG64 aLogicalSize, IProgress **aProgress);
|
---|
151 | STDMETHOD(Reset)(IProgress **aProgress);
|
---|
152 |
|
---|
153 | // public methods for internal purposes only
|
---|
154 |
|
---|
155 | HRESULT FinalConstruct();
|
---|
156 | void FinalRelease();
|
---|
157 |
|
---|
158 | HRESULT updatePath(const char *aOldPath, const char *aNewPath);
|
---|
159 |
|
---|
160 | HRESULT attachTo(const Guid &aMachineId,
|
---|
161 | const Guid &aSnapshotId = Guid::Empty);
|
---|
162 | HRESULT detachFrom(const Guid &aMachineId,
|
---|
163 | const Guid &aSnapshotId = Guid::Empty);
|
---|
164 |
|
---|
165 | #ifdef DEBUG
|
---|
166 | void dumpBackRefs();
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | const Guid& id() const;
|
---|
170 | MediumState_T state() const;
|
---|
171 | const Utf8Str& location() const;
|
---|
172 | const Utf8Str& locationFull() const;
|
---|
173 | uint64_t size() const;
|
---|
174 |
|
---|
175 | const Guid* getFirstMachineBackrefId() const;
|
---|
176 | const Guid* getFirstMachineBackrefSnapshotId() const;
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Shortcut to VirtualBoxBaseWithTypedChildrenNEXT::dependentChildren().
|
---|
180 | */
|
---|
181 | const List &children() const { return dependentChildren(); }
|
---|
182 |
|
---|
183 | void updatePaths(const char *aOldPath, const char *aNewPath);
|
---|
184 |
|
---|
185 | ComObjPtr<Medium> base(uint32_t *aLevel = NULL);
|
---|
186 |
|
---|
187 | bool isReadOnly();
|
---|
188 |
|
---|
189 | HRESULT saveSettings(settings::Medium &data);
|
---|
190 |
|
---|
191 | HRESULT compareLocationTo(const char *aLocation, int &aResult);
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Shortcut to #deleteStorage() that doesn't wait for operation completion
|
---|
195 | * and implies the progress object will be used for waiting.
|
---|
196 | */
|
---|
197 | HRESULT deleteStorageNoWait(ComObjPtr<Progress> &aProgress)
|
---|
198 | { return deleteStorage(&aProgress, false /* aWait */); }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Shortcut to #deleteStorage() that wait for operation completion by
|
---|
202 | * blocking the current thread.
|
---|
203 | */
|
---|
204 | HRESULT deleteStorageAndWait(ComObjPtr<Progress> *aProgress = NULL)
|
---|
205 | { return deleteStorage(aProgress, true /* aWait */); }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Shortcut to #createDiffStorage() that doesn't wait for operation
|
---|
209 | * completion and implies the progress object will be used for waiting.
|
---|
210 | */
|
---|
211 | HRESULT createDiffStorageNoWait(ComObjPtr<Medium> &aTarget,
|
---|
212 | MediumVariant_T aVariant,
|
---|
213 | ComObjPtr<Progress> &aProgress)
|
---|
214 | { return createDiffStorage(aTarget, aVariant, &aProgress, false /* aWait */); }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Shortcut to #createDiffStorage() that wait for operation completion by
|
---|
218 | * blocking the current thread.
|
---|
219 | */
|
---|
220 | HRESULT createDiffStorageAndWait(ComObjPtr<Medium> &aTarget,
|
---|
221 | MediumVariant_T aVariant,
|
---|
222 | ComObjPtr<Progress> *aProgress = NULL)
|
---|
223 | { return createDiffStorage(aTarget, aVariant, aProgress, true /* aWait */); }
|
---|
224 |
|
---|
225 | HRESULT prepareMergeTo(Medium *aTarget, MergeChain * &aChain,
|
---|
226 | bool aIgnoreAttachments = false);
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Shortcut to #mergeTo() that doesn't wait for operation completion and
|
---|
230 | * implies the progress object will be used for waiting.
|
---|
231 | */
|
---|
232 | HRESULT mergeToNoWait(MergeChain *aChain,
|
---|
233 | ComObjPtr<Progress> &aProgress)
|
---|
234 | { return mergeTo(aChain, &aProgress, false /* aWait */); }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Shortcut to #mergeTo() that wait for operation completion by
|
---|
238 | * blocking the current thread.
|
---|
239 | */
|
---|
240 | HRESULT mergeToAndWait(MergeChain *aChain,
|
---|
241 | ComObjPtr<Progress> *aProgress = NULL)
|
---|
242 | { return mergeTo(aChain, aProgress, true /* aWait */); }
|
---|
243 |
|
---|
244 | void cancelMergeTo(MergeChain *aChain);
|
---|
245 |
|
---|
246 | Utf8Str name();
|
---|
247 |
|
---|
248 | HRESULT prepareDiscard(MergeChain * &aChain);
|
---|
249 | HRESULT discard(ComObjPtr<Progress> &aProgress, ULONG ulWeight, MergeChain *aChain);
|
---|
250 | void cancelDiscard(MergeChain *aChain);
|
---|
251 |
|
---|
252 | /** Returns a preferred format for a differencing hard disk. */
|
---|
253 | Bstr preferredDiffFormat();
|
---|
254 |
|
---|
255 | // unsafe inline public methods for internal purposes only (ensure there is
|
---|
256 | // a caller and a read lock before calling them!)
|
---|
257 |
|
---|
258 | ComObjPtr<Medium> parent() const { return static_cast<Medium *>(mParent); }
|
---|
259 | MediumType_T type() const;
|
---|
260 |
|
---|
261 | /** For com::SupportErrorInfoImpl. */
|
---|
262 | static const char *ComponentName() { return "Medium"; }
|
---|
263 |
|
---|
264 | protected:
|
---|
265 |
|
---|
266 | RWLockHandle* treeLock();
|
---|
267 |
|
---|
268 | /** Reimplements VirtualBoxWithTypedChildren::childrenLock() to return
|
---|
269 | * treeLock(). */
|
---|
270 | RWLockHandle *childrenLock() { return treeLock(); }
|
---|
271 |
|
---|
272 | private:
|
---|
273 |
|
---|
274 | HRESULT queryInfo();
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
278 | * this case. Otherwise, returns a respective error message. Called by
|
---|
279 | * Close() from within this object's AutoMayUninitSpan and from under
|
---|
280 | * mVirtualBox write lock.
|
---|
281 | */
|
---|
282 | HRESULT canClose();
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Unregisters this medium with mVirtualBox. Called by Close() from within
|
---|
286 | * this object's AutoMayUninitSpan and from under mVirtualBox write lock.
|
---|
287 | */
|
---|
288 | HRESULT unregisterWithVirtualBox();
|
---|
289 |
|
---|
290 | HRESULT setStateError();
|
---|
291 |
|
---|
292 | /** weak VirtualBox parent */
|
---|
293 | const ComObjPtr<VirtualBox, ComWeakRef> mVirtualBox;
|
---|
294 |
|
---|
295 | HRESULT deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait);
|
---|
296 |
|
---|
297 | HRESULT createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
298 | MediumVariant_T aVariant,
|
---|
299 | ComObjPtr<Progress> *aProgress,
|
---|
300 | bool aWait);
|
---|
301 |
|
---|
302 | HRESULT mergeTo(MergeChain *aChain,
|
---|
303 | ComObjPtr<Progress> *aProgress,
|
---|
304 | bool aWait);
|
---|
305 |
|
---|
306 | HRESULT setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str());
|
---|
307 | HRESULT setFormat(CBSTR aFormat);
|
---|
308 |
|
---|
309 | Utf8Str vdError(int aVRC);
|
---|
310 |
|
---|
311 | static DECLCALLBACK(void) vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
312 | const char *pszFormat, va_list va);
|
---|
313 |
|
---|
314 | static DECLCALLBACK(int) vdProgressCall(VM* /* pVM */, unsigned uPercent,
|
---|
315 | void *pvUser);
|
---|
316 |
|
---|
317 | static DECLCALLBACK(bool) vdConfigAreKeysValid(void *pvUser,
|
---|
318 | const char *pszzValid);
|
---|
319 | static DECLCALLBACK(int) vdConfigQuerySize(void *pvUser, const char *pszName,
|
---|
320 | size_t *pcbValue);
|
---|
321 | static DECLCALLBACK(int) vdConfigQuery(void *pvUser, const char *pszName,
|
---|
322 | char *pszValue, size_t cchValue);
|
---|
323 |
|
---|
324 | static DECLCALLBACK(int) taskThread(RTTHREAD thread, void *pvUser);
|
---|
325 |
|
---|
326 | /** weak parent */
|
---|
327 | ComObjPtr<Medium, ComWeakRef> mParent;
|
---|
328 |
|
---|
329 | struct Task;
|
---|
330 | friend struct Task;
|
---|
331 |
|
---|
332 | struct Data; // opaque data struct, defined in MediumImpl.cpp
|
---|
333 | Data *m;
|
---|
334 | };
|
---|
335 |
|
---|
336 | #endif /* ____H_MEDIUMIMPL */
|
---|
337 |
|
---|