VirtualBox

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

Last change on this file since 18177 was 18177, checked in by vboxsync, 16 years ago

Main: turn read/write param in OpenHardDisk into an enum

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 11.4 KB
Line 
1/* $Id: HardDiskImpl.h 18177 2009-03-24 13:21:12Z 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 , public IHardDisk
51{
52public:
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 IProgress **aProgress);
129 STDMETHOD(FlattenTo) (IHardDisk *aTarget, HardDiskVariant_T aVariant,
130 IProgress **aProgress);
131 STDMETHOD(Compact) (IProgress **aProgress);
132 STDMETHOD(Reset) (IProgress **aProgress);
133
134 // public methods for internal purposes only
135
136 /**
137 * Shortcut to VirtualBoxBaseWithTypedChildrenNEXT::dependentChildren().
138 */
139 const List &children() const { return dependentChildren(); }
140
141 void updatePaths (const char *aOldPath, const char *aNewPath);
142
143 ComObjPtr<HardDisk> root (uint32_t *aLevel = NULL);
144
145 bool isReadOnly();
146
147 HRESULT saveSettings (settings::Key &aParentNode);
148
149 HRESULT compareLocationTo (const char *aLocation, int &aResult);
150
151 /**
152 * Shortcut to #deleteStorage() that doesn't wait for operation completion
153 * and implies the progress object will be used for waiting.
154 */
155 HRESULT deleteStorageNoWait (ComObjPtr <Progress> &aProgress)
156 { return deleteStorage (&aProgress, false /* aWait */); }
157
158 /**
159 * Shortcut to #deleteStorage() that wait for operation completion by
160 * blocking the current thread.
161 */
162 HRESULT deleteStorageAndWait (ComObjPtr <Progress> *aProgress = NULL)
163 { return deleteStorage (aProgress, true /* aWait */); }
164
165 /**
166 * Shortcut to #createDiffStorage() that doesn't wait for operation
167 * completion and implies the progress object will be used for waiting.
168 */
169 HRESULT createDiffStorageNoWait (ComObjPtr<HardDisk> &aTarget,
170 HardDiskVariant_T aVariant,
171 ComObjPtr <Progress> &aProgress)
172 { return createDiffStorage (aTarget, aVariant, &aProgress, false /* aWait */); }
173
174 /**
175 * Shortcut to #createDiffStorage() that wait for operation completion by
176 * blocking the current thread.
177 */
178 HRESULT createDiffStorageAndWait (ComObjPtr<HardDisk> &aTarget,
179 HardDiskVariant_T aVariant,
180 ComObjPtr <Progress> *aProgress = NULL)
181 { return createDiffStorage (aTarget, aVariant, aProgress, true /* aWait */); }
182
183 HRESULT prepareMergeTo (HardDisk *aTarget, MergeChain * &aChain,
184 bool aIgnoreAttachments = false);
185
186 /**
187 * Shortcut to #mergeTo() that doesn't wait for operation completion and
188 * implies the progress object will be used for waiting.
189 */
190 HRESULT mergeToNoWait (MergeChain *aChain,
191 ComObjPtr <Progress> &aProgress)
192 { return mergeTo (aChain, &aProgress, false /* aWait */); }
193
194 /**
195 * Shortcut to #mergeTo() that wait for operation completion by
196 * blocking the current thread.
197 */
198 HRESULT mergeToAndWait (MergeChain *aChain,
199 ComObjPtr <Progress> *aProgress = NULL)
200 { return mergeTo (aChain, aProgress, true /* aWait */); }
201
202 void cancelMergeTo (MergeChain *aChain);
203
204 Utf8Str name();
205
206 HRESULT prepareDiscard (MergeChain * &aChain);
207 HRESULT discard (ComObjPtr <Progress> &aProgress, MergeChain *aChain);
208 void cancelDiscard (MergeChain *aChain);
209
210 /** Returns a preferred format for a differencing hard disk. */
211 Bstr preferredDiffFormat();
212
213 // unsafe inline public methods for internal purposes only (ensure there is
214 // a caller and a read lock before calling them!)
215
216 ComObjPtr <HardDisk> parent() const { return static_cast <HardDisk *> (mParent); }
217 HardDiskType_T type() const { return mm.type; }
218
219 /** For com::SupportErrorInfoImpl. */
220 static const char *ComponentName() { return "HardDisk"; }
221
222protected:
223
224 HRESULT deleteStorage (ComObjPtr <Progress> *aProgress, bool aWait);
225
226 HRESULT createDiffStorage (ComObjPtr <HardDisk> &aTarget,
227 HardDiskVariant_T aVariant,
228 ComObjPtr <Progress> *aProgress,
229 bool aWait);
230
231 HRESULT mergeTo (MergeChain *aChain,
232 ComObjPtr <Progress> *aProgress,
233 bool aWait);
234
235 /**
236 * Returns VirtualBox::hardDiskTreeHandle(), for convenience. Don't forget
237 * to follow these locking rules:
238 *
239 * 1. The write lock on this handle must be either held alone on the thread
240 * or requested *after* the VirtualBox object lock. Mixing with other
241 * locks is prohibited.
242 *
243 * 2. The read lock on this handle may be intermixed with any other lock
244 * with the exception that it must be requested *after* the VirtualBox
245 * object lock.
246 */
247 RWLockHandle *treeLock() { return mVirtualBox->hardDiskTreeLockHandle(); }
248
249 /** Reimplements VirtualBoxWithTypedChildren::childrenLock() to return
250 * treeLock(). */
251 RWLockHandle *childrenLock() { return treeLock(); }
252
253private:
254
255 HRESULT setLocation (CBSTR aLocation);
256 HRESULT setFormat (CBSTR aFormat);
257
258 HRESULT queryInfo(bool fWrite);
259
260 HRESULT canClose();
261 HRESULT canAttach (const Guid &aMachineId,
262 const Guid &aSnapshotId);
263
264 HRESULT unregisterWithVirtualBox();
265
266 Utf8Str vdError (int aVRC);
267
268 static DECLCALLBACK(void) vdErrorCall (void *pvUser, int rc, RT_SRC_POS_DECL,
269 const char *pszFormat, va_list va);
270
271 static DECLCALLBACK(int) vdProgressCall (PVM /* pVM */, unsigned uPercent,
272 void *pvUser);
273
274 static DECLCALLBACK(bool) vdConfigAreKeysValid (void *pvUser,
275 const char *pszzValid);
276 static DECLCALLBACK(int) vdConfigQuerySize (void *pvUser, const char *pszName,
277 size_t *pcbValue);
278 static DECLCALLBACK(int) vdConfigQuery (void *pvUser, const char *pszName,
279 char *pszValue, size_t cchValue);
280
281 static DECLCALLBACK(int) taskThread (RTTHREAD thread, void *pvUser);
282
283 /** weak parent */
284 ComObjPtr <HardDisk, ComWeakRef> mParent;
285
286 struct Task;
287 friend struct Task;
288
289 struct Data
290 {
291 Data() : type (HardDiskType_Normal), logicalSize (0), autoReset (false)
292 , implicit (false), numCreateDiffTasks (0)
293 , vdProgress (NULL) , vdDiskIfaces (NULL) {}
294
295 const Bstr format;
296 ComObjPtr <HardDiskFormat> formatObj;
297
298 HardDiskType_T type;
299 uint64_t logicalSize; /*< In MBytes. */
300
301 BOOL autoReset : 1;
302
303 typedef std::map <Bstr, Bstr> PropertyMap;
304 PropertyMap properties;
305
306 bool implicit : 1;
307
308 uint32_t numCreateDiffTasks;
309
310 Utf8Str vdError; /*< Error remembered by the VD error callback. */
311 Progress *vdProgress; /*< Progress for the VD progress callback. */
312
313 VDINTERFACE vdIfError;
314 VDINTERFACEERROR vdIfCallsError;
315
316 VDINTERFACE vdIfProgress;
317 VDINTERFACEPROGRESS vdIfCallsProgress;
318
319 VDINTERFACE vdIfConfig;
320 VDINTERFACECONFIG vdIfCallsConfig;
321
322 VDINTERFACE vdIfTcpNet;
323 VDINTERFACETCPNET vdIfCallsTcpNet;
324
325 PVDINTERFACE vdDiskIfaces;
326 };
327
328 Data mm;
329};
330
331#endif /* ____H_HARDDISKIMPL */
332
333/* 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