summaryrefslogtreecommitdiff
path: root/src/VBox/Main/include/MediumImpl.h
blob: 3ece921d2ef04b6da6d80ea2581986cb616ef0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* $Id$ */

/** @file
 *
 * VirtualBox COM class implementation
 */

/*
 * Copyright (C) 2008-2010 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#ifndef ____H_MEDIUMIMPL
#define ____H_MEDIUMIMPL

#include <VBox/vd.h>

#include "VirtualBoxBase.h"
#include "MediumLock.h"

class Progress;
class MediumFormat;

namespace settings
{
    struct Medium;
}

////////////////////////////////////////////////////////////////////////////////

/**
 * Medium component class for all media types.
 */
class ATL_NO_VTABLE Medium :
    public VirtualBoxBase,
    VBOX_SCRIPTABLE_IMPL(IMedium)
{
public:
    VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(Medium, IMedium)

    DECLARE_NOT_AGGREGATABLE(Medium)

    DECLARE_PROTECT_FINAL_CONSTRUCT()

    BEGIN_COM_MAP(Medium)
        VBOX_DEFAULT_INTERFACE_ENTRIES(IMedium)
    END_COM_MAP()

    DECLARE_EMPTY_CTOR_DTOR(Medium)

    HRESULT FinalConstruct();
    void FinalRelease();

    enum HDDOpenMode  { OpenReadWrite, OpenReadOnly };
                // have to use a special enum for the overloaded init() below;
                // can't use AccessMode_T from XIDL because that's mapped to an int
                // and would be ambiguous

    // public initializer/uninitializer for internal purposes only

    // initializer to create empty medium (VirtualBox::CreateHardDisk())
    HRESULT init(VirtualBox *aVirtualBox,
                 const Utf8Str &aFormat,
                 const Utf8Str &aLocation,
                 const Guid &uuidMachineRegistry,
                 GuidList *pllRegistriesThatNeedSaving);

    // initializer for opening existing media
    // (VirtualBox::OpenMedium(); Machine::AttachDevice())
    HRESULT init(VirtualBox *aVirtualBox,
                 const Utf8Str &aLocation,
                 HDDOpenMode enOpenMode,
                 DeviceType_T aDeviceType);

    // initializer used when loading settings
    HRESULT init(VirtualBox *aVirtualBox,
                 Medium *aParent,
                 DeviceType_T aDeviceType,
                 const Guid &uuidMachineRegistry,
                 const settings::Medium &data,
                 const Utf8Str &strMachineFolder);

    // initializer for host floppy/DVD
    HRESULT init(VirtualBox *aVirtualBox,
                 DeviceType_T aDeviceType,
                 const Utf8Str &aLocation,
                 const Utf8Str &aDescription = Utf8Str::Empty);

    void uninit();

    void deparent();
    void setParent(const ComObjPtr<Medium> &pParent);

    // IMedium properties
    STDMETHOD(COMGETTER(Id))(BSTR *aId);
    STDMETHOD(COMGETTER(Description))(BSTR *aDescription);
    STDMETHOD(COMSETTER(Description))(IN_BSTR aDescription);
    STDMETHOD(COMGETTER(State))(MediumState_T *aState);
    STDMETHOD(COMGETTER(Variant))(ULONG *aVariant);
    STDMETHOD(COMGETTER(Location))(BSTR *aLocation);
    STDMETHOD(COMSETTER(Location))(IN_BSTR aLocation);
    STDMETHOD(COMGETTER(Name))(BSTR *aName);
    STDMETHOD(COMGETTER(DeviceType))(DeviceType_T *aDeviceType);
    STDMETHOD(COMGETTER(HostDrive))(BOOL *aHostDrive);
    STDMETHOD(COMGETTER(Size))(LONG64 *aSize);
    STDMETHOD(COMGETTER(Format))(BSTR *aFormat);
    STDMETHOD(COMGETTER(MediumFormat))(IMediumFormat **aMediumFormat);
    STDMETHOD(COMGETTER(Type))(MediumType_T *aType);
    STDMETHOD(COMSETTER(Type))(MediumType_T aType);
    STDMETHOD(COMGETTER(Parent))(IMedium **aParent);
    STDMETHOD(COMGETTER(Children))(ComSafeArrayOut(IMedium *, aChildren));
    STDMETHOD(COMGETTER(Base))(IMedium **aBase);
    STDMETHOD(COMGETTER(ReadOnly))(BOOL *aReadOnly);
    STDMETHOD(COMGETTER(LogicalSize))(LONG64 *aLogicalSize);
    STDMETHOD(COMGETTER(AutoReset))(BOOL *aAutoReset);
    STDMETHOD(COMSETTER(AutoReset))(BOOL aAutoReset);
    STDMETHOD(COMGETTER(LastAccessError))(BSTR *aLastAccessError);
    STDMETHOD(COMGETTER(MachineIds))(ComSafeArrayOut(BSTR, aMachineIds));

    // IMedium methods
    STDMETHOD(SetIDs)(BOOL aSetImageId, IN_BSTR aImageId,
                      BOOL aSetParentId, IN_BSTR aParentId);
    STDMETHOD(RefreshState)(MediumState_T *aState);
    STDMETHOD(GetSnapshotIds)(IN_BSTR aMachineId,
                              ComSafeArrayOut(BSTR, aSnapshotIds));
    STDMETHOD(LockRead)(MediumState_T *aState);
    STDMETHOD(UnlockRead)(MediumState_T *aState);
    STDMETHOD(LockWrite)(MediumState_T *aState);
    STDMETHOD(UnlockWrite)(MediumState_T *aState);
    STDMETHOD(Close)();
    STDMETHOD(GetProperty)(IN_BSTR aName, BSTR *aValue);
    STDMETHOD(SetProperty)(IN_BSTR aName, IN_BSTR aValue);
    STDMETHOD(GetProperties)(IN_BSTR aNames,
                             ComSafeArrayOut(BSTR, aReturnNames),
                             ComSafeArrayOut(BSTR, aReturnValues));
    STDMETHOD(SetProperties)(ComSafeArrayIn(IN_BSTR, aNames),
                             ComSafeArrayIn(IN_BSTR, aValues));
    STDMETHOD(CreateBaseStorage)(LONG64 aLogicalSize,
                                 ULONG aVariant,
                                 IProgress **aProgress);
    STDMETHOD(DeleteStorage)(IProgress **aProgress);
    STDMETHOD(CreateDiffStorage)(IMedium *aTarget,
                                 ULONG aVariant,
                                 IProgress **aProgress);
    STDMETHOD(MergeTo)(IMedium *aTarget, IProgress **aProgress);
    STDMETHOD(CloneTo)(IMedium *aTarget, ULONG aVariant,
                        IMedium *aParent, IProgress **aProgress);
    STDMETHOD(Compact)(IProgress **aProgress);
    STDMETHOD(Resize)(LONG64 aLogicalSize, IProgress **aProgress);
    STDMETHOD(Reset)(IProgress **aProgress);

    // unsafe methods for internal purposes only (ensure there is
    // a caller and a read lock before calling them!)
    const ComObjPtr<Medium>& getParent() const;
    const MediaList& getChildren() const;

    const Guid& getId() const;
    MediumState_T getState() const;
    MediumVariant_T getVariant() const;
    bool isHostDrive() const;
    const Utf8Str& getLocationFull() const;
    const Utf8Str& getFormat() const;
    const ComObjPtr<MediumFormat> & getMediumFormat() const;
    bool isMediumFormatFile() const;
    uint64_t getSize() const;
    DeviceType_T getDeviceType() const;
    MediumType_T getType() const;
    Utf8Str getName();

    bool addRegistry(const Guid& id);
    bool isInRegistry(const Guid& id);
    const Guid& getFirstRegistryMachineId() const;
    HRESULT addToRegistryIDList(GuidList &llRegistryIDs);

    HRESULT addBackReference(const Guid &aMachineId,
                             const Guid &aSnapshotId = Guid::Empty);
    HRESULT removeBackReference(const Guid &aMachineId,
                                const Guid &aSnapshotId = Guid::Empty);

    const Guid* getFirstMachineBackrefId() const;
    const Guid* getFirstMachineBackrefSnapshotId() const;

#ifdef DEBUG
    void dumpBackRefs();
#endif

    HRESULT updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath);

    ComObjPtr<Medium> getBase(uint32_t *aLevel = NULL);

    bool isReadOnly();

    HRESULT saveSettings(settings::Medium &data,
                         const Utf8Str &strHardDiskFolder);

    HRESULT createMediumLockList(bool fFailIfInaccessible,
                                 bool fMediumLockWrite,
                                 Medium *pToBeParent,
                                 MediumLockList &mediumLockList);

    HRESULT createDiffStorage(ComObjPtr<Medium> &aTarget,
                              MediumVariant_T aVariant,
                              MediumLockList *pMediumLockList,
                              ComObjPtr<Progress> *aProgress,
                              bool aWait,
                              GuidList *pllRegistriesThatNeedSaving);
    Utf8Str getPreferredDiffFormat();

    HRESULT close(GuidList *pllRegistriesThatNeedSaving, AutoCaller &autoCaller);
    HRESULT deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait, GuidList *pllRegistriesThatNeedSaving);
    HRESULT markForDeletion();
    HRESULT unmarkForDeletion();
    HRESULT markLockedForDeletion();
    HRESULT unmarkLockedForDeletion();

    HRESULT prepareMergeTo(const ComObjPtr<Medium> &pTarget,
                           const Guid *aMachineId,
                           const Guid *aSnapshotId,
                           bool fLockMedia,
                           bool &fMergeForward,
                           ComObjPtr<Medium> &pParentForTarget,
                           MediaList &aChildrenToReparent,
                           MediumLockList * &aMediumLockList);
    HRESULT mergeTo(const ComObjPtr<Medium> &pTarget,
                    bool fMergeForward,
                    const ComObjPtr<Medium> &pParentForTarget,
                    const MediaList &aChildrenToReparent,
                    MediumLockList *aMediumLockList,
                    ComObjPtr<Progress> *aProgress,
                    bool aWait,
                    GuidList *pllRegistriesThatNeedSaving);
    void cancelMergeTo(const MediaList &aChildrenToReparent,
                       MediumLockList *aMediumLockList);

    HRESULT fixParentUuidOfChildren(const MediaList &childrenToReparent);

    HRESULT exportFile(const char *aFilename,
                       const ComObjPtr<MediumFormat> &aFormat,
                       MediumVariant_T aVariant,
                       void *aVDImageIOCallbacks, void *aVDImageIOUser,
                       const ComObjPtr<Progress> &aProgress);
    HRESULT importFile(const char *aFilename,
                       const ComObjPtr<MediumFormat> &aFormat,
                       MediumVariant_T aVariant,
                       void *aVDImageIOCallbacks, void *aVDImageIOUser,
                       const ComObjPtr<Medium> &aParent,
                       const ComObjPtr<Progress> &aProgress);

