VirtualBox

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

Last change on this file since 1344 was 953, checked in by vboxsync, 18 years ago

Main: Prototyped IVMDKImage.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
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
34class VirtualBox;
35class Progress;
36class HVirtualDiskImage;
37
38////////////////////////////////////////////////////////////////////////////////
39
40class ATL_NO_VTABLE HardDisk :
41 public VirtualBoxSupportErrorInfoImpl <HardDisk, IHardDisk>,
42 public VirtualBoxSupportTranslation <HardDisk>,
43 public VirtualBoxBaseWithTypedChildren <HardDisk>,
44 public IHardDisk
45{
46
47public:
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
66protected:
67
68 // protected initializer/uninitializer for internal purposes only
69 HRESULT protectedInit (VirtualBox *aVirtualBox, HardDisk *aParent);
70 void protectedUninit (AutoLock &alock);
71
72public:
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;
149public:
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 static HRESULT openHardDisk (VirtualBox *aVirtualBox, INPTR BSTR aLocation,
177 ComObjPtr <HardDisk> &hardDisk);
178
179 // for VirtualBoxSupportErrorInfoImpl
180 static const wchar_t *getComponentName() { return L"HardDisk"; }
181
182protected:
183
184 HRESULT loadSettings (CFGNODE aHDNode);
185 HRESULT saveSettings (CFGNODE aHDNode);
186
187 /** weak VirualBox parent */
188 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
189
190 BOOL mRegistered;
191
192 ComObjPtr <HardDisk, ComWeakRef> mParent;
193
194 Guid mId;
195 HardDiskStorageType_T mStorageType;
196 HardDiskType_T mType;
197 Guid mMachineId;
198 Guid mSnapshotId;
199
200private:
201
202 Bstr mLastAccessError;
203
204 bool mBusy;
205 unsigned mReaders;
206};
207
208////////////////////////////////////////////////////////////////////////////////
209
210class ATL_NO_VTABLE HVirtualDiskImage :
211 public HardDisk,
212 public VirtualBoxSupportTranslation <HVirtualDiskImage>,
213 public IVirtualDiskImage
214{
215
216public:
217
218 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(HVirtualDiskImage)
219
220 DECLARE_NOT_AGGREGATABLE(HVirtualDiskImage)
221
222 DECLARE_PROTECT_FINAL_CONSTRUCT()
223
224 BEGIN_COM_MAP(HVirtualDiskImage)
225 COM_INTERFACE_ENTRY(ISupportErrorInfo)
226 COM_INTERFACE_ENTRY(IHardDisk)
227 COM_INTERFACE_ENTRY(IVirtualDiskImage)
228 END_COM_MAP()
229
230 NS_DECL_ISUPPORTS
231
232 HRESULT FinalConstruct();
233 void FinalRelease();
234
235 // public initializer/uninitializer for internal purposes only
236
237 HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
238 CFGNODE aHDNode, CFGNODE aVDINode);
239 HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
240 const BSTR aFilePath, BOOL aRegistered = FALSE);
241 void uninit();
242
243 // IHardDisk properties
244 STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
245 STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
246 STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
247 STDMETHOD(COMGETTER(ActualSize)) (ULONG64 *aActualSize);
248
249 // IVirtualDiskImage properties
250 STDMETHOD(COMGETTER(FilePath)) (BSTR *aFilePath);
251 STDMETHOD(COMSETTER(FilePath)) (INPTR BSTR aFilePath);
252 STDMETHOD(COMGETTER(Created)) (BOOL *aCreated);
253
254 // IVirtualDiskImage methods
255 STDMETHOD(CreateDynamicImage) (ULONG64 aSize, IProgress **aProgress);
256 STDMETHOD(CreateFixedImage) (ULONG64 aSize, IProgress **aProgress);
257 STDMETHOD(DeleteImage)();
258
259 // public methods for internal purposes only
260
261 const Bstr &filePath() const { return mFilePath; }
262 const Bstr &filePathFull() const { return mFilePathFull; }
263
264 HRESULT trySetRegistered (BOOL aRegistered);
265 HRESULT getAccessible (Bstr &aAccessError);
266
267 HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode);
268
269 void updatePath (const char *aOldPath, const char *aNewPath);
270
271 Bstr toString (bool aShort = false);
272
273 HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
274 Progress *aProgress);
275 HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
276 Progress *aProgress);
277
278 HRESULT cloneDiffImage (const Bstr &aFolder, const Guid &aMachineId,
279 ComObjPtr <HVirtualDiskImage> &aHardDisk,
280 Progress *aProgress);
281
282 HRESULT mergeImageToParent (Progress *aProgress);
283 HRESULT mergeImageToChildren (Progress *aProgress);
284
285 HRESULT wipeOutImage();
286
287 // for VirtualBoxSupportErrorInfoImpl
288 static const wchar_t *getComponentName() { return L"VirtualDiskImage"; }
289
290private:
291
292 HRESULT setFilePath (const BSTR aFilePath);
293 HRESULT queryInformation (Bstr *aAccessError);
294 HRESULT createImage (ULONG64 aSize, BOOL aDynamic, IProgress **aProgress);
295
296 /** VDI asynchronous operation thread function */
297 static DECLCALLBACK(int) vdiTaskThread (RTTHREAD thread, void *pvUser);
298
299 enum State
300 {
301 NotCreated,
302 Created,
303 /* the following must be greater than Created */
304 Accessible,
305 };
306
307 State mState;
308
309 RTSEMEVENTMULTI mStateCheckSem;
310 ULONG mStateCheckWaiters;
311
312 Bstr mDescription;
313
314 ULONG64 mSize;
315 ULONG64 mActualSize;
316
317 Bstr mFilePath;
318 Bstr mFilePathFull;
319
320 friend class HardDisk;
321};
322
323// dependent inline members
324////////////////////////////////////////////////////////////////////////////////
325
326inline HVirtualDiskImage *HardDisk::asVDI()
327{
328 AssertReturn (mStorageType == HardDiskStorageType_VirtualDiskImage, 0);
329 return static_cast <HVirtualDiskImage *> (this);
330}
331
332////////////////////////////////////////////////////////////////////////////////
333
334class ATL_NO_VTABLE HISCSIHardDisk :
335 public HardDisk,
336 public VirtualBoxSupportTranslation <HISCSIHardDisk>,
337 public IISCSIHardDisk
338{
339
340public:
341
342 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(HISCSIHardDisk)
343
344 DECLARE_NOT_AGGREGATABLE(HISCSIHardDisk)
345
346 DECLARE_PROTECT_FINAL_CONSTRUCT()
347
348 BEGIN_COM_MAP(HISCSIHardDisk)
349 COM_INTERFACE_ENTRY(ISupportErrorInfo)
350 COM_INTERFACE_ENTRY(IHardDisk)
351 COM_INTERFACE_ENTRY(IISCSIHardDisk)
352 END_COM_MAP()
353
354 NS_DECL_ISUPPORTS
355
356 HRESULT FinalConstruct();
357 void FinalRelease();
358
359 // public initializer/uninitializer for internal purposes only
360
361 HRESULT init (VirtualBox *aVirtualBox,
362 CFGNODE aHDNode, CFGNODE aISCSINode);
363 HRESULT init (VirtualBox *aVirtualBox);
364 void uninit();
365
366 // IHardDisk properties
367 STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
368 STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
369 STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
370 STDMETHOD(COMGETTER(ActualSize)) (ULONG64 *aActualSize);
371
372 // IISCSIHardDisk properties
373 STDMETHOD(COMGETTER(Server)) (BSTR *aServer);
374 STDMETHOD(COMSETTER(Server)) (INPTR BSTR aServer);
375 STDMETHOD(COMGETTER(Port)) (USHORT *aPort);
376 STDMETHOD(COMSETTER(Port)) (USHORT aPort);
377 STDMETHOD(COMGETTER(Target)) (BSTR *aTarget);
378 STDMETHOD(COMSETTER(Target)) (INPTR BSTR aTarget);
379 STDMETHOD(COMGETTER(Lun)) (ULONG64 *aLun);
380 STDMETHOD(COMSETTER(Lun)) (ULONG64 aLun);
381 STDMETHOD(COMGETTER(UserName)) (BSTR *aUserName);
382 STDMETHOD(COMSETTER(UserName)) (INPTR BSTR aUserName);
383 STDMETHOD(COMGETTER(Password)) (BSTR *aPassword);
384 STDMETHOD(COMSETTER(Password)) (INPTR BSTR aPassword);
385
386 // public methods for internal purposes only
387
388 const Bstr &server() const { return mServer; }
389 USHORT port() const { return mPort; }
390 const Bstr &target() const { return mTarget; }
391 ULONG64 lun() const { return mLun; }
392 const Bstr &userName() const { return mUserName; }
393 const Bstr &password() const { return mPassword; }
394
395 HRESULT trySetRegistered (BOOL aRegistered);
396 HRESULT getAccessible (Bstr &aAccessError);
397
398 HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode);
399
400 Bstr toString (bool aShort = false);
401
402 HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
403 Progress *aProgress);
404 HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
405 Progress *aProgress);
406
407public:
408
409 // for VirtualBoxSupportErrorInfoImpl
410 static const wchar_t *getComponentName() { return L"ISCSIHardDisk"; }
411
412private:
413
414 HRESULT queryInformation (Bstr &aAccessError);
415
416 Bstr mDescription;
417
418 ULONG64 mSize;
419 ULONG64 mActualSize;
420
421 Bstr mServer;
422 USHORT mPort;
423 Bstr mTarget;
424 ULONG64 mLun;
425 Bstr mUserName;
426 Bstr mPassword;
427};
428
429////////////////////////////////////////////////////////////////////////////////
430
431class ATL_NO_VTABLE HVMDKImage :
432 public HardDisk,
433 public VirtualBoxSupportTranslation <HVMDKImage>,
434 public IVMDKImage
435{
436
437public:
438
439 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(HVMDKImage)
440
441 DECLARE_NOT_AGGREGATABLE(HVMDKImage)
442
443 DECLARE_PROTECT_FINAL_CONSTRUCT()
444
445 BEGIN_COM_MAP(HVMDKImage)
446 COM_INTERFACE_ENTRY(ISupportErrorInfo)
447 COM_INTERFACE_ENTRY(IHardDisk)
448 COM_INTERFACE_ENTRY(IVMDKImage)
449 END_COM_MAP()
450
451 NS_DECL_ISUPPORTS
452
453 HRESULT FinalConstruct();
454 void FinalRelease();
455
456 // public initializer/uninitializer for internal purposes only
457
458 HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
459 CFGNODE aHDNode, CFGNODE aVMDKNode);
460 HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
461 INPTR BSTR aFilePath, BOOL aRegistered = FALSE);
462 void uninit();
463
464 // IHardDisk properties
465 STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
466 STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
467 STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
468 STDMETHOD(COMGETTER(ActualSize)) (ULONG64 *aActualSize);
469
470 // IVirtualDiskImage properties
471 STDMETHOD(COMGETTER(FilePath)) (BSTR *aFilePath);
472 STDMETHOD(COMSETTER(FilePath)) (INPTR BSTR aFilePath);
473 STDMETHOD(COMGETTER(Created)) (BOOL *aCreated);
474
475 // IVirtualDiskImage methods
476 STDMETHOD(CreateDynamicImage) (ULONG64 aSize, IProgress **aProgress);
477 STDMETHOD(CreateFixedImage) (ULONG64 aSize, IProgress **aProgress);
478 STDMETHOD(DeleteImage)();
479
480 // public methods for internal purposes only
481
482 const Bstr &filePath() const { return mFilePath; }
483 const Bstr &filePathFull() const { return mFilePathFull; }
484
485 HRESULT trySetRegistered (BOOL aRegistered);
486 HRESULT getAccessible (Bstr &aAccessError);
487
488 HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode);
489
490 void updatePath (const char *aOldPath, const char *aNewPath);
491
492 Bstr toString (bool aShort = false);
493
494 HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
495 Progress *aProgress);
496 HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
497 Progress *aProgress);
498
499 // for VirtualBoxSupportErrorInfoImpl
500 static const wchar_t *getComponentName() { return L"VMDKImage"; }
501
502private:
503
504 HRESULT setFilePath (const BSTR aFilePath);
505 HRESULT queryInformation (Bstr *aAccessError);
506 HRESULT createImage (ULONG64 aSize, BOOL aDynamic, IProgress **aProgress);
507
508 /** VDI asynchronous operation thread function */
509 static DECLCALLBACK(int) vdiTaskThread (RTTHREAD thread, void *pvUser);
510
511 enum State
512 {
513 NotCreated,
514 Created,
515 /* the following must be greater than Created */
516 Accessible,
517 };
518
519 State mState;
520
521 RTSEMEVENTMULTI mStateCheckSem;
522 ULONG mStateCheckWaiters;
523
524 Bstr mDescription;
525
526 ULONG64 mSize;
527 ULONG64 mActualSize;
528
529 Bstr mFilePath;
530 Bstr mFilePathFull;
531
532 friend class HardDisk;
533};
534
535
536COM_DECL_READONLY_ENUM_AND_COLLECTION (HardDisk)
537
538#endif // ____H_HARDDISKIMPL
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