VirtualBox

source: vbox/trunk/src/VBox/Main/include/HardDiskImpl.h@ 20961

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

API and Frontends: change IVirtualBox::openHardDisk to allow modifying the image UUID and parent UUID on open

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