VirtualBox

source: vbox/trunk/src/VBox/Main/include/MediumImpl.h@ 72948

Last change on this file since 72948 was 72948, checked in by vboxsync, 6 years ago

Main: Introducing IMediumIO for accessing the content of a medium. The new IMedium::openForIO method instantiates and returns this interface. N.B. Write access hasn't been implemented yet.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.7 KB
Line 
1/* $Id: MediumImpl.h 72948 2018-07-07 16:20:42Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2008-2017 Oracle Corporation
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19#ifndef ____H_MEDIUMIMPL
20#define ____H_MEDIUMIMPL
21
22#include <VBox/vd.h>
23#include "MediumWrap.h"
24#include "VirtualBoxBase.h"
25#include "AutoCaller.h"
26#include "SecretKeyStore.h"
27class Progress;
28class MediumFormat;
29class MediumLockList;
30struct MediumCryptoFilterSettings;
31
32namespace settings
33{
34 struct Medium;
35}
36
37////////////////////////////////////////////////////////////////////////////////
38
39/**
40 * Medium component class for all media types.
41 */
42class ATL_NO_VTABLE Medium :
43 public MediumWrap
44{
45public:
46 DECLARE_EMPTY_CTOR_DTOR(Medium)
47
48 HRESULT FinalConstruct();
49 void FinalRelease();
50
51 enum HDDOpenMode { OpenReadWrite, OpenReadOnly };
52 // have to use a special enum for the overloaded init() below;
53 // can't use AccessMode_T from XIDL because that's mapped to an int
54 // and would be ambiguous
55
56 // public initializer/uninitializer for internal purposes only
57
58 // initializer to create empty medium (VirtualBox::CreateMedium())
59 HRESULT init(VirtualBox *aVirtualBox,
60 const Utf8Str &aFormat,
61 const Utf8Str &aLocation,
62 const Guid &uuidMachineRegistry,
63 const DeviceType_T aDeviceType);
64
65 // initializer for opening existing media
66 // (VirtualBox::OpenMedium(); Machine::AttachDevice())
67 HRESULT init(VirtualBox *aVirtualBox,
68 const Utf8Str &aLocation,
69 HDDOpenMode enOpenMode,
70 bool fForceNewUuid,
71 DeviceType_T aDeviceType);
72
73 // initializer used when loading settings
74 HRESULT initOne(Medium *aParent,
75 DeviceType_T aDeviceType,
76 const Guid &uuidMachineRegistry,
77 const settings::Medium &data,
78 const Utf8Str &strMachineFolder);
79 HRESULT init(VirtualBox *aVirtualBox,
80 Medium *aParent,
81 DeviceType_T aDeviceType,
82 const Guid &uuidMachineRegistry,
83 const settings::Medium &data,
84 const Utf8Str &strMachineFolder,
85 AutoWriteLock &mediaTreeLock);
86
87 // initializer for host floppy/DVD
88 HRESULT init(VirtualBox *aVirtualBox,
89 DeviceType_T aDeviceType,
90 const Utf8Str &aLocation,
91 const Utf8Str &aDescription = Utf8Str::Empty);
92
93 void uninit();
94
95 void i_deparent();
96 void i_setParent(const ComObjPtr<Medium> &pParent);
97
98 // unsafe methods for internal purposes only (ensure there is
99 // a caller and a read lock before calling them!)
100 const ComObjPtr<Medium>& i_getParent() const;
101 const MediaList& i_getChildren() const;
102
103 const Guid& i_getId() const;
104 MediumState_T i_getState() const;
105 MediumVariant_T i_getVariant() const;
106 bool i_isHostDrive() const;
107 const Utf8Str& i_getLocationFull() const;
108 const Utf8Str& i_getFormat() const;
109 const ComObjPtr<MediumFormat> & i_getMediumFormat() const;
110 bool i_isMediumFormatFile() const;
111 uint64_t i_getSize() const;
112 uint64_t i_getLogicalSize() const;
113 DeviceType_T i_getDeviceType() const;
114 MediumType_T i_getType() const;
115 Utf8Str i_getName();
116
117 /* handles caller/locking itself */
118 bool i_addRegistry(const Guid &id);
119 /* handles caller/locking itself, caller is responsible for tree lock */
120 bool i_addRegistryRecursive(const Guid &id);
121 /* handles caller/locking itself */
122 bool i_removeRegistry(const Guid& id);
123 /* handles caller/locking itself, caller is responsible for tree lock */
124 bool i_removeRegistryRecursive(const Guid& id);
125 bool i_isInRegistry(const Guid& id);
126 bool i_getFirstRegistryMachineId(Guid &uuid) const;
127 void i_markRegistriesModified();
128
129 HRESULT i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue);
130
131 HRESULT i_addBackReference(const Guid &aMachineId,
132 const Guid &aSnapshotId = Guid::Empty);
133 HRESULT i_removeBackReference(const Guid &aMachineId,
134 const Guid &aSnapshotId = Guid::Empty);
135
136
137 const Guid* i_getFirstMachineBackrefId() const;
138 const Guid* i_getAnyMachineBackref() const;
139 const Guid* i_getFirstMachineBackrefSnapshotId() const;
140 size_t i_getMachineBackRefCount() const;
141
142#ifdef DEBUG
143 void i_dumpBackRefs();
144#endif
145
146 HRESULT i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath);
147
148 /* handles caller/locking itself */
149 ComObjPtr<Medium> i_getBase(uint32_t *aLevel = NULL);
150 /* handles caller/locking itself */
151 uint32_t i_getDepth();
152
153 bool i_isReadOnly();
154 void i_updateId(const Guid &id);
155
156 void i_saveSettingsOne(settings::Medium &data,
157 const Utf8Str &strHardDiskFolder);
158 HRESULT i_saveSettings(settings::Medium &data,
159 const Utf8Str &strHardDiskFolder);
160
161 HRESULT i_createMediumLockList(bool fFailIfInaccessible,
162 Medium *pToLock,
163 bool fMediumLockWriteAll,
164 Medium *pToBeParent,
165 MediumLockList &mediumLockList);
166
167 HRESULT i_createDiffStorage(ComObjPtr<Medium> &aTarget,
168 MediumVariant_T aVariant,
169 MediumLockList *pMediumLockList,
170 ComObjPtr<Progress> *aProgress,
171 bool aWait);
172 Utf8Str i_getPreferredDiffFormat();
173 MediumVariant_T i_getPreferredDiffVariant();
174
175 HRESULT i_close(AutoCaller &autoCaller);
176 HRESULT i_unlockRead(MediumState_T *aState);
177 HRESULT i_unlockWrite(MediumState_T *aState);
178 HRESULT i_deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait);
179 HRESULT i_markForDeletion();
180 HRESULT i_unmarkForDeletion();
181 HRESULT i_markLockedForDeletion();
182 HRESULT i_unmarkLockedForDeletion();
183
184 HRESULT i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
185 bool &fMergeForward);
186
187 HRESULT i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
188 const Guid *aMachineId,
189 const Guid *aSnapshotId,
190 bool fLockMedia,
191 bool &fMergeForward,
192 ComObjPtr<Medium> &pParentForTarget,
193 MediumLockList * &aChildrenToReparent,
194 MediumLockList * &aMediumLockList);
195 HRESULT i_mergeTo(const ComObjPtr<Medium> &pTarget,
196 bool fMergeForward,
197 const ComObjPtr<Medium> &pParentForTarget,
198 MediumLockList *aChildrenToReparent,
199 MediumLockList *aMediumLockList,
200 ComObjPtr<Progress> *aProgress,
201 bool aWait);
202 void i_cancelMergeTo(MediumLockList *aChildrenToReparent,
203 MediumLockList *aMediumLockList);
204
205 HRESULT i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent);
206
207 HRESULT i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
208 const ComObjPtr<Progress> &aProgress, bool fSparse);
209
210 HRESULT i_exportFile(const char *aFilename,
211 const ComObjPtr<MediumFormat> &aFormat,
212 MediumVariant_T aVariant,
213 SecretKeyStore *pKeyStore,
214 RTVFSIOSTREAM hVfsIosDst,
215 const ComObjPtr<Progress> &aProgress);
216 HRESULT i_importFile(const char *aFilename,
217 const ComObjPtr<MediumFormat> &aFormat,
218 MediumVariant_T aVariant,
219 RTVFSIOSTREAM hVfsIosSrc,
220 const ComObjPtr<Medium> &aParent,
221 const ComObjPtr<Progress> &aProgress);
222
223 HRESULT i_cloneToEx(const ComObjPtr<Medium> &aTarget, MediumVariant_T aVariant,
224 const ComObjPtr<Medium> &aParent, IProgress **aProgress,
225 uint32_t idxSrcImageSame, uint32_t idxDstImageSame);
226
227 const Utf8Str& i_getKeyId();
228
229 HRESULT i_openHddForIO(bool fWritable, SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
230 struct MediumCryptoFilterSettings *pCryptoSettingsRead);
231
232private:
233
234 // wrapped IMedium properties
235 HRESULT getId(com::Guid &aId);
236 HRESULT getDescription(AutoCaller &autoCaller, com::Utf8Str &aDescription);
237 HRESULT setDescription(AutoCaller &autoCaller, const com::Utf8Str &aDescription);
238 HRESULT getState(MediumState_T *aState);
239 HRESULT getVariant(std::vector<MediumVariant_T> &aVariant);
240 HRESULT getLocation(com::Utf8Str &aLocation);
241 HRESULT getName(com::Utf8Str &aName);
242 HRESULT getDeviceType(DeviceType_T *aDeviceType);
243 HRESULT getHostDrive(BOOL *aHostDrive);
244 HRESULT getSize(LONG64 *aSize);
245 HRESULT getFormat(com::Utf8Str &aFormat);
246 HRESULT getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat);
247 HRESULT getType(AutoCaller &autoCaller, MediumType_T *aType);
248 HRESULT setType(AutoCaller &autoCaller, MediumType_T aType);
249 HRESULT getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes);
250 HRESULT getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent);
251 HRESULT getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren);
252 HRESULT getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase);
253 HRESULT getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly);
254 HRESULT getLogicalSize(LONG64 *aLogicalSize);
255 HRESULT getAutoReset(BOOL *aAutoReset);
256 HRESULT setAutoReset(BOOL aAutoReset);
257 HRESULT getLastAccessError(com::Utf8Str &aLastAccessError);
258 HRESULT getMachineIds(std::vector<com::Guid> &aMachineIds);
259
260 // wrapped IMedium methods
261 HRESULT setIds(AutoCaller &aAutoCaller,
262 BOOL aSetImageId,
263 const com::Guid &aImageId,
264 BOOL aSetParentId,
265 const com::Guid &aParentId);
266 HRESULT refreshState(AutoCaller &aAutoCaller,
267 MediumState_T *aState);
268 HRESULT getSnapshotIds(const com::Guid &aMachineId,
269 std::vector<com::Guid> &aSnapshotIds);
270 HRESULT lockRead(ComPtr<IToken> &aToken);
271 HRESULT lockWrite(ComPtr<IToken> &aToken);
272 HRESULT close(AutoCaller &aAutoCaller);
273 HRESULT getProperty(const com::Utf8Str &aName,
274 com::Utf8Str &aValue);
275 HRESULT setProperty(const com::Utf8Str &aName,
276 const com::Utf8Str &aValue);
277 HRESULT getProperties(const com::Utf8Str &aNames,
278 std::vector<com::Utf8Str> &aReturnNames,
279 std::vector<com::Utf8Str> &aReturnValues);
280 HRESULT setProperties(const std::vector<com::Utf8Str> &aNames,
281 const std::vector<com::Utf8Str> &aValues);
282 HRESULT createBaseStorage(LONG64 aLogicalSize,
283 const std::vector<MediumVariant_T> &aVariant,
284 ComPtr<IProgress> &aProgress);
285 HRESULT deleteStorage(ComPtr<IProgress> &aProgress);
286 HRESULT createDiffStorage(AutoCaller &autoCaller,
287 const ComPtr<IMedium> &aTarget,
288 const std::vector<MediumVariant_T> &aVariant,
289 ComPtr<IProgress> &aProgress);
290 HRESULT mergeTo(const ComPtr<IMedium> &aTarget,
291 ComPtr<IProgress> &aProgress);
292 HRESULT cloneTo(const ComPtr<IMedium> &aTarget,
293 const std::vector<MediumVariant_T> &aVariant,
294 const ComPtr<IMedium> &aParent,
295 ComPtr<IProgress> &aProgress);
296 HRESULT cloneToBase(const ComPtr<IMedium> &aTarget,
297 const std::vector<MediumVariant_T> &aVariant,
298 ComPtr<IProgress> &aProgress);
299 HRESULT setLocation(AutoCaller &autoCaller,
300 const com::Utf8Str &aLocation,
301 ComPtr<IProgress> &aProgress);
302 HRESULT compact(ComPtr<IProgress> &aProgress);
303 HRESULT resize(LONG64 aLogicalSize,
304 ComPtr<IProgress> &aProgress);
305 HRESULT reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress);
306 HRESULT changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
307 const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
308 ComPtr<IProgress> &aProgress);
309 HRESULT getEncryptionSettings(AutoCaller &autoCaller, com::Utf8Str &aCipher, com::Utf8Str &aPasswordId);
310 HRESULT checkEncryptionPassword(const com::Utf8Str &aPassword);
311 HRESULT openForIO(BOOL aWritable, com::Utf8Str const &aPassword, ComPtr<IMediumIO> &aMediumIO);
312
313 // Private internal nmethods
314 HRESULT i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller);
315 HRESULT i_canClose();
316 HRESULT i_unregisterWithVirtualBox();
317 HRESULT i_setStateError();
318 HRESULT i_setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str::Empty);
319 HRESULT i_setFormat(const Utf8Str &aFormat);
320 VDTYPE i_convertDeviceType();
321 DeviceType_T i_convertToDeviceType(VDTYPE enmType);
322 Utf8Str i_vdError(int aVRC);
323
324 bool i_isPropertyForFilter(const com::Utf8Str &aName);
325
326 HRESULT i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
327 std::vector<com::Utf8Str> &aReturnValues);
328
329 HRESULT i_preparationForMoving(const Utf8Str &aLocation);
330 bool i_isMoveOperation(const ComObjPtr<Medium> &pTarget) const;
331 bool i_resetMoveOperationData();
332 Utf8Str i_getNewLocationForMoving() const;
333
334 static DECLCALLBACK(void) i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
335 const char *pszFormat, va_list va);
336 static DECLCALLBACK(bool) i_vdConfigAreKeysValid(void *pvUser,
337 const char *pszzValid);
338 static DECLCALLBACK(int) i_vdConfigQuerySize(void *pvUser, const char *pszName,
339 size_t *pcbValue);
340 static DECLCALLBACK(int) i_vdConfigQuery(void *pvUser, const char *pszName,
341 char *pszValue, size_t cchValue);
342 static DECLCALLBACK(int) i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock);
343 static DECLCALLBACK(int) i_vdTcpSocketDestroy(VDSOCKET Sock);
344 static DECLCALLBACK(int) i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
345 RTMSINTERVAL cMillies);
346 static DECLCALLBACK(int) i_vdTcpClientClose(VDSOCKET Sock);
347 static DECLCALLBACK(bool) i_vdTcpIsClientConnected(VDSOCKET Sock);
348 static DECLCALLBACK(int) i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies);
349 static DECLCALLBACK(int) i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
350 static DECLCALLBACK(int) i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
351 static DECLCALLBACK(int) i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf);
352 static DECLCALLBACK(int) i_vdTcpFlush(VDSOCKET Sock);
353 static DECLCALLBACK(int) i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable);
354 static DECLCALLBACK(int) i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr);
355 static DECLCALLBACK(int) i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr);
356
357 static DECLCALLBACK(bool) i_vdCryptoConfigAreKeysValid(void *pvUser,
358 const char *pszzValid);
359 static DECLCALLBACK(int) i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName,
360 size_t *pcbValue);
361 static DECLCALLBACK(int) i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
362 char *pszValue, size_t cchValue);
363
364 static DECLCALLBACK(int) i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
365 const uint8_t **ppbKey, size_t *pcbKey);
366 static DECLCALLBACK(int) i_vdCryptoKeyRelease(void *pvUser, const char *pszId);
367 static DECLCALLBACK(int) i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword);
368 static DECLCALLBACK(int) i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId);
369 static DECLCALLBACK(int) i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore);
370 static DECLCALLBACK(int) i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
371 const uint8_t *pbDek, size_t cbDek);
372
373 HRESULT i_openHddForReading(SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
374 struct MediumCryptoFilterSettings *pCryptoSettingsRead);
375
376 class Task;
377 class CreateBaseTask;
378 class CreateDiffTask;
379 class CloneTask;
380 class MoveTask;
381 class CompactTask;
382 class ResizeTask;
383 class ResetTask;
384 class DeleteTask;
385 class MergeTask;
386 class ImportTask;
387 class EncryptTask;
388 friend class Task;
389 friend class CreateBaseTask;
390 friend class CreateDiffTask;
391 friend class CloneTask;
392 friend class MoveTask;
393 friend class CompactTask;
394 friend class ResizeTask;
395 friend class ResetTask;
396 friend class DeleteTask;
397 friend class MergeTask;
398 friend class ImportTask;
399 friend class EncryptTask;
400
401 HRESULT i_taskCreateBaseHandler(Medium::CreateBaseTask &task);
402 HRESULT i_taskCreateDiffHandler(Medium::CreateDiffTask &task);
403 HRESULT i_taskMergeHandler(Medium::MergeTask &task);
404 HRESULT i_taskCloneHandler(Medium::CloneTask &task);
405 HRESULT i_taskMoveHandler(Medium::MoveTask &task);
406 HRESULT i_taskDeleteHandler(Medium::DeleteTask &task);
407 HRESULT i_taskResetHandler(Medium::ResetTask &task);
408 HRESULT i_taskCompactHandler(Medium::CompactTask &task);
409 HRESULT i_taskResizeHandler(Medium::ResizeTask &task);
410 HRESULT i_taskImportHandler(Medium::ImportTask &task);
411 HRESULT i_taskEncryptHandler(Medium::EncryptTask &task);
412
413 void i_taskEncryptSettingsSetup(struct MediumCryptoFilterSettings *pSettings, const char *pszCipher,
414 const char *pszKeyStore, const char *pszPassword,
415 bool fCreateKeyStore);
416
417 struct Data; // opaque data struct, defined in MediumImpl.cpp
418 Data *m;
419};
420
421
422/**
423 * Settings for a crypto filter instance.
424 */
425struct MediumCryptoFilterSettings
426{
427 MediumCryptoFilterSettings()
428 : fCreateKeyStore(false),
429 pszPassword(NULL),
430 pszKeyStore(NULL),
431 pszKeyStoreLoad(NULL),
432 pbDek(NULL),
433 cbDek(0),
434 pszCipher(NULL),
435 pszCipherReturned(NULL)
436 { }
437
438 bool fCreateKeyStore;
439 const char *pszPassword;
440 char *pszKeyStore;
441 const char *pszKeyStoreLoad;
442
443 const uint8_t *pbDek;
444 size_t cbDek;
445 const char *pszCipher;
446
447 /** The cipher returned by the crypto filter. */
448 char *pszCipherReturned;
449
450 PVDINTERFACE vdFilterIfaces;
451
452 VDINTERFACECONFIG vdIfCfg;
453 VDINTERFACECRYPTO vdIfCrypto;
454};
455
456
457
458#endif /* !____H_MEDIUMIMPL */
459
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