1 | /* $Id: MediumImpl.h 25903 2010-01-18 18:15:43Z 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 | class VirtualBox;
|
---|
29 | class Progress;
|
---|
30 | struct VM;
|
---|
31 |
|
---|
32 | namespace settings
|
---|
33 | {
|
---|
34 | struct Medium;
|
---|
35 | }
|
---|
36 |
|
---|
37 | ////////////////////////////////////////////////////////////////////////////////
|
---|
38 |
|
---|
39 | class Medium;
|
---|
40 | typedef std::list< ComObjPtr<Medium> > MediaList;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Medium component class for all media types.
|
---|
44 | */
|
---|
45 | class ATL_NO_VTABLE Medium :
|
---|
46 | public VirtualBoxBase,
|
---|
47 | public com::SupportErrorInfoImpl<Medium, IMedium>,
|
---|
48 | public VirtualBoxSupportTranslation<Medium>,
|
---|
49 | VBOX_SCRIPTABLE_IMPL(IMedium)
|
---|
50 | {
|
---|
51 | public:
|
---|
52 | class MergeChain;
|
---|
53 | class ImageChain;
|
---|
54 |
|
---|
55 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(Medium)
|
---|
56 |
|
---|
57 | DECLARE_NOT_AGGREGATABLE(Medium)
|
---|
58 |
|
---|
59 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
60 |
|
---|
61 | BEGIN_COM_MAP(Medium)
|
---|
62 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
63 | COM_INTERFACE_ENTRY(IMedium)
|
---|
64 | COM_INTERFACE_ENTRY(IDispatch)
|
---|
65 | END_COM_MAP()
|
---|
66 |
|
---|
67 | DECLARE_EMPTY_CTOR_DTOR(Medium)
|
---|
68 |
|
---|
69 | HRESULT FinalConstruct();
|
---|
70 | void FinalRelease();
|
---|
71 |
|
---|
72 | enum HDDOpenMode { OpenReadWrite, OpenReadOnly };
|
---|
73 | // have to use a special enum for the overloaded init() below;
|
---|
74 | // can't use AccessMode_T from XIDL because that's mapped to an int
|
---|
75 | // and would be ambiguous
|
---|
76 |
|
---|
77 | // public initializer/uninitializer for internal purposes only
|
---|
78 | HRESULT init(VirtualBox *aVirtualBox,
|
---|
79 | CBSTR aFormat,
|
---|
80 | CBSTR aLocation,
|
---|
81 | bool *pfNeedsSaveSettings);
|
---|
82 | HRESULT init(VirtualBox *aVirtualBox,
|
---|
83 | CBSTR aLocation,
|
---|
84 | HDDOpenMode enOpenMode,
|
---|
85 | DeviceType_T aDeviceType,
|
---|
86 | BOOL aSetImageId,
|
---|
87 | const Guid &aImageId,
|
---|
88 | BOOL aSetParentId,
|
---|
89 | const Guid &aParentId);
|
---|
90 | // initializer used when loading settings
|
---|
91 | HRESULT init(VirtualBox *aVirtualBox,
|
---|
92 | Medium *aParent,
|
---|
93 | DeviceType_T aDeviceType,
|
---|
94 | const settings::Medium &data);
|
---|
95 | // initializer for host floppy/DVD
|
---|
96 | HRESULT init(VirtualBox *aVirtualBox,
|
---|
97 | DeviceType_T aDeviceType,
|
---|
98 | CBSTR aLocation,
|
---|
99 | CBSTR aDescription = NULL);
|
---|
100 | void uninit();
|
---|
101 |
|
---|
102 | void deparent();
|
---|
103 |
|
---|
104 | // IMedium properties
|
---|
105 | STDMETHOD(COMGETTER(Id))(BSTR *aId);
|
---|
106 | STDMETHOD(COMGETTER(Description))(BSTR *aDescription);
|
---|
107 | STDMETHOD(COMSETTER(Description))(IN_BSTR aDescription);
|
---|
108 | STDMETHOD(COMGETTER(State))(MediumState_T *aState);
|
---|
109 | STDMETHOD(COMGETTER(Location))(BSTR *aLocation);
|
---|
110 | STDMETHOD(COMSETTER(Location))(IN_BSTR aLocation);
|
---|
111 | STDMETHOD(COMGETTER(Name))(BSTR *aName);
|
---|
112 | STDMETHOD(COMGETTER(DeviceType))(DeviceType_T *aDeviceType);
|
---|
113 | STDMETHOD(COMGETTER(HostDrive))(BOOL *aHostDrive);
|
---|
114 | STDMETHOD(COMGETTER(Size))(ULONG64 *aSize);
|
---|
115 | STDMETHOD(COMGETTER(Format))(BSTR *aFormat);
|
---|
116 | STDMETHOD(COMGETTER(Type))(MediumType_T *aType);
|
---|
117 | STDMETHOD(COMSETTER(Type))(MediumType_T aType);
|
---|
118 | STDMETHOD(COMGETTER(Parent))(IMedium **aParent);
|
---|
119 | STDMETHOD(COMGETTER(Children))(ComSafeArrayOut(IMedium *, aChildren));
|
---|
120 | STDMETHOD(COMGETTER(Base))(IMedium **aBase);
|
---|
121 | STDMETHOD(COMGETTER(ReadOnly))(BOOL *aReadOnly);
|
---|
122 | STDMETHOD(COMGETTER(LogicalSize))(ULONG64 *aLogicalSize);
|
---|
123 | STDMETHOD(COMGETTER(AutoReset))(BOOL *aAutoReset);
|
---|
124 | STDMETHOD(COMSETTER(AutoReset))(BOOL aAutoReset);
|
---|
125 | STDMETHOD(COMGETTER(LastAccessError))(BSTR *aLastAccessError);
|
---|
126 | STDMETHOD(COMGETTER(MachineIds))(ComSafeArrayOut(BSTR, aMachineIds));
|
---|
127 |
|
---|
128 | // IMedium methods
|
---|
129 | STDMETHOD(RefreshState)(MediumState_T *aState);
|
---|
130 | STDMETHOD(GetSnapshotIds)(IN_BSTR aMachineId,
|
---|
131 | ComSafeArrayOut(BSTR, aSnapshotIds));
|
---|
132 | STDMETHOD(LockRead)(MediumState_T *aState);
|
---|
133 | STDMETHOD(UnlockRead)(MediumState_T *aState);
|
---|
134 | STDMETHOD(LockWrite)(MediumState_T *aState);
|
---|
135 | STDMETHOD(UnlockWrite)(MediumState_T *aState);
|
---|
136 | STDMETHOD(Close)();
|
---|
137 | STDMETHOD(GetProperty)(IN_BSTR aName, BSTR *aValue);
|
---|
138 | STDMETHOD(SetProperty)(IN_BSTR aName, IN_BSTR aValue);
|
---|
139 | STDMETHOD(GetProperties)(IN_BSTR aNames,
|
---|
140 | ComSafeArrayOut(BSTR, aReturnNames),
|
---|
141 | ComSafeArrayOut(BSTR, aReturnValues));
|
---|
142 | STDMETHOD(SetProperties)(ComSafeArrayIn(IN_BSTR, aNames),
|
---|
143 | ComSafeArrayIn(IN_BSTR, aValues));
|
---|
144 | STDMETHOD(CreateBaseStorage)(ULONG64 aLogicalSize,
|
---|
145 | MediumVariant_T aVariant,
|
---|
146 | IProgress **aProgress);
|
---|
147 | STDMETHOD(DeleteStorage)(IProgress **aProgress);
|
---|
148 | STDMETHOD(CreateDiffStorage)(IMedium *aTarget,
|
---|
149 | MediumVariant_T aVariant,
|
---|
150 | IProgress **aProgress);
|
---|
151 | STDMETHOD(MergeTo)(IN_BSTR aTargetId, IProgress **aProgress);
|
---|
152 | STDMETHOD(CloneTo)(IMedium *aTarget, MediumVariant_T aVariant,
|
---|
153 | IMedium *aParent, IProgress **aProgress);
|
---|
154 | STDMETHOD(Compact)(IProgress **aProgress);
|
---|
155 | STDMETHOD(Resize)(ULONG64 aLogicalSize, IProgress **aProgress);
|
---|
156 | STDMETHOD(Reset)(IProgress **aProgress);
|
---|
157 |
|
---|
158 | // public methods for internal purposes only
|
---|
159 | const ComObjPtr<Medium>& getParent() const;
|
---|
160 | const MediaList& getChildren() const;
|
---|
161 |
|
---|
162 | const Guid& getId() const;
|
---|
163 | MediumState_T getState() const;
|
---|
164 | const Utf8Str& getLocation() const;
|
---|
165 | const Utf8Str& getLocationFull() const;
|
---|
166 | uint64_t getSize() const;
|
---|
167 |
|
---|
168 | HRESULT attachTo(const Guid &aMachineId,
|
---|
169 | const Guid &aSnapshotId = Guid::Empty);
|
---|
170 | HRESULT detachFrom(const Guid &aMachineId,
|
---|
171 | const Guid &aSnapshotId = Guid::Empty);
|
---|
172 |
|
---|
173 | const Guid* getFirstMachineBackrefId() const;
|
---|
174 | const Guid* getFirstMachineBackrefSnapshotId() const;
|
---|
175 |
|
---|
176 | #ifdef DEBUG
|
---|
177 | void dumpBackRefs();
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | HRESULT updatePath(const char *aOldPath, const char *aNewPath);
|
---|
181 | void updatePaths(const char *aOldPath, const char *aNewPath);
|
---|
182 |
|
---|
183 | ComObjPtr<Medium> getBase(uint32_t *aLevel = NULL);
|
---|
184 |
|
---|
185 | bool isReadOnly();
|
---|
186 |
|
---|
187 | HRESULT saveSettings(settings::Medium &data);
|
---|
188 |
|
---|
189 | HRESULT compareLocationTo(const char *aLocation, int &aResult);
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Shortcut to #deleteStorage() that doesn't wait for operation completion
|
---|
193 | * and implies the progress object will be used for waiting.
|
---|
194 | */
|
---|
195 | HRESULT deleteStorageNoWait(ComObjPtr<Progress> &aProgress)
|
---|
196 | { return deleteStorage(&aProgress, false /* aWait */, NULL /* pfNeedsSaveSettings */); }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Shortcut to #deleteStorage() that wait for operation completion by
|
---|
200 | * blocking the current thread.
|
---|
201 | */
|
---|
202 | HRESULT deleteStorageAndWait(ComObjPtr<Progress> *aProgress, bool *pfNeedsSaveSettings)
|
---|
203 | { return deleteStorage(aProgress, true /* aWait */, pfNeedsSaveSettings); }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Shortcut to #createDiffStorage() that doesn't wait for operation
|
---|
207 | * completion and implies the progress object will be used for waiting.
|
---|
208 | */
|
---|
209 | HRESULT createDiffStorageNoWait(ComObjPtr<Medium> &aTarget,
|
---|
210 | MediumVariant_T aVariant,
|
---|
211 | ComObjPtr<Progress> &aProgress)
|
---|
212 | { return createDiffStorage(aTarget, aVariant, &aProgress, false /* aWait */, NULL /* pfNeedsSaveSettings*/ ); }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Shortcut to #createDiffStorage() that wait for operation completion by
|
---|
216 | * blocking the current thread.
|
---|
217 | */
|
---|
218 | HRESULT createDiffStorageAndWait(ComObjPtr<Medium> &aTarget,
|
---|
219 | MediumVariant_T aVariant,
|
---|
220 | bool *pfNeedsSaveSettings)
|
---|
221 | { return createDiffStorage(aTarget, aVariant, NULL /*aProgress*/, true /* aWait */, pfNeedsSaveSettings); }
|
---|
222 |
|
---|
223 | HRESULT prepareMergeTo(Medium *aTarget, MergeChain * &aChain,
|
---|
224 | bool aIgnoreAttachments = false);
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Shortcut to #mergeTo() that doesn't wait for operation completion and
|
---|
228 | * implies the progress object will be used for waiting.
|
---|
229 | */
|
---|
230 | HRESULT mergeToNoWait(MergeChain *aChain,
|
---|
231 | ComObjPtr<Progress> &aProgress)
|
---|
232 | { return mergeTo(aChain, &aProgress, false /* aWait */, NULL /*pfNeedsSaveSettings*/); }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Shortcut to #mergeTo() that wait for operation completion by
|
---|
236 | * blocking the current thread.
|
---|
237 | */
|
---|
238 | HRESULT mergeToAndWait(MergeChain *aChain,
|
---|
239 | ComObjPtr<Progress> *aProgress,
|
---|
240 | bool *pfNeedsSaveSettings)
|
---|
241 | { return mergeTo(aChain, aProgress, true /* aWait */, pfNeedsSaveSettings); }
|
---|
242 |
|
---|
243 | void cancelMergeTo(MergeChain *aChain);
|
---|
244 |
|
---|
245 | Utf8Str getName();
|
---|
246 |
|
---|
247 | HRESULT prepareDiscard(MergeChain * &aChain);
|
---|
248 | HRESULT discard(ComObjPtr<Progress> &aProgress, ULONG ulWeight, MergeChain *aChain, bool *pfNeedsSaveSettings);
|
---|
249 | void cancelDiscard(MergeChain *aChain);
|
---|
250 |
|
---|
251 | /** Returns a preferred format for a differencing hard disk. */
|
---|
252 | Bstr preferredDiffFormat();
|
---|
253 |
|
---|
254 | // unsafe inline public methods for internal purposes only (ensure there is
|
---|
255 | // a caller and a read lock before calling them!)
|
---|
256 | MediumType_T getType() const;
|
---|
257 |
|
---|
258 | /** For com::SupportErrorInfoImpl. */
|
---|
259 | static const char *ComponentName() { return "Medium"; }
|
---|
260 |
|
---|
261 | private:
|
---|
262 |
|
---|
263 | HRESULT queryInfo();
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
267 | * this case. Otherwise, returns a respective error message. Called by
|
---|
268 | * Close() from within this object's AutoMayUninitSpan and from under
|
---|
269 | * mVirtualBox write lock.
|
---|
270 | */
|
---|
271 | HRESULT canClose();
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Unregisters this medium with mVirtualBox. Called by Close() from within
|
---|
275 | * this object's AutoMayUninitSpan and from under mVirtualBox write lock.
|
---|
276 | */
|
---|
277 | HRESULT unregisterWithVirtualBox(bool *pfNeedsSaveSettings);
|
---|
278 |
|
---|
279 | HRESULT setStateError();
|
---|
280 |
|
---|
281 | HRESULT deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait, bool *pfNeedsSaveSettings);
|
---|
282 |
|
---|
283 | HRESULT createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
284 | MediumVariant_T aVariant,
|
---|
285 | ComObjPtr<Progress> *aProgress,
|
---|
286 | bool aWait,
|
---|
287 | bool *pfNeedsSaveSettings);
|
---|
288 |
|
---|
289 | HRESULT mergeTo(MergeChain *aChain,
|
---|
290 | ComObjPtr<Progress> *aProgress,
|
---|
291 | bool aWait,
|
---|
292 | bool *pfNeedsSaveSettings);
|
---|
293 |
|
---|
294 | HRESULT setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str());
|
---|
295 | HRESULT setFormat(CBSTR aFormat);
|
---|
296 |
|
---|
297 | Utf8Str vdError(int aVRC);
|
---|
298 |
|
---|
299 | static DECLCALLBACK(void) vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
300 | const char *pszFormat, va_list va);
|
---|
301 |
|
---|
302 | static DECLCALLBACK(int) vdProgressCall(VM* /* pVM */, unsigned uPercent,
|
---|
303 | void *pvUser);
|
---|
304 |
|
---|
305 | static DECLCALLBACK(bool) vdConfigAreKeysValid(void *pvUser,
|
---|
306 | const char *pszzValid);
|
---|
307 | static DECLCALLBACK(int) vdConfigQuerySize(void *pvUser, const char *pszName,
|
---|
308 | size_t *pcbValue);
|
---|
309 | static DECLCALLBACK(int) vdConfigQuery(void *pvUser, const char *pszName,
|
---|
310 | char *pszValue, size_t cchValue);
|
---|
311 |
|
---|
312 | struct Task;
|
---|
313 | friend struct Task;
|
---|
314 |
|
---|
315 | HRESULT taskThreadCreateBase(Task &task, void *pvdOperationIfaces);
|
---|
316 | HRESULT taskThreadCreateDiff(Task &task, void *pvdOperationIfaces, bool fIsAsync);
|
---|
317 | HRESULT taskThreadMerge(Task &task, void *pvdOperationIfaces, bool fIsAsync);
|
---|
318 | HRESULT taskThreadClone(Task &task, void *pvdOperationIfaces);
|
---|
319 | HRESULT taskThreadDelete();
|
---|
320 | HRESULT taskThreadReset(void *pvdOperationIfaces, bool fIsAsync);
|
---|
321 | HRESULT taskThreadCompact(Task &task, void *pvdOperationIfaces);
|
---|
322 |
|
---|
323 | static DECLCALLBACK(int) taskThread(RTTHREAD thread, void *pvUser);
|
---|
324 |
|
---|
325 | struct Data; // opaque data struct, defined in MediumImpl.cpp
|
---|
326 | Data *m;
|
---|
327 | };
|
---|
328 |
|
---|
329 | #endif /* ____H_MEDIUMIMPL */
|
---|
330 |
|
---|