VirtualBox

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

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

Main/Machine+Snapshot+Medium: Big medium locking cleanup and straightened up the responsibilities between Snapshot and Medium. Rewritten task handling and cleaned up task implementation for medium operations. Implemented IMedium::mergeTo (no way to call it via any frontend yet), making the method parameters similar to IMedium::cloneto. Plus lots of other minor cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: MediumImpl.h 28401 2010-04-16 09:14:54Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2008-2010 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#ifndef ____H_MEDIUMIMPL
25#define ____H_MEDIUMIMPL
26
27#include "VirtualBoxBase.h"
28#include "MediumLock.h"
29
30class Progress;
31
32namespace settings
33{
34 struct Medium;
35}
36
37////////////////////////////////////////////////////////////////////////////////
38
39/**
40 * Medium component class for all media types.
41 */
42class ATL_NO_VTABLE Medium :
43 public VirtualBoxBase,
44 public com::SupportErrorInfoImpl<Medium, IMedium>,
45 public VirtualBoxSupportTranslation<Medium>,
46 VBOX_SCRIPTABLE_IMPL(IMedium)
47{
48public:
49 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(Medium)
50
51 DECLARE_NOT_AGGREGATABLE(Medium)
52
53 DECLARE_PROTECT_FINAL_CONSTRUCT()
54
55 BEGIN_COM_MAP(Medium)
56 COM_INTERFACE_ENTRY(ISupportErrorInfo)
57 COM_INTERFACE_ENTRY(IMedium)
58 COM_INTERFACE_ENTRY(IDispatch)
59 END_COM_MAP()
60
61 DECLARE_EMPTY_CTOR_DTOR(Medium)
62
63 HRESULT FinalConstruct();
64 void FinalRelease();
65
66 enum HDDOpenMode { OpenReadWrite, OpenReadOnly };
67 // have to use a special enum for the overloaded init() below;
68 // can't use AccessMode_T from XIDL because that's mapped to an int
69 // and would be ambiguous
70
71 // public initializer/uninitializer for internal purposes only
72 HRESULT init(VirtualBox *aVirtualBox,
73 CBSTR aFormat,
74 CBSTR aLocation,
75 bool *pfNeedsSaveSettings);
76 HRESULT init(VirtualBox *aVirtualBox,
77 CBSTR aLocation,
78 HDDOpenMode enOpenMode,
79 DeviceType_T aDeviceType,
80 BOOL aSetImageId,
81 const Guid &aImageId,
82 BOOL aSetParentId,
83 const Guid &aParentId);
84 // initializer used when loading settings
85 HRESULT init(VirtualBox *aVirtualBox,
86 Medium *aParent,
87 DeviceType_T aDeviceType,
88 const settings::Medium &data);
89 // initializer for host floppy/DVD
90 HRESULT init(VirtualBox *aVirtualBox,
91 DeviceType_T aDeviceType,
92 CBSTR aLocation,
93 CBSTR aDescription = NULL);
94 void uninit();
95
96 void deparent();
97
98 // IMedium properties
99 STDMETHOD(COMGETTER(Id))(BSTR *aId);
100 STDMETHOD(COMGETTER(Description))(BSTR *aDescription);
101 STDMETHOD(COMSETTER(Description))(IN_BSTR aDescription);
102 STDMETHOD(COMGETTER(State))(MediumState_T *aState);
103 STDMETHOD(COMGETTER(Location))(BSTR *aLocation);
104 STDMETHOD(COMSETTER(Location))(IN_BSTR aLocation);
105 STDMETHOD(COMGETTER(Name))(BSTR *aName);
106 STDMETHOD(COMGETTER(DeviceType))(DeviceType_T *aDeviceType);
107 STDMETHOD(COMGETTER(HostDrive))(BOOL *aHostDrive);
108 STDMETHOD(COMGETTER(Size))(ULONG64 *aSize);
109 STDMETHOD(COMGETTER(Format))(BSTR *aFormat);
110 STDMETHOD(COMGETTER(Type))(MediumType_T *aType);
111 STDMETHOD(COMSETTER(Type))(MediumType_T aType);
112 STDMETHOD(COMGETTER(Parent))(IMedium **aParent);
113 STDMETHOD(COMGETTER(Children))(ComSafeArrayOut(IMedium *, aChildren));
114 STDMETHOD(COMGETTER(Base))(IMedium **aBase);
115 STDMETHOD(COMGETTER(ReadOnly))(BOOL *aReadOnly);
116 STDMETHOD(COMGETTER(LogicalSize))(ULONG64 *aLogicalSize);
117 STDMETHOD(COMGETTER(AutoReset))(BOOL *aAutoReset);
118 STDMETHOD(COMSETTER(AutoReset))(BOOL aAutoReset);
119 STDMETHOD(COMGETTER(LastAccessError))(BSTR *aLastAccessError);
120 STDMETHOD(COMGETTER(MachineIds))(ComSafeArrayOut(BSTR, aMachineIds));
121
122 // IMedium methods
123 STDMETHOD(RefreshState)(MediumState_T *aState);
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)(IMedium *aTarget, 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 const ComObjPtr<Medium>& getParent() const;
154 const MediaList& getChildren() const;
155
156 // unsafe methods for internal purposes only (ensure there is
157 // a caller and a read lock before calling them!)
158 const Guid& getId() const;
159 MediumState_T getState() const;
160 const Utf8Str& getLocation() const;
161 const Utf8Str& getLocationFull() const;
162 uint64_t getSize() const;
163 MediumType_T getType() const;
164 Utf8Str getName();
165
166 HRESULT attachTo(const Guid &aMachineId,
167 const Guid &aSnapshotId = Guid::Empty);
168 HRESULT detachFrom(const Guid &aMachineId,
169 const Guid &aSnapshotId = Guid::Empty);
170
171 const Guid* getFirstMachineBackrefId() const;
172 const Guid* getFirstMachineBackrefSnapshotId() const;
173
174#ifdef DEBUG
175 void dumpBackRefs();
176#endif
177
178 HRESULT updatePath(const char *aOldPath, const char *aNewPath);
179 void updatePaths(const char *aOldPath, const char *aNewPath);
180
181 ComObjPtr<Medium> getBase(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 HRESULT createMediumLockList(bool fMediumWritable, Medium *pToBeParent, MediumLockList &mediumLockList);
190
191 HRESULT createDiffStorage(ComObjPtr<Medium> &aTarget,
192 MediumVariant_T aVariant,
193 MediumLockList *pMediumLockList,
194 ComObjPtr<Progress> *aProgress,
195 bool aWait,
196 bool *pfNeedsSaveSettings);
197
198 HRESULT deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait, bool *pfNeedsSaveSettings);
199 HRESULT markForDeletion();
200 HRESULT unmarkForDeletion();
201
202 HRESULT prepareMergeTo(const ComObjPtr<Medium> &pTarget,
203 const Guid *aMachineId,
204 const Guid *aSnapshotId,
205 bool &fMergeForward,
206 ComObjPtr<Medium> &pParentForTarget,
207 MediaList &aChildrenToReparent,
208 MediumLockList * &aMediumLockList);
209 HRESULT mergeTo(const ComObjPtr<Medium> &pTarget,
210 bool &fMergeForward,
211 ComObjPtr<Medium> pParentForTarget,
212 const MediaList &aChildrenToReparent,
213 MediumLockList *aMediumLockList,
214 ComObjPtr<Progress> *aProgress,
215 bool aWait,
216 bool *pfNeedsSaveSettings);
217 void cancelMergeTo(const MediaList &aChildrenToReparent,
218 MediumLockList *aMediumLockList);
219
220 /** Returns a preferred format for a differencing hard disk. */
221 Bstr preferredDiffFormat();
222
223 /** For com::SupportErrorInfoImpl. */
224 static const char *ComponentName() { return "Medium"; }
225
226private:
227
228 HRESULT queryInfo();
229
230 /**
231 * Performs extra checks if the medium can be closed and returns S_OK in
232 * this case. Otherwise, returns a respective error message. Called by
233 * Close() under the medium tree lock and the medium lock.
234 */
235 HRESULT canClose();
236
237 /**
238 * Unregisters this medium with mVirtualBox. Called by Close() under
239 * the medium tree lock.
240 */
241 HRESULT unregisterWithVirtualBox(bool *pfNeedsSaveSettings);
242
243 HRESULT setStateError();
244
245 HRESULT setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str());
246 HRESULT setFormat(CBSTR aFormat);
247
248 Utf8Str vdError(int aVRC);
249
250 static DECLCALLBACK(void) vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
251 const char *pszFormat, va_list va);
252
253 static DECLCALLBACK(bool) vdConfigAreKeysValid(void *pvUser,
254 const char *pszzValid);
255 static DECLCALLBACK(int) vdConfigQuerySize(void *pvUser, const char *pszName,
256 size_t *pcbValue);
257 static DECLCALLBACK(int) vdConfigQuery(void *pvUser, const char *pszName,
258 char *pszValue, size_t cchValue);
259
260 class Task;
261 class CreateBaseTask;
262 class CreateDiffTask;
263 class CloneTask;
264 class CompactTask;
265 class ResetTask;
266 class DeleteTask;
267 class MergeTask;
268 friend class Task;
269 friend class CreateBaseTask;
270 friend class CreateDiffTask;
271 friend class CloneTask;
272 friend class CompactTask;
273 friend class ResetTask;
274 friend class DeleteTask;
275 friend class MergeTask;
276
277 HRESULT startThread(Medium::Task *pTask);
278 HRESULT runNow(Medium::Task *pTask, bool *pfNeedsSaveSettings);
279
280 HRESULT taskCreateBaseHandler(Medium::CreateBaseTask &task);
281 HRESULT taskCreateDiffHandler(Medium::CreateDiffTask &task);
282 HRESULT taskMergeHandler(Medium::MergeTask &task);
283 HRESULT taskCloneHandler(Medium::CloneTask &task);
284 HRESULT taskDeleteHandler(Medium::DeleteTask &task);
285 HRESULT taskResetHandler(Medium::ResetTask &task);
286 HRESULT taskCompactHandler(Medium::CompactTask &task);
287
288 struct Data; // opaque data struct, defined in MediumImpl.cpp
289 Data *m;
290};
291
292#endif /* ____H_MEDIUMIMPL */
293
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette