VirtualBox

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

Last change on this file since 97344 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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