VirtualBox

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

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

Main: split up huge Medium task thread function, part 4 (done)

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