1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef ____H_HARDDISKIMPL
|
---|
23 | #define ____H_HARDDISKIMPL
|
---|
24 |
|
---|
25 | #include "VirtualBoxBase.h"
|
---|
26 | #include "Collection.h"
|
---|
27 |
|
---|
28 | #include <VBox/cfgldr.h>
|
---|
29 |
|
---|
30 | #include <iprt/semaphore.h>
|
---|
31 |
|
---|
32 | #include <list>
|
---|
33 |
|
---|
34 | class VirtualBox;
|
---|
35 | class Progress;
|
---|
36 | class HVirtualDiskImage;
|
---|
37 |
|
---|
38 | ////////////////////////////////////////////////////////////////////////////////
|
---|
39 |
|
---|
40 | class ATL_NO_VTABLE HardDisk :
|
---|
41 | public VirtualBoxSupportErrorInfoImpl <HardDisk, IHardDisk>,
|
---|
42 | public VirtualBoxSupportTranslation <HardDisk>,
|
---|
43 | public VirtualBoxBaseWithTypedChildren <HardDisk>,
|
---|
44 | public IHardDisk
|
---|
45 | {
|
---|
46 |
|
---|
47 | public:
|
---|
48 |
|
---|
49 | typedef VirtualBoxBaseWithTypedChildren <HardDisk>::DependentChildren
|
---|
50 | HardDiskList;
|
---|
51 |
|
---|
52 | DECLARE_NOT_AGGREGATABLE(HardDisk)
|
---|
53 |
|
---|
54 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
55 |
|
---|
56 | BEGIN_COM_MAP(HardDisk)
|
---|
57 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
58 | COM_INTERFACE_ENTRY(IHardDisk)
|
---|
59 | END_COM_MAP()
|
---|
60 |
|
---|
61 | NS_DECL_ISUPPORTS
|
---|
62 |
|
---|
63 | HRESULT FinalConstruct();
|
---|
64 | void FinalRelease();
|
---|
65 |
|
---|
66 | protected:
|
---|
67 |
|
---|
68 | // protected initializer/uninitializer for internal purposes only
|
---|
69 | HRESULT protectedInit (VirtualBox *aVirtualBox, HardDisk *aParent);
|
---|
70 | void protectedUninit (AutoLock &alock);
|
---|
71 |
|
---|
72 | public:
|
---|
73 |
|
---|
74 | // IHardDisk properties
|
---|
75 | STDMETHOD(COMGETTER(Id)) (GUIDPARAMOUT aId);
|
---|
76 | STDMETHOD(COMGETTER(StorageType)) (HardDiskStorageType_T *aStorageType);
|
---|
77 | STDMETHOD(COMGETTER(Location)) (BSTR *aLocation);
|
---|
78 | STDMETHOD(COMGETTER(Type)) (HardDiskType_T *aType);
|
---|
79 | STDMETHOD(COMSETTER(Type)) (HardDiskType_T aType);
|
---|
80 | STDMETHOD(COMGETTER(Parent)) (IHardDisk **aParent);
|
---|
81 | STDMETHOD(COMGETTER(Children)) (IHardDiskCollection **aChildren);
|
---|
82 | STDMETHOD(COMGETTER(Root)) (IHardDisk **aRoot);
|
---|
83 | STDMETHOD(COMGETTER(Accessible)) (BOOL *aAccessible);
|
---|
84 | STDMETHOD(COMGETTER(AllAccessible)) (BOOL *aAllAccessible);
|
---|
85 | STDMETHOD(COMGETTER(LastAccessError)) (BSTR *aLastAccessError);
|
---|
86 | STDMETHOD(COMGETTER(MachineId)) (GUIDPARAMOUT aMachineId);
|
---|
87 | STDMETHOD(COMGETTER(SnapshotId)) (GUIDPARAMOUT aSnapshotId);
|
---|
88 |
|
---|
89 | // IHardDisk methods
|
---|
90 | STDMETHOD(CloneToImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage,
|
---|
91 | IProgress **aProgress);
|
---|
92 |
|
---|
93 | // public methods for internal purposes only
|
---|
94 |
|
---|
95 | const Guid &id() const { return mId; }
|
---|
96 | HardDiskStorageType_T storageType() const { return mStorageType; }
|
---|
97 | HardDiskType_T type() const { return mType; }
|
---|
98 | const Guid &machineId() const { return mMachineId; }
|
---|
99 | const Guid &snapshotId() const { return mSnapshotId; }
|
---|
100 |
|
---|
101 | void setMachineId (const Guid &aId) { mMachineId = aId; }
|
---|
102 | void setSnapshotId (const Guid &aId) { mSnapshotId = aId; }
|
---|
103 |
|
---|
104 | bool isDifferencing() const
|
---|
105 | {
|
---|
106 | return mType == HardDiskType_NormalHardDisk &&
|
---|
107 | mStorageType == HardDiskStorageType_VirtualDiskImage &&
|
---|
108 | !mParent.isNull();
|
---|
109 | }
|
---|
110 | bool isParentImmutable() const
|
---|
111 | {
|
---|
112 | AutoLock parentLock (mParent);
|
---|
113 | return !mParent.isNull() && mParent->type() == HardDiskType_ImmutableHardDisk;
|
---|
114 | }
|
---|
115 |
|
---|
116 | inline HVirtualDiskImage *asVDI();
|
---|
117 |
|
---|
118 | ComObjPtr <HardDisk> parent() const { return static_cast <HardDisk *> (mParent); }
|
---|
119 |
|
---|
120 | /** Shortcut to #dependentChildrenLock() */
|
---|
121 | AutoLock::Handle &childrenLock() const { return dependentChildrenLock(); }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Shortcut to #dependentChildren().
|
---|
125 | * Do |AutoLock alock (childrenLock());| before acceessing the returned list!
|
---|
126 | */
|
---|
127 | const HardDiskList &children() const { return dependentChildren(); }
|
---|
128 |
|
---|
129 | ComObjPtr <HardDisk> root() const;
|
---|
130 |
|
---|
131 | HRESULT getBaseAccessible (Bstr &aAccessError, bool aCheckBusy = false);
|
---|
132 |
|
---|
133 | // virtual methods that need to be [re]implemented by every subclass
|
---|
134 |
|
---|
135 | virtual HRESULT trySetRegistered (BOOL aRegistered);
|
---|
136 | virtual HRESULT getAccessible (Bstr &aAccessError) = 0;
|
---|
137 |
|
---|
138 | virtual HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode) = 0;
|
---|
139 |
|
---|
140 | virtual void updatePath (const char *aOldPath, const char *aNewPath) {}
|
---|
141 |
|
---|
142 | virtual Bstr toString (bool aShort = false) = 0;
|
---|
143 | virtual bool sameAs (HardDisk *that);
|
---|
144 |
|
---|
145 | virtual HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
|
---|
146 | Progress *aProgress) = 0;
|
---|
147 | virtual HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
|
---|
148 | Progress *aProgress) = 0;
|
---|
149 | public:
|
---|
150 |
|
---|
151 | void setBusy();
|
---|
152 | void clearBusy();
|
---|
153 | void addReader();
|
---|
154 | void releaseReader();
|
---|
155 | void addReaderOnAncestors();
|
---|
156 | void releaseReaderOnAncestors();
|
---|
157 | bool hasForeignChildren();
|
---|
158 |
|
---|
159 | HRESULT setBusyWithChildren();
|
---|
160 | void clearBusyWithChildren();
|
---|
161 | HRESULT getAccessibleWithChildren (Bstr &aAccessError);
|
---|
162 |
|
---|
163 | HRESULT checkConsistency();
|
---|
164 |
|
---|
165 | HRESULT createDiffHardDisk (const Bstr &aFolder, const Guid &aMachineId,
|
---|
166 | ComObjPtr <HVirtualDiskImage> &aHardDisk,
|
---|
167 | Progress *aProgress);
|
---|
168 |
|
---|
169 | void updatePaths (const char *aOldPath, const char *aNewPath);
|
---|
170 |
|
---|
171 | /* the following must be called from under the lock */
|
---|
172 | bool isBusy() { isLockedOnCurrentThread(); return mBusy; }
|
---|
173 | unsigned readers() { isLockedOnCurrentThread(); return mReaders; }
|
---|
174 | const Bstr &lastAccessError() const { return mLastAccessError; }
|
---|
175 |
|
---|
176 | // for VirtualBoxSupportErrorInfoImpl
|
---|
177 | static const wchar_t *getComponentName() { return L"HardDisk"; }
|
---|
178 |
|
---|
179 | protected:
|
---|
180 |
|
---|
181 | HRESULT loadSettings (CFGNODE aHDNode);
|
---|
182 | HRESULT saveSettings (CFGNODE aHDNode);
|
---|
183 |
|
---|
184 | /** weak VirualBox parent */
|
---|
185 | ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
|
---|
186 |
|
---|
187 | BOOL mRegistered;
|
---|
188 |
|
---|
189 | ComObjPtr <HardDisk, ComWeakRef> mParent;
|
---|
190 |
|
---|
191 | Guid mId;
|
---|
192 | HardDiskStorageType_T mStorageType;
|
---|
193 | HardDiskType_T mType;
|
---|
194 | Guid mMachineId;
|
---|
195 | Guid mSnapshotId;
|
---|
196 |
|
---|
197 | private:
|
---|
198 |
|
---|
199 | Bstr mLastAccessError;
|
---|
200 |
|
---|
201 | bool mBusy;
|
---|
202 | unsigned mReaders;
|
---|
203 | };
|
---|
204 |
|
---|
205 | ////////////////////////////////////////////////////////////////////////////////
|
---|
206 |
|
---|
207 | class ATL_NO_VTABLE HVirtualDiskImage :
|
---|
208 | public HardDisk,
|
---|
209 | public VirtualBoxSupportTranslation <HVirtualDiskImage>,
|
---|
210 | public IVirtualDiskImage
|
---|
211 | {
|
---|
212 |
|
---|
213 | public:
|
---|
214 |
|
---|
215 | VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(HVirtualDiskImage)
|
---|
216 |
|
---|
217 | DECLARE_NOT_AGGREGATABLE(HVirtualDiskImage)
|
---|
218 |
|
---|
219 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
220 |
|
---|
221 | BEGIN_COM_MAP(HVirtualDiskImage)
|
---|
222 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
223 | COM_INTERFACE_ENTRY(IHardDisk)
|
---|
224 | COM_INTERFACE_ENTRY(IVirtualDiskImage)
|
---|
225 | END_COM_MAP()
|
---|
226 |
|
---|
227 | NS_DECL_ISUPPORTS
|
---|
228 |
|
---|
229 | HRESULT FinalConstruct();
|
---|
230 | void FinalRelease();
|
---|
231 |
|
---|
232 | // public initializer/uninitializer for internal purposes only
|
---|
233 |
|
---|
234 | HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
|
---|
235 | CFGNODE aHDNode, CFGNODE aVDINode);
|
---|
236 | HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
|
---|
237 | const BSTR aFilePath, BOOL aRegistered = FALSE);
|
---|
238 | void uninit();
|
---|
239 |
|
---|
240 | // IHardDisk properties
|
---|
241 | STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
|
---|
242 | STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
|
---|
243 | STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
|
---|
244 | STDMETHOD(COMGETTER(ActualSize)) (ULONG64 *aActualSize);
|
---|
245 |
|
---|
246 | // IVirtualDiskImage properties
|
---|
247 | STDMETHOD(COMGETTER(FilePath)) (BSTR *aFilePath);
|
---|
248 | STDMETHOD(COMSETTER(FilePath)) (INPTR BSTR aFilePath);
|
---|
249 | STDMETHOD(COMGETTER(Created)) (BOOL *aCreated);
|
---|
250 |
|
---|
251 | // IVirtualDiskImage methods
|
---|
252 | STDMETHOD(CreateDynamicImage) (ULONG64 aSize, IProgress **aProgress);
|
---|
253 | STDMETHOD(CreateFixedImage) (ULONG64 aSize, IProgress **aProgress);
|
---|
254 | STDMETHOD(DeleteImage)();
|
---|
255 |
|
---|
256 | // public methods for internal purposes only
|
---|
257 |
|
---|
258 | const Bstr &filePath() const { return mFilePath; }
|
---|
259 | const Bstr &filePathFull() const { return mFilePathFull; }
|
---|
260 |
|
---|
261 | HRESULT trySetRegistered (BOOL aRegistered);
|
---|
262 | HRESULT getAccessible (Bstr &aAccessError);
|
---|
263 |
|
---|
264 | HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode);
|
---|
265 |
|
---|
266 | void updatePath (const char *aOldPath, const char *aNewPath);
|
---|
267 |
|
---|
268 | Bstr toString (bool aShort = false);
|
---|
269 |
|
---|
270 | HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
|
---|
271 | Progress *aProgress);
|
---|
272 | HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
|
---|
273 | Progress *aProgress);
|
---|
274 |
|
---|
275 | HRESULT cloneDiffImage (const Bstr &aFolder, const Guid &aMachineId,
|
---|
276 | ComObjPtr <HVirtualDiskImage> &aHardDisk,
|
---|
277 | Progress *aProgress);
|
---|
278 |
|
---|
279 | HRESULT mergeImageToParent (Progress *aProgress);
|
---|
280 | HRESULT mergeImageToChildren (Progress *aProgress);
|
---|
281 |
|
---|
282 | HRESULT wipeOutImage();
|
---|
283 |
|
---|
284 | // for VirtualBoxSupportErrorInfoImpl
|
---|
285 | static const wchar_t *getComponentName() { return L"VirtualDiskImage"; }
|
---|
286 |
|
---|
287 | private:
|
---|
288 |
|
---|
289 | HRESULT setFilePath (const BSTR aFilePath);
|
---|
290 | HRESULT queryInformation (Bstr *aAccessError);
|
---|
291 | HRESULT createImage (ULONG64 aSize, BOOL aDynamic, IProgress **aProgress);
|
---|
292 |
|
---|
293 | /** VDI asynchronous operation thread function */
|
---|
294 | static DECLCALLBACK(int) vdiTaskThread (RTTHREAD thread, void *pvUser);
|
---|
295 |
|
---|
296 | enum State
|
---|
297 | {
|
---|
298 | NotCreated,
|
---|
299 | Created,
|
---|
300 | /* the following must be greater than Created */
|
---|
301 | Accessible,
|
---|
302 | };
|
---|
303 |
|
---|
304 | State mState;
|
---|
305 |
|
---|
306 | RTSEMEVENTMULTI mStateCheckSem;
|
---|
307 | ULONG mStateCheckWaiters;
|
---|
308 |
|
---|
309 | Bstr mDescription;
|
---|
310 |
|
---|
311 | ULONG64 mSize;
|
---|
312 | ULONG64 mActualSize;
|
---|
313 |
|
---|
314 | Bstr mFilePath;
|
---|
315 | Bstr mFilePathFull;
|
---|
316 |
|
---|
317 | friend class HardDisk;
|
---|
318 | };
|
---|
319 |
|
---|
320 | // dependent inline members
|
---|
321 | ////////////////////////////////////////////////////////////////////////////////
|
---|
322 |
|
---|
323 | inline HVirtualDiskImage *HardDisk::asVDI()
|
---|
324 | {
|
---|
325 | AssertReturn (mStorageType == HardDiskStorageType_VirtualDiskImage, 0);
|
---|
326 | return static_cast <HVirtualDiskImage *> (this);
|
---|
327 | }
|
---|
328 |
|
---|
329 | ////////////////////////////////////////////////////////////////////////////////
|
---|
330 |
|
---|
331 | class ATL_NO_VTABLE HISCSIHardDisk :
|
---|
332 | public HardDisk,
|
---|
333 | public VirtualBoxSupportTranslation <HISCSIHardDisk>,
|
---|
334 | public IISCSIHardDisk
|
---|
335 | {
|
---|
336 |
|
---|
337 | public:
|
---|
338 |
|
---|
339 | VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(HISCSIHardDisk)
|
---|
340 |
|
---|
341 | DECLARE_NOT_AGGREGATABLE(HISCSIHardDisk)
|
---|
342 |
|
---|
343 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
344 |
|
---|
345 | BEGIN_COM_MAP(HISCSIHardDisk)
|
---|
346 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
347 | COM_INTERFACE_ENTRY(IHardDisk)
|
---|
348 | COM_INTERFACE_ENTRY(IISCSIHardDisk)
|
---|
349 | END_COM_MAP()
|
---|
350 |
|
---|
351 | NS_DECL_ISUPPORTS
|
---|
352 |
|
---|
353 | HRESULT FinalConstruct();
|
---|
354 | void FinalRelease();
|
---|
355 |
|
---|
356 | // public initializer/uninitializer for internal purposes only
|
---|
357 |
|
---|
358 | HRESULT init (VirtualBox *aVirtualBox,
|
---|
359 | CFGNODE aHDNode, CFGNODE aISCSINode);
|
---|
360 | HRESULT init (VirtualBox *aVirtualBox);
|
---|
361 | void uninit();
|
---|
362 |
|
---|
363 | // IHardDisk properties
|
---|
364 | STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
|
---|
365 | STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
|
---|
366 | STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
|
---|
367 | STDMETHOD(COMGETTER(ActualSize)) (ULONG64 *aActualSize);
|
---|
368 |
|
---|
369 | // IISCSIHardDisk properties
|
---|
370 | STDMETHOD(COMGETTER(Server)) (BSTR *aServer);
|
---|
371 | STDMETHOD(COMSETTER(Server)) (INPTR BSTR aServer);
|
---|
372 | STDMETHOD(COMGETTER(Port)) (USHORT *aPort);
|
---|
373 | STDMETHOD(COMSETTER(Port)) (USHORT aPort);
|
---|
374 | STDMETHOD(COMGETTER(Target)) (BSTR *aTarget);
|
---|
375 | STDMETHOD(COMSETTER(Target)) (INPTR BSTR aTarget);
|
---|
376 | STDMETHOD(COMGETTER(Lun)) (ULONG64 *aLun);
|
---|
377 | STDMETHOD(COMSETTER(Lun)) (ULONG64 aLun);
|
---|
378 | STDMETHOD(COMGETTER(UserName)) (BSTR *aUserName);
|
---|
379 | STDMETHOD(COMSETTER(UserName)) (INPTR BSTR aUserName);
|
---|
380 | STDMETHOD(COMGETTER(Password)) (BSTR *aPassword);
|
---|
381 | STDMETHOD(COMSETTER(Password)) (INPTR BSTR aPassword);
|
---|
382 |
|
---|
383 | // public methods for internal purposes only
|
---|
384 |
|
---|
385 | const Bstr &server() const { return mServer; }
|
---|
386 | USHORT port() const { return mPort; }
|
---|
387 | const Bstr &target() const { return mTarget; }
|
---|
388 | ULONG64 lun() const { return mLun; }
|
---|
389 | const Bstr &userName() const { return mUserName; }
|
---|
390 | const Bstr &password() const { return mPassword; }
|
---|
391 |
|
---|
392 | HRESULT trySetRegistered (BOOL aRegistered);
|
---|
393 | HRESULT getAccessible (Bstr &aAccessError);
|
---|
394 |
|
---|
395 | HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode);
|
---|
396 |
|
---|
397 | Bstr toString (bool aShort = false);
|
---|
398 |
|
---|
399 | HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
|
---|
400 | Progress *aProgress);
|
---|
401 | HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
|
---|
402 | Progress *aProgress);
|
---|
403 |
|
---|
404 | public:
|
---|
405 |
|
---|
406 | // for VirtualBoxSupportErrorInfoImpl
|
---|
407 | static const wchar_t *getComponentName() { return L"ISCSIHardDisk"; }
|
---|
408 |
|
---|
409 | private:
|
---|
410 |
|
---|
411 | HRESULT queryInformation (Bstr &aAccessError);
|
---|
412 |
|
---|
413 | Bstr mDescription;
|
---|
414 |
|
---|
415 | ULONG64 mSize;
|
---|
416 | ULONG64 mActualSize;
|
---|
417 |
|
---|
418 | Bstr mServer;
|
---|
419 | USHORT mPort;
|
---|
420 | Bstr mTarget;
|
---|
421 | ULONG64 mLun;
|
---|
422 | Bstr mUserName;
|
---|
423 | Bstr mPassword;
|
---|
424 | };
|
---|
425 |
|
---|
426 |
|
---|
427 | COM_DECL_READONLY_ENUM_AND_COLLECTION (HardDisk)
|
---|
428 |
|
---|
429 | #endif // ____H_HARDDISKIMPL
|
---|