VirtualBox

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

Last change on this file since 85557 was 85284, checked in by vboxsync, 4 years ago

Main/MediumImpl.cpp: Signed/unsigned conversion issues, a whole bunch which was mixing VBox status codes and COM statues via setErrorVrc in the moveTo() code. Changed i_resize to take an uint64_t size rather than LONG64, and made resize() fail on negative or zero size. bugref:9790

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