VirtualBox

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

Last change on this file since 98123 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.8 KB
Line 
1/* $Id: MediumImpl.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2008-2023 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 resizeAndCloneTo(const ComPtr<IMedium> &aTarget,
320 LONG64 aLogicalSize,
321 const std::vector<MediumVariant_T> &aVariant,
322 const ComPtr<IMedium> &aParent,
323 ComPtr<IProgress> &aProgress);
324 HRESULT cloneToBase(const ComPtr<IMedium> &aTarget,
325 const std::vector<MediumVariant_T> &aVariant,
326 ComPtr<IProgress> &aProgress);
327 HRESULT moveTo(AutoCaller &autoCaller,
328 const com::Utf8Str &aLocation,
329 ComPtr<IProgress> &aProgress);
330 HRESULT compact(ComPtr<IProgress> &aProgress);
331 HRESULT resize(LONG64 aLogicalSize,
332 ComPtr<IProgress> &aProgress);
333 HRESULT reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress);
334 HRESULT changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
335 const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
336 ComPtr<IProgress> &aProgress);
337 HRESULT getEncryptionSettings(AutoCaller &autoCaller, com::Utf8Str &aCipher, com::Utf8Str &aPasswordId);
338 HRESULT checkEncryptionPassword(const com::Utf8Str &aPassword);
339 HRESULT openForIO(BOOL aWritable, com::Utf8Str const &aPassword, ComPtr<IMediumIO> &aMediumIO);
340
341 // Private internal nmethods
342 HRESULT i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller);
343 HRESULT i_canClose();
344 HRESULT i_unregisterWithVirtualBox();
345 HRESULT i_setStateError();
346 HRESULT i_setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str::Empty);
347 HRESULT i_setFormat(const Utf8Str &aFormat);
348 VDTYPE i_convertDeviceType();
349 DeviceType_T i_convertToDeviceType(VDTYPE enmType);
350 Utf8Str i_vdError(int aVRC);
351
352 bool i_isPropertyForFilter(const com::Utf8Str &aName);
353
354 HRESULT i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
355 std::vector<com::Utf8Str> &aReturnValues);
356
357 HRESULT i_preparationForMoving(const Utf8Str &aLocation);
358 bool i_isMoveOperation(const ComObjPtr<Medium> &pTarget) const;
359 bool i_resetMoveOperationData();
360 Utf8Str i_getNewLocationForMoving() const;
361
362 static DECLCALLBACK(void) i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
363 const char *pszFormat, va_list va);
364 static DECLCALLBACK(bool) i_vdConfigAreKeysValid(void *pvUser,
365 const char *pszzValid);
366 static DECLCALLBACK(int) i_vdConfigQuerySize(void *pvUser, const char *pszName,
367 size_t *pcbValue);
368 static DECLCALLBACK(int) i_vdConfigUpdate(void *pvUser, bool fCreate,
369 const char *pszName, const char *pszValue);
370
371 static DECLCALLBACK(int) i_vdConfigQuery(void *pvUser, const char *pszName,
372 char *pszValue, size_t cchValue);
373
374 static DECLCALLBACK(bool) i_vdCryptoConfigAreKeysValid(void *pvUser,
375 const char *pszzValid);
376 static DECLCALLBACK(int) i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName,
377 size_t *pcbValue);
378 static DECLCALLBACK(int) i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
379 char *pszValue, size_t cchValue);
380
381 static DECLCALLBACK(int) i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
382 const uint8_t **ppbKey, size_t *pcbKey);
383 static DECLCALLBACK(int) i_vdCryptoKeyRelease(void *pvUser, const char *pszId);
384 static DECLCALLBACK(int) i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword);
385 static DECLCALLBACK(int) i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId);
386 static DECLCALLBACK(int) i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore);
387 static DECLCALLBACK(int) i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
388 const uint8_t *pbDek, size_t cbDek);
389
390 class Task;
391 class CreateBaseTask;
392 class CreateDiffTask;
393 class CloneTask;
394 class MoveTask;
395 class CompactTask;
396 class ResizeTask;
397 class ResetTask;
398 class DeleteTask;
399 class MergeTask;
400 class ImportTask;
401 class EncryptTask;
402 friend class Task;
403 friend class CreateBaseTask;
404 friend class CreateDiffTask;
405 friend class CloneTask;
406 friend class MoveTask;
407 friend class CompactTask;
408 friend class ResizeTask;
409 friend class ResetTask;
410 friend class DeleteTask;
411 friend class MergeTask;
412 friend class ImportTask;
413 friend class EncryptTask;
414
415 HRESULT i_taskCreateBaseHandler(Medium::CreateBaseTask &task);
416 HRESULT i_taskCreateDiffHandler(Medium::CreateDiffTask &task);
417 HRESULT i_taskMergeHandler(Medium::MergeTask &task);
418 HRESULT i_taskCloneHandler(Medium::CloneTask &task);
419 HRESULT i_taskMoveHandler(Medium::MoveTask &task);
420 HRESULT i_taskDeleteHandler(Medium::DeleteTask &task);
421 HRESULT i_taskResetHandler(Medium::ResetTask &task);
422 HRESULT i_taskCompactHandler(Medium::CompactTask &task);
423 HRESULT i_taskResizeHandler(Medium::ResizeTask &task);
424 HRESULT i_taskImportHandler(Medium::ImportTask &task);
425 HRESULT i_taskEncryptHandler(Medium::EncryptTask &task);
426
427 void i_taskEncryptSettingsSetup(struct MediumCryptoFilterSettings *pSettings, const char *pszCipher,
428 const char *pszKeyStore, const char *pszPassword,
429 bool fCreateKeyStore);
430
431 struct Data; // opaque data struct, defined in MediumImpl.cpp
432 Data *m;
433};
434
435
436/**
437 * Settings for a crypto filter instance.
438 */
439struct MediumCryptoFilterSettings
440{
441 MediumCryptoFilterSettings()
442 : fCreateKeyStore(false),
443 pszPassword(NULL),
444 pszKeyStore(NULL),
445 pszKeyStoreLoad(NULL),
446 pbDek(NULL),
447 cbDek(0),
448 pszCipher(NULL),
449 pszCipherReturned(NULL)
450 { }
451
452 bool fCreateKeyStore;
453 const char *pszPassword;
454 char *pszKeyStore;
455 const char *pszKeyStoreLoad;
456
457 const uint8_t *pbDek;
458 size_t cbDek;
459 const char *pszCipher;
460
461 /** The cipher returned by the crypto filter. */
462 char *pszCipherReturned;
463
464 PVDINTERFACE vdFilterIfaces;
465
466 VDINTERFACECONFIG vdIfCfg;
467 VDINTERFACECRYPTO vdIfCrypto;
468};
469
470
471
472#endif /* !MAIN_INCLUDED_MediumImpl_h */
473
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