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