private:

    HRESULT queryInfo(bool fSetImageId, bool fSetParentId);

    HRESULT canClose();
    HRESULT unregisterWithVirtualBox(GuidList *pllRegistriesThatNeedSaving);

    HRESULT setStateError();

    HRESULT setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str::Empty);
    HRESULT setFormat(const Utf8Str &aFormat);

    VDTYPE convertDeviceType();
    DeviceType_T convertToDeviceType(VDTYPE enmType);

    Utf8Str vdError(int aVRC);

    static DECLCALLBACK(void) vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
                                          const char *pszFormat, va_list va);

    static DECLCALLBACK(bool) vdConfigAreKeysValid(void *pvUser,
                                                   const char *pszzValid);
    static DECLCALLBACK(int) vdConfigQuerySize(void *pvUser, const char *pszName,
                                               size_t *pcbValue);
    static DECLCALLBACK(int) vdConfigQuery(void *pvUser, const char *pszName,
                                           char *pszValue, size_t cchValue);

    static DECLCALLBACK(int) vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock);
    static DECLCALLBACK(int) vdTcpSocketDestroy(VDSOCKET Sock);
    static DECLCALLBACK(int) vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort);
    static DECLCALLBACK(int) vdTcpClientClose(VDSOCKET Sock);
    static DECLCALLBACK(bool) vdTcpIsClientConnected(VDSOCKET Sock);
    static DECLCALLBACK(int) vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies);
    static DECLCALLBACK(int) vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
    static DECLCALLBACK(int) vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
    static DECLCALLBACK(int) vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf);
    static DECLCALLBACK(int) vdTcpFlush(VDSOCKET Sock);
    static DECLCALLBACK(int) vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable);
    static DECLCALLBACK(int) vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr);
    static DECLCALLBACK(int) vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr);

    class Task;
    class CreateBaseTask;
    class CreateDiffTask;
    class CloneTask;
    class CompactTask;
    class ResizeTask;
    class ResetTask;
    class DeleteTask;
    class MergeTask;
    class ExportTask;
    class ImportTask;
    friend class Task;
    friend class CreateBaseTask;
    friend class CreateDiffTask;
    friend class CloneTask;
    friend class CompactTask;
    friend class ResizeTask;
    friend class ResetTask;
    friend class DeleteTask;
    friend class MergeTask;
    friend class ExportTask;
    friend class ImportTask;

    HRESULT startThread(Medium::Task *pTask);
    HRESULT runNow(Medium::Task *pTask, GuidList *pllRegistriesThatNeedSaving);

    HRESULT taskCreateBaseHandler(Medium::CreateBaseTask &task);
    HRESULT taskCreateDiffHandler(Medium::CreateDiffTask &task);
    HRESULT taskMergeHandler(Medium::MergeTask &task);
    HRESULT taskCloneHandler(Medium::CloneTask &task);
    HRESULT taskDeleteHandler(Medium::DeleteTask &task);
    HRESULT taskResetHandler(Medium::ResetTask &task);
    HRESULT taskCompactHandler(Medium::CompactTask &task);
    HRESULT taskResizeHandler(Medium::ResizeTask &task);
    HRESULT taskExportHandler(Medium::ExportTask &task);
    HRESULT taskImportHandler(Medium::ImportTask &task);

    struct Data;            // opaque data struct, defined in MediumImpl.cpp
    Data *m;
};

#endif /* ____H_MEDIUMIMPL */