VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/MediumFormatImpl.cpp@ 99771

Last change on this file since 99771 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: 8.7 KB
Line 
1/* $Id: MediumFormatImpl.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * MediumFormat 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#define LOG_GROUP LOG_GROUP_MAIN_MEDIUMFORMAT
29#include "MediumFormatImpl.h"
30#include "AutoCaller.h"
31#include "LoggingNew.h"
32
33#include <VBox/vd.h>
34
35#include <iprt/cpp/utils.h>
36
37// constructor / destructor
38/////////////////////////////////////////////////////////////////////////////
39
40DEFINE_EMPTY_CTOR_DTOR(MediumFormat)
41
42HRESULT MediumFormat::FinalConstruct()
43{
44 return BaseFinalConstruct();
45}
46
47void MediumFormat::FinalRelease()
48{
49 uninit();
50
51 BaseFinalRelease();
52}
53
54// public initializer/uninitializer for internal purposes only
55/////////////////////////////////////////////////////////////////////////////
56
57/**
58 * Initializes the hard disk format object.
59 *
60 * @param aVDInfo Pointer to a backend info object.
61 */
62HRESULT MediumFormat::init(const VDBACKENDINFO *aVDInfo)
63{
64 LogFlowThisFunc(("aVDInfo=%p\n", aVDInfo));
65
66 ComAssertRet(aVDInfo, E_INVALIDARG);
67
68 /* Enclose the state transition NotReady->InInit->Ready */
69 AutoInitSpan autoInitSpan(this);
70 AssertReturn(autoInitSpan.isOk(), E_FAIL);
71
72 /* The ID of the backend */
73 unconst(m.strId) = aVDInfo->pszBackend;
74 /* The Name of the backend */
75 /* Use id for now as long as VDBACKENDINFO hasn't any extra
76 * name/description field. */
77 unconst(m.strName) = aVDInfo->pszBackend;
78 /* The capabilities of the backend. Assumes 1:1 mapping! */
79 unconst(m.capabilities) = (MediumFormatCapabilities_T)aVDInfo->uBackendCaps;
80 /* Save the supported file extensions in a list */
81 if (aVDInfo->paFileExtensions)
82 {
83 PCVDFILEEXTENSION papExtension = aVDInfo->paFileExtensions;
84 while (papExtension->pszExtension != NULL)
85 {
86 DeviceType_T devType;
87
88 unconst(m.maFileExtensions).push_back(papExtension->pszExtension);
89
90 switch(papExtension->enmType)
91 {
92 case VDTYPE_HDD:
93 devType = DeviceType_HardDisk;
94 break;
95 case VDTYPE_OPTICAL_DISC:
96 devType = DeviceType_DVD;
97 break;
98 case VDTYPE_FLOPPY:
99 devType = DeviceType_Floppy;
100 break;
101 default:
102 AssertMsgFailed(("Invalid enm type %d!\n", papExtension->enmType));
103 return E_INVALIDARG;
104 }
105
106 unconst(m.maDeviceTypes).push_back(devType);
107 ++papExtension;
108 }
109 }
110 /* Save a list of configure properties */
111 if (aVDInfo->paConfigInfo)
112 {
113 PCVDCONFIGINFO pa = aVDInfo->paConfigInfo;
114 /* Walk through all available keys */
115 while (pa->pszKey != NULL)
116 {
117 Utf8Str defaultValue("");
118 DataType_T dt;
119 ULONG flags = static_cast<ULONG>(pa->uKeyFlags);
120 /* Check for the configure data type */
121 switch (pa->enmValueType)
122 {
123 case VDCFGVALUETYPE_INTEGER:
124 {
125 dt = DataType_Int32;
126 /* If there is a default value get them in the right format */
127 if (pa->pszDefaultValue)
128 defaultValue = pa->pszDefaultValue;
129 break;
130 }
131 case VDCFGVALUETYPE_BYTES:
132 {
133 dt = DataType_Int8;
134 /* If there is a default value get them in the right format */
135 if (pa->pszDefaultValue)
136 {
137 /* Copy the bytes over - treated simply as a string */
138 defaultValue = pa->pszDefaultValue;
139 flags |= DataFlags_Array;
140 }
141 break;
142 }
143 case VDCFGVALUETYPE_STRING:
144 {
145 dt = DataType_String;
146 /* If there is a default value get them in the right format */
147 if (pa->pszDefaultValue)
148 defaultValue = pa->pszDefaultValue;
149 break;
150 }
151
152 default:
153 AssertMsgFailed(("Invalid enm type %d!\n", pa->enmValueType));
154 return E_INVALIDARG;
155 }
156
157 /// @todo add extendedFlags to Property when we reach the 32 bit
158 /// limit (or make the argument ULONG64 after checking that COM is
159 /// capable of defining enums (used to represent bit flags) that
160 /// contain 64-bit values)
161 ComAssertRet(pa->uKeyFlags == ((ULONG)pa->uKeyFlags), E_FAIL);
162
163 /* Create one property structure */
164 const Property prop = { Utf8Str(pa->pszKey),
165 Utf8Str(""),
166 dt,
167 flags,
168 defaultValue };
169 unconst(m.maProperties).push_back(prop);
170 ++pa;
171 }
172 }
173
174 /* Confirm a successful initialization */
175 autoInitSpan.setSucceeded();
176
177 return S_OK;
178}
179
180/**
181 * Uninitializes the instance and sets the ready flag to FALSE.
182 * Called either from FinalRelease() or by the parent when it gets destroyed.
183 */
184void MediumFormat::uninit()
185{
186 LogFlowThisFunc(("\n"));
187
188 /* Enclose the state transition Ready->InUninit->NotReady */
189 AutoUninitSpan autoUninitSpan(this);
190 if (autoUninitSpan.uninitDone())
191 return;
192
193 unconst(m.maProperties).clear();
194 unconst(m.maFileExtensions).clear();
195 unconst(m.maDeviceTypes).clear();
196 unconst(m.capabilities) = (MediumFormatCapabilities_T)0;
197 unconst(m.strName).setNull();
198 unconst(m.strId).setNull();
199}
200
201// IMediumFormat properties
202/////////////////////////////////////////////////////////////////////////////
203
204HRESULT MediumFormat::getId(com::Utf8Str &aId)
205{
206 /* this is const, no need to lock */
207 aId = m.strId;
208
209 return S_OK;
210}
211
212HRESULT MediumFormat::getName(com::Utf8Str &aName)
213{
214 /* this is const, no need to lock */
215 aName = m.strName;
216
217 return S_OK;
218}
219
220HRESULT MediumFormat::getCapabilities(std::vector<MediumFormatCapabilities_T> &aCapabilities)
221{
222 /* m.capabilities is const, no need to lock */
223
224 aCapabilities.resize(sizeof(MediumFormatCapabilities_T) * 8);
225 size_t cCapabilities = 0;
226 for (size_t i = 0; i < aCapabilities.size(); i++)
227 {
228 uint64_t tmp = m.capabilities;
229 tmp &= 1ULL << i;
230 if (tmp)
231 aCapabilities[cCapabilities++] = (MediumFormatCapabilities_T)tmp;
232 }
233 aCapabilities.resize(RT_MAX(cCapabilities, 1));
234
235 return S_OK;
236}
237
238// IMediumFormat methods
239/////////////////////////////////////////////////////////////////////////////
240
241HRESULT MediumFormat::describeFileExtensions(std::vector<com::Utf8Str> &aExtensions,
242 std::vector<DeviceType_T> &aTypes)
243{
244 /* this is const, no need to lock */
245 aExtensions = m.maFileExtensions;
246 aTypes = m.maDeviceTypes;
247
248 return S_OK;
249}
250
251HRESULT MediumFormat::describeProperties(std::vector<com::Utf8Str> &aNames,
252 std::vector<com::Utf8Str> &aDescriptions,
253 std::vector<DataType_T> &aTypes,
254 std::vector<ULONG> &aFlags,
255 std::vector<com::Utf8Str> &aDefaults)
256{
257 /* this is const, no need to lock */
258 size_t c = m.maProperties.size();
259 aNames.resize(c);
260 aDescriptions.resize(c);
261 aTypes.resize(c);
262 aFlags.resize(c);
263 aDefaults.resize(c);
264 for (size_t i = 0; i < c; i++)
265 {
266 const Property &prop = m.maProperties[i];
267 aNames[i] = prop.strName;
268 aDescriptions[i] = prop.strDescription;
269 aTypes[i] = prop.type;
270 aFlags[i] = prop.flags;
271 aDefaults[i] = prop.strDefaultValue;
272 }
273
274 return S_OK;
275}
276
277// public methods only for internal purposes
278/////////////////////////////////////////////////////////////////////////////
279/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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