VirtualBox

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

Last change on this file since 54486 was 54486, checked in by vboxsync, 10 years ago

Main/Medium: Add basic support to encrypt/decrypt media, work in progress

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.4 KB
Line 
1/* $Id: MediumImpl.h 54486 2015-02-25 12:49:39Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2008-2014 Oracle Corporation
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
20
21#ifndef ____H_MEDIUMIMPL
22#define ____H_MEDIUMIMPL
23
24#include <VBox/vd.h>
25#include "MediumWrap.h"
26#include "VirtualBoxBase.h"
27#include "AutoCaller.h"
28class Progress;
29class MediumFormat;
30class MediumLockList;
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 init(VirtualBox *aVirtualBox,
75 Medium *aParent,
76 DeviceType_T aDeviceType,
77 const Guid &uuidMachineRegistry,
78 const settings::Medium &data,
79 const Utf8Str &strMachineFolder);
80
81 // initializer for host floppy/DVD
82 HRESULT init(VirtualBox *aVirtualBox,
83 DeviceType_T aDeviceType,
84 const Utf8Str &aLocation,
85 const Utf8Str &aDescription = Utf8Str::Empty);
86
87 void uninit();
88
89 void i_deparent();
90 void i_setParent(const ComObjPtr<Medium> &pParent);
91
92 // unsafe methods for internal purposes only (ensure there is
93 // a caller and a read lock before calling them!)
94 const ComObjPtr<Medium>& i_getParent() const;
95 const MediaList& i_getChildren() const;
96
97 const Guid& i_getId() const;
98 MediumState_T i_getState() const;
99 MediumVariant_T i_getVariant() const;
100 bool i_isHostDrive() const;
101 const Utf8Str& i_getLocationFull() const;
102 const Utf8Str& i_getFormat() const;
103 const ComObjPtr<MediumFormat> & i_getMediumFormat() const;
104 bool i_isMediumFormatFile() const;
105 uint64_t i_getSize() const;
106 DeviceType_T i_getDeviceType() const;
107 MediumType_T i_getType() const;
108 Utf8Str i_getName();
109
110 /* handles caller/locking itself */
111 bool i_addRegistry(const Guid& id, bool fRecurse);
112 /* handles caller/locking itself */
113 bool i_removeRegistry(const Guid& id, bool fRecurse);
114 bool i_isInRegistry(const Guid& id);
115 bool i_getFirstRegistryMachineId(Guid &uuid) const;
116 void i_markRegistriesModified();
117
118 HRESULT i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue);
119
120 HRESULT i_addBackReference(const Guid &aMachineId,
121 const Guid &aSnapshotId = Guid::Empty);
122 HRESULT i_removeBackReference(const Guid &aMachineId,
123 const Guid &aSnapshotId = Guid::Empty);
124
125
126 const Guid* i_getFirstMachineBackrefId() const;
127 const Guid* i_getAnyMachineBackref() const;
128 const Guid* i_getFirstMachineBackrefSnapshotId() const;
129 size_t i_getMachineBackRefCount() const;
130
131#ifdef DEBUG
132 void i_dumpBackRefs();
133#endif
134
135 HRESULT i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath);
136
137 ComObjPtr<Medium> i_getBase(uint32_t *aLevel = NULL);
138
139 bool i_isReadOnly();
140 void i_updateId(const Guid &id);
141
142 HRESULT i_saveSettings(settings::Medium &data,
143 const Utf8Str &strHardDiskFolder);
144
145 HRESULT i_createMediumLockList(bool fFailIfInaccessible,
146 bool fMediumLockWrite,
147 Medium *pToBeParent,
148 MediumLockList &mediumLockList);
149
150 HRESULT i_createDiffStorage(ComObjPtr<Medium> &aTarget,
151 MediumVariant_T aVariant,
152 MediumLockList *pMediumLockList,
153 ComObjPtr<Progress> *aProgress,
154 bool aWait);
155 Utf8Str i_getPreferredDiffFormat();
156
157 HRESULT i_close(AutoCaller &autoCaller);
158 HRESULT i_unlockRead(MediumState_T *aState);
159 HRESULT i_unlockWrite(MediumState_T *aState);
160 HRESULT i_deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait);
161 HRESULT i_markForDeletion();
162 HRESULT i_unmarkForDeletion();
163 HRESULT i_markLockedForDeletion();
164 HRESULT i_unmarkLockedForDeletion();
165
166 HRESULT i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
167 bool &fMergeForward);
168
169 HRESULT i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
170 const Guid *aMachineId,
171 const Guid *aSnapshotId,
172 bool fLockMedia,
173 bool &fMergeForward,
174 ComObjPtr<Medium> &pParentForTarget,
175 MediumLockList * &aChildrenToReparent,
176 MediumLockList * &aMediumLockList);
177 HRESULT i_mergeTo(const ComObjPtr<Medium> &pTarget,
178 bool fMergeForward,
179 const ComObjPtr<Medium> &pParentForTarget,
180 MediumLockList *aChildrenToReparent,
181 MediumLockList *aMediumLockList,
182 ComObjPtr<Progress> *aProgress,
183 bool aWait);
184 void i_cancelMergeTo(MediumLockList *aChildrenToReparent,
185 MediumLockList *aMediumLockList);
186
187 HRESULT i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent);
188
189 HRESULT i_exportFile(const char *aFilename,
190 const ComObjPtr<MediumFormat> &aFormat,
191 MediumVariant_T aVariant,
192 PVDINTERFACEIO aVDImageIOIf, void *aVDImageIOUser,
193 const ComObjPtr<Progress> &aProgress);
194 HRESULT i_importFile(const char *aFilename,
195 const ComObjPtr<MediumFormat> &aFormat,
196 MediumVariant_T aVariant,
197 PVDINTERFACEIO aVDImageIOIf, void *aVDImageIOUser,
198 const ComObjPtr<Medium> &aParent,
199 const ComObjPtr<Progress> &aProgress);
200
201 HRESULT i_cloneToEx(const ComObjPtr<Medium> &aTarget, ULONG aVariant,
202 const ComObjPtr<Medium> &aParent, IProgress **aProgress,
203 uint32_t idxSrcImageSame, uint32_t idxDstImageSame);
204
205private:
206
207 // wrapped IMedium properties
208 HRESULT getId(com::Guid &aId);
209 HRESULT getDescription(com::Utf8Str &aDescription);
210 HRESULT setDescription(const com::Utf8Str &aDescription);
211 HRESULT getState(MediumState_T *aState);
212 HRESULT getVariant(std::vector<MediumVariant_T> &aVariant);
213 HRESULT getLocation(com::Utf8Str &aLocation);
214 HRESULT getName(com::Utf8Str &aName);
215 HRESULT getDeviceType(DeviceType_T *aDeviceType);
216 HRESULT getHostDrive(BOOL *aHostDrive);
217 HRESULT getSize(LONG64 *aSize);
218 HRESULT getFormat(com::Utf8Str &aFormat);
219 HRESULT getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat);
220 HRESULT getType(MediumType_T *aType);
221 HRESULT setType(MediumType_T aType);
222 HRESULT getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes);
223 HRESULT getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent);
224 HRESULT getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren);
225 HRESULT getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase);
226 HRESULT getReadOnly(BOOL *aReadOnly);
227 HRESULT getLogicalSize(LONG64 *aLogicalSize);
228 HRESULT getAutoReset(BOOL *aAutoReset);
229 HRESULT setAutoReset(BOOL aAutoReset);
230 HRESULT getLastAccessError(com::Utf8Str &aLastAccessError);
231 HRESULT getMachineIds(std::vector<com::Guid> &aMachineIds);
232
233 // wrapped IMedium methods
234 HRESULT setIds(AutoCaller &aAutoCaller,
235 BOOL aSetImageId,
236 const com::Guid &aImageId,
237 BOOL aSetParentId,
238 const com::Guid &aParentId);
239 HRESULT refreshState(AutoCaller &aAutoCaller,
240 MediumState_T *aState);
241 HRESULT getSnapshotIds(const com::Guid &aMachineId,
242 std::vector<com::Guid> &aSnapshotIds);
243 HRESULT lockRead(ComPtr<IToken> &aToken);
244 HRESULT lockWrite(ComPtr<IToken> &aToken);
245 HRESULT close(AutoCaller &aAutoCaller);
246 HRESULT getProperty(const com::Utf8Str &aName,
247 com::Utf8Str &aValue);
248 HRESULT setProperty(const com::Utf8Str &aName,
249 const com::Utf8Str &aValue);
250 HRESULT getProperties(const com::Utf8Str &aNames,
251 std::vector<com::Utf8Str> &aReturnNames,
252 std::vector<com::Utf8Str> &aReturnValues);
253 HRESULT setProperties(const std::vector<com::Utf8Str> &aNames,
254 const std::vector<com::Utf8Str> &aValues);
255 HRESULT createBaseStorage(LONG64 aLogicalSize,
256 const std::vector<MediumVariant_T> &aVariant,
257 ComPtr<IProgress> &aProgress);
258 HRESULT deleteStorage(ComPtr<IProgress> &aProgress);
259 HRESULT createDiffStorage(const ComPtr<IMedium> &aTarget,
260 const std::vector<MediumVariant_T> &aVariant,
261 ComPtr<IProgress> &aProgress);
262 HRESULT mergeTo(const ComPtr<IMedium> &aTarget,
263 ComPtr<IProgress> &aProgress);
264 HRESULT cloneTo(const ComPtr<IMedium> &aTarget,
265 const std::vector<MediumVariant_T> &aVariant,
266 const ComPtr<IMedium> &aParent,
267 ComPtr<IProgress> &aProgress);
268 HRESULT cloneToBase(const ComPtr<IMedium> &aTarget,
269 const std::vector<MediumVariant_T> &aVariant,
270 ComPtr<IProgress> &aProgress);
271 HRESULT setLocation(const com::Utf8Str &aLocation,
272 ComPtr<IProgress> &aProgress);
273 HRESULT compact(ComPtr<IProgress> &aProgress);
274 HRESULT resize(LONG64 aLogicalSize,
275 ComPtr<IProgress> &aProgress);
276 HRESULT reset(ComPtr<IProgress> &aProgress);
277 HRESULT changeEncryption(const com::Utf8Str &aNewPassword, const com::Utf8Str &aOldPassword,
278 const com::Utf8Str &aCipher, ComPtr<IProgress> &aProgress);
279
280 // Private internal nmethods
281 HRESULT i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller);
282 HRESULT i_canClose();
283 HRESULT i_unregisterWithVirtualBox();
284 HRESULT i_setStateError();
285 HRESULT i_setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str::Empty);
286 HRESULT i_setFormat(const Utf8Str &aFormat);
287 VDTYPE i_convertDeviceType();
288 DeviceType_T i_convertToDeviceType(VDTYPE enmType);
289 Utf8Str i_vdError(int aVRC);
290
291 bool i_isPropertyForFilter(const com::Utf8Str &aName);
292
293 static DECLCALLBACK(void) i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
294 const char *pszFormat, va_list va);
295 static DECLCALLBACK(bool) i_vdConfigAreKeysValid(void *pvUser,
296 const char *pszzValid);
297 static DECLCALLBACK(int) i_vdConfigQuerySize(void *pvUser, const char *pszName,
298 size_t *pcbValue);
299 static DECLCALLBACK(int) i_vdConfigQuery(void *pvUser, const char *pszName,
300 char *pszValue, size_t cchValue);
301 static DECLCALLBACK(int) i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock);
302 static DECLCALLBACK(int) i_vdTcpSocketDestroy(VDSOCKET Sock);
303 static DECLCALLBACK(int) i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
304 RTMSINTERVAL cMillies);
305 static DECLCALLBACK(int) i_vdTcpClientClose(VDSOCKET Sock);
306 static DECLCALLBACK(bool) i_vdTcpIsClientConnected(VDSOCKET Sock);
307 static DECLCALLBACK(int) i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies);
308 static DECLCALLBACK(int) i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
309 static DECLCALLBACK(int) i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
310 static DECLCALLBACK(int) i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf);
311 static DECLCALLBACK(int) i_vdTcpFlush(VDSOCKET Sock);
312 static DECLCALLBACK(int) i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable);
313 static DECLCALLBACK(int) i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr);
314 static DECLCALLBACK(int) i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr);
315
316 static DECLCALLBACK(bool) i_vdCryptoConfigAreKeysValid(void *pvUser,
317 const char *pszzValid);
318 static DECLCALLBACK(int) i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName,
319 size_t *pcbValue);
320 static DECLCALLBACK(int) i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
321 char *pszValue, size_t cchValue);
322
323 static DECLCALLBACK(int) i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
324 const uint8_t **ppbKey, size_t *pcbKey);
325 static DECLCALLBACK(int) i_vdCryptoKeyRelease(void *pvUser, const char *pszId);
326 static DECLCALLBACK(int) i_vdCryptoKeyStoreGetPassword(void *pvUser, const char **ppszPassword);
327 static DECLCALLBACK(int) i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore);
328 static DECLCALLBACK(int) i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
329 const uint8_t *pbDek, size_t cbDek);
330
331 class Task;
332 class CreateBaseTask;
333 class CreateDiffTask;
334 class CloneTask;
335 class CompactTask;
336 class ResizeTask;
337 class ResetTask;
338 class DeleteTask;
339 class MergeTask;
340 class ExportTask;
341 class ImportTask;
342 class EncryptTask;
343 friend class Task;
344 friend class CreateBaseTask;
345 friend class CreateDiffTask;
346 friend class CloneTask;
347 friend class CompactTask;
348 friend class ResizeTask;
349 friend class ResetTask;
350 friend class DeleteTask;
351 friend class MergeTask;
352 friend class ExportTask;
353 friend class ImportTask;
354 friend class EncryptTask;
355
356 HRESULT i_startThread(Medium::Task *pTask);
357 HRESULT i_runNow(Medium::Task *pTask);
358
359 HRESULT i_taskCreateBaseHandler(Medium::CreateBaseTask &task);
360 HRESULT i_taskCreateDiffHandler(Medium::CreateDiffTask &task);
361 HRESULT i_taskMergeHandler(Medium::MergeTask &task);
362 HRESULT i_taskCloneHandler(Medium::CloneTask &task);
363 HRESULT i_taskDeleteHandler(Medium::DeleteTask &task);
364 HRESULT i_taskResetHandler(Medium::ResetTask &task);
365 HRESULT i_taskCompactHandler(Medium::CompactTask &task);
366 HRESULT i_taskResizeHandler(Medium::ResizeTask &task);
367 HRESULT i_taskExportHandler(Medium::ExportTask &task);
368 HRESULT i_taskImportHandler(Medium::ImportTask &task);
369 HRESULT i_taskEncryptHandler(Medium::EncryptTask &task);
370
371 struct CryptoFilterSettings;
372 void i_taskEncryptSettingsSetup(CryptoFilterSettings *pSettings, const char *pszCipher,
373 const char *pszKeyStore, const char *pszPassword,
374 bool fCreateKeyStore);
375
376 struct Data; // opaque data struct, defined in MediumImpl.cpp
377 Data *m;
378};
379
380#endif /* ____H_MEDIUMIMPL */
381
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