1 | /* $Id: HardDiskImpl.h 18388 2009-03-27 13:11:42Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VirtualBox COM class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008 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_HARDDISKIMPL
|
---|
25 | #define ____H_HARDDISKIMPL
|
---|
26 |
|
---|
27 | #include "VirtualBoxBase.h"
|
---|
28 |
|
---|
29 | #include "VirtualBoxImpl.h"
|
---|
30 | #include "HardDiskFormatImpl.h"
|
---|
31 | #include "MediumImpl.h"
|
---|
32 |
|
---|
33 | #include <VBox/com/SupportErrorInfo.h>
|
---|
34 |
|
---|
35 | #include <VBox/VBoxHDD.h>
|
---|
36 |
|
---|
37 | #include <map>
|
---|
38 |
|
---|
39 | class Progress;
|
---|
40 |
|
---|
41 | ////////////////////////////////////////////////////////////////////////////////
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * The HardDisk component class implements the IHardDisk interface.
|
---|
45 | */
|
---|
46 | class ATL_NO_VTABLE HardDisk
|
---|
47 | : public com::SupportErrorInfoDerived<MediumBase, HardDisk, IHardDisk>
|
---|
48 | , public VirtualBoxBaseWithTypedChildrenNEXT<HardDisk>
|
---|
49 | , public VirtualBoxSupportTranslation<HardDisk>
|
---|
50 | , public IHardDisk
|
---|
51 | {
|
---|
52 | public:
|
---|
53 |
|
---|
54 | typedef VirtualBoxBaseWithTypedChildrenNEXT <HardDisk>::DependentChildren
|
---|
55 | List;
|
---|
56 |
|
---|
57 | class MergeChain;
|
---|
58 | class CloneChain;
|
---|
59 |
|
---|
60 | VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE (HardDisk)
|
---|
61 |
|
---|
62 | DECLARE_NOT_AGGREGATABLE (HardDisk)
|
---|
63 |
|
---|
64 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
65 |
|
---|
66 | BEGIN_COM_MAP (HardDisk)
|
---|
67 | COM_INTERFACE_ENTRY (ISupportErrorInfo)
|
---|
68 | COM_INTERFACE_ENTRY2 (IMedium, MediumBase)
|
---|
69 | COM_INTERFACE_ENTRY (IHardDisk)
|
---|
70 | END_COM_MAP()
|
---|
71 |
|
---|
72 | NS_DECL_ISUPPORTS
|
---|
73 |
|
---|
74 | DECLARE_EMPTY_CTOR_DTOR (HardDisk)
|
---|
75 |
|
---|
76 | HRESULT FinalConstruct();
|
---|
77 | void FinalRelease();
|
---|
78 |
|
---|
79 | enum HDDOpenMode { OpenReadWrite, OpenReadOnly };
|
---|
80 | // have to use a special enum for the overloaded init() below;
|
---|
81 | // can't use AccessMode_T from XIDL because that's mapped to an int
|
---|
82 | // and would be ambiguous
|
---|
83 |
|
---|
84 | // public initializer/uninitializer for internal purposes only
|
---|
85 | HRESULT init(VirtualBox *aVirtualBox,
|
---|
86 | CBSTR aFormat,
|
---|
87 | CBSTR aLocation);
|
---|
88 | HRESULT init(VirtualBox *aVirtualBox,
|
---|
89 | CBSTR aLocation,
|
---|
90 | HDDOpenMode enOpenMode);
|
---|
91 | HRESULT init(VirtualBox *aVirtualBox,
|
---|
92 | HardDisk *aParent,
|
---|
93 | const settings::Key &aNode);
|
---|
94 | void uninit();
|
---|
95 |
|
---|
96 | // IMedium properties & methods
|
---|
97 | COM_FORWARD_IMedium_TO_BASE (MediumBase)
|
---|
98 |
|
---|
99 | // IHardDisk properties
|
---|
100 | STDMETHOD(COMGETTER(Format)) (BSTR *aFormat);
|
---|
101 | STDMETHOD(COMGETTER(Type)) (HardDiskType_T *aType);
|
---|
102 | STDMETHOD(COMSETTER(Type)) (HardDiskType_T aType);
|
---|
103 | STDMETHOD(COMGETTER(Parent)) (IHardDisk **aParent);
|
---|
104 | STDMETHOD(COMGETTER(Children)) (ComSafeArrayOut (IHardDisk *, aChildren));
|
---|
105 | STDMETHOD(COMGETTER(Root)) (IHardDisk **aRoot);
|
---|
106 | STDMETHOD(COMGETTER(ReadOnly)) (BOOL *aReadOnly);
|
---|
107 | STDMETHOD(COMGETTER(LogicalSize)) (ULONG64 *aLogicalSize);
|
---|
108 | STDMETHOD(COMGETTER(AutoReset)) (BOOL *aAutoReset);
|
---|
109 | STDMETHOD(COMSETTER(AutoReset)) (BOOL aAutoReset);
|
---|
110 |
|
---|
111 | // IHardDisk methods
|
---|
112 | STDMETHOD(GetProperty) (IN_BSTR aName, BSTR *aValue);
|
---|
113 | STDMETHOD(SetProperty) (IN_BSTR aName, IN_BSTR aValue);
|
---|
114 | STDMETHOD(GetProperties) (IN_BSTR aNames,
|
---|
115 | ComSafeArrayOut (BSTR, aReturnNames),
|
---|
116 | ComSafeArrayOut (BSTR, aReturnValues));
|
---|
117 | STDMETHOD(SetProperties) (ComSafeArrayIn (IN_BSTR, aNames),
|
---|
118 | ComSafeArrayIn (IN_BSTR, aValues));
|
---|
119 | STDMETHOD(CreateBaseStorage) (ULONG64 aLogicalSize,
|
---|
120 | HardDiskVariant_T aVariant,
|
---|
121 | IProgress **aProgress);
|
---|
122 | STDMETHOD(DeleteStorage) (IProgress **aProgress);
|
---|
123 | STDMETHOD(CreateDiffStorage) (IHardDisk *aTarget,
|
---|
124 | HardDiskVariant_T aVariant,
|
---|
125 | IProgress **aProgress);
|
---|
126 | STDMETHOD(MergeTo) (IN_GUID aTargetId, IProgress **aProgress);
|
---|
127 | STDMETHOD(CloneTo) (IHardDisk *aTarget, HardDiskVariant_T aVariant,
|
---|
128 | IHardDisk *aParent, IProgress **aProgress);
|
---|
129 | STDMETHOD(Compact) (IProgress **aProgress);
|
---|
130 | STDMETHOD(Reset) (IProgress **aProgress);
|
---|
131 |
|
---|
132 | // public methods for internal purposes only
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Shortcut to VirtualBoxBaseWithTypedChildrenNEXT::dependentChildren().
|
---|
136 | */
|
---|
137 | const List &children() const { return dependentChildren(); }
|
---|
138 |
|
---|
139 | void updatePaths (const char *aOldPath, const char *aNewPath);
|
---|
140 |
|
---|
141 | ComObjPtr<HardDisk> root (uint32_t *aLevel = NULL);
|
---|
142 |
|
---|
143 | bool isReadOnly();
|
---|
144 |
|
---|
145 | HRESULT saveSettings (settings::Key &aParentNode);
|
---|
146 |
|
---|
147 | HRESULT compareLocationTo (const char *aLocation, int &aResult);
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Shortcut to #deleteStorage() that doesn't wait for operation completion
|
---|
151 | * and implies the progress object will be used for waiting.
|
---|
152 | */
|
---|
153 | HRESULT deleteStorageNoWait (ComObjPtr <Progress> &aProgress)
|
---|
154 | { return deleteStorage (&aProgress, false /* aWait */); }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Shortcut to #deleteStorage() that wait for operation completion by
|
---|
158 | * blocking the current thread.
|
---|
159 | */
|
---|
160 | HRESULT deleteStorageAndWait (ComObjPtr <Progress> *aProgress = NULL)
|
---|
161 | { return deleteStorage (aProgress, true /* aWait */); }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Shortcut to #createDiffStorage() that doesn't wait for operation
|
---|
165 | * completion and implies the progress object will be used for waiting.
|
---|
166 | */
|
---|
167 | HRESULT createDiffStorageNoWait (ComObjPtr<HardDisk> &aTarget,
|
---|
168 | HardDiskVariant_T aVariant,
|
---|
169 | ComObjPtr <Progress> &aProgress)
|
---|
170 | { return createDiffStorage (aTarget, aVariant, &aProgress, false /* aWait */); }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Shortcut to #createDiffStorage() that wait for operation completion by
|
---|
174 | * blocking the current thread.
|
---|
175 | */
|
---|
176 | HRESULT createDiffStorageAndWait (ComObjPtr<HardDisk> &aTarget,
|
---|
177 | HardDiskVariant_T aVariant,
|
---|
178 | ComObjPtr <Progress> *aProgress = NULL)
|
---|
179 | { return createDiffStorage (aTarget, aVariant, aProgress, true /* aWait */); }
|
---|
180 |
|
---|
181 | HRESULT prepareMergeTo (HardDisk *aTarget, MergeChain * &aChain,
|
---|
182 | bool aIgnoreAttachments = false);
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Shortcut to #mergeTo() that doesn't wait for operation completion and
|
---|
186 | * implies the progress object will be used for waiting.
|
---|
187 | */
|
---|
188 | HRESULT mergeToNoWait (MergeChain *aChain,
|
---|
189 | ComObjPtr <Progress> &aProgress)
|
---|
190 | { return mergeTo (aChain, &aProgress, false /* aWait */); }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Shortcut to #mergeTo() that wait for operation completion by
|
---|
194 | * blocking the current thread.
|
---|
195 | */
|
---|
196 | HRESULT mergeToAndWait (MergeChain *aChain,
|
---|
197 | ComObjPtr <Progress> *aProgress = NULL)
|
---|
198 | { return mergeTo (aChain, aProgress, true /* aWait */); }
|
---|
199 |
|
---|
200 | void cancelMergeTo (MergeChain *aChain);
|
---|
201 |
|
---|
202 | Utf8Str name();
|
---|
203 |
|
---|
204 | HRESULT prepareDiscard (MergeChain * &aChain);
|
---|
205 | HRESULT discard (ComObjPtr <Progress> &aProgress, MergeChain *aChain);
|
---|
206 | void cancelDiscard (MergeChain *aChain);
|
---|
207 |
|
---|
208 | /** Returns a preferred format for a differencing hard disk. */
|
---|
209 | Bstr preferredDiffFormat();
|
---|
210 |
|
---|
211 | // unsafe inline public methods for internal purposes only (ensure there is
|
---|
212 | // a caller and a read lock before calling them!)
|
---|
213 |
|
---|
214 | ComObjPtr <HardDisk> parent() const { return static_cast <HardDisk *> (mParent); }
|
---|
215 | HardDiskType_T type() const { return mm.type; }
|
---|
216 |
|
---|
217 | /** For com::SupportErrorInfoImpl. */
|
---|
218 | static const char *ComponentName() { return "HardDisk"; }
|
---|
219 |
|
---|
220 | protected:
|
---|
221 |
|
---|
222 | HRESULT deleteStorage (ComObjPtr <Progress> *aProgress, bool aWait);
|
---|
223 |
|
---|
224 | HRESULT createDiffStorage (ComObjPtr <HardDisk> &aTarget,
|
---|
225 | HardDiskVariant_T aVariant,
|
---|
226 | ComObjPtr <Progress> *aProgress,
|
---|
227 | bool aWait);
|
---|
228 |
|
---|
229 | HRESULT mergeTo (MergeChain *aChain,
|
---|
230 | ComObjPtr <Progress> *aProgress,
|
---|
231 | bool aWait);
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Returns VirtualBox::hardDiskTreeHandle(), for convenience. Don't forget
|
---|
235 | * to follow these locking rules:
|
---|
236 | *
|
---|
237 | * 1. The write lock on this handle must be either held alone on the thread
|
---|
238 | * or requested *after* the VirtualBox object lock. Mixing with other
|
---|
239 | * locks is prohibited.
|
---|
240 | *
|
---|
241 | * 2. The read lock on this handle may be intermixed with any other lock
|
---|
242 | * with the exception that it must be requested *after* the VirtualBox
|
---|
243 | * object lock.
|
---|
244 | */
|
---|
245 | RWLockHandle *treeLock() { return mVirtualBox->hardDiskTreeLockHandle(); }
|
---|
246 |
|
---|
247 | /** Reimplements VirtualBoxWithTypedChildren::childrenLock() to return
|
---|
248 | * treeLock(). */
|
---|
249 | RWLockHandle *childrenLock() { return treeLock(); }
|
---|
250 |
|
---|
251 | private:
|
---|
252 |
|
---|
253 | HRESULT setLocation (CBSTR aLocation);
|
---|
254 | HRESULT setFormat (CBSTR aFormat);
|
---|
255 |
|
---|
256 | virtual HRESULT queryInfo();
|
---|
257 |
|
---|
258 | HRESULT canClose();
|
---|
259 | HRESULT canAttach (const Guid &aMachineId,
|
---|
260 | const Guid &aSnapshotId);
|
---|
261 |
|
---|
262 | HRESULT unregisterWithVirtualBox();
|
---|
263 |
|
---|
264 | Utf8Str vdError (int aVRC);
|
---|
265 |
|
---|
266 | static DECLCALLBACK(void) vdErrorCall (void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
267 | const char *pszFormat, va_list va);
|
---|
268 |
|
---|
269 | static DECLCALLBACK(int) vdProgressCall (PVM /* pVM */, unsigned uPercent,
|
---|
270 | void *pvUser);
|
---|
271 |
|
---|
272 | static DECLCALLBACK(bool) vdConfigAreKeysValid (void *pvUser,
|
---|
273 | const char *pszzValid);
|
---|
274 | static DECLCALLBACK(int) vdConfigQuerySize (void *pvUser, const char *pszName,
|
---|
275 | size_t *pcbValue);
|
---|
276 | static DECLCALLBACK(int) vdConfigQuery (void *pvUser, const char *pszName,
|
---|
277 | char *pszValue, size_t cchValue);
|
---|
278 |
|
---|
279 | static DECLCALLBACK(int) taskThread (RTTHREAD thread, void *pvUser);
|
---|
280 |
|
---|
281 | /** weak parent */
|
---|
282 | ComObjPtr <HardDisk, ComWeakRef> mParent;
|
---|
283 |
|
---|
284 | struct Task;
|
---|
285 | friend struct Task;
|
---|
286 |
|
---|
287 | struct Data
|
---|
288 | {
|
---|
289 | Data()
|
---|
290 | : type(HardDiskType_Normal),
|
---|
291 | logicalSize(0),
|
---|
292 | hddOpenMode(OpenReadWrite),
|
---|
293 | autoReset(false),
|
---|
294 | implicit(false),
|
---|
295 | numCreateDiffTasks(0),
|
---|
296 | vdProgress(NULL),
|
---|
297 | vdDiskIfaces(NULL)
|
---|
298 | {}
|
---|
299 |
|
---|
300 | const Bstr format;
|
---|
301 | ComObjPtr <HardDiskFormat> formatObj;
|
---|
302 |
|
---|
303 | HardDiskType_T type;
|
---|
304 | uint64_t logicalSize; /*< In MBytes. */
|
---|
305 |
|
---|
306 | HDDOpenMode hddOpenMode;
|
---|
307 |
|
---|
308 | BOOL autoReset : 1;
|
---|
309 |
|
---|
310 | typedef std::map <Bstr, Bstr> PropertyMap;
|
---|
311 | PropertyMap properties;
|
---|
312 |
|
---|
313 | bool implicit : 1;
|
---|
314 |
|
---|
315 | uint32_t numCreateDiffTasks;
|
---|
316 |
|
---|
317 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
318 | Progress *vdProgress; /*< Progress for the VD progress callback. */
|
---|
319 |
|
---|
320 | VDINTERFACE vdIfError;
|
---|
321 | VDINTERFACEERROR vdIfCallsError;
|
---|
322 |
|
---|
323 | VDINTERFACE vdIfProgress;
|
---|
324 | VDINTERFACEPROGRESS vdIfCallsProgress;
|
---|
325 |
|
---|
326 | VDINTERFACE vdIfConfig;
|
---|
327 | VDINTERFACECONFIG vdIfCallsConfig;
|
---|
328 |
|
---|
329 | VDINTERFACE vdIfTcpNet;
|
---|
330 | VDINTERFACETCPNET vdIfCallsTcpNet;
|
---|
331 |
|
---|
332 | PVDINTERFACE vdDiskIfaces;
|
---|
333 | };
|
---|
334 |
|
---|
335 | Data mm;
|
---|
336 | };
|
---|
337 |
|
---|
338 | #endif /* ____H_HARDDISKIMPL */
|
---|
339 |
|
---|
340 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